mirror of
https://gitlab.crans.org/bde/nk20
synced 2024-11-27 02:43:01 +00:00
Display guests list
This commit is contained in:
parent
0d43d74c60
commit
d6e202a26f
@ -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',
|
||||||
|
|
||||||
invite = tables.columns.LinkColumn('activity:activity_invite',
|
|
||||||
args=[A('pk'), ],
|
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()
|
||||||
|
@ -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):
|
||||||
|
@ -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)
|
||||||
|
@ -3,10 +3,11 @@
|
|||||||
{% 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>
|
||||||
@ -41,10 +42,40 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-footer text-center">
|
<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 %}
|
{% 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>
|
<a class="btn btn-primary btn-sm my-1" href="{% url 'activity:activity_invite' pk=activity.pk %}"> {% trans "Invite" %}</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if guests.data %}
|
||||||
|
<hr>
|
||||||
|
<h2>{% trans "Guests list" %}</h2>
|
||||||
|
<div id="guests_table">
|
||||||
|
{% render_table guests %}
|
||||||
|
</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 %}
|
||||||
|
Loading…
Reference in New Issue
Block a user