def ajaxSavePoints(request): exam = request.context.exam submitted_points, json_data = parse_points(request.POST, exam.exercises) lecture_students = exam.lecture.lecture_students_for_tutorials( tutorials=request.context.tutorials) json_data['students'] = [] for student_id in request.POST['student_id'].split(','): student = lecture_students.filter( models.LectureStudent.student_id == student_id).one().student json_data['students'].append(student.id) for exercise_id in submitted_points: if submitted_points[exercise_id] is None: request.db.query(ExerciseStudent).filter(models.ExerciseStudent.student_id == student_id)\ .filter(models.ExerciseStudent.exercise_id == exercise_id).delete() else: ep = models.ExerciseStudent() ep.student_id = student_id ep.exercise_id = exercise_id ep.points = submitted_points[exercise_id] request.db.merge(ep) request.db.commit() return json_data
def ajaxSavePoints(request): exam = request.context.exam lecture_students = exam.lecture.lecture_students_for_tutorials( tutorials=request.context.tutorials) student_id = request.POST['student_id'] student = lecture_students.filter( models.LectureStudent.student_id == student_id).one().student exercise_points = exam.exercise_points.filter( models.ExerciseStudent.student_id == student.id) d_points = {} json_data = {'msg': '', 'format_error_cells': []} for ep in exercise_points: d_points[ep.exercise_id] = ep for exercise in exam.exercises: if not exercise.id in d_points: ep = models.ExerciseStudent() ep.student = student ep.exercise = exercise request.db.add(ep) d_points[exercise.id] = ep get_param = 'points-%s' % exercise.id if get_param in request.POST: p = request.POST[get_param].replace(',', '.') if p: try: p = float(p) d_points[exercise.id].points = p except ValueError: json_data['format_error'] = 1 json_data[ 'msg'] += "Invalid value '%s' for exercise '%s'. " % ( p, exercise.id) json_data['format_error_cells'].append(exercise.id) else: if d_points[exercise.id] in request.db: #obj = d_points[exercise.id] #from sqlalchemy.orm import object_session #from sqlalchemy.orm.util import has_identity #if object_session(obj) is None and not has_identity(obj): #print "transient" #if object_session(obj) is not None and not has_identity(obj): #print "pending" #if object_session(obj) is None and has_identity(obj): #print "detached" #if object_session(obj) is not None and has_identity(obj): #print "persistent" try: request.db.delete(d_points[exercise.id]) #print "deleted" except sqlalchemy.exc.InvalidRequestError: # Object not really added # Seems not to work really #print "not deleted" pass request.db.commit() json_data['msg'] = json_data['msg'] or 'sucessfull' return json_data
def __call__(self): exam = self.request.context.exam tutorials = self.request.context.tutorials students = exam.lecture.lecture_students_for_tutorials( tutorials).options( sqlalchemy.orm.joinedload(LectureStudent.student)).all() pointsQuery = exam.exercise_points.filter( ExerciseStudent.student_id.in_([ s.student.id for s in students ])).options(sqlalchemy.orm.joinedload(ExerciseStudent.student), sqlalchemy.orm.joinedload(ExerciseStudent.exercise)) points = DictOfObjects(lambda: {}) for point in pointsQuery: points[point.student_id][point.exercise_id] = point for student in students: for e in exam.exercises: if not e.id in points[student.student_id]: exerciseStudent = models.ExerciseStudent() exerciseStudent.student = student.student exerciseStudent.exercise = e points[student.student_id][e.id] = exerciseStudent self.db.add(exerciseStudent) if self.db.new or self.db.dirty or self.db.deleted: self.db.commit() for student in points: points[student]['total'] = sum( [v.points for v in points[student].values() if v.points]) if exam.admission != None or exam.registration != None or exam.medical_certificate != None: admissions = exam.exam_admissions for a in admissions: if exam.admission != None: points[a.student_id]['admission'] = a.admission if exam.registration != None: points[a.student_id]['registration'] = a.registration if exam.medical_certificate != None: points[a.student_id][ 'medical_certificate'] = a.medical_certificate for student in students: if exam.admission != None and not 'admission' in points[ student.student_id]: points[student.student_id]['admission'] = None if exam.registration != None and not 'registration' in points[ student.student_id]: points[student.student_id]['registration'] = None if exam.medical_certificate != None and not 'medical_certificate' in points[ student.student_id]: points[student.student_id]['medical_certificate'] = None return { 'exam': exam, 'tutorial_ids': self.request.matchdict['tutorial_ids'], 'students': students, 'points': points }
def __call__(self): error_msgs = [] exam = self.request.context.exam tutorials = self.request.context.tutorials students = exam.lecture.lecture_students_for_tutorials(tutorials).options(sqlalchemy.orm.joinedload(LectureStudent.student))\ .options(sqlalchemy.orm.joinedload_all('tutorial.tutor')) students = students.all() if 'students' in self.request.GET: student_ids = self.request.GET['students'].split(',') student_ids = [int(sid.strip()) for sid in student_ids] students = [s for s in students if s.student.id in student_ids] pointsQuery = exam.exercise_points.filter( ExerciseStudent.student_id.in_([ s.student.id for s in students ])).options(sqlalchemy.orm.joinedload(ExerciseStudent.student), sqlalchemy.orm.joinedload(ExerciseStudent.exercise)) points = DictOfObjects(lambda: {}) #for s in students: # for e in exam.exercises: # points[s.student_id][e.id] = None for point in pointsQuery.all(): points[point.student_id][point.exercise_id] = point for student in students: if not student.student_id in points: points[student.student_id] = {} for e in exam.exercises: if not e.id in points[student.student_id]: exerciseStudent = models.ExerciseStudent() exerciseStudent.student = student.student exerciseStudent.exercise = e points[student.student_id][e.id] = exerciseStudent self.db.add(exerciseStudent) if self.request.method == 'POST': if not self.request.permissionInfo.has_permission('enter_points'): return HTTPForbidden( 'Sie haben keine Rechte um Punkte einzutragen!') for student in students: for e in exam.exercises: param = 'points-%u-%u' % (student.student_id, e.id) if param in self.request.POST: value = self.request.POST[param] if not value: points[student.student_id][e.id].points = None else: value = value.replace(',', '.') try: value = float(value) points[student.student_id][e.id].points = value except: error_msgs.append( u'Could not convert "%s" (%s, Exercise %i)' % (value, student.student.name(), e.nr)) for student in points: points[student]['total'] = sum( [v.points for v in points[student].values() if v.points]) if self.db.new or self.db.dirty or self.db.deleted: self.db.commit() # TODO: Die Statistik scheint recht langsm zu sein. Evt lohnt es sich, # die selber auszurechnen... if tutorials: statistics = exam.getStatistics(students=None) statistics.update( exam.getStatistics(students=students, prefix='tut')) else: statistics = exam.getStatistics(students=students) if not self.raw: self.request.javascript.add('prototype.js') return { 'exam': exam, 'tutorial_ids': self.request.matchdict['tutorial_ids'], 'students': students, 'points': points, 'statistics': statistics, 'error_msg': u'\n'.join(error_msgs) }