示例#1
0
def connect_github(request):
    if request.method == 'POST':
        username = request.user.username
        github_username = request.POST.get('username')
        person = Person.objects.get(username=username)

        if github_username != config('GITHUB_ADMIN_USERNAME') and \
                not person.github_accounts.filter(username=github_username).exists():
            account = GitHubAccount(username=github_username)
            account.save()
            person.github_accounts.add(account)
            person.save()

            try:
                for course in person.courses.all():
                    repo = get_github_repo(username, course.id)
                    give_github_permissions(person, repo, "push")
            except GithubException as e:
                print(e)
                account.delete()
                return hredirect(request, '/error')

            return hredirect(request, "/manage_github/")

    return hrender(request, "upload/connect_github.html")
示例#2
0
def clear_error(request, username):
    errors = Error.objects.filter(user__username=username)
    if errors.exists():
        errors.delete()
    path = request.GET.get('q')
    print('Path', path)
    return hredirect(request, path)
示例#3
0
def courses(request):
    username = request.user.username
    if Person.objects.filter(username=username).exists():
        person = Person.objects.get(username=username)
        return hrender(request, 'upload/courses.html',
                       {'courses': person.courses.all()}, person)
    else:
        return hredirect(request, '/register/')
示例#4
0
def create_course(request):
    if request.method == 'POST':
        course_number = request.POST.get('course_number')
        section = request.POST.get('section')
        title = request.POST.get('title')
        term = request.POST.get('term')
        id = '{0}.{1}-{2}'.format(course_number.replace(' ', '-'), section, term)
        prof = Person.objects.get(pk=request.user.username)
        try:
            # TODO: check for course existence
            course = Course(id=id, number=course_number,
                            title=title, section=section, prof=prof)
            course.save()
        except Exception as e:
            print(e)
            return hredirect(request, '/error', person=prof)
        return hredirect(request, '/prof', person=prof)
    return hrender(request, 'upload/prof/create_course.html')
示例#5
0
def assignment_description(request, course_id, assignment_id):
    assignment = Assignment.objects.get(pk=assignment_id)
    if request.method == 'POST':
        # TODO: Delete already submitted assignments
        assignment.delete()
        return hredirect(request, '/prof/courses/{0}'.format(course_id))
    course = Course.objects.get(pk=course_id)
    return hrender(request,
                   'upload/prof/assignment_description.html',
                   {'assignment': assignment, 'course': course})
示例#6
0
def create_assignment(request, course_id):
    course = Course.objects.get(id=course_id)
    if request.method == 'POST':
        title = request.POST.get('title')
        description = request.POST.get('description')
        # TODO: time zones
        central = timezone('US/Central')
        due_date = central.localize(datetime.strptime(request.POST.get('due_date'),
                                                      '%Y-%m-%dT%H:%M'))
        try:
            # TODO: you can select any course, even ones you are not the prof of
            assignment = Assignment(title=title,
                                    description=description,
                                    course=course,
                                    deadline=due_date)
            assignment.save()
        except Exception as e:
            print(e)
            return hredirect(request, '/error')
        return hredirect(request, '/prof/courses/{0}'.format(course_id))
    return hrender(request,
                   'upload/prof/create_assignment.html', {'course': course})
示例#7
0
def edit_assignment(request, course_id, assignment_id):
    assignment = Assignment.objects.get(pk=assignment_id)
    course = Course.objects.get(pk=course_id)
    if request.method == 'POST':
        title = request.POST.get('title')
        description = request.POST.get('description')
        # TODO: time zones
        central = timezone('US/Central')
        due_date = central.localize(datetime.strptime(request.POST.get('due_date'),
                                                      '%Y-%m-%dT%H:%M'))
        try:
            assignment.title = title
            assignment.description = description
            assignment.course = course
            assignment.deadline = due_date
            assignment.save()
        except Exception as e:
            print(e)
            return hredirect(request, '/error')
        return hredirect(request, '/prof/courses/{0}/{1}/assignment_description'.format(course.id, assignment.id))
    return hrender(request,
                   'upload/prof/edit_assignment.html',
                   {'assignment': assignment, 'course': course})
示例#8
0
def register(request):
    if request.method == 'POST':
        # Create user if not exists (done here since it is the main redirect)
        username = request.user.username
        person = Person.objects.get(username=username)

        course_id = request.POST.get('course-id')

        try:
            course = Course.objects.get(id=course_id)
        except ObjectDoesNotExist:
            hredirect(request, '/error')

        # If person is not registered for the course
        if not person.courses.filter(id=course_id).exists():
            person.courses.add(course)
            person.save()

        target = threading.Thread(target=add_student_to_course,
                                  args=(username, course_id),
                                  daemon=True)
        target.start()
        return hredirect(request, '/courses')

    if request.user.is_authenticated:
        person = Person.objects.get(username=request.user.username)
        return hrender(
            request, 'upload/register.html', {
                'courses': [
                    course for course in Course.objects.all()
                    if course not in person.courses.all()
                ]
            })
    else:
        return hrender(request, 'upload/register.html',
                       {'courses': Course.objects.all()})
示例#9
0
def course(request, course_id):
    course = Course.objects.get(id=course_id)
    if request.method == 'POST':
        course.delete()
        return hredirect(request, '/prof/courses/')

    username = request.user.username
    assignments = Assignment.objects.filter(course__id=course_id).order_by('deadline')

    submissions_items = get_submission_items(username, course_id, None)
    # TODO: display variable length history (GUI toggle like in Moodle?)
    return hrender(request, 'upload/prof/course.html', {
                   'submissions': submissions_items[:HISTORY_LENGTH],
                   'course': course,
                   'assignments': assignments})
