Пример #1
0
    def get(self, request, *args, **kwargs):
        creds = UserCredentials()
        api = CanvasAPI()

        activeTerms = ActiveTerm.objects.order_by('-active_term')

        for activeTerm in activeTerms:
            term = activeTerm.active_term
            canvasID = api.get_canvasID(creds.get_OUNetID())
            api_response = api.get_class_by_teacher(canvasID, term.term_id)
            api_list = list(api_response)

            for course in api_list:
                course_id = int(course['id'])
                course_found = self.model.objects.filter(
                    course_id=course_id).first()

                if course_found is None:
                    subaccount = Subaccount.objects.filter(
                        subaccount_id=course['account_id']).first()
                    course_create = self.model.objects.create(
                        course_id=course_id,
                        name=course['name'],
                        course_code=course['course_code'],
                        term=term,
                        subaccount=subaccount)
                    course_create.save()

        return super(CourseListView, self).get(request, *args, **kwargs)
Пример #2
0
 def reload_usercourse(self, request, term_id):
     term = Term.objects.filter(term_id = term_id).first()        
     #self.model.objects.filter(course__term = term).delete()
     creds = UserCredentials()
     api = CanvasAPI(term = term_id)
     
     course_list = Course.objects.filter(term = term)
     for course in course_list:
         course_users = api.get_course_enrollments(course.course_id)
         for user in course_users:
             localuser = User.objects.filter(canvas_id = user["user_id"]).first()
             if localuser is not None:
                 isTeacher = False
                 if (user["type"] == "TeacherEnrollment"):
                     isTeacher = True
                     
                 usercourse_record = self.model.objects.filter(user = localuser, course = course).first()
                 if usercourse_record is None: 
                     #logging.warning("Creating UserCourse record for: " + str(localuser))
                     usercourse_create = self.model.objects.create(user = localuser, course = course, is_teacher = isTeacher)
                     usercourse_create.save()
                 else:
                     usercourse_record.is_teacher = isTeacher
                     usercourse_record.save()
         
     self.message_user(request, "User Course (Enrollments) list successfully reloaded!")
     return HttpResponseRedirect("../../")
Пример #3
0
    def formset_valid(self, formset):
        course_id = self.kwargs['course_id']
        course = CanvasCourse.objects.filter(course_id=course_id).first()
        capi = CanvasAPI(term=course.term.term_id)
        creds = UserCredentials()

        cleaned_data = formset.clean()

        for form in formset:
            if form.has_changed():
                form.save()

                assignment_id = form.cleaned_data['id'].assignment_id
                has_override = form.cleaned_data['id'].has_override

                # Fixed time to add to dates
                starttime = time(0, 0, 0)
                endtime = time(23, 59, 00)

                # Get dates from DB
                start_date = form.cleaned_data['start_date']
                due_date = form.cleaned_data['due_date']
                end_date = form.cleaned_data['end_date']

                # Combine date with fixed time and convert to isoformat
                start_date_time = datetime.combine(start_date,
                                                   starttime).isoformat()
                due_date_time = datetime.combine(due_date, endtime).isoformat()
                end_date_time = datetime.combine(end_date, endtime).isoformat()

                if ('assignment_id' in self.kwargs
                        and 'student_id' in self.kwargs):
                    self.assignment_id = self.kwargs['assignment_id']
                    self.student_id = self.kwargs['student_id']

                    if has_override:
                        capi.update_assignment_override(
                            course_id, self.assignment_id, self.student_id,
                            due_date_time, start_date_time, end_date_time)
                        self.success_message = "Assignment override updated!"
                    else:
                        capi.create_assignment_override(
                            course_id, self.assignment_id, self.student_id,
                            due_date_time, start_date_time, end_date_time)
                        self.success_message = "Assignment override created!"

                else:
                    if has_override:
                        capi.delete_assignment_overrides(
                            course_id, assignment_id)

                    capi.update_assignment_dates(course_id, assignment_id,
                                                 due_date_time,
                                                 start_date_time,
                                                 end_date_time)

        return super(EditDueDates, self).formset_valid(formset)
