def add_comment(request, course_name): form = None if request.method == "POST": form = CommentForm(data=request.POST) if form.is_valid() and request.POST.get('bulletin_id'): comment = form.save(commit=False) comment.bulletin = Bulletin.objects.get( id=request.POST.get('bulletin_id')) comment.user = request.user comment.date = datetime.datetime.now() comment.save() #add notification Notification.create_added_comment_notification( request.course, comment.bulletin, comment) return JsonResponse({ 'success': True, 'comment': comment.get_data() }) else: return JsonResponse({ 'success': False, 'error': "* This field is required." }) else: form = CommentForm() return render_to_response("courses/bulletins/add_comment_form.html", {'form': form})
def instructor_grade_submission(request, course_name, assignment_id, submission_id): submission = get_object_or_404(AssignmentSubmission, id=submission_id) form = None if request.method == "POST": form = GradeAssignmentSubmissionForm(instance=submission, data=request.POST) if form.is_valid(): graded_submission = form.save(commit=False) graded_submission.status = 1 #Graded graded_submission.save() Notification.create_graded_submission_notification( request.course, submission.assignment, graded_submission.user) graded_data = date_format(graded_submission.date, "D. N n, Y \\a\\t P") return JsonResponse({ 'success': True, 'score': graded_submission.score, 'date': graded_data }) else: return JsonResponse(form.compile_errors()) else: form = GradeAssignmentSubmissionForm(instance=submission) files = submission.files.all() return render_to_response("assignments/grade_submission_form.html", { 'form': form, 'submission': submission, 'files': files })
def instructor_add_assignment(request, course_name): form = None time.sleep(5) if request.method == "POST": form = AssignmentForm(course=request.course, data=request.POST) if form.is_valid(): new_assignment = form.save( commit=False) # get new model instance from form new_assignment.course = request.course new_assignment.group_id = form.cleaned_data['group_id'] new_assignment.save() form_data = form.cleaned_data form_data['due_date'] = str(form_data['due_date']) form_data['viewable_date'] = str(form_data['viewable_date']) form_data.update({'id': new_assignment.id}) form_data = {'success': True, 'assignment': form_data} Notification.create_added_assignment_student_notification( request.course, new_assignment) Notification.create_added_assignment_instructor_notification( request.course, request.user, new_assignment) return JsonResponse(form_data) else: return JsonResponse(form.compile_errors()) else: form = AssignmentForm(course=request.course) return render_to_response('assignments/new_assignment_form.html', {'form': form})
def instructor_add_assignment(request, course_name): form = None time.sleep(5) if request.method == "POST": form = AssignmentForm(course=request.course, data=request.POST) if form.is_valid(): new_assignment = form.save(commit=False) # get new model instance from form new_assignment.course = request.course new_assignment.group_id = form.cleaned_data['group_id'] new_assignment.save() form_data = form.cleaned_data form_data['due_date'] = str(form_data['due_date']) form_data['viewable_date'] = str(form_data['viewable_date']) form_data.update({ 'id': new_assignment.id }) form_data = { 'success': True, 'assignment': form_data } Notification.create_added_assignment_student_notification(request.course, new_assignment) Notification.create_added_assignment_instructor_notification(request.course, request.user, new_assignment) return JsonResponse(form_data) else: return JsonResponse(form.compile_errors()) else: form = AssignmentForm(course=request.course) return render_to_response('assignments/new_assignment_form.html', { 'form': form })
def add_bulletin(request, course_name): form = None if request.method == "POST": form = BulletinForm(data=request.POST) if form.is_valid(): bulletin = form.save(commit=False) bulletin.user = request.user bulletin_board, created = BulletinBoard.objects.get_or_create( course=request.course) bulletin.board = bulletin_board bulletin.save() Notification.create_added_bulletin_notification( request.course, bulletin) return JsonResponse({ 'success': True, 'bulletin': bulletin.get_data() }) else: return JsonResponse(form.compile_errors()) else: form = BulletinForm() return render_to_response("courses/bulletins/add_bulletin_form.html", {'form': form})
def testCreateAddedResourceNotification(self): resource = self.assignment.resources.create(name="resource") notification = Notification.create_added_resource_notification(self.course, self.assignment, resource) self.assertTrue(self.student1.notifications.filter(resource=resource).exists()) self.assertTrue( notification.text, "Resource %s was added to %s" % (resource.get_link(), self.assignment.get_link()) )
def testCreateGradedAssignmentsNotification(self): notification = Notification.create_graded_assignments_notification( self.course, self.assignment, self.assignment.submissions.all() ) self.assertTrue(self.student1.notifications.filter(pk=notification.pk).exists()) no_grade_student = self.assignment.submissions.filter(score__isnull=True)[0].user self.assertFalse(no_grade_student.notifications.filter(pk=notification.pk).exists())
def testCreateAddedResourceNotification(self): resource = self.assignment.resources.create(name="resource") notification = Notification.create_added_resource_notification( self.course, self.assignment, resource) self.assertTrue( self.student1.notifications.filter(resource=resource).exists()) self.assertTrue( notification.text, "Resource %s was added to %s" % (resource.get_link(), self.assignment.get_link()))
def testCreateGradedAssignmentsNotification(self): notification = Notification.create_graded_assignments_notification( self.course, self.assignment, self.assignment.submissions.all()) self.assertTrue( self.student1.notifications.filter(pk=notification.pk).exists()) no_grade_student = self.assignment.submissions.filter( score__isnull=True)[0].user self.assertFalse( no_grade_student.notifications.filter(pk=notification.pk).exists())
def add_comment(request, course_name): form = None if request.method == "POST": form = CommentForm(data=request.POST) if form.is_valid() and request.POST.get("bulletin_id"): comment = form.save(commit=False) comment.bulletin = Bulletin.objects.get(id=request.POST.get("bulletin_id")) comment.user = request.user comment.date = datetime.datetime.now() comment.save() # add notification Notification.create_added_comment_notification(request.course, comment.bulletin, comment) return JsonResponse({"success": True, "comment": comment.get_data()}) else: return JsonResponse({"success": False, "error": "* This field is required."}) else: form = CommentForm() return render_to_response("courses/bulletins/add_comment_form.html", {"form": form})
def add_bulletin(request, course_name): form = None if request.method == "POST": form = BulletinForm(data=request.POST) if form.is_valid(): bulletin = form.save(commit=False) bulletin.user = request.user bulletin_board, created = BulletinBoard.objects.get_or_create(course=request.course) bulletin.board = bulletin_board bulletin.save() Notification.create_added_bulletin_notification(request.course, bulletin) return JsonResponse({"success": True, "bulletin": bulletin.get_data()}) else: return JsonResponse(form.compile_errors()) else: form = BulletinForm() return render_to_response("courses/bulletins/add_bulletin_form.html", {"form": form})
def instructor_grade_submission(request, course_name, assignment_id, submission_id): submission = get_object_or_404(AssignmentSubmission, id=submission_id) form = None if request.method == "POST": form = GradeAssignmentSubmissionForm(instance=submission, data=request.POST) if form.is_valid(): graded_submission = form.save(commit=False) graded_submission.status = 1 #Graded graded_submission.save() Notification.create_graded_submission_notification(request.course, submission.assignment, graded_submission.user) graded_data = date_format(graded_submission.date, "D. N n, Y \\a\\t P") return JsonResponse({ 'success': True, 'score': graded_submission.score, 'date': graded_data }) else: return JsonResponse(form.compile_errors()) else: form = GradeAssignmentSubmissionForm(instance=submission) files = submission.files.all() return render_to_response("assignments/grade_submission_form.html", { 'form': form, 'submission': submission, 'files': files })
def instructor_view_assignment(request, course_name, assignment_id): try: assignment = request.assignment except AttributeError: assignment = get_object_or_404(Assignment, id=assignment_id) #delete notifications #request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False) form = None if request.method == "POST" and request.POST.get( "submit_type") == "assignment": form = AssignmentForm(instance=assignment, course=request.course, data=request.POST) if form.is_valid() and form.has_changed(): form.save() messages.success(request, "Assignment updated successfully.") else: form = AssignmentForm(instance=assignment, course=request.course) quiz = None grade_formset = None grade_form = None has_submitted_quiz = [] submissions = assignment.get_submissions(request) if assignment.type == 's': #if assignment is submitted online file_submissions = QueryList( AssignmentFileSubmission.objects.filter( assignment_submission__assignment=assignment)) for file in file_submissions: file._set_cached_course(request.course) #for performance for submission in submissions: submission.current_files = file_submissions.filter( assignment_submission_id=submission.id) elif assignment.type == 'c' or assignment.type == 'o': #if assignment is taken in class or taken online if assignment.type == 'o': #if assignment is taken online quiz_submissions = QuizSubmission.objects.filter( submission__assignment=assignment) for quiz_submission in quiz_submissions: if quiz_submission.submission in submissions: has_submitted_quiz.append(quiz_submission.submission.id) def custom_field_callback(field): return field.formfield(required=False) AssignmentGradeFormset = modelformset_factory( AssignmentSubmission, fields=("score", "text"), formfield_callback=custom_field_callback, extra=0) grade_queryset = assignment.submissions.select_related( "user").order_by("user__last_name", "user__first_name") if request.method == "POST" and request.POST.get( "submit_type") == "grades": grade_formset = AssignmentGradeFormset(queryset=grade_queryset, data=request.POST, auto_id=False) if grade_formset.is_valid(): unsaved_submissions = grade_formset.save(commit=False) for submission in unsaved_submissions: submission.status = 1 #graded submission.save() if assignment.type == 'c': Notification.create_graded_assignments_notification( request.course, assignment, submissions) messages.success(request, "Students were graded successfully.") else: grade_formset = AssignmentGradeFormset(queryset=grade_queryset, auto_id=False) #resource form handling resource_form = None if request.method == "POST" and request.POST.get( "submit_type") == "resource": resource_form = ResourceForm(assignment=assignment, data=request.POST, files=request.FILES, auto_id=False) if resource_form.is_valid(): resource = resource_form.save(request.user.username) Notification.create_added_resource_notification( request.course, assignment, resource) messages.success(request, "Resource added successfully.") resource_form = ResourceForm(assignment=assignment, auto_id=False) else: resource_form = ResourceForm(assignment=assignment, auto_id=False) resources = assignment.resources.all() for resource in resources: resource._set_cached_course(request.course) #for performance return render_with_context( request, "assignments/instructor/view_assignment.html", { 'assignment': assignment, 'assignment_form': form, 'grade_formset': grade_formset, 'grade_form': grade_form, 'resource_form': resource_form, 'resources': resources, 'submissions': submissions, 'has_submitted_quiz': has_submitted_quiz })
def testCreateAddedCommentNotification(self): notification = Notification.create_added_comment_notification(self.course, self.bulletin, self.comment) self.assertTrue(self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateSubmittedAssignmentNotification(self): notification = Notification.create_submitted_assignment_notification( self.course, self.student1, self.assignment ) self.assertTrue(self.instructor.notifications.filter(pk=notification.pk).exists())
def instructor_view_assignment(request, course_name, assignment_id): try: assignment = request.assignment except AttributeError: assignment = get_object_or_404(Assignment, id=assignment_id) #delete notifications #request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False) form = None if request.method == "POST" and request.POST.get("submit_type") == "assignment": form = AssignmentForm(instance=assignment, course=request.course, data=request.POST) if form.is_valid() and form.has_changed(): form.save() messages.success(request, "Assignment updated successfully.") else: form = AssignmentForm(instance=assignment, course=request.course) quiz = None grade_formset = None grade_form = None has_submitted_quiz = [] submissions = assignment.get_submissions(request) if assignment.type == 's': #if assignment is submitted online file_submissions = QueryList( AssignmentFileSubmission.objects.filter(assignment_submission__assignment=assignment) ) for file in file_submissions: file._set_cached_course(request.course) #for performance for submission in submissions: submission.current_files = file_submissions.filter(assignment_submission_id=submission.id) elif assignment.type == 'c' or assignment.type == 'o': #if assignment is taken in class or taken online if assignment.type == 'o': #if assignment is taken online quiz_submissions = QuizSubmission.objects.filter(submission__assignment=assignment) for quiz_submission in quiz_submissions: if quiz_submission.submission in submissions: has_submitted_quiz.append(quiz_submission.submission.id) def custom_field_callback(field): return field.formfield(required=False) AssignmentGradeFormset = modelformset_factory(AssignmentSubmission, fields=("score", "text"), formfield_callback=custom_field_callback, extra=0) grade_queryset = assignment.submissions.select_related("user").order_by("user__last_name", "user__first_name") if request.method == "POST" and request.POST.get("submit_type") == "grades": grade_formset = AssignmentGradeFormset(queryset=grade_queryset, data=request.POST, auto_id=False) if grade_formset.is_valid(): unsaved_submissions = grade_formset.save(commit=False) for submission in unsaved_submissions: submission.status = 1 #graded submission.save() if assignment.type == 'c': Notification.create_graded_assignments_notification(request.course, assignment, submissions) messages.success(request, "Students were graded successfully.") else: grade_formset = AssignmentGradeFormset(queryset=grade_queryset, auto_id=False) #resource form handling resource_form = None if request.method == "POST" and request.POST.get("submit_type") == "resource": resource_form = ResourceForm(assignment=assignment, data=request.POST, files=request.FILES, auto_id=False) if resource_form.is_valid(): resource = resource_form.save(request.user.username) Notification.create_added_resource_notification(request.course, assignment, resource) messages.success(request, "Resource added successfully.") resource_form = ResourceForm(assignment=assignment, auto_id=False) else: resource_form = ResourceForm(assignment=assignment, auto_id=False) resources = assignment.resources.all() for resource in resources: resource._set_cached_course(request.course) #for performance return render_with_context(request, "assignments/instructor/view_assignment.html", { 'assignment': assignment, 'assignment_form': form, 'grade_formset': grade_formset, 'grade_form': grade_form, 'resource_form': resource_form, 'resources': resources, 'submissions': submissions, 'has_submitted_quiz': has_submitted_quiz })
def testCreateGradedSubmissionNotification(self): notification = Notification.create_graded_submission_notification(self.course, self.assignment, self.student1) self.assertTrue(self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateAddedAssignmentStudentNotification(self): notification = Notification.create_added_assignment_student_notification(self.course, self.assignment) self.assertTrue(self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateAddedAssignmentInstructorNotification(self): notification = Notification.create_added_assignment_instructor_notification( self.course, self.admin, self.assignment ) self.assertTrue(self.instructor.notifications.filter(pk=notification.pk).exists())
def student_view_assignment(request, course_name, assignment_name): try: assignment = request.assignment except AttributeError: assignment_name = assignment_name.replace("_", " ") assignment = get_object_or_404(Assignment, name=assignment_name, course=request.course) #request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False) resources = assignment.resources.all() """ for resource in resources: resource.notification_count = resource.get_notification_count(request.user) """ try: submission = request.user.submissions.select_related("assignment").get(assignment=assignment) except AssignmentSubmission.DoesNotExist: if request.user.is_superuser or request.user.is_staff: submission = AssignmentSubmission(assignment=assignment, user=request.user) else: submission = request.user.submissions.create(assignment=assignment) quiz = None quiz_submission = None if assignment.type == 'o': quiz, created = OnlineQuiz.objects.get_or_create(assignment=assignment) quiz_submission, created = QuizSubmission.objects.get_or_create(quiz=quiz, submission=submission) quiz_submission._set_cached_quiz(quiz) try: scheme = request.course.get_grading_scheme() submission.grade = scheme.grade_for_percent(100 * submission.get_grade()) except: pass due_date_expired = assignment.due_date < datetime.datetime.now() submission_errors = {} if request.method == "POST": if assignment.type == 's' and not due_date_expired: submission_errors = save_files_for_submission(request.FILES.items(), submission) submission.text = request.POST['submission-text'] if submission_errors == {}: submission.status = 0 #submitted status messages.success(request, "Your response was submitted successfully.") if not (request.user.is_superuser or request.user.is_staff): submission.save() Notification.create_submitted_assignment_notification(request.course, request.user, assignment) files = submission.files.all() for file in files: file._set_cached_course(request.course) #for performance return render_with_context(request, "assignments/student/view_assignment.html", { 'assignment': assignment, 'submission': submission, 'files': files, 'errors': submission_errors, 'due_date_expired': due_date_expired, 'resources': resources, 'MAX_UPLOAD_FILE_SIZE': settings.MAX_UPLOAD_FILE_SIZE, 'quiz': quiz, 'quiz_submission': quiz_submission })
def student_view_assignment(request, course_name, assignment_name): try: assignment = request.assignment except AttributeError: assignment_name = assignment_name.replace("_", " ") assignment = get_object_or_404(Assignment, name=assignment_name, course=request.course) #request.user.notifications.filter(assignment=assignment, resource__isnull=True, is_new=True).update(is_new=False) resources = assignment.resources.all() """ for resource in resources: resource.notification_count = resource.get_notification_count(request.user) """ try: submission = request.user.submissions.select_related("assignment").get( assignment=assignment) except AssignmentSubmission.DoesNotExist: if request.user.is_superuser or request.user.is_staff: submission = AssignmentSubmission(assignment=assignment, user=request.user) else: submission = request.user.submissions.create(assignment=assignment) quiz = None quiz_submission = None if assignment.type == 'o': quiz, created = OnlineQuiz.objects.get_or_create(assignment=assignment) quiz_submission, created = QuizSubmission.objects.get_or_create( quiz=quiz, submission=submission) quiz_submission._set_cached_quiz(quiz) try: scheme = request.course.get_grading_scheme() submission.grade = scheme.grade_for_percent(100 * submission.get_grade()) except: pass due_date_expired = assignment.due_date < datetime.datetime.now() submission_errors = {} if request.method == "POST": if assignment.type == 's' and not due_date_expired: submission_errors = save_files_for_submission( request.FILES.items(), submission) submission.text = request.POST['submission-text'] if submission_errors == {}: submission.status = 0 #submitted status messages.success(request, "Your response was submitted successfully.") if not (request.user.is_superuser or request.user.is_staff): submission.save() Notification.create_submitted_assignment_notification( request.course, request.user, assignment) files = submission.files.all() for file in files: file._set_cached_course(request.course) #for performance return render_with_context( request, "assignments/student/view_assignment.html", { 'assignment': assignment, 'submission': submission, 'files': files, 'errors': submission_errors, 'due_date_expired': due_date_expired, 'resources': resources, 'MAX_UPLOAD_FILE_SIZE': settings.MAX_UPLOAD_FILE_SIZE, 'quiz': quiz, 'quiz_submission': quiz_submission })
def testCreateSubmittedAssignmentNotification(self): notification = Notification.create_submitted_assignment_notification( self.course, self.student1, self.assignment) self.assertTrue( self.instructor.notifications.filter(pk=notification.pk).exists())
def testCreateAddedCommentNotification(self): notification = Notification.create_added_comment_notification( self.course, self.bulletin, self.comment) self.assertTrue( self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateAddedAssignmentStudentNotification(self): notification = Notification.create_added_assignment_student_notification( self.course, self.assignment) self.assertTrue( self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateGradedSubmissionNotification(self): notification = Notification.create_graded_submission_notification( self.course, self.assignment, self.student1) self.assertTrue( self.student1.notifications.filter(pk=notification.pk).exists())
def testCreateAddedAssignmentInstructorNotification(self): notification = Notification.create_added_assignment_instructor_notification( self.course, self.admin, self.assignment) self.assertTrue( self.instructor.notifications.filter(pk=notification.pk).exists())