Load result data in tables

This commit is contained in:
Yohann D'ANELLO 2020-10-15 20:39:34 +02:00
parent 7fb811b87f
commit 144577bd89
8 changed files with 125 additions and 11 deletions

View File

@ -6,6 +6,7 @@ class ParticipationConfig(AppConfig):
name = 'participation'
def ready(self):
from participation.signals import create_team_participation, update_mailing_list
from participation.signals import create_team_participation, delete_related_videos, update_mailing_list
pre_save.connect(update_mailing_list, "participation.Team")
pre_delete.connect(delete_related_videos, "participation.Participation")
post_save.connect(create_team_participation, "participation.Team")

View File

@ -22,4 +22,3 @@ class VideoIndex(indexes.ModelSearchIndex, indexes.Indexable):
class Meta:
model = Video

View File

@ -0,0 +1,70 @@
from django.utils.translation import gettext_lazy as _
import django_tables2 as tables
from .models import Team
# noinspection PyTypeChecker
class TeamTable(tables.Table):
name = tables.LinkColumn(
'participation:team_detail',
args=[tables.A("id")],
verbose_name=lambda: _("name").capitalize(),
)
problem = tables.Column(
accessor="participation__problem",
verbose_name=lambda: _("problem number").capitalize(),
)
class Meta:
attrs = {
'class': 'table table condensed table-striped',
}
model = Team
fields = ('name', 'trigram', 'problem',)
template_name = 'django_tables2/bootstrap4.html'
# noinspection PyTypeChecker
class ParticipationTable(tables.Table):
name = tables.LinkColumn(
'participation:participation_detail',
args=[tables.A("id")],
verbose_name=lambda: _("name").capitalize(),
accessor="team__name",
)
trigram = tables.Column(
verbose_name=lambda: _("trigram").capitalize(),
accessor="team__trigram",
)
problem = tables.Column(
verbose_name=lambda: _("problem number").capitalize(),
)
class Meta:
attrs = {
'class': 'table table condensed table-striped',
}
model = Team
fields = ('name', 'trigram', 'problem',)
template_name = 'django_tables2/bootstrap4.html'
class VideoTable(tables.Table):
participation_name = tables.LinkColumn(
'participation:participation_detail',
args=[tables.A("participation__pk")],
verbose_name=lambda: _("name").capitalize(),
accessor="participation__name",
)
class Meta:
attrs = {
'class': 'table table condensed table-striped',
}
model = Team
fields = ('participation_name', 'link',)
template_name = 'django_tables2/bootstrap4.html'

View File

@ -1,12 +1,11 @@
import os
from io import BytesIO
import os
from zipfile import ZipFile
from django.core.mail import send_mail
from corres2math.lists import get_sympa_client
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.db import transaction
from django.http import HttpResponse
from django.template.loader import render_to_string

View File

@ -0,0 +1,21 @@
from django.utils.translation import gettext_lazy as _
import django_tables2 as tables
from .models import Registration
class RegistrationTable(tables.Table):
last_name = tables.LinkColumn(
'registration:user_detail',
args=[tables.A("user_id")],
verbose_name=lambda: _("last name").capitalize(),
accessor="user__last_name",
)
class Meta:
attrs = {
'class': 'table table condensed table-striped',
}
model = Registration
fields = ('last_name', 'user__first_name', 'user__email', 'type',)
template_name = 'django_tables2/bootstrap4.html'

View File

@ -0,0 +1,26 @@
from django import template
from django_tables2 import Table
from participation.models import Participation, Team, Video
from participation.tables import ParticipationTable, TeamTable, VideoTable
from ..models import Registration
from ..tables import RegistrationTable
def search_table(results):
model_class = results[0].object.__class__
if issubclass(model_class, Registration):
table_class = RegistrationTable
elif issubclass(model_class, Team):
table_class = TeamTable
elif issubclass(model_class, Participation):
table_class = ParticipationTable
elif issubclass(model_class, Video):
table_class = VideoTable
else:
table_class = Table
return table_class([result.object for result in results], prefix=model_class._meta.model_name)
register = template.Library()
register.filter("search_table", search_table)

View File

@ -1,6 +1,6 @@
{% extends 'base.html' %}
{% load crispy_forms_filters highlight i18n %}
{% load crispy_forms_filters highlight i18n search_results_tables django_tables2 %}
{% block content %}
<h2>{% trans "Search" %}</h2>
@ -19,11 +19,9 @@
{% regroup page.object_list by model_name as categories %}
{% for category in categories %}
<h4>{% trans category.grouper|capfirst %}</h4>
{% for result in category.list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object }}</a>
</p>
{% endfor %}
{% with table=category.list|search_table %}
{% render_table table %}
{% endwith %}
{% empty %}
<p>{% trans "No results found." %}</p>
{% endfor %}