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
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',
name = tables.LinkColumn(
'activity:activity_detail',
args=[A('pk'), ],
verbose_name=_("Invite"),
text=_("Invite"),)
)
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()

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_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):

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
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)

View File

@ -3,10 +3,11 @@
{% load i18n %}
{% load render_table from django_tables2 %}
{% load pretty_money %}
{% load perms %}
{% block content %}
<div class="card bg-light shadow">
<div class="card bg-light shadow">
<div class="card-header text-center">
<h4>{{ activity.name }}</h4>
</div>
@ -41,10 +42,40 @@
</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 %}
{% 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>
{% if guests.data %}
<hr>
<h2>{% trans "Guests list" %}</h2>
<div id="guests_table">
{% render_table guests %}
</div>
{% endif %}
{% 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 %}