Esempio n. 1
0
    def post(self, request, course_id):
        res, response = check_authenticated_and(
            request, CourseMember.is_teacher_of(request.user.pk, course_id),
            USER_NOT_AUTHORIZED)
        if res == False:
            return response

        # Authorized as teacher
        form = TaskForm(request.POST)
        if form.is_valid():
            # Submitted fields: title, description, tips, close_date, answer
            task = form.save()
            task.course = get_object_or_404(Course, pk=course_id)
            task.creator = request.user
            task.save()
            messages.info(request, "Create Task successfully")
            return redirect(reverse("courses:task_list", args=[course_id]))

        context = self.get_context_data()
        context['form'] = form
        context['course_id'] = course_id
        return render(request, self.template_name, {
            "form": form,
            'course_id': course_id
        })
Esempio n. 2
0
def get_navigation(user):
    try:
        privilege = CourseMember.get_highest_privilege(user.pk)
        # User privilege: navi content
        # 4(Not even student): Homepage
        # 3(Student): My Courses, My Tasks, My Submissions
        # 2(TA): Student + Teaching
        # 1(Teacher): TA + Background
        # 0(Course Admin): Teacher

        # list(dict(name, url))
        navi_list = []
        if privilege <= 3:
            pass
            if privilege <= 2:
                if privilege <= 1:
                    if privilege == 0:
                        pass
        else:
            # 4 or something else
            pass
        return render_to_string("generics/navi.html", {"navi_list": navi_list})
    except Exception as e:
        print(e)
        return ""
Esempio n. 3
0
 def get(self, request, course_id):
     context = self.get_context_data()
     context['form'] = TaskForm()
     context['course_id'] = course_id
     context['is_teacher'] = (CourseMember.get_highest_course_privilege(
         request.user.pk, course_id) < 2)
     return render(request, self.template_name, context)
Esempio n. 4
0
 def get_context_data(self, *args, object_list=None, **kwargs):
     context = super().get_context_data(*args,
                                        object_list=object_list,
                                        **kwargs)
     context['course_id'] = self.course_id
     context['title'] = "Task List"
     context['is_teacher'] = (CourseMember.get_highest_course_privilege(
         self.request.user.pk, context['course_id']) < 2)
     return context
Esempio n. 5
0
 def test_func(self):
     if self.course_id == None:
         raise Exception("course_id not set")
     else:
         print("Check {} least({}) in Course({})".format(
             self.request.user.pk, self.least_privilege, self.course_id))
         return (CourseMember.get_highest_course_privilege(
             self.request.user.pk, self.course_id) <=
                 self.least_privilege) | self.request.user.is_superuser
     return False
Esempio n. 6
0
    def get(self, request, task_id):
        task = get_object_or_404(Task, pk=task_id)

        res, response = check_authenticated_and(
            request, CourseMember.is_teacher_of(request.user.pk,
                                                task.course.pk),
            USER_NOT_AUTHORIZED)
        if res == False:
            return response

        # Authorized user
        return super(SubmissionListView, self).get(self, request)
Esempio n. 7
0
 def get(self, request, task_id):
     task = get_object_or_404(Task, pk=task_id)
     if request.user.is_authenticated and CourseMember.is_member_of(
             request.user.pk, task.course.pk):
         # User is member
         privilege = CourseMember.get_highest_course_privilege(
             request.user.pk, task.course.pk)
         context = self.get_context_data()
         context['task'] = task
         if privilege == 3:
             # One and only one
             try:
                 submission = Submission.objects.get(
                     task__pk=task_id, submitter__pk=request.user.pk)
                 context['submission'] = submission
             except Submission.DoesNotExist:
                 # submission = None
                 submission = Submission(submitter=request.user, task=task)
                 submission.save()
             except Exception as e:
                 # More than one copy
                 print(e)
                 # messages.error("More than one copy for {} in {}".format(request.user.pk, task_id))
                 raise e
             finally:
                 context['submission'] = submission
                 try:
                     file = submission.file
                     print(file.name)
                 except:
                     context['to_submit'] = True
                     context['form'] = UploadFileForm()
         else:
             # link to submission list
             context["is_teacher"] = True
             # context['']
         return render(request, self.template_name, context=context)
     return return_error(request, USER_NOT_AUTHENTICATED)
Esempio n. 8
0
    def post(self, request, task_id):
        context = self.get_context_data()
        task = get_object_or_404(Task, pk=task_id)

        if request.user.is_authenticated and CourseMember.is_student_of(
                request.user.pk, task.course.pk):
            try:
                submission = Submission.objects.get(
                    task__pk=task_id, submitter__pk=request.user.pk)
                form = UploadFileForm(files=request.FILES)
                if form.is_valid():
                    file = form.save()
                    submission.file = file
                    submission.save()
                    messages.info(request, "Successfully submit homework")
                    return redirect(
                        reverse("courses:task_detail", args=[task_id]))
                messages.info(request, "Fail to validate form.")
                context['form'] = form
                print(form.errors)
                return render(request, self.template_name, context=context)
            except Submission.DoesNotExist:
                # Normal case
                form = UploadFileForm(files=request.FILES)
                if form.is_valid():
                    file = form.save()
                    submission = Submission(submitter=request.user,
                                            task=task,
                                            file=file)
                    submission.save()
                    messages.info(request, "Successfully submit homework")
                    return redirect(
                        reverse("courses:task_detail", args=[task_id]))
                messages.info(request, "Fail to validate form.")
                context['form'] = form
                print(form.errors)
                return render(request, self.template_name, context=context)
            except Exception as e:
                # More than one copy
                print(e)
                messages.error("More than one copy for {} in {}".format(
                    request.user.pk, task_id))
                return redirect(reverse("courses:task_detail", args=[task_id]))
        return redirect(reverse("courses:task_detail", args=[task_id]))