def check_if_need_VLE_publish(): for node in Node.objects.filter( entry__vle_coupling=Entry.NEED_SUBMISSION, journal__sourcedid__isnull=False, journal__user__participation__role__can_have_journal=True): needs_grading_task(node.pk) for journal in Entry.objects.filter( vle_coupling=Entry.GRADING, grade__published=True, node__journal__user__participation__role__can_have_journal=True ).values('node__journal').distinct(): lti_grade.replace_result( Journal.objects.get(pk=journal['node__journal']))
def partial_update(self, request, *args, **kwargs): """Update an existing journal. Arguments: request -- request data data -- the new data for the journal pk -- journal ID Returns: On failure: unauthorized -- when the user is not logged in not found -- when the journal does not exist forbidden -- User not allowed to edit this journal unauthorized -- when the user is unauthorized to edit the journal bad_request -- when there is invalid data in the request On success: success -- with the new journal data """ pk, = utils.required_typed_params(kwargs, (int, 'pk')) journal = Journal.objects.get(pk=pk) request.user.check_can_view(journal.assignment) req_data = request.data published, = utils.optional_params(request.data, 'published') if published: request.user.check_permission('can_publish_grades', journal.assignment) req_data.pop('published', None) return self.publish(request, journal) bonus_points, = utils.optional_typed_params(request.data, (float, 'bonus_points')) if bonus_points is not None: request.user.check_permission('can_grade', journal.assignment) req_data.pop('bonus_points', None) journal.bonus_points = bonus_points journal.save() lti_grade.replace_result(journal) return response.success( {'journal': JournalSerializer(journal).data}) if not request.user.is_superuser: return response.forbidden( 'You are not allowed to edit this journal.') return self.admin_update(request, journal)
def publish_all_assignment_grades(user, assignment_pk): """Publish all grades that are not None for an assignment. - assignment: the assignment in question """ assignment = Assignment.objects.get(pk=assignment_pk) entries = Entry.objects.filter( node__journal__assignment=assignment).exclude(grade=None) for entry in entries: factory.make_grade(entry, user.pk, entry.grade.grade, True) Comment.objects.filter(entry__node__journal__assignment=assignment) \ .exclude(entry__grade=None).update(published=True) for journal in Journal.objects.filter(assignment=assignment).exclude( Q(sourcedid=None) | Q(grade_url=None)): lti_grade.replace_result(journal)
def test_replace_result_no_url(self): """Hopefully doesnt crash.""" entry = factory.make_entry( Template.objects.filter(name='Default Template')[0]) entry.published = True entry.save() factory.make_node(self.journal, entry) self.journal.sourcedid = None self.journal.grade_url = None self.journal.save() self.assertEqual(lti_grade.replace_result(self.journal), None)
def create(self, request): """Set a new grade for an entry. Arguments: request -- request data entry_id -- entry ID grade -- grade published -- published state Returns: On failure: unauthorized -- when the user is not logged in key_error -- missing keys not_found -- could not find the entry, author or assignment On success: success -- with the assignment data """ entry_id, grade, published = utils.required_typed_params( request.data, (int, 'entry_id'), (float, 'grade'), (bool, 'published')) entry = Entry.objects.get(pk=entry_id) journal = entry.node.journal assignment = journal.assignment request.user.check_permission('can_grade', assignment) if published: request.user.check_permission('can_publish_grades', assignment) if grade is not None and grade < 0: return response.bad_request( 'Grade must be greater than or equal to zero.') grade = factory.make_grade(entry, request.user.pk, grade, published) if published: Comment.objects.filter(entry=entry).update(published=True) # TODO: Is the lti flag ever used? Else move replace_result to celery return response.created({ 'entry': EntrySerializer(entry, context={ 'user': request.user }).data, 'lti': lti_grade.replace_result(journal) })
def test_replace_result_no_url(self): """Hopefully doesnt crash.""" entry = factory.Entry(node__journal=self.journal) api.create(self, 'grades', params={ 'entry_id': entry.id, 'grade': 0, 'published': True }, user=self.teacher) self.journal.sourcedid = None self.journal.grade_url = None self.journal.save() assert lti_grade.replace_result(self.journal) is None
def publish(self, request, journal): grading.publish_all_journal_grades(journal, request.user) if journal.sourcedid: payload = lti_grade.replace_result(journal) if payload and 'code_mayor' in payload and payload[ 'code_mayor'] == 'success': return response.success({ 'lti_info': payload, 'journal': JournalSerializer(journal).data }) else: return response.bad_request({ 'lti_info': payload, 'journal': JournalSerializer(journal).data }) else: return response.success( {'journal': JournalSerializer(journal).data})
def publish(self, request, assignment, published=True): utils.publish_all_assignment_grades(assignment, published) if published: for journal in assignment.journal_set.all(): lti_grade.replace_result(journal)
def add_bonus_points(self, request, *args, **kwargs): """Give students bonus points though file submission. This will scan over the included file, and give all the users the bonus points supplied. Format: [username1], [bonus_points1] [username2], [bonus_points2] Arguments: request file -- list of all the bonus points pk -- assignment ID """ assignment_id, = utils.required_typed_params(kwargs, (int, 'pk')) assignment = Assignment.objects.get(pk=assignment_id) request.user.check_permission('can_grade', assignment) if not (request.FILES and 'file' in request.FILES): return response.bad_request( 'No accompanying file found in the request.') validators.validate_user_file(request.FILES['file'], request.user) bonuses = dict() incorrect_format_lines = dict() unknown_users = dict() duplicates = dict() non_participants = dict() for line_nr, line in enumerate(request.FILES['file'], 1): try: decoded_line = line.decode() # Ignore empty lines. if not decoded_line.strip(): continue username, bonus = decoded_line[:-1].split(',')[:2] bonus = float(bonus) user = User.objects.get(username=str(username)) journal = Journal.objects.get(assignment=assignment, user=user) if journal in bonuses: duplicates[line_nr] = line.decode().split(',')[0] else: bonuses[journal] = bonus except UnicodeDecodeError: return response.bad_request( {'general': 'Not a valid csv file.'}) except ValueError: incorrect_format_lines[line_nr] = line.decode() except User.DoesNotExist: unknown_users[line_nr] = line.decode().split(',')[0] except Journal.DoesNotExist: non_participants[line_nr] = line.decode().split(',')[0] if unknown_users or incorrect_format_lines or duplicates: errors = dict() if incorrect_format_lines: errors['incorrect_format_lines'] = incorrect_format_lines if duplicates: errors['duplicates'] = duplicates if unknown_users: errors['unknown_users'] = unknown_users if non_participants: errors['non_participants'] = non_participants return response.bad_request(errors) for j, b in bonuses.items(): j.bonus_points = b j.save() lti_grade.replace_result(journal) return response.success()
def publish(self, request, journal, published=True): utils.publish_all_journal_grades(journal, published) payload = lti_grade.replace_result(journal) return response.success({'lti_info': payload})