From d6e202a26f78f59cd4b26d91b6406b409ee96b36 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 27 Mar 2020 21:18:27 +0100 Subject: [PATCH] Display guests list --- apps/activity/tables.py | 43 ++++++++---- apps/activity/views.py | 20 +++++- apps/permission/templatetags/perms.py | 7 ++ templates/activity/activity_detail.html | 91 +++++++++++++++++-------- 4 files changed, 113 insertions(+), 48 deletions(-) diff --git a/apps/activity/tables.py b/apps/activity/tables.py index 44939bc4..95f43e51 100644 --- a/apps/activity/tables.py +++ b/apps/activity/tables.py @@ -2,19 +2,17 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.utils.translation import gettext_lazy as _ -from django_tables2 import tables, A +import django_tables2 as tables +from django_tables2 import A from .models import Activity, Guest class ActivityTable(tables.Table): - name = tables.columns.LinkColumn('activity:activity_detail', - args=[A('pk'), ],) - - invite = tables.columns.LinkColumn('activity:activity_invite', - args=[A('pk'), ], - verbose_name=_("Invite"), - text=_("Invite"),) + name = tables.LinkColumn( + 'activity:activity_detail', + args=[A('pk'), ], + ) class Meta: attrs = { @@ -22,19 +20,34 @@ class ActivityTable(tables.Table): } model = Activity template_name = 'django_tables2/bootstrap4.html' - fields = ('name', 'activity_type', 'organizer', 'attendees_club', 'date_start', 'date_end', 'invite', ) + fields = ('name', 'activity_type', 'organizer', 'attendees_club', 'date_start', 'date_end', ) class GuestTable(tables.Table): + inviter = tables.LinkColumn( + 'member:user_detail', + args=[A('inviter.user.pk'), ], + ) + + entry = tables.Column( + empty_values=(), + attrs={ + "td": { + "class": lambda record: "" if record.entry else "validate btn btn-danger", + "onclick": lambda record: "" if record.entry else "remove_guest(" + str(record.pk) + ")" + } + } + ) + class Meta: attrs = { 'class': 'table table-condensed table-striped table-hover' } model = Guest template_name = 'django_tables2/bootstrap4.html' - fields = ('name', 'inviter', ) - row_attrs = { - 'class': 'table-row', - 'id': lambda record: "row-" + str(record.pk), - 'data-href': lambda record: record.pk - } + fields = ("last_name", "first_name", "inviter", ) + + def render_entry(self, record): + if record.entry: + return str(record.date) + return _("remove").capitalize() diff --git a/apps/activity/views.py b/apps/activity/views.py index 952ea59c..38e487e0 100644 --- a/apps/activity/views.py +++ b/apps/activity/views.py @@ -7,15 +7,18 @@ from django.views.generic import CreateView, DetailView, UpdateView, TemplateVie from django.utils.translation import gettext_lazy as _ from django_tables2.views import SingleTableView +from permission.backends import PermissionBackend from .forms import ActivityForm, GuestForm from .models import Activity, Guest -from .tables import ActivityTable +from .tables import ActivityTable, GuestTable class ActivityCreateView(LoginRequiredMixin, CreateView): model = Activity form_class = ActivityForm - success_url = reverse_lazy('activity:activity_list') + + def get_success_url(self, **kwargs): + return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]}) class ActivityListView(LoginRequiredMixin, SingleTableView): @@ -34,11 +37,22 @@ class ActivityDetailView(LoginRequiredMixin, DetailView): model = Activity context_object_name = "activity" + 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 + + return ctx + class ActivityUpdateView(LoginRequiredMixin, UpdateView): model = Activity form_class = ActivityForm - success_url = reverse_lazy('activity:activity_list') + + def get_success_url(self, **kwargs): + return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]}) class ActivityInviteView(LoginRequiredMixin, CreateView): diff --git a/apps/permission/templatetags/perms.py b/apps/permission/templatetags/perms.py index 8bcd3597..f1ecfacb 100644 --- a/apps/permission/templatetags/perms.py +++ b/apps/permission/templatetags/perms.py @@ -48,6 +48,13 @@ def not_empty_model_change_list(model_name): return session.get("not_empty_model_change_list_" + model_name) == 1 +def has_perm(t, obj, field=None): + print(t) + perm = "." + t + ("__" + field if field else "_") + return PermissionBackend().has_perm(get_current_authenticated_user(), perm, obj) + + register = template.Library() register.filter('not_empty_model_list', not_empty_model_list) register.filter('not_empty_model_change_list', not_empty_model_change_list) +register.filter('has_perm', has_perm) diff --git a/templates/activity/activity_detail.html b/templates/activity/activity_detail.html index bd946643..339d087b 100644 --- a/templates/activity/activity_detail.html +++ b/templates/activity/activity_detail.html @@ -3,48 +3,79 @@ {% load i18n %} {% load render_table from django_tables2 %} {% load pretty_money %} +{% load perms %} {% block content %} -
-
-

{{ activity.name }}

-
-
-
-
{% trans 'description'|capfirst %}
-
{{ activity.description }}
+
+
+

{{ activity.name }}

+
+
+
+
{% trans 'description'|capfirst %}
+
{{ activity.description }}
-
{% trans 'type'|capfirst %}
-
{{ activity.activity_type }}
+
{% trans 'type'|capfirst %}
+
{{ activity.activity_type }}
-
{% trans 'start date'|capfirst %}
-
{{ activity.date_start }}
+
{% trans 'start date'|capfirst %}
+
{{ activity.date_start }}
-
{% trans 'end date'|capfirst %}
-
{{ activity.date_end }}
+
{% trans 'end date'|capfirst %}
+
{{ activity.date_end }}
-
{% trans 'organizer'|capfirst %}
-
{{ activity.organizer }}
+
{% trans 'organizer'|capfirst %}
+
{{ activity.organizer }}
-
{% trans 'attendees club'|capfirst %}
-
{{ activity.attendees_club }}
+
{% trans 'attendees club'|capfirst %}
+
{{ activity.attendees_club }}
-
{% trans 'can invite'|capfirst %}
-
{{ activity.activity_type.can_invite|yesno }}
+
{% trans 'can invite'|capfirst %}
+
{{ activity.activity_type.can_invite|yesno }}
- {% if activity.activity_type.can_invite %} -
{% trans 'guest entry fee'|capfirst %}
-
{{ activity.activity_type.guest_entry_fee|pretty_money }}
+ {% if activity.activity_type.can_invite %} +
{% trans 'guest entry fee'|capfirst %}
+
{{ activity.activity_type.guest_entry_fee|pretty_money }}
+ {% endif %} +
+
+ +
+ {% if activity.activity_type.can_invite %} + {% trans "Invite" %} + {% endif %} +
- - + {% if guests.data %} +
+

{% trans "Guests list" %}

+
+ {% render_table guests %} +
+ {% endif %} {% endblock %} + +{% block extrajavascript %} + +{% endblock %}