Пример #4
0
    def get(self, request, *args, **kwargs):
        course_id = int(self.kwargs['course_id'])
        self.model.objects.filter(course__course_id=course_id).delete()
        course = CanvasCourse.objects.filter(course_id=course_id).first()
        api = CanvasAPI(term=course.term.term_id)

        json_data = api.get_assignments(course_id)
        json_list = list(json_data)  #the data from canvas

        for assignment in json_list:  #get the stuff i need from the canvas data
            assignment_id = assignment['id']
            assignment_name = assignment['name']
            has_override = assignment['has_overrides']
            is_quiz = assignment['is_quiz_assignment']

            td = timedelta(hours=6)  #adjust to local time
            if assignment['unlock_at'] is not None:
                unlockCanvas = datetime.strptime(
                    assignment['unlock_at'],
                    '%Y-%m-%dT%H:%M:%SZ')  #save in datetime object
                unlockCanvas = unlockCanvas - td  #adjust time.  else it goes past midnight altering the date
                start_date = datetime.strftime(
                    unlockCanvas, '%Y-%m-%d'
                )  #remove time and save just the date as a string
            else:
                start_date = None
            if assignment['due_at'] is not None:
                dueCanvas = datetime.strptime(assignment['due_at'],
                                              '%Y-%m-%dT%H:%M:%SZ')
                dueCanvas = dueCanvas - td
                due_date = datetime.strftime(
                    dueCanvas, '%Y-%m-%d'
                )  #saving date as string and in m/d/y for use with datepicker
            else:
                due_date = None
            if assignment['lock_at'] is not None:
                lockCanvas = datetime.strptime(assignment['lock_at'],
                                               '%Y-%m-%dT%H:%M:%SZ')
                lockCanvas = lockCanvas - td
                end_date = datetime.strftime(lockCanvas, '%Y-%m-%d')
            else:
                end_date = None

            Assignment.user_objects.create(assignment_id=assignment_id,
                                           name=assignment_name,
                                           start_date=start_date,
                                           due_date=due_date,
                                           end_date=end_date,
                                           has_override=has_override,
                                           is_quiz=is_quiz,
                                           course=course)

        return super(AssignmentListView, self).get(request, *args, **kwargs)
Пример #5
0
    def get_queryset(self):
        creds = UserCredentials()
        api = CanvasAPI()

        canvasID = api.get_canvasID(creds.get_OUNetID())
        api_response = api.get_class_by_teacher(canvasID)
        api_list = list(api_response)

        course_ids = []
        for course in api_list:
            course_ids.append(int(course['id']))

        return self.model.objects.filter(course_id__in=course_ids)
Пример #6
0
    def get_context_data(self, *args, **kwargs):
        course_id = int(self.kwargs['course_id'])
        if ('assignment_id' in self.kwargs and 'student_id' in self.kwargs):
            self.assignment_id = self.kwargs['assignment_id']
            self.student_id = self.kwargs['student_id']
        data = super(EditDueDates, self).get_context_data(**kwargs)

        course = CanvasCourse.objects.filter(course_id=course_id).first()
        api = CanvasAPI(term=course.term.term_id)
        creds = UserCredentials()

        if (hasattr(self, 'assignment_id') and hasattr(self, 'student_id')):
            data['assignment_id'] = self.assignment_id
            data['assignment_name'] = Assignment.objects.filter(
                assignment_id=self.assignment_id).first()
            if not data['assignment_name']:
                data['assignment_name'] = api.get_assignment(
                    course_id, self.assignment_id)['name']
            data['student_id'] = self.student_id
            data['student_name'] = User.objects.filter(
                canvas_id=self.student_id).first()
            if not data['student_name']:
                data['student_name'] = api.get_user(self.student_id)['name']
            data['is_override_create'] = True
        data['course_id'] = course_id
        data['course_name'] = CanvasCourse.objects.filter(
            course_id=course_id).first()
        if not data['course_name']:
            data['course_name'] = api.get_courses(course_id)['name']

        has_override_dict = {}
        is_quiz_dict = {}
        has_override = False

        for object in data['object_list']:
            if object.is_quiz:
                is_quiz_dict[object.id] = True
            else:
                is_quiz_dict[object.id] = True
        for object in data['object_list']:
            if object.has_override:
                has_override_dict[object.id] = True
                has_override = True
            else:
                has_override_dict[object.id] = True

        data['has_override_dict'] = has_override_dict
        data['is_quiz_dict'] = is_quiz_dict
        data['has_override'] = has_override

        return data
