diff --git a/src/pretix/control/forms/filter.py b/src/pretix/control/forms/filter.py index 10ff9fa884..2ea1d9567f 100644 --- a/src/pretix/control/forms/filter.py +++ b/src/pretix/control/forms/filter.py @@ -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 diff --git a/src/pretix/control/templates/pretixcontrol/organizers/devices.html b/src/pretix/control/templates/pretixcontrol/organizers/devices.html index f9d07f9ff8..bf94a73985 100644 --- a/src/pretix/control/templates/pretixcontrol/organizers/devices.html +++ b/src/pretix/control/templates/pretixcontrol/organizers/devices.html @@ -1,6 +1,7 @@ {% extends "pretixcontrol/organizers/base.html" %} {% load i18n %} {% load bootstrap3 %} +{% load urlreplace %} {% block inner %}
{% blocktrans trimmed %} @@ -23,6 +24,30 @@ class="btn btn-primary btn-lg"> {% trans "Connect a device" %}
{% trans "Connect a device" %} @@ -31,11 +56,20 @@