mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
Allow to filter and sort the list of devices
This commit is contained in:
@@ -42,7 +42,7 @@ from django.conf import settings
|
||||
from django.db.models import (
|
||||
Count, Exists, F, Max, Model, OrderBy, OuterRef, Q, QuerySet,
|
||||
)
|
||||
from django.db.models.functions import Coalesce, ExtractWeekDay
|
||||
from django.db.models.functions import Coalesce, ExtractWeekDay, Upper
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils.formats import date_format, localize
|
||||
from django.utils.functional import cached_property
|
||||
@@ -1950,3 +1950,68 @@ class CheckinFilterForm(FilterForm):
|
||||
qs = qs.filter(datetime__lte=fdata.get('datetime_until'))
|
||||
|
||||
return qs
|
||||
|
||||
|
||||
class DeviceFilterForm(FilterForm):
|
||||
orders = {
|
||||
'name': Upper('name'),
|
||||
'device_id': 'device_id',
|
||||
'initialized': F('initialized').asc(nulls_last=True),
|
||||
'-initialized': F('initialized').desc(nulls_first=True),
|
||||
}
|
||||
query = forms.CharField(
|
||||
label=_('Search query'),
|
||||
widget=forms.TextInput(attrs={
|
||||
'placeholder': _('Search query'),
|
||||
'autofocus': 'autofocus'
|
||||
}),
|
||||
required=False
|
||||
)
|
||||
gate = forms.ModelChoiceField(
|
||||
queryset=Gate.objects.none(),
|
||||
label=_('Gate'),
|
||||
empty_label=_('All gates'),
|
||||
required=False,
|
||||
)
|
||||
software_brand = forms.ChoiceField(
|
||||
label=_('Software'),
|
||||
choices=[
|
||||
('', _('All')),
|
||||
],
|
||||
required=False,
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
request = kwargs.pop('request')
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['gate'].queryset = request.organizer.gates.all()
|
||||
self.fields['software_brand'].choices = [
|
||||
('', _('All')),
|
||||
] + [
|
||||
(f['software_brand'], f['software_brand']) for f in
|
||||
request.organizer.devices.order_by().values('software_brand').annotate(c=Count('*'))
|
||||
if f['software_brand']
|
||||
]
|
||||
|
||||
def filter_qs(self, qs):
|
||||
fdata = self.cleaned_data
|
||||
|
||||
if fdata.get('query'):
|
||||
query = fdata.get('query')
|
||||
qs = qs.filter(
|
||||
Q(name__icontains=query)
|
||||
| Q(unique_serial__icontains=query)
|
||||
| Q(hardware_brand__icontains=query)
|
||||
| Q(hardware_model__icontains=query)
|
||||
| Q(software_brand__icontains=query)
|
||||
)
|
||||
|
||||
if fdata.get('gate'):
|
||||
qs = qs.filter(gate=fdata['gate'])
|
||||
|
||||
if fdata.get('ordering'):
|
||||
qs = qs.order_by(self.get_order_by())
|
||||
else:
|
||||
qs = qs.order_by('-device_id')
|
||||
|
||||
return qs
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{% extends "pretixcontrol/organizers/base.html" %}
|
||||
{% load i18n %}
|
||||
{% load bootstrap3 %}
|
||||
{% load urlreplace %}
|
||||
{% block inner %}
|
||||
<h1>
|
||||
{% trans "Connected devices" %}
|
||||
@@ -11,7 +12,7 @@
|
||||
your account.
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
{% if devices|length == 0 %}
|
||||
{% if devices|length == 0 and not filter_form.filtered %}
|
||||
<div class="empty-collection">
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
@@ -23,6 +24,30 @@
|
||||
class="btn btn-primary btn-lg"><i class="fa fa-plus"></i> {% trans "Connect a device" %}</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">{% trans "Filter" %}</h3>
|
||||
</div>
|
||||
<form class="panel-body filter-form" action="" method="get">
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-sm-6 col-xs-12">
|
||||
{% bootstrap_field filter_form.query %}
|
||||
</div>
|
||||
<div class="col-md-3 col-sm-6 col-xs-12">
|
||||
{% bootstrap_field filter_form.gate %}
|
||||
</div>
|
||||
<div class="col-md-3 col-sm-6 col-xs-12">
|
||||
{% bootstrap_field filter_form.software_brand %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-right">
|
||||
<button class="btn btn-primary btn-lg" type="submit">
|
||||
<span class="fa fa-filter"></span>
|
||||
{% trans "Filter" %}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<p>
|
||||
<a href="{% url "control:organizer.device.add" organizer=request.organizer.slug %}"
|
||||
class="btn btn-default"><i class="fa fa-plus"></i> {% trans "Connect a device" %}</a>
|
||||
@@ -31,11 +56,20 @@
|
||||
<table class="table table-condensed table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{% trans "Device ID" %}</th>
|
||||
<th>{% trans "Name" %}</th>
|
||||
<th>{% trans "Device ID" %}
|
||||
<a href="?{% url_replace request 'ordering' '-device_id' %}"><i class="fa fa-caret-down"></i></a>
|
||||
<a href="?{% url_replace request 'ordering' 'device_id' %}"><i class="fa fa-caret-up"></i></a></th>
|
||||
</th>
|
||||
<th>{% trans "Name" %}
|
||||
<a href="?{% url_replace request 'ordering' '-name' %}"><i class="fa fa-caret-down"></i></a>
|
||||
<a href="?{% url_replace request 'ordering' 'name' %}"><i class="fa fa-caret-up"></i></a></th>
|
||||
</th>
|
||||
<th>{% trans "Hardware model" %}</th>
|
||||
<th>{% trans "Software" %}</th>
|
||||
<th>{% trans "Setup date" %}</th>
|
||||
<th>{% trans "Setup date" %}
|
||||
<a href="?{% url_replace request 'ordering' '-initialized' %}"><i class="fa fa-caret-down"></i></a>
|
||||
<a href="?{% url_replace request 'ordering' 'initialized' %}"><i class="fa fa-caret-up"></i></a></th>
|
||||
</th>
|
||||
<th>{% trans "Events" %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
@@ -50,8 +84,12 @@
|
||||
{% if d.revoked %}<del>{% endif %}
|
||||
{{ d.name }}
|
||||
{% if d.revoked %}</del>{% endif %}
|
||||
{% if d.gate %}
|
||||
<br>
|
||||
<small class="text-muted">{{ d.gate.name }}</small>
|
||||
{% endif %}
|
||||
<br>
|
||||
<small>{{ d.unique_serial }}</small>
|
||||
<small class="text-muted">{{ d.unique_serial }}</small>
|
||||
</td>
|
||||
<td>
|
||||
{{ d.hardware_brand|default_if_none:"" }} {{ d.hardware_model|default_if_none:"" }}
|
||||
|
||||
@@ -84,7 +84,7 @@ from pretix.base.signals import register_multievent_data_exporters
|
||||
from pretix.base.templatetags.rich_text import markdown_compile_email
|
||||
from pretix.base.views.tasks import AsyncAction
|
||||
from pretix.control.forms.filter import (
|
||||
CustomerFilterForm, EventFilterForm, GiftCardFilterForm,
|
||||
CustomerFilterForm, DeviceFilterForm, EventFilterForm, GiftCardFilterForm,
|
||||
OrganizerFilterForm, TeamFilterForm,
|
||||
)
|
||||
from pretix.control.forms.orders import ExporterForm
|
||||
@@ -824,9 +824,21 @@ class DeviceListView(OrganizerDetailViewMixin, OrganizerPermissionRequiredMixin,
|
||||
context_object_name = 'devices'
|
||||
|
||||
def get_queryset(self):
|
||||
return self.request.organizer.devices.prefetch_related(
|
||||
'limit_events'
|
||||
qs = self.request.organizer.devices.prefetch_related(
|
||||
'limit_events', 'gate',
|
||||
).order_by('revoked', '-device_id')
|
||||
if self.filter_form.is_valid():
|
||||
qs = self.filter_form.filter_qs(qs)
|
||||
return qs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
ctx['filter_form'] = self.filter_form
|
||||
return ctx
|
||||
|
||||
@cached_property
|
||||
def filter_form(self):
|
||||
return DeviceFilterForm(data=self.request.GET, request=self.request)
|
||||
|
||||
|
||||
class DeviceCreateView(OrganizerDetailViewMixin, OrganizerPermissionRequiredMixin, CreateView):
|
||||
|
||||
Reference in New Issue
Block a user