示例#10
0
def delete_grader(request, course_id):
    course = Course.objects.get(id=course_id)
    if request.method == 'POST':
        grader_username = request.POST.get('grader_username')
        grader = Person.objects.filter(pk=grader_username)
        if grader.exists():
            grader = grader.first()
            course.graders.remove(grader)
            course.save()
            for student in Person.objects.filter(courses__in=(course,)).all():
                repo = get_github_repo(student.username, course_id)
                remove_github_permissions(grader, repo, "pull")
            return hredirect(request, '/prof/courses/{0}'.format(course_id))
        else:
            prof = Person.objects.get(username=request.user.username)
            make_error(prof, "Grader does not exist")
            return hrender(request,
                           'upload/prof/assign_grader.html',
                           {'course': course})
    graders = course.graders.all()
    return hrender(request, 'upload/prof/delete_grader.html',
                   {'course': course, 'graders': graders})
示例#11
0
def course(request, course_id):
    username = request.user.username
    course = Course.objects.get(id=course_id)
    # TODO: think about deleting github repos
    if request.method == 'POST':
        student = Person.objects.get(username=username)
        student.courses.remove(course)
        student.save()
        return hredirect(request, '/courses/', person=student)

    submissions_items = get_submission_items(username, course_id, None)

    assignments = Assignment.objects.filter(
        course__id=course_id).order_by('deadline')

    # TODO: display variable length history
    # (GUI toggle like in Moodle?)
    return hrender(
        request, 'upload/course.html', {
            'submissions': submissions_items[:HISTORY_LENGTH],
            'course': course,
            'assignments': assignments
        })
示例#12
0
def upload_assignment(request, course_id, assignment_id):
    username = request.user.username
    course_directory = os.path.join(MEDIA_ROOT, username, course_id)
    pending_submissions = File.objects.filter(submission__isnull=True,
                                              person__username=username,
                                              assignment__id=assignment_id)

    if Assignment.objects.filter(id=assignment_id).exists():
        assignment = Assignment.objects.get(id=assignment_id)
        assignment_title = assignment.title.replace(' ', '_')
    else:
        assignment, assignment_title = None, None

    if request.method == 'POST':
        if "clear" in request.POST:
            # if a file was cleared out of the upload box, remove it
            # from the pending submissions and delete it
            file_id = request.POST["clear"]
            file = File.objects.get(id=file_id)
            # check to make sure the same file isn't being submitted
            # for another assignment before deleting it
            clear_file(assignment_id, file, username)
            pending_submissions.exclude(file=file)
            form = FileForm()
        elif "submit" in request.POST:

            # if the form is ready to submit, make a new submission entry
            # in the database with the current pending files
            # then add to Git, commit and push to GitHub
            if pending_submissions:
                commit_message = request.POST["description"]
                submission = Submission.objects.create(
                    description=commit_message, assignment=assignment)

                file_paths = []
                for file in pending_submissions:
                    file_paths.append(os.path.join(MEDIA_ROOT, file.file.name))
                    file.submission = submission
                    file.save()

                submit(username, course_id, file_paths, commit_message,
                       assignment_title)

                return hredirect(
                    request, '/submitted/{}/{}'.format(course_id,
                                                       assignment_id))
            else:
                form = FileForm()
        else:
            # if a file was uploaded but not submitted yet,
            # save it to the database and return its data so the
            # JavaScript can display it in the upload box
            form = FileForm(request.POST, request.FILES)
            if form.is_valid():
                file = form.save(commit=False)
                file.person = Person.objects.get(username=username)
                file.assignment = Assignment.objects.get(id=assignment_id)
                file.save()
                data = {
                    'is_valid': True,
                    'name': file.file.name,
                    'url': file.file.url,
                    'id': file.id
                }
            else:
                data = {'is_valid': False}
            return JsonResponse(data)
    # if no file is being uploaded, display an empty form
    else:
        form = FileForm()

    repo_directory = os.path.join(course_directory, ".git")
    repo_name = "{}-{}".format(course_id, username)

    if not os.path.exists(repo_directory):
        try:
            clone_course_repo(course_id, repo_name, username)
        except GithubException as e:
            print(e)
            return hredirect(request, '/error/')

    if Assignment.objects.filter(id=assignment_id).exists():
        assignment = Assignment.objects.get(id=assignment_id)
    else:
        assignment = None

    submissions_items = get_submission_items(username, course_id,
                                             assignment_id)

    # if a submission has been made, a branch corresponding
    # to the assignment will have been created, so link to it.
    # otherwise, link to the main github page
    if submissions_items:
        url = get_branch_url(repo_name, assignment_title)
    else:
        url = get_github_url(repo_name)

    return hrender(
        request, 'upload/upload.html', {
            'form': form,
            'course': Course.objects.get(id=course_id),
            'url': url,
            'pending': pending_submissions,
            'assignment': assignment,
            'submissions': submissions_items[:HISTORY_LENGTH]
        })
示例#13
0
def logout(request):
    """Logs out user"""
    auth_logout(request)
    return hredirect(request, '/')
示例#14
0
def home(request, next=''):
    if request.user.username:
        return hredirect(request, "/courses/")
    return hrender(request, 'upload/home.html')