Newer
Older
# -*- coding: utf-8 -*-
from __future__ import division
__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 django.utils.translation import (
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.urls import reverse
from django.db import transaction, IntegrityError
Andreas Klöckner
committed
from django import http # noqa
from django.utils.safestring import mark_safe
from crispy_forms.layout import Submit
Andreas Klöckner
committed
Participation,
ParticipationPreapproval,
Andreas Klöckner
committed
ParticipationRole,
Andreas Klöckner
committed
participation_status)
from course.constants import (
PARTICIPATION_PERMISSION_CHOICES,
Andreas Klöckner
committed
participation_permission as pperm,
)
from course.auth import UserSearchWidget
from course.utils import course_view, render_course_page, LanguageOverride
from relate.utils import StyledForm, StyledModelForm, string_concat
from typing import Any, Tuple, Text, Optional, List, FrozenSet, TYPE_CHECKING # noqa
if TYPE_CHECKING:
Andreas Klöckner
committed
# {{{ get_participation_for_{user,request}
Andreas Klöckner
committed
def get_participation_for_user(user, course):
# type: (accounts.models.User, Course) -> Optional[Participation]
# "wake up" lazy object
# http://stackoverflow.com/questions/20534577/int-argument-must-be-a-string-or-a-number-not-simplelazyobject # noqa
try:
possible_user = user._wrapped
except AttributeError:
pass
else:
if isinstance(possible_user, get_user_model()):
user = possible_user
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, course):
# type: (http.HttpRequest, Course) -> Optional[Participation]
return get_participation_for_user(request.user, course)
# }}}
# {{{ get_participation_role_identifiers
def get_participation_role_identifiers(course, participation):
# type: (Course, Optional[Participation]) -> List[Text]
if participation is None:
return (
ParticipationRole.objects.filter(
course=course,
is_default_for_unenrolled=True)
else:
return [r.identifier for r in participation.roles.all()]
# }}}
course, # type: Course
participation, # type: Optional[Participation]
):
# type: (...) -> FrozenSet[Tuple[Text, Optional[Text]]]
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, course_identifier):
Andreas Klöckner
committed
# type: (http.HttpRequest, str) -> http.HttpResponse
course = get_object_or_404(Course, identifier=course_identifier)
user = request.user
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,
Loading
Loading full blame...