Пример #7
0
 def reload_course(self, request, term_id):
     term = Term.objects.filter(term_id = term_id).first()        
     self.model.objects.filter(term = term).delete()
     creds = UserCredentials()
     api = CanvasAPI(term = term_id)
     
     course_list = api.get_courses_by_term()
     for course in course_list:
         subaccount = Subaccount.objects.filter(subaccount_id = course['account_id']).first()
         self.model.objects.filter(course_id = course['id']).delete()
         course_create = self.model.objects.create(course_id = course['id'], name = course['name'], course_code = course['course_code'], term = term, subaccount = subaccount)
         course_create.save()
         
     self.message_user(request, "Course list successfully reloaded!")
     return HttpResponseRedirect("../../")
Пример #8
0
 def reload_user(self, request):
     #self.model.objects.all().delete()
     Submission.objects.all().delete()
     creds = UserCredentials()
     api = CanvasAPI()
     
     user_list = api.get_account_users()
     for user in user_list:
         userRecord = self.model.objects.filter(canvas_id = user['id']).first()
         if userRecord is None:
             user_create = self.model.objects.create(canvas_id = user['id'], name = user['name'], sortable_name = user['sortable_name'], short_name = user['short_name'], sis_user_id = user['sis_user_id'], login_id = user['login_id']) 
             user_create.save()
         
     self.message_user(request, "Canvas User list successfully reloaded!")
     return HttpResponseRedirect("../")
Пример #9
0
    def get_queryset(self):
        creds = UserCredentials()
        api = CanvasAPI()

        activeTerms = ActiveTerm.objects.order_by('-active_term')

        canvasID = api.get_canvasID(creds.get_OUNetID())
        course_ids = []
        for activeTerm in activeTerms:
            term = activeTerm.active_term
            api_response = api.get_class_by_teacher(canvasID, term.term_id)
            api_list = list(api_response)

            for course in api_list:
                course_ids.append(int(course['id']))

        return self.model.objects.filter(course_id__in=course_ids)
Пример #10
0
    def get(self, request, *args, **kwargs):
        course_id = int(self.kwargs['course_id'])
        StudentCourse.objects.filter(course__course_id=course_id).delete()
        course = CanvasCourse.objects.filter(course_id=course_id).first()
        api = CanvasAPI()
        assignment_id = self.kwargs['assignment_id']

        json_data = api.get_students(course_id)
        json_list = list(json_data)  #the data from canvas

        for student_record in json_list:  #get the stuff i need from the canvas data
            student_id = student_record['id']
            student = Student.objects.filter(canvas_id=int(student_id)).first()
            if student is not None:
                StudentCourse.user_objects.create(student=student,
                                                  course=course)

        return super(StudentListView, self).get(request, *args, **kwargs)
Пример #11
0
 def reload_subaccounts(self, request):
     self.model.objects.all().delete()
     creds = UserCredentials()
     api = CanvasAPI()
     
     first_iteration = api.get_subaccounts()
     for sub_account in first_iteration:
         sub_account_create = self.model.objects.create(subaccount_id = sub_account['id'], name = sub_account['name'])
         sub_account_create.save()
 
     second_iteration = api.get_subaccounts()
     for sub_account in second_iteration:
         sub_account_parent = self.model.objects.filter(subaccount_id = sub_account['parent_account_id']).first()
         sub_account_child = self.model.objects.filter(subaccount_id = sub_account['id']).first()
         sub_account_child.parent = sub_account_parent
         sub_account_child.save()
         
     self.message_user(request, "Subaccounts successfully reloaded!")
     return HttpResponseRedirect("../")
