2020-04-29 23:20:50 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-05-04 19:02:57 +00:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2020-04-29 23:20:50 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2020-04-30 19:07:12 +00:00
|
|
|
from django.db.models import Q
|
2020-04-29 23:20:50 +00:00
|
|
|
from django.http import FileResponse
|
2020-05-04 19:02:57 +00:00
|
|
|
from django.shortcuts import redirect
|
2020-04-30 19:07:12 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-04-29 23:20:50 +00:00
|
|
|
from django.views import View
|
2020-05-04 18:21:53 +00:00
|
|
|
from django.views.generic import CreateView, UpdateView, DetailView
|
2020-04-30 19:07:12 +00:00
|
|
|
from django_tables2 import SingleTableView
|
2020-04-29 02:06:02 +00:00
|
|
|
|
2020-05-04 20:27:45 +00:00
|
|
|
from tournament.models import Team
|
|
|
|
from tournament.views import AdminMixin, TeamMixin
|
2020-05-04 18:21:53 +00:00
|
|
|
from .forms import SignUpForm, TFJMUserForm
|
2020-05-04 21:37:21 +00:00
|
|
|
from .models import TFJMUser, Document, Solution, MotivationLetter, Synthesis
|
2020-04-30 19:07:12 +00:00
|
|
|
from .tables import UserTable
|
2020-04-29 13:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateUserView(CreateView):
|
|
|
|
model = TFJMUser
|
|
|
|
form_class = SignUpForm
|
|
|
|
template_name = "registration/signup.html"
|
2020-04-29 23:20:50 +00:00
|
|
|
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
class MyAccountView(LoginRequiredMixin, UpdateView):
|
|
|
|
model = TFJMUser
|
|
|
|
form_class = TFJMUserForm
|
|
|
|
template_name = "member/my_account.html"
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
|
|
class UserDetailView(LoginRequiredMixin, DetailView):
|
|
|
|
model = TFJMUser
|
|
|
|
form_class = TFJMUserForm
|
2020-05-04 19:02:57 +00:00
|
|
|
context_object_name = "tfjmuser"
|
2020-05-04 18:21:53 +00:00
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2020-05-04 19:02:57 +00:00
|
|
|
if isinstance(request.user, AnonymousUser):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
if not request.user.admin \
|
2020-05-04 19:02:57 +00:00
|
|
|
and (self.object.team is not None and request.user not in self.object.team.tournament.organizers.all())\
|
2020-05-04 18:21:53 +00:00
|
|
|
and self.request.user != self.object:
|
|
|
|
raise PermissionDenied
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
2020-05-04 19:02:57 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if "view_as" in request.POST:
|
|
|
|
session = request.session
|
|
|
|
session["admin"] = request.user.pk
|
|
|
|
obj = self.get_object()
|
|
|
|
session["_fake_user_id"] = obj.pk
|
|
|
|
return redirect(request.path)
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
context["title"] = str(self.object)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-05-04 20:27:45 +00:00
|
|
|
class MyTeamView(TeamMixin, DetailView):
|
|
|
|
model = Team
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return self.request.user.team
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if isinstance(request.user, AnonymousUser):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
team = self.get_object()
|
|
|
|
|
|
|
|
if not request.user.participates or team is None:
|
|
|
|
raise PermissionDenied
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
context["title"] = str(self.object)
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-04-29 23:20:50 +00:00
|
|
|
class DocumentView(LoginRequiredMixin, View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
doc = Document.objects.get(file=self.kwargs["file"])
|
|
|
|
|
2020-05-04 21:37:21 +00:00
|
|
|
grant = request.user.admin
|
|
|
|
|
|
|
|
if isinstance(doc, Solution) or isinstance(doc, Synthesis) or isinstance(doc, MotivationLetter):
|
|
|
|
grant = grant or doc.team == request.user.team or request.user in doc.team.tournament.organizers.all()
|
|
|
|
|
|
|
|
if not grant:
|
2020-04-29 23:20:50 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
|
|
|
|
return FileResponse(doc.file, content_type="application/pdf")
|
2020-04-30 19:07:12 +00:00
|
|
|
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
class ProfileListView(AdminMixin, SingleTableView):
|
2020-04-30 19:07:12 +00:00
|
|
|
model = TFJMUser
|
|
|
|
queryset = TFJMUser.objects.order_by("role", "last_name", "first_name")
|
|
|
|
table_class = UserTable
|
|
|
|
template_name = "member/profile_list.html"
|
|
|
|
extra_context = dict(title=_("All profiles"))
|
|
|
|
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
class OrphanedProfileListView(AdminMixin, SingleTableView):
|
2020-04-30 19:07:12 +00:00
|
|
|
model = TFJMUser
|
|
|
|
queryset = TFJMUser.objects.filter((Q(role="2coach") | Q(role="3participant")) & Q(team__isnull=True))\
|
|
|
|
.order_by("role", "last_name", "first_name")
|
|
|
|
table_class = UserTable
|
|
|
|
template_name = "member/profile_list.html"
|
|
|
|
extra_context = dict(title=_("Orphaned profiles"))
|
|
|
|
|
|
|
|
|
2020-05-04 18:21:53 +00:00
|
|
|
class OrganizersListView(AdminMixin, SingleTableView):
|
2020-04-30 19:07:12 +00:00
|
|
|
model = TFJMUser
|
|
|
|
queryset = TFJMUser.objects.filter(Q(role="0admin") | Q(role="1volunteer"))\
|
|
|
|
.order_by("role", "last_name", "first_name")
|
|
|
|
table_class = UserTable
|
|
|
|
template_name = "member/profile_list.html"
|
|
|
|
extra_context = dict(title=_("Organizers"))
|
2020-05-04 19:02:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ResetAdminView(AdminMixin, View):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if "_fake_user_id" in request.session:
|
|
|
|
del request.session["_fake_user_id"]
|
|
|
|
return redirect(request.GET["path"])
|