Display guests list

This commit is contained in:
Yohann D'ANELLO 2020-03-27 21:18:27 +01:00
parent 0d43d74c60
commit d6e202a26f
4 changed files with 113 additions and 48 deletions

View File

@ -2,19 +2,17 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.utils.translation import gettext_lazy as _ 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 from .models import Activity, Guest
class ActivityTable(tables.Table): class ActivityTable(tables.Table):
name = tables.columns.LinkColumn('activity:activity_detail', name = tables.LinkColumn(
args=[A('pk'), ],) 'activity:activity_detail',
args=[A('pk'), ],
invite = tables.columns.LinkColumn('activity:activity_invite', )
args=[A('pk'), ],
verbose_name=_("Invite"),
text=_("Invite"),)
class Meta: class Meta:
attrs = { attrs = {
@ -22,19 +20,34 @@ class ActivityTable(tables.Table):
} }
model = Activity model = Activity
template_name = 'django_tables2/bootstrap4.html' 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): 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: class Meta:
attrs = { attrs = {
'class': 'table table-condensed table-striped table-hover' 'class': 'table table-condensed table-striped table-hover'
} }
model = Guest model = Guest
template_name = 'django_tables2/bootstrap4.html' template_name = 'django_tables2/bootstrap4.html'
fields = ('name', 'inviter', ) fields = ("last_name", "first_name", "inviter", )
row_attrs = {
'class': 'table-row', def render_entry(self, record):
'id': lambda record: "row-" + str(record.pk), if record.entry:
'data-href': lambda record: record.pk return str(record.date)
} return _("remove").capitalize()

View File

@ -7,15 +7,18 @@ from django.views.generic import CreateView, DetailView, UpdateView, TemplateVie
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django_tables2.views import SingleTableView from django_tables2.views import SingleTableView
from permission.backends import PermissionBackend
from .forms import ActivityForm, GuestForm from .forms import ActivityForm, GuestForm
from .models import Activity, Guest from .models import Activity, Guest
from .tables import ActivityTable from .tables import ActivityTable, GuestTable
class ActivityCreateView(LoginRequiredMixin, CreateView): class ActivityCreateView(LoginRequiredMixin, CreateView):
model = Activity model = Activity
form_class = ActivityForm 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): class ActivityListView(LoginRequiredMixin, SingleTableView):
@ -34,11 +37,22 @@ class ActivityDetailView(LoginRequiredMixin, DetailView):
model = Activity model = Activity
context_object_name = "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): class ActivityUpdateView(LoginRequiredMixin, UpdateView):
model = Activity model = Activity
form_class = ActivityForm 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): class ActivityInviteView(LoginRequiredMixin, CreateView):

View File

@ -48,6 +48,13 @@ def not_empty_model_change_list(model_name):
return session.get("not_empty_model_change_list_" + model_name) == 1 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 = template.Library()
register.filter('not_empty_model_list', not_empty_model_list) register.filter('not_empty_model_list', not_empty_model_list)
register.filter('not_empty_model_change_list', not_empty_model_change_list) register.filter('not_empty_model_change_list', not_empty_model_change_list)
register.filter('has_perm', has_perm)

View File

@ -3,48 +3,79 @@
{% load i18n %} {% load i18n %}
{% load render_table from django_tables2 %} {% load render_table from django_tables2 %}
{% load pretty_money %} {% load pretty_money %}
{% load perms %}
{% block content %} {% block content %}
<div class="card bg-light shadow"> <div class="card bg-light shadow">
<div class="card-header text-center"> <div class="card-header text-center">
<h4>{{ activity.name }}</h4> <h4>{{ activity.name }}</h4>
</div> </div>
<div class="card-body" id="profile_infos"> <div class="card-body" id="profile_infos">
<dl class="row"> <dl class="row">
<dt class="col-xl-6">{% trans 'description'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'description'|capfirst %}</dt>
<dd class="col-xl-6"> {{ activity.description }}</dd> <dd class="col-xl-6"> {{ activity.description }}</dd>
<dt class="col-xl-6">{% trans 'type'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'type'|capfirst %}</dt>
<dd class="col-xl-6"> {{ activity.activity_type }}</dd> <dd class="col-xl-6"> {{ activity.activity_type }}</dd>
<dt class="col-xl-6">{% trans 'start date'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'start date'|capfirst %}</dt>
<dd class="col-xl-6">{{ activity.date_start }}</dd> <dd class="col-xl-6">{{ activity.date_start }}</dd>
<dt class="col-xl-6">{% trans 'end date'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'end date'|capfirst %}</dt>
<dd class="col-xl-6">{{ activity.date_end }}</dd> <dd class="col-xl-6">{{ activity.date_end }}</dd>
<dt class="col-xl-6">{% trans 'organizer'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'organizer'|capfirst %}</dt>
<dd class="col-xl-6"><a href="{% url "member:club_detail" pk=activity.organizer.pk %}">{{ activity.organizer }}</a></dd> <dd class="col-xl-6"><a href="{% url "member:club_detail" pk=activity.organizer.pk %}">{{ activity.organizer }}</a></dd>
<dt class="col-xl-6">{% trans 'attendees club'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'attendees club'|capfirst %}</dt>
<dd class="col-xl-6"><a href="{% url "member:club_detail" pk=activity.attendees_club.pk %}">{{ activity.attendees_club }}</a></dd> <dd class="col-xl-6"><a href="{% url "member:club_detail" pk=activity.attendees_club.pk %}">{{ activity.attendees_club }}</a></dd>
<dt class="col-xl-6">{% trans 'can invite'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'can invite'|capfirst %}</dt>
<dd class="col-xl-6">{{ activity.activity_type.can_invite|yesno }}</dd> <dd class="col-xl-6">{{ activity.activity_type.can_invite|yesno }}</dd>
{% if activity.activity_type.can_invite %} {% if activity.activity_type.can_invite %}
<dt class="col-xl-6">{% trans 'guest entry fee'|capfirst %}</dt> <dt class="col-xl-6">{% trans 'guest entry fee'|capfirst %}</dt>
<dd class="col-xl-6">{{ activity.activity_type.guest_entry_fee|pretty_money }}</dd> <dd class="col-xl-6">{{ activity.activity_type.guest_entry_fee|pretty_money }}</dd>
{% endif %}
</dl>
</div>
<div class="card-footer text-center">
{% if "view"|has_perm:activity %}
<a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_update' pk=activity.pk %}"> {% trans "edit"|capfirst %}</a>
{% endif %} {% endif %}
</dl> {% if activity.activity_type.can_invite %}
<a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_invite' pk=activity.pk %}"> {% trans "Invite" %}</a>
{% endif %}
</div>
</div> </div>
<div class="card-footer text-center"> {% if guests.data %}
{% if activity.activity_type.can_invite %} <hr>
<a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_invite' pk=activity.pk %}"> {% trans "Invite" %}</a> <h2>{% trans "Guests list" %}</h2>
{% endif %} <div id="guests_table">
</div> {% render_table guests %}
</div> </div>
{% endif %}
{% endblock %} {% endblock %}
{% block extrajavascript %}
<script>
function remove_guest(guest_id) {
$.ajax({
url:"/api/activity/guest/" + guest_id + "/",
method:"DELETE",
headers: {"X-CSRFTOKEN": CSRF_TOKEN}
})
.done(function() {
addMsg('Invité supprimé','success');
$("#guests_table").load(location.href + " #guests_table");
})
.fail(function(xhr, textStatus, error) {
errMsg(xhr.responseJSON);
});
}
</script>
{% endblock %}