Пример #12
0
 def changelist_view(self, request, extra_context = None):
     extra_context = extra_context or {}
     most_recent_timestamp = self.model.objects.all().order_by('created_at').first()
     if most_recent_timestamp is not None:
         extra_context['load_date'] = most_recent_timestamp.created_at
         
     api = CanvasAPI()
     extra_context['terms'] = Term.objects.all()
     extra_context['current_term_id'] = api.TERM
     return super(CourseAdmin, self).changelist_view(request, extra_context = extra_context)
Пример #13
0
def load_usercourse_data_for_term(term_id):
    if term_id is not None:
        term = Term.objects.filter(term_id=term_id).first()
        UserCourse.objects.filter(course__term=term).delete()
        api = CanvasAPI(term_id)

        course_list = Course.objects.filter(term=term)
        for course in course_list:
            course_users = api.get_course_enrollments(course.course_id)
            for user in course_users:
                localuser = User.objects.filter(
                    canvas_id=user["user_id"]).first()
                if localuser is not None:
                    isTeacher = False
                    if (user["type"] == "TeacherEnrollment"):
                        isTeacher = True
                    usercourse_create = UserCourse.objects.create(
                        user=localuser, course=course, is_teacher=isTeacher)
                    usercourse_create.save()
Пример #14
0
    def reload_student(self, request):
        self.model.objects.all().delete()
        Submission.objects.all().delete()
        creds = UserCredentials()
        api = CanvasAPI()

        student_list = api.get_account_users()
        for student in student_list:
            student_create = self.model.objects.create(
                canvas_id=student['id'],
                name=student['name'],
                sortable_name=student['sortable_name'],
                short_name=student['short_name'],
                sis_user_id=student['sis_user_id'],
                login_id=student['login_id'])
            student_create.save()

        self.message_user(request, "Student list successfully reloaded!")
        return HttpResponseRedirect("../")
Пример #15
0
def get_teacher_weekly_report_assignments(teacherweeklyreportrecord):

    start_date = teacherweeklyreportrecord.start_date
    end_date = teacherweeklyreportrecord.end_date
    course_id = teacherweeklyreportrecord.usercourse.course.course_id
    teacher_id = teacherweeklyreportrecord.usercourse.user.canvas_id

    course = Course.objects.filter(course_id=course_id).first()

    api = CanvasAPI(course.term.term_id)

    assignments = api.get_online_upload_assignments_with_due_date_in_range(
        course_id, start_date, end_date)
    for assignment in assignments:
        assignment_id = assignment["id"]
        assignment_name = assignment["name"].encode('ascii', 'ignore')
        due_date = datetime.strptime(assignment["due_at"],
                                     '%Y-%m-%dT%H:%M:%SZ')

        submissions = api.get_assignment_submissions(course_id, assignment_id)
        submission_counter = 0
        comment_counter = 0
        for submission in submissions:
            submission_counter = submission_counter + 1
            submission_comments = submission["submission_comments"]
            for submission_comment in submission_comments:
                if submission_comment[
                        "comment"] is not None and submission_comment[
                            "author_id"] == teacher_id:
                    comment_counter = comment_counter + 1
                    #print ("Comment Counter: " + str(comment_counter))

        teacherweeklyreportassignmentsrecord = TeacherWeeklyReportAssignments.objects.create(
            teacherweeklyreport=teacherweeklyreportrecord,
            assignment_id=assignment_id,
            assignment_name=assignment_name,
            due_date=due_date,
            submission_count=submission_counter,
            comment_count=comment_counter)
        teacherweeklyreportassignmentsrecord.save()
Пример #16
0
    def get(self, request, *args, **kwargs):
        course_id = None
        if 'course_id' in self.kwargs:
            course_id = int(self.kwargs['course_id'])
        reload = request.GET.get('reload', False) == "True"

        if course_id is not None:
            existing_records = self.model.objects.filter(
                course__course_id=course_id).first()

            if existing_records is None or reload:
                api = CanvasAPI()
                course = Course.objects.filter(course_id=course_id).first()
                self.model.objects.filter(course=course).all().delete()

                user_list = api.get_users(course_id)
                for user in user_list:
                    localuser = User.objects.filter(
                        canvas_id=int(user['id'])).first()
                    if localuser is not None:
                        self.model.user_objects.create(user=localuser,
                                                       course=course)

        return super(UserCourseListView, self).get(request, *args, **kwargs)
