Entry page (we can search people)

This commit is contained in:
Yohann D'ANELLO 2020-03-28 01:45:13 +01:00
parent 8c1d902c30
commit a8422411bc
5 changed files with 123 additions and 5 deletions

View File

@ -42,4 +42,4 @@ class GuestViewSet(ReadProtectedModelViewSet):
queryset = Guest.objects.all() queryset = Guest.objects.all()
serializer_class = GuestSerializer serializer_class = GuestSerializer
filter_backends = [SearchFilter] filter_backends = [SearchFilter]
search_fields = ['$name', ] search_fields = ['$last_name', '$first_name', '$inviter__alias__name', '$inviter__alias__normalized_name', ]

View File

@ -1,10 +1,11 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import django_tables2 as tables import django_tables2 as tables
from django_tables2 import A from django_tables2 import A
from note.templatetags.pretty_money import pretty_money
from .models import Activity, Guest from .models import Activity, Guest
@ -51,3 +52,36 @@ class GuestTable(tables.Table):
if record.entry: if record.entry:
return str(record.date) return str(record.date)
return _("remove").capitalize() return _("remove").capitalize()
class EntryTable(tables.Table):
type = tables.Column()
last_name = tables.Column()
first_name = tables.Column()
note_name = tables.Column()
balance = tables.Column()
def render_note_name(self, value, record):
if hasattr(record, 'username'):
username = record.username
if username != value:
return format_html(value + " <em>aka.</em> " + username)
return value
def render_balance(self, value):
return pretty_money(value)
class Meta:
attrs = {
'class': 'table table-condensed table-striped table-hover'
}
template_name = 'django_tables2/bootstrap4.html'
row_attrs = {
'class': 'table-row',
'id': lambda record: "row-" + str(record.type),
'data-href': lambda record: record.type
}

View File

@ -2,15 +2,18 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.db.models import F, Q
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import CreateView, DetailView, UpdateView, TemplateView from django.views.generic import CreateView, DetailView, UpdateView, TemplateView
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 note.models import NoteUser, Alias
from permission.backends import PermissionBackend 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, GuestTable from .tables import ActivityTable, GuestTable, EntryTable
class ActivityCreateView(LoginRequiredMixin, CreateView): class ActivityCreateView(LoginRequiredMixin, CreateView):
@ -69,4 +72,51 @@ class ActivityInviteView(LoginRequiredMixin, CreateView):
class ActivityEntryView(LoginRequiredMixin, TemplateView): class ActivityEntryView(LoginRequiredMixin, TemplateView):
pass template_name = "activity/activity_entry.html"
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
activity = Activity.objects.get(pk=self.kwargs["pk"])
ctx["activity"] = activity
matched = []
pattern = "^$"
if "search" in self.request.GET:
pattern = self.request.GET["search"]
print(pattern)
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)
| Q(inviter__alias__normalized_name__startswith=Alias.normalize(pattern)))\
.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)
| Q(name__regex="^" + pattern)
| Q(normalized_name__startswith=Alias.normalize(pattern))))\
.distinct()[:20]
for note in note_qs:
note.type = "Adhérent"
matched.append(note)
table = EntryTable(data=matched)
ctx["table"] = table
ctx["title"] = _('Entry for activity "{}"').format(activity.name)
ctx["noteuser_ctype"] = ContentType.objects.get_for_model(NoteUser).pk
return ctx

View File

@ -48,6 +48,10 @@
</div> </div>
<div class="card-footer text-center"> <div class="card-footer text-center">
{% if activity.open %}
<a class="btn btn-warning btn-sm my-1" href="{% url 'activity:activity_entry' pk=activity.pk %}"> {% trans "Entry page" %}</a>
{% endif %}
{% if activity.valid and "change__open"|has_perm:activity %} {% if activity.valid and "change__open"|has_perm:activity %}
<a class="btn btn-warning btn-sm my-1" id="open_activity"> {% if activity.open %}{% trans "close"|capfirst %}{% else %}{% trans "open"|capfirst %}{% endif %}</a> <a class="btn btn-warning btn-sm my-1" id="open_activity"> {% if activity.open %}{% trans "close"|capfirst %}{% else %}{% trans "open"|capfirst %}{% endif %}</a>
{% endif %} {% endif %}

View File

@ -0,0 +1,30 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}
{% load render_table from django_tables2 %}
{% load pretty_money %}
{% load perms %}
{% block content %}
<input id="alias" type="text" class="form-control" placeholder="Nom/note ...">
<div id="entry_table">
{% render_table table %}
</div>
{% endblock %}
{% block extrajavascript %}
<script>
old_pattern = null;
alias_obj = $("#alias");
alias_obj.keyup(function() {
let pattern = alias_obj.val();
if (pattern === old_pattern || pattern === "")
return;
$("#entry_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #entry_table");
});
</script>
{% endblock %}