Newer
Older
from __future__ import annotations
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from typing import TYPE_CHECKING, Any
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.db import IntegrityError, transaction
from django.shortcuts import get_object_or_404, redirect, render # noqa
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _, pgettext
from pytools.lex import RE as REBase # noqa
from course.auth import UserSearchWidget
PARTICIPATION_PERMISSION_CHOICES,
participation_permission as pperm,
Course,
Participation,
ParticipationPermission,
ParticipationPreapproval,
ParticipationRole,
ParticipationTag,
participation_status,
user_status,
)
from course.utils import LanguageOverride, course_view, render_course_page
from relate.utils import StyledForm, StyledModelForm, string_concat
if TYPE_CHECKING:
import accounts.models
from course.utils import CoursePageContext
Andreas Klöckner
committed
# {{{ get_participation_for_{user,request}
def get_participation_for_user(
user: accounts.models.User | AnonymousUser, course: Course
) -> Participation | None:
if not user.is_authenticated:
return None
participations = list(Participation.objects.filter(
user=user,
course=course,
status=participation_status.active
))
# The uniqueness constraint should have ensured that.
assert len(participations) <= 1
if len(participations) == 0:
return None
return participations[0]
Andreas Klöckner
committed
def get_participation_for_request(
request: http.HttpRequest, course: Course) -> Participation | None:
Andreas Klöckner
committed
return get_participation_for_user(request.user, course)
# }}}
# {{{ get_participation_role_identifiers
def get_participation_role_identifiers(
course: Course, participation: Participation | None) -> list[str]:
if participation is None:
ParticipationRole.objects.filter(
course=course,
is_default_for_unenrolled=True)
else:
return [r.identifier for r in participation.roles.all()]
# }}}
course: Course,
participation: Participation | None,
) -> frozenset[tuple[str, str | None]]:
if participation is not None:
return participation.permissions()
else:
from course.models import ParticipationRolePermission
perm_list = list(
ParticipationRolePermission.objects.filter(
role__is_default_for_unenrolled=True)
.values_list("permission", "argument"))
perm = frozenset(
(permission, argument) if argument else (permission, None)
for permission, argument in perm_list)
return perm
# {{{ enrollment
@login_required
def enroll_view(
request: http.HttpRequest, course_identifier: str) -> http.HttpResponse:
course = get_object_or_404(Course, identifier=course_identifier)
participations = Participation.objects.filter(course=course, user=user)
if not participations.count():
participation = None
else:
participation = participations.first()
Andreas Klöckner
committed
if participation is not None:
if participation.status == participation_status.requested:
messages.add_message(request, messages.ERROR,
_("You have previously sent the enrollment "
"request. Re-sending the request is not "
"allowed."))
return redirect("relate-course_page", course_identifier)
elif participation.status == participation_status.denied:
messages.add_message(request, messages.ERROR,
_("Your enrollment request had been denied. "
"Enrollment is not allowed."))
return redirect("relate-course_page", course_identifier)
elif participation.status == participation_status.dropped:
messages.add_message(request, messages.ERROR,
_("You had been dropped from the course. "
"Re-enrollment is not allowed."))
return redirect("relate-course_page", course_identifier)
else:
assert participation.status == participation_status.active
messages.add_message(request, messages.ERROR,
_("Already enrolled. Cannot re-enroll."))
return redirect("relate-course_page", course_identifier)
if not course.accepts_enrollment:
messages.add_message(request, messages.ERROR,
return redirect("relate-course_page", course_identifier)
if request.method != "POST":
# This can happen if someone tries to refresh the page, or switches to
# desktop view on mobile.
messages.add_message(request, messages.ERROR,
_("Can only enroll using POST request"))
return redirect("relate-course_page", course_identifier)
Loading
Loading full blame...