Пример #17
0
    def get(self, request, *args, **kwargs):
        course_id = int(self.kwargs['course_id'])
        reload = request.GET.get('reload', False) == "True"
        existing_records = self.model.objects.filter(
            assignment__course__course_id=course_id).first()
        course = CanvasCourse.objects.filter(course_id=course_id).first()

        if existing_records is None or reload:
            api = CanvasAPI(course.term.term_id)

            # Delete existing data
            self.model.objects.filter(assignment__course=course).all().delete()
            Assignment.objects.filter(course=course).all().delete()
            UserCourse.objects.filter(course=course).all().delete()

            json_data = api.get_assignments(course_id)
            json_list = list(json_data)  #the data from canvas

            for assignment in json_list:  #get the stuff i need from the canvas data
                assignment_id = assignment['id']
                assignment_name = assignment['name']
                has_override = assignment['has_overrides']
                is_quiz = assignment['is_quiz_assignment']

                td = timedelta(hours=6)  #adjust to local time
                if assignment['unlock_at'] is not None:
                    unlockCanvas = datetime.strptime(
                        assignment['unlock_at'],
                        '%Y-%m-%dT%H:%M:%SZ')  #save in datetime object
                    unlockCanvas = unlockCanvas - td  #adjust time.  else it goes past midnight altering the date
                    start_date = datetime.strftime(
                        unlockCanvas, '%Y-%m-%d'
                    )  #remove time and save just the date as a string
                else:
                    start_date = None
                if assignment['due_at'] is not None:
                    dueCanvas = datetime.strptime(assignment['due_at'],
                                                  '%Y-%m-%dT%H:%M:%SZ')
                    dueCanvas = dueCanvas - td
                    due_date = datetime.strftime(
                        dueCanvas, '%Y-%m-%d'
                    )  #saving date as string and in m/d/y for use with datepicker
                else:
                    due_date = None
                if assignment['lock_at'] is not None:
                    lockCanvas = datetime.strptime(assignment['lock_at'],
                                                   '%Y-%m-%dT%H:%M:%SZ')
                    lockCanvas = lockCanvas - td
                    end_date = datetime.strftime(lockCanvas, '%Y-%m-%d')
                else:
                    end_date = None

                Assignment.user_objects.create(assignment_id=assignment_id,
                                               name=assignment_name,
                                               start_date=start_date,
                                               due_date=due_date,
                                               end_date=end_date,
                                               has_override=has_override,
                                               is_quiz=is_quiz,
                                               course=course)

            student_list = api.get_students(course_id)
            for student in student_list:
                localstudent = User.objects.filter(
                    canvas_id=int(student['id'])).first()
                if localstudent is not None:
                    usercourse = UserCourse.objects.filter(
                        course=course).filter(user=localstudent).first()
                    if usercourse is None:
                        UserCourse.user_objects.create(user=localstudent,
                                                       course=course)

            json_data = api.get_submissions(course_id)
            json_list = list(json_data)  #the data from canvas

            for sub in json_list:
                student_id = sub['user_id']
                student = User.objects.filter(canvas_id=student_id).first()

                assignment_id = sub['assignment_id']
                assignment = Assignment.objects.filter(
                    assignment_id=assignment_id).first()

                submitted = False
                if sub['workflow_state'] != 'unsubmitted':
                    submitted = True
                late = sub['late']
                self.model.user_objects.create(student=student,
                                               assignment=assignment,
                                               submitted=submitted,
                                               late=late)

        return super(SubmissionListView, self).get(request, *args, **kwargs)
