Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from sqlalchemy.orm import aliased
from sqlalchemy.sql import and_
cd=coursedir.CourseDirectory(root='/home/daniel/Teaching/L2python')
api=NbGraderAPI(cd)
api.exchange='/home/daniel/Teaching/L2python/exchange'
#print (api.get_submissions('a_a'))
notebook_id='lessEmpty'
assignment_id='lessEmpty'
res = api.gradebook.db.query(
SubmittedNotebook.id,
func.sum(Grade.score).label("code_score"),
func.sum(GradeCell.max_score).label("max_code_score"),
).join(SubmittedAssignment, Notebook, Assignment, Student, Grade, GradeCell)\
.filter(GradeCell.cell_type == "code")\
.group_by(SubmittedNotebook.id)\
.all()
print (res)
import sys
#sys.exit()
res = api.gradebook.db.query(
SubmittedNotebook.id,
func.sum(Grade.score).label("written_score"),
func.sum(GradeCell.max_score).label("max_written_score"),
).join(SubmittedAssignment, Notebook, Assignment, Student, Grade, GradeCell)\
"""
if 'timestamp' in kwargs:
kwargs['timestamp'] = utils.parse_utc(kwargs['timestamp'])
try:
submission = SubmittedAssignment(
assignment=self.find_assignment(assignment),
student=self.find_student(student),
**kwargs)
for notebook in submission.assignment.notebooks:
nb = SubmittedNotebook(notebook=notebook, assignment=submission)
for grade_cell in notebook.grade_cells:
Grade(cell=grade_cell, notebook=nb)
for solution_cell in notebook.solution_cells:
Comment(cell=solution_cell, notebook=nb)
self.db.add(submission)
self.db.commit()
except (IntegrityError, FlushError) as e:
self.db.rollback()
raise InvalidEntry(*e.args)
return submission
## Written scores
SubmittedNotebook.written_score = column_property(
select([func.coalesce(func.sum(Grade.score), 0.0)])\
.where(and_(
Grade.notebook_id == SubmittedNotebook.id,
GradeCell.id == Grade.cell_id,
GradeCell.cell_type == "markdown"))\
.correlate_except(Grade), deferred=True)
SubmittedAssignment.written_score = column_property(
select([func.coalesce(func.sum(Grade.score), 0.0)])\
.where(and_(
SubmittedNotebook.assignment_id == SubmittedAssignment.id,
Grade.notebook_id == SubmittedNotebook.id,
GradeCell.id == Grade.cell_id,
GradeCell.cell_type == "markdown"))\
.correlate_except(Grade), deferred=True)
## Written max scores
Notebook.max_written_score = column_property(
select([func.coalesce(func.sum(GradeCell.max_score), 0.0)])\
.where(and_(
GradeCell.notebook_id == Notebook.id,
GradeCell.cell_type == "markdown"))\
.correlate_except(GradeCell), deferred=True)
SubmittedNotebook.max_written_score = column_property(
select([Notebook.max_written_score])\
.filter(
Grade.notebook_id == SubmittedNotebook.id,
Grade.failed_tests)\
.group_by(SubmittedNotebook.id)\
.subquery()
# full query
submissions = self.db.query(
SubmittedNotebook.id, Notebook.name, Student.id,
func.sum(Grade.score), func.sum(GradeCell.max_score),
code_scores.c.code_score, code_scores.c.max_code_score,
written_scores.c.written_score, written_scores.c.max_written_score,
func.coalesce(manual_grade.c.needs_manual_grade, False),
func.coalesce(failed_tests.c.failed_tests, False),
SubmittedNotebook.flagged
).join(SubmittedAssignment, Notebook, Assignment, Student, Grade, GradeCell)\
.outerjoin(code_scores, SubmittedNotebook.id == code_scores.c.id)\
.outerjoin(written_scores, SubmittedNotebook.id == written_scores.c.id)\
.outerjoin(manual_grade, SubmittedNotebook.id == manual_grade.c.id)\
.outerjoin(failed_tests, SubmittedNotebook.id == failed_tests.c.id)\
.filter(and_(
Notebook.name == notebook_id,
Assignment.name == assignment_id,
Student.id == SubmittedAssignment.student_id,
SubmittedAssignment.id == SubmittedNotebook.assignment_id,
SubmittedNotebook.id == Grade.notebook_id,
GradeCell.id == Grade.cell_id))\
.group_by(Student.id)\
.all()
keys = [
"id", "name", "student",
## Number of submissions
Assignment.num_submissions = column_property(
select([func.count(SubmittedAssignment.id)])\
.where(SubmittedAssignment.assignment_id == Assignment.id)\
.correlate_except(SubmittedAssignment), deferred=True)
Notebook.num_submissions = column_property(
select([func.count(SubmittedNotebook.id)])\
.where(SubmittedNotebook.notebook_id == Notebook.id)\
.correlate_except(SubmittedNotebook), deferred=True)
## Cell type
Grade.cell_type = column_property(
select([GradeCell.cell_type])\
.where(Grade.cell_id == GradeCell.id)\
.correlate_except(GradeCell), deferred=True)
## Failed tests
Grade.failed_tests = column_property(
(Grade.auto_score < Grade.max_score) & (Grade.cell_type == "code"))
SubmittedNotebook.failed_tests = column_property(
exists().where(and_(
Grade.notebook_id == SubmittedNotebook.id,
Grade.failed_tests))\
.correlate_except(Grade), deferred=True)
self.assignment.name, self.notebook.name, self.name, self.student.id)
## Needs manual grade
SubmittedNotebook.needs_manual_grade = column_property(
exists().where(and_(
Grade.notebook_id == SubmittedNotebook.id,
Grade.needs_manual_grade))\
.correlate_except(Grade), deferred=True)
SubmittedAssignment.needs_manual_grade = column_property(
exists().where(and_(
SubmittedNotebook.assignment_id == SubmittedAssignment.id,
Grade.notebook_id == SubmittedNotebook.id,
Grade.needs_manual_grade))\
.correlate_except(Grade), deferred=True)
Notebook.needs_manual_grade = column_property(
exists().where(and_(
Notebook.id == SubmittedNotebook.notebook_id,
Grade.notebook_id == SubmittedNotebook.id,
Grade.needs_manual_grade))\
.correlate_except(Grade), deferred=True)
## Overall scores
SubmittedNotebook.score = column_property(
select([func.coalesce(func.sum(Grade.score), 0.0)])\
.where(Grade.notebook_id == SubmittedNotebook.id)\
.correlate_except(Grade), deferred=True)
.where(SubmittedNotebook.notebook_id == Notebook.id)\
.correlate_except(SubmittedNotebook), deferred=True)
## Cell type
Grade.cell_type = column_property(
select([GradeCell.cell_type])\
.where(Grade.cell_id == GradeCell.id)\
.correlate_except(GradeCell), deferred=True)
## Failed tests
Grade.failed_tests = column_property(
(Grade.auto_score < Grade.max_score) & (Grade.cell_type == "code"))
SubmittedNotebook.failed_tests = column_property(
exists().where(and_(
Grade.notebook_id == SubmittedNotebook.id,
Grade.failed_tests))\
.correlate_except(Grade), deferred=True)
class Gradebook(object):
"""The gradebook object to interface with the database holding
nbgrader grades.
"""
def __init__(self, db_url):
"""Initialize the connection to the database.
def find_grade_by_id(self, grade_id):
"""Find a grade by its unique id.
Parameters
----------
grade_id : string
the unique id of the grade
Returns
-------
grade : :class:`~nbgrader.api.Grade`
"""
try:
grade = self.db.query(Grade).filter(Grade.id == grade_id).one()
except NoResultFound:
raise MissingEntry("No such grade: {}".format(grade_id))
return grade