def enroll(status, role): participations = Participation.objects.filter(course=course, user=user) assert participations.count() <= 1 if participations.count() == 0: participation = Participation() participation.user = user participation.course = course participation.role = role participation.status = status participation.save() else: (participation, ) = participations participation.status = status participation.save() return participation
def enroll(status, role): participations = Participation.objects.filter(course=course, user=user) assert participations.count() <= 1 if participations.count() == 0: participation = Participation() participation.user = user participation.course = course participation.role = role participation.status = status participation.save() else: (participation,) = participations participation.status = status participation.save() return participation
def handle_enrollment_request(course, user, status, role, request=None): participations = Participation.objects.filter(course=course, user=user) assert participations.count() <= 1 if participations.count() == 0: participation = Participation() participation.user = user participation.course = course participation.role = role participation.status = status participation.save() else: (participation,) = participations participation.status = status participation.save() if status == participation_status.active: send_enrollment_decision(participation, True, request) elif status == participation_status.denied: send_enrollment_decision(participation, False, request) else: return
def handle_enrollment_request(course, user, status, role, request=None): participations = Participation.objects.filter(course=course, user=user) assert participations.count() <= 1 if participations.count() == 0: participation = Participation() participation.user = user participation.course = course participation.role = role participation.status = status participation.save() else: (participation, ) = participations participation.status = status participation.save() if status == participation_status.active: send_enrollment_decision(participation, True, request) elif status == participation_status.denied: send_enrollment_decision(participation, False, request) else: return
def set_up_new_course(request): if not request.user.is_staff: raise PermissionDenied(_("only staff may create courses")) if request.method == "POST": form = CourseCreationForm(request.POST) if form.is_valid(): new_course = form.save(commit=False) from course.content import get_course_repo_path repo_path = get_course_repo_path(new_course) try: import os os.makedirs(repo_path) repo = None try: with transaction.atomic(): from dulwich.repo import Repo repo = Repo.init(repo_path) client, remote_path = \ get_dulwich_client_and_remote_path_from_course( new_course) remote_refs = client.fetch(remote_path, repo) transfer_remote_refs(repo, remote_refs) new_sha = repo[b"HEAD"] = remote_refs[b"HEAD"] vrepo = repo if new_course.course_root_path: from course.content import SubdirRepoWrapper vrepo = SubdirRepoWrapper( vrepo, new_course.course_root_path) from course.validation import validate_course_content validate_course_content( vrepo, new_course.course_file, new_course.events_file, new_sha) del repo del vrepo new_course.active_git_commit_sha = new_sha.decode() new_course.save() # {{{ set up a participation for the course creator part = Participation() part.user = request.user part.course = new_course part.role = participation_role.instructor part.status = participation_status.active part.save() # }}} messages.add_message(request, messages.INFO, _("Course content validated, creation " "succeeded.")) except: # Don't coalesce this handler with the one below. We only want # to delete the directory if we created it. Trust me. # Work around read-only files on Windows. # https://docs.python.org/3.5/library/shutil.html#rmtree-example import os import stat import shutil # Make sure files opened for 'repo' above are actually closed. if repo is not None: # noqa repo.close() # noqa def remove_readonly(func, path, _): # noqa "Clear the readonly bit and reattempt the removal" os.chmod(path, stat.S_IWRITE) func(path) try: shutil.rmtree(repo_path, onerror=remove_readonly) except OSError: messages.add_message(request, messages.WARNING, ugettext("Failed to delete unused " "repository directory '%s'.") % repo_path) raise except Exception as e: from traceback import print_exc print_exc() messages.add_message(request, messages.ERROR, string_concat( _("Course creation failed"), ": %(err_type)s: %(err_str)s") % {"err_type": type(e).__name__, "err_str": str(e)}) else: return redirect( "relate-course_page", new_course.identifier) else: form = CourseCreationForm() return render(request, "generic-form.html", { "form_description": _("Set up new course"), "form": form })
def set_up_new_course(request): if not request.user.is_staff: raise PermissionDenied("only staff may create courses") if request.method == "POST": form = CourseCreationForm(request.POST) if form.is_valid(): new_course = form.save(commit=False) from course.content import get_course_repo_path repo_path = get_course_repo_path(new_course) try: import os os.makedirs(repo_path) try: with transaction.atomic(): from dulwich.repo import Repo repo = Repo.init(repo_path) client, remote_path = \ get_dulwich_client_and_remote_path_from_course( new_course) remote_refs = client.fetch(remote_path, repo) new_sha = repo["HEAD"] = remote_refs["HEAD"] from course.validation import validate_course_content validate_course_content( repo, new_course.course_file, new_course.events_file, new_sha) new_course.valid = True new_course.active_git_commit_sha = new_sha new_course.save() # {{{ set up a participation for the course creator part = Participation() part.user = request.user part.course = new_course part.role = participation_role.instructor part.status = participation_status.active part.save() # }}} messages.add_message(request, messages.INFO, "Course content validated, creation succeeded. " "You may want to view the events used " "in the course content and create them. " + '<a href="%s" class="btn btn-primary">' 'Check »</a>' % reverse("course.calendar.check_events", args=(new_course.identifier,))) except: # Don't coalesce this handler with the one below. We only want # to delete the directory if we created it. Trust me. import shutil shutil.rmtree(repo_path) raise except Exception as e: from traceback import print_exc print_exc() messages.add_message(request, messages.ERROR, "Course creation failed: %s: %s" % ( type(e).__name__, str(e))) else: return redirect( "course.views.course_page", new_course.identifier) else: form = CourseCreationForm() return render(request, "generic-form.html", { "form_description": "Set up new course", "form": form })
def set_up_new_course(request): if not request.user.is_staff: raise PermissionDenied(_("only staff may create courses")) if request.method == "POST": form = CourseCreationForm(request.POST) if form.is_valid(): new_course = form.save(commit=False) from course.content import get_course_repo_path repo_path = get_course_repo_path(new_course) try: import os os.makedirs(repo_path) repo = None try: with transaction.atomic(): from dulwich.repo import Repo repo = Repo.init(repo_path) client, remote_path = \ get_dulwich_client_and_remote_path_from_course( new_course) remote_refs = client.fetch(remote_path, repo) transfer_remote_refs(repo, remote_refs) new_sha = repo[b"HEAD"] = remote_refs[b"HEAD"] vrepo = repo if new_course.course_root_path: from course.content import SubdirRepoWrapper vrepo = SubdirRepoWrapper( vrepo, new_course.course_root_path) from course.validation import validate_course_content validate_course_content(vrepo, new_course.course_file, new_course.events_file, new_sha) del repo del vrepo new_course.active_git_commit_sha = new_sha.decode() new_course.save() # {{{ set up a participation for the course creator part = Participation() part.user = request.user part.course = new_course part.role = participation_role.instructor part.status = participation_status.active part.save() # }}} messages.add_message( request, messages.INFO, _("Course content validated, creation " "succeeded.")) except: # Don't coalesce this handler with the one below. We only want # to delete the directory if we created it. Trust me. # Work around read-only files on Windows. # https://docs.python.org/3.5/library/shutil.html#rmtree-example import os import stat import shutil # Make sure files opened for 'repo' above are actually closed. if repo is not None: # noqa repo.close() # noqa def remove_readonly(func, path, _): # noqa "Clear the readonly bit and reattempt the removal" os.chmod(path, stat.S_IWRITE) func(path) try: shutil.rmtree(repo_path, onerror=remove_readonly) except OSError: messages.add_message( request, messages.WARNING, ugettext("Failed to delete unused " "repository directory '%s'.") % repo_path) raise except Exception as e: from traceback import print_exc print_exc() messages.add_message( request, messages.ERROR, string_concat(_("Course creation failed"), ": %(err_type)s: %(err_str)s") % { "err_type": type(e).__name__, "err_str": str(e) }) else: return redirect("relate-course_page", new_course.identifier) else: form = CourseCreationForm() return render(request, "generic-form.html", { "form_description": _("Set up new course"), "form": form })