Пример #18
0
    def get(self, request, *args, **kwargs):
        course_id = None
        if 'course_id' in self.kwargs:
            course_id = int(self.kwargs['course_id'])
        reload = request.GET.get('reload', False) == "True"

        if course_id is not None:
            existing_records = self.model.objects.filter(
                course__course_id=course_id).first()

            if existing_records is None or reload:
                api = CanvasAPI()

                course = Course.objects.filter(course_id=course_id).first()
                self.model.objects.filter(course=course).all().delete()
                json_data = api.get_assignments(course_id)
                json_list = list(json_data)  #the data from canvas

                for assignment in json_list:  #get the stuff i need from the canvas data
                    assignment_id = assignment['id']
                    assignment_name = assignment['name']
                    has_override = assignment['has_overrides']
                    is_quiz = assignment['is_quiz_assignment']
                    logging.warning("ASSIGNMENT")
                    logging.warning(str(assignment))
                    td = timedelta(hours=6)  #adjust to local time
                    if assignment['unlock_at'] is not None:
                        unlockCanvas = datetime.strptime(
                            assignment['unlock_at'],
                            '%Y-%m-%dT%H:%M:%SZ')  #save in datetime object
                        unlockCanvas = unlockCanvas - td  #adjust time.  else it goes past midnight altering the date
                        start_date = datetime.strftime(
                            unlockCanvas, '%Y-%m-%d'
                        )  #remove time and save just the date as a string
                    else:
                        start_date = None
                    if assignment['due_at'] is not None:
                        dueCanvas = datetime.strptime(assignment['due_at'],
                                                      '%Y-%m-%dT%H:%M:%SZ')
                        dueCanvas = dueCanvas - td
                        due_date = datetime.strftime(
                            dueCanvas, '%Y-%m-%d'
                        )  #saving date as string and in m/d/y for use with datepicker
                    else:
                        due_date = None
                    if assignment['lock_at'] is not None:
                        lockCanvas = datetime.strptime(assignment['lock_at'],
                                                       '%Y-%m-%dT%H:%M:%SZ')
                        lockCanvas = lockCanvas - td
                        end_date = datetime.strftime(lockCanvas, '%Y-%m-%d')
                    else:
                        end_date = None

                    self.model.user_objects.create(assignment_id=assignment_id,
                                                   name=assignment_name,
                                                   start_date=start_date,
                                                   due_date=due_date,
                                                   end_date=end_date,
                                                   has_override=has_override,
                                                   is_quiz=is_quiz,
                                                   course=course)

        return super(AssignmentListView, self).get(request, *args, **kwargs)
Пример #19
0
    def get(self, request, *args, **kwargs):
        creds = UserCredentials()

        course_id = int(self.kwargs['course_id'])
        course = CanvasCourse.objects.filter(course_id=course_id).first()
        api = CanvasAPI(term=course.term.term_id)
        logging.warning("Term Set as : " + str(course.term.term_id))

        api.is_teacher_of_course(course_id, creds.get_OUNetID())
        if not api.is_teacher_of_course(course_id, creds.get_OUNetID()):
            raise PermissionDenied

        if ('assignment_id' in self.kwargs and 'student_id' in self.kwargs):
            self.assignment_id = self.kwargs['assignment_id']
            self.student_id = self.kwargs['student_id']
            self.page_title = "Extend Due Dates"

            json_data = api.get_assignment(course_id, self.assignment_id)

            override = api.get_assignment_override(course_id,
                                                   self.assignment_id,
                                                   self.student_id)
            if override:
                if 'due_at' in override:
                    json_data['due_at'] = override['due_at']
                else:
                    json_data['due_at'] = None
                if 'unlock_at' in override:
                    json_data['unlock_at'] = override['unlock_at']
                else:
                    json_data['unlock_at'] = None
                if 'lock_at' in override:
                    json_data['lock_at'] = override['lock_at']
                else:
                    json_data['lock_at'] = None
                json_data['has_overrides'] = True
            else:
                json_data['has_overrides'] = False

            json_list = [json_data]  #the data from canvas

        else:
            json_data = api.get_assignments(course_id)
            json_list = list(json_data)  #the data from canvas

        for assignment in json_list:  #get the stuff i need from the canvas data
            assignment_id = assignment['id']
            assignment_name = assignment['name']
            is_quiz = assignment['is_quiz_assignment']
            has_override = assignment['has_overrides']

            td = timedelta(hours=6)  #adjust to local time
            if assignment['unlock_at'] is not None:
                unlockCanvas = datetime.strptime(
                    assignment['unlock_at'],
                    '%Y-%m-%dT%H:%M:%SZ')  #save in datetime object
                unlockCanvas = unlockCanvas - td  #adjust time.  else it goes past midnight altering the date
                start_date = datetime.strftime(
                    unlockCanvas, '%Y-%m-%d'
                )  #remove time and save just the date as a string
            else:
                start_date = None
            if assignment['due_at'] is not None:
                dueCanvas = datetime.strptime(assignment['due_at'],
                                              '%Y-%m-%dT%H:%M:%SZ')
                dueCanvas = dueCanvas - td
                due_date = datetime.strftime(
                    dueCanvas, '%Y-%m-%d'
                )  #saving date as string and in m/d/y for use with datepicker
            else:
                due_date = None
            if assignment['lock_at'] is not None:
                lockCanvas = datetime.strptime(assignment['lock_at'],
                                               '%Y-%m-%dT%H:%M:%SZ')
                lockCanvas = lockCanvas - td
                end_date = datetime.strftime(lockCanvas, '%Y-%m-%d')
            else:
                end_date = None

            assignment_record = self.model.objects.filter(
                assignment_id=assignment_id)
            if assignment_record is None or not assignment_record:
                self.model.objects.create(assignment_id=assignment_id,
                                          name=assignment_name,
                                          start_date=start_date,
                                          due_date=due_date,
                                          end_date=end_date,
                                          has_override=has_override,
                                          is_quiz=is_quiz,
                                          course=course)
            else:
                assignment_record.update(assignment_id=assignment_id,
                                         name=assignment_name,
                                         start_date=start_date,
                                         due_date=due_date,
                                         end_date=end_date,
                                         has_override=has_override,
                                         is_quiz=is_quiz,
                                         course=course)

        return super(EditDueDates, self).get(request, *args, **kwargs)
