def update_grades_with_post_params(self, grades, lecture_students, grading, error_msgs): if self.request.method == 'POST': for ls in lecture_students: param = 'grade-%u' % (ls.student_id) if param in self.request.POST: value = self.request.POST[param] if not grades[ls.student_id]['grade']: studentGrade = models.StudentGrade() studentGrade.student = ls.student studentGrade.grading = grading grades[ls.student_id]['grade'] = studentGrade self.db.add(studentGrade) if not value: grades[ls.student_id]['grade'].grade = None grades[ls.student_id]['gradestr'] = '' else: value = value.replace(',', '.') try: value = float(value) if not 0 <= value <= 6: # Only accept valid grades from 0 to 6. (usually 5, but let's be lenient here) raise ValueError grades[ls.student_id]['grade'].grade = value grades[ls.student_id]['gradestr'] = value except: error_msgs.append('Could not convert "%s" (%s)' % (value, ls.student.name)) if self.db.new or self.db.dirty or self.db.deleted: self.db.commit() return grades, error_msgs
def __call__(self): grading = self.request.context.grading formula = self.request.GET.get('formula', None) if formula: grading.formula = formula self.db.commit() else: formula = grading.formula exam_id = self.request.GET.get('students', None) if exam_id: exam = self.request.db.query(models.Exam).get(exam_id) else: exam = None student_id = self.request.GET.get('student', None) lecture_students = grading.lecture.lecture_students_for_tutorials([])\ .options(sqlalchemy.orm.joinedload(models.LectureStudent.student)) if student_id: lecture_students = lecture_students.filter( models.LectureStudent.student_id == student_id) if exam: lecture_students = lecture_students\ .join(models.ExerciseStudent, models.LectureStudent.student_id==models.ExerciseStudent.student_id)\ .join(models.Exercise)\ .join(models.Exam)\ .filter(models.Exam.id==exam_id) lecture_students = lecture_students.all() gradesQuery = grading.student_grades.filter( models.StudentGrade.student_id.in_( [ls.student_id for ls in lecture_students])) grades = utils.autovivify() exam_ids = [e.id for e in grading.exams] examvars = dict([['$%i' % i, e.id] for i, e in enumerate(grading.exams)]) varsForExam = dict([[examvars[var], var] for var in examvars]) for ls in lecture_students: grades[ls.student_id]['grade'] = '' grades[ls.student_id]['gradestr'] = '' grades[ls.student_id]['invalid'] = None grades[ls.student_id]['exams'] = dict([[ i, { 'points': '', 'admission': None, 'registration': None, 'medical_certificate': None } ] for i in exam_ids]) grades[ls.student_id]['calc'] = '' for grade in gradesQuery: grades[grade.student_id]['grade'] = grade grades[grade.student_id]['gradestr'] = grade.grade #for ls in lecture_students: # if not grades[ls.student_id]['grade']: # studentGrade = models.StudentGrade() # studentGrade.student = ls.student # studentGrade.grading = grading # grades[ls.student_id]['grade'] = studentGrade # self.db.add(studentGrade) if self.request.method == 'POST': for ls in lecture_students: param = 'grade-%u' % (ls.student_id) if param in self.request.POST: value = self.request.POST[param] if not grades[ls.student_id]['grade']: studentGrade = models.StudentGrade() studentGrade.student = ls.student studentGrade.grading = grading grades[ls.student_id]['grade'] = studentGrade self.db.add(studentGrade) if not value: grades[ls.student_id]['grade'].grade = None grades[ls.student_id]['gradestr'] = '' else: value = value.replace(',', '.') try: value = float(value) grades[ls.student_id]['grade'].grade = value grades[ls.student_id]['gradestr'] = value except: error_msgs.append('Could not convert "%s" (%s)' % (value, ls.student.name())) if self.db.new or self.db.dirty or self.db.deleted: self.db.commit() for exam in grading.exams: results = exam.getResults(students=lecture_students) for result in results: grades[result.student_id]['exams'][ exam.id]['points'] = result.points if exam.admission != None or exam.registration != None or exam.medical_certificate != None: student_ids = [ls.student_id for ls in lecture_students] admissions = exam.exam_admissions for a in admissions: if a.student_id in student_ids: if exam.admission != None: grades[a.student_id]['exams'][ exam.id]['admission'] = a.admission if exam.registration != None: grades[a.student_id]['exams'][ exam.id]['registration'] = a.registration if exam.medical_certificate != None: grades[a.student_id]['exams'][exam.id][ 'medical_certificate'] = a.medical_certificate error_msgs = [] if formula: parser = Parser() try: parser.parseString(formula) for ls in lecture_students: #print(student) d = {} for exam in grading.exams: result = grades[ls.student_id]['exams'][ exam.id]['points'] if result == '': result = None d[varsForExam[exam.id]] = result result = parser.calculate(d) grades[ls.student_id][ 'calc'] = result if result != None else '' if 'fill' in self.request.GET: grades[ls.student_id][ 'gradestr'] = result if result != None else '' except Exception as err: error_msgs.append(str(err)) if 'fill' in self.request.GET: self.request.session.flash( 'Achtung, die Noten sind noch nicht abgespeichert!', queue='errors') #self.request.javascript.append('prototype.js') self.request.javascript.append('jquery/jquery.min.js') self.request.javascript.append('jquery/jquery.fancybox.min.js') #grades = {key: value for key,value in grades.items()} return { 'grading': grading, 'error_msg': '\n'.join(error_msgs), 'formula': formula, 'exam_id': exam_id, 'tutorial_ids': '', 'grades': grades, 'examvars': examvars, 'varsForExam': varsForExam, 'lecture_students': lecture_students }