2020-03-01 16:16:38 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-31 02:16:30 +00:00
|
|
|
|
2020-03-30 15:27:02 +00:00
|
|
|
from datetime import datetime, timezone
|
2020-03-01 16:16:38 +00:00
|
|
|
|
2020-03-27 12:50:02 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2020-03-28 00:45:13 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.db.models import F, Q
|
2020-03-27 15:19:33 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-03-26 23:40:35 +00:00
|
|
|
from django.views.generic import CreateView, DetailView, UpdateView, TemplateView
|
2020-03-27 00:31:54 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-03-01 16:16:38 +00:00
|
|
|
from django_tables2.views import SingleTableView
|
2020-03-28 15:52:58 +00:00
|
|
|
from note.models import NoteUser, Alias, NoteSpecial
|
2020-03-27 20:18:27 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-03-31 02:16:30 +00:00
|
|
|
from permission.views import ProtectQuerysetMixin
|
2020-03-28 00:45:13 +00:00
|
|
|
|
2020-03-27 17:02:22 +00:00
|
|
|
from .forms import ActivityForm, GuestForm
|
2020-03-28 15:52:58 +00:00
|
|
|
from .models import Activity, Guest, Entry
|
2020-03-28 00:45:13 +00:00
|
|
|
from .tables import ActivityTable, GuestTable, EntryTable
|
2020-03-26 23:40:35 +00:00
|
|
|
|
2020-03-01 16:16:38 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ActivityCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
2020-03-01 16:16:38 +00:00
|
|
|
model = Activity
|
2020-03-27 00:31:54 +00:00
|
|
|
form_class = ActivityForm
|
2020-03-27 20:18:27 +00:00
|
|
|
|
2020-03-28 18:05:21 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.creater = self.request.user
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
2020-03-27 20:18:27 +00:00
|
|
|
def get_success_url(self, **kwargs):
|
2020-03-28 18:05:21 +00:00
|
|
|
self.object.refresh_from_db()
|
|
|
|
return reverse_lazy('activity:activity_detail', kwargs={"pk": self.object.pk})
|
2020-03-26 23:40:35 +00:00
|
|
|
|
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ActivityListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
2020-03-01 16:16:38 +00:00
|
|
|
model = Activity
|
2020-03-27 17:02:22 +00:00
|
|
|
table_class = ActivityTable
|
2020-03-27 00:31:54 +00:00
|
|
|
|
2020-03-28 18:05:21 +00:00
|
|
|
def get_queryset(self):
|
2020-03-31 02:16:30 +00:00
|
|
|
return super().get_queryset().reverse()
|
2020-03-28 18:05:21 +00:00
|
|
|
|
2020-03-27 00:31:54 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
ctx = super().get_context_data(**kwargs)
|
|
|
|
|
2020-03-28 15:52:58 +00:00
|
|
|
ctx['title'] = _("Activities")
|
|
|
|
|
|
|
|
upcoming_activities = Activity.objects.filter(date_end__gt=datetime.now())
|
2020-03-28 18:05:21 +00:00
|
|
|
ctx['upcoming'] = ActivityTable(data=upcoming_activities
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view")))
|
2020-03-27 00:31:54 +00:00
|
|
|
|
|
|
|
return ctx
|
2020-03-01 16:16:38 +00:00
|
|
|
|
2020-03-26 23:40:35 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ActivityDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
2020-03-01 16:16:38 +00:00
|
|
|
model = Activity
|
2020-03-27 18:47:43 +00:00
|
|
|
context_object_name = "activity"
|
2020-03-01 16:16:38 +00:00
|
|
|
|
2020-03-27 20:18:27 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
ctx = super().get_context_data()
|
|
|
|
|
|
|
|
table = GuestTable(data=Guest.objects.filter(activity=self.object)
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Guest, "view")))
|
|
|
|
ctx["guests"] = table
|
|
|
|
|
2020-03-30 15:27:02 +00:00
|
|
|
ctx["activity_started"] = datetime.now(timezone.utc) > self.object.date_start
|
|
|
|
|
2020-03-27 20:18:27 +00:00
|
|
|
return ctx
|
|
|
|
|
2020-03-26 23:40:35 +00:00
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ActivityUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView):
|
2020-03-01 16:16:38 +00:00
|
|
|
model = Activity
|
2020-03-27 00:31:54 +00:00
|
|
|
form_class = ActivityForm
|
2020-03-27 20:18:27 +00:00
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]})
|
2020-03-26 23:40:35 +00:00
|
|
|
|
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
class ActivityInviteView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView):
|
2020-03-27 17:02:22 +00:00
|
|
|
model = Guest
|
|
|
|
form_class = GuestForm
|
|
|
|
template_name = "activity/activity_invite.html"
|
|
|
|
|
2020-03-29 22:42:32 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class)
|
2020-03-31 02:16:30 +00:00
|
|
|
form.activity = Activity.objects.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\
|
|
|
|
.get(pk=self.kwargs["pk"])
|
2020-03-29 22:42:32 +00:00
|
|
|
return form
|
|
|
|
|
2020-03-27 18:16:38 +00:00
|
|
|
def form_valid(self, form):
|
2020-03-31 02:16:30 +00:00
|
|
|
form.instance.activity = Activity.objects\
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view")).get(pk=self.kwargs["pk"])
|
2020-03-27 18:16:38 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]})
|
|
|
|
|
2020-03-27 17:02:22 +00:00
|
|
|
|
2020-03-27 12:50:02 +00:00
|
|
|
class ActivityEntryView(LoginRequiredMixin, TemplateView):
|
2020-03-28 00:45:13 +00:00
|
|
|
template_name = "activity/activity_entry.html"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
ctx = super().get_context_data(**kwargs)
|
|
|
|
|
2020-03-31 02:16:30 +00:00
|
|
|
activity = Activity.objects.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\
|
|
|
|
.get(pk=self.kwargs["pk"])
|
2020-03-28 00:45:13 +00:00
|
|
|
ctx["activity"] = activity
|
|
|
|
|
|
|
|
matched = []
|
|
|
|
|
|
|
|
pattern = "^$"
|
|
|
|
if "search" in self.request.GET:
|
|
|
|
pattern = self.request.GET["search"]
|
|
|
|
|
2020-03-28 12:38:31 +00:00
|
|
|
if not pattern:
|
|
|
|
pattern = "^$"
|
|
|
|
|
|
|
|
if pattern[0] != "^":
|
|
|
|
pattern = "^" + pattern
|
2020-03-28 00:45:13 +00:00
|
|
|
|
|
|
|
guest_qs = Guest.objects\
|
|
|
|
.annotate(balance=F("inviter__balance"), note_name=F("inviter__user__username"))\
|
|
|
|
.filter(Q(first_name__regex=pattern) | Q(last_name__regex=pattern)
|
|
|
|
| Q(inviter__alias__name__regex=pattern)
|
2020-03-28 18:05:21 +00:00
|
|
|
| Q(inviter__alias__normalized_name__regex=Alias.normalize(pattern))) \
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Guest, "view"))\
|
2020-03-28 00:45:13 +00:00
|
|
|
.distinct()[:20]
|
|
|
|
for guest in guest_qs:
|
|
|
|
guest.type = "Invité"
|
|
|
|
matched.append(guest)
|
|
|
|
|
|
|
|
note_qs = Alias.objects.annotate(last_name=F("note__noteuser__user__last_name"),
|
|
|
|
first_name=F("note__noteuser__user__first_name"),
|
|
|
|
username=F("note__noteuser__user__username"),
|
|
|
|
note_name=F("name"),
|
|
|
|
balance=F("note__balance"))\
|
|
|
|
.filter(Q(note__polymorphic_ctype__model="noteuser")
|
|
|
|
& (Q(note__noteuser__user__first_name__regex=pattern)
|
|
|
|
| Q(note__noteuser__user__last_name__regex=pattern)
|
2020-03-28 12:38:31 +00:00
|
|
|
| Q(name__regex=pattern)
|
2020-03-28 18:05:21 +00:00
|
|
|
| Q(normalized_name__regex=Alias.normalize(pattern)))) \
|
|
|
|
.filter(PermissionBackend.filter_queryset(self.request.user, Alias, "view"))\
|
2020-03-28 12:38:31 +00:00
|
|
|
.distinct("username")[:20]
|
2020-03-28 00:45:13 +00:00
|
|
|
for note in note_qs:
|
|
|
|
note.type = "Adhérent"
|
2020-03-28 15:52:58 +00:00
|
|
|
note.activity = activity
|
2020-03-28 00:45:13 +00:00
|
|
|
matched.append(note)
|
|
|
|
|
|
|
|
table = EntryTable(data=matched)
|
|
|
|
ctx["table"] = table
|
|
|
|
|
2020-03-28 15:52:58 +00:00
|
|
|
ctx["entries"] = Entry.objects.filter(activity=activity)
|
|
|
|
|
2020-03-28 00:45:13 +00:00
|
|
|
ctx["title"] = _('Entry for activity "{}"').format(activity.name)
|
|
|
|
ctx["noteuser_ctype"] = ContentType.objects.get_for_model(NoteUser).pk
|
2020-03-28 15:52:58 +00:00
|
|
|
ctx["notespecial_ctype"] = ContentType.objects.get_for_model(NoteSpecial).pk
|
2020-03-28 00:45:13 +00:00
|
|
|
|
|
|
|
return ctx
|