Пример #20
0
def run_teacher_weekly_report(report_date=None):
    print("Report Date: " + str(report_date))

    if report_date is None:
        report_date = datetime.today()
    if type(report_date) is str:
        report_date = datetime.strptime(report_date, '%Y-%m-%d')

    (start_of_week, end_of_week,
     week_num) = getPrevWeekDateRangeFromDate(report_date)
    year = end_of_week.year
    start_of_week_str = start_of_week.strftime("%Y-%m-%d")
    end_of_week_str = end_of_week.strftime("%Y-%m-%d")

    # Get oldest active term and run report based on that
    oldestActiveTerm = ActiveTerm.objects.order_by(
        'active_term')[0].active_term.term_id
    api = CanvasAPI(term=oldestActiveTerm)

    # Delete existing records for this year/week
    TeacherWeeklyReport.objects.filter(year=year).filter(
        week_number=week_num).delete()

    print("Start of Week: " + str(start_of_week))
    print("End of Week: " + str(end_of_week))
    print("Start of Week Str: " + str(start_of_week_str))
    print("End of Week Str: " + str(end_of_week_str))
    print("Week Num: " + str(week_num))

    print("Current Term: " + str(api.TERM))

    term = Term.objects.filter(term_id=api.TERM).first()

    teachercourse_list = UserCourse.objects.filter(course__term=term).filter(
        is_teacher=True)
    for teachercourse in teachercourse_list:
        #teacher_weekly_report = TeacherWeeklyReport.objects.create()
        canvas_user_details = api.get_user_details(
            teachercourse.user.canvas_id)
        last_login = datetime.strptime(canvas_user_details["last_login"],
                                       '%Y-%m-%dT%H:%M:%SZ')

        announcements = api.get_course_announcements_for_date_range_by_teacher(
            teachercourse.course.course_id, teachercourse.user.canvas_id,
            start_of_week_str, end_of_week_str)
        num_announcements = len(announcements)
        #print ("TeacherCourse: CourseID:" + str(teachercourse.course) + " Announcements: " + str(num_announcements) + " LastLogin: "******"posted_at"], '%Y-%m-%dT%H:%M:%SZ')
            most_recent_announcement_content = announcements[counter][
                "message"]
            for announcement in announcements:
                current_date = datetime.strptime(announcement["posted_at"],
                                                 '%Y-%m-%dT%H:%M:%SZ')
                current_content = announcement["message"]
                if current_date > most_recent_announcement_date:
                    most_recent_announcement_date = current_date
                    most_recent_announcement_content = current_content
                counter = counter + 1
            #print ("Announcement Posted Date: " + str(most_recent_announcement_date))
            #print ("Announcement Posted Date: " + str(most_recent_announcement_date) + " Content: " + str(most_recent_announcement_content))
        else:
            announcement_posted = False
            #print ("No Announcements Posted")

        if announcement_posted:
            teacherweeklyreportrecord = TeacherWeeklyReport.objects.create(
                usercourse=teachercourse,
                start_date=start_of_week,
                end_date=end_of_week,
                year=year,
                week_number=week_num,
                last_login=last_login,
                announcement_posted=announcement_posted,
                announcement_post_date=most_recent_announcement_date,
                announcement_most_recent=most_recent_announcement_content)
        else:
            teacherweeklyreportrecord = TeacherWeeklyReport.objects.create(
                usercourse=teachercourse,
                start_date=start_of_week,
                end_date=end_of_week,
                year=year,
                week_number=week_num,
                last_login=last_login,
                announcement_posted=announcement_posted)

        teacherweeklyreportrecord.save()
        get_teacher_weekly_report_assignments(teacherweeklyreportrecord)
        get_teacher_weekly_report_discussions(teacherweeklyreportrecord)
Пример #21
0
def get_teacher_weekly_report_discussions(teacherweeklyreportrecord):

    start_date = teacherweeklyreportrecord.start_date
    end_date = teacherweeklyreportrecord.end_date
    course_id = teacherweeklyreportrecord.usercourse.course.course_id
    teacher_id = teacherweeklyreportrecord.usercourse.user.canvas_id

    course = Course.objects.filter(course_id=course_id).first()

    api = CanvasAPI(course.term.term_id)

    discussions = api.get_course_discussions_with_due_date_in_range(
        course_id, start_date, end_date)
    for discussion in discussions:
        discussion_id = discussion["id"]
        discussion_name = discussion["title"].encode('ascii', 'ignore')
        due_date = datetime.strptime(discussion["assignment"]["due_at"],
                                     '%Y-%m-%dT%H:%M:%SZ')
        discussion_assignment_id = discussion["assignment_id"]

        discussion_all_views = api.get_course_discussion_full(
            course_id, discussion_id)
        views = discussion_all_views["view"]

        teacher_top_level_reply_counter = 0
        top_level_reply_counter = 0
        reply_counter = 0
        for view in views:
            #print ("Current Submission: " + str(submission))
            if 'user_id' in view:
                if view["user_id"] == teacher_id:
                    teacher_top_level_reply_counter = teacher_top_level_reply_counter + 1
                top_level_reply_counter = top_level_reply_counter + 1
                if view.has_key("replies"):
                    replies = view["replies"]
                    for reply in replies:
                        if reply.has_key("user_id"):
                            if reply["user_id"] == teacher_id:
                                reply_counter = reply_counter + 1
                                #print ("Reply Counter: " + str(reply_counter))
                                break

        discussion_submissions = api.get_assignment_submissions(
            course_id, discussion_assignment_id)

        discussion_submission_counter = 0
        discussion_teacher_comment_counter = 0
        for discussion_submission in discussion_submissions:
            if 'missing' in discussion_submission:
                if discussion_submission['missing'] is False:
                    discussion_submission_counter = discussion_submission_counter + 1
                    submission_comments = discussion_submission[
                        "submission_comments"]
                    for submission_comment in submission_comments:
                        if submission_comment["author_id"] == teacher_id:
                            discussion_teacher_comment_counter = discussion_teacher_comment_counter + 1
                            break

        teacherweeklyreportdiscussionsrecord = TeacherWeeklyReportDiscussions.objects.create(teacherweeklyreport = teacherweeklyreportrecord, discussion_id = discussion_id, \
                                                                                             discussion_name = discussion_name, due_date = due_date, unique_entry_count = top_level_reply_counter, \
                                                                                             reply_count = reply_counter, teacher_unique_entry_count = teacher_top_level_reply_counter, \
                                                                                             submission_count = discussion_submission_counter, submission_comment_count = discussion_teacher_comment_counter)
        teacherweeklyreportdiscussionsrecord.save()