From 68caea5937cc43b598638967ad1ec8aacd532387 Mon Sep 17 00:00:00 2001 From: "Andrew K. Choi" Date: Wed, 6 Aug 2025 11:55:15 +0900 Subject: [PATCH] add_participants: Refactor admin interface for participant management --- lottery/draw/admin.py | 24 +++- lottery/draw/forms.py | 2 +- lottery/templates/admin/add_participants.html | 106 +++++++++--------- 3 files changed, 76 insertions(+), 56 deletions(-) diff --git a/lottery/draw/admin.py b/lottery/draw/admin.py index 9bacd3a..9c6ebc3 100644 --- a/lottery/draw/admin.py +++ b/lottery/draw/admin.py @@ -20,6 +20,8 @@ from bot.utils import create_bot_instance from .views import view_draw_results import os +from django.db.models import Q + logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) if not logger.handlers: @@ -36,9 +38,11 @@ def add_participants_view(request): return HttpResponse("Не указан параметр lottery_id", status=400) lottery = get_object_or_404(Lottery, id=lottery_id) + # Все доступные счета, не участвующие в этой лотерее used_invoice_ids = LotteryParticipant.objects.filter(lottery=lottery).values_list("invoice_id", flat=True) qs = Invoice.objects.filter(used=False).exclude(id__in=used_invoice_ids) + # Фильтрация deposit_min = request.GET.get("deposit_min") deposit_max = request.GET.get("deposit_max") created_after = request.GET.get("created_after") @@ -48,21 +52,30 @@ def add_participants_view(request): qs = qs.filter(deposit_sum__gte=deposit_min) if deposit_max: qs = qs.filter(deposit_sum__lte=deposit_max) + if created_after: try: dt = parse_date(created_after) if dt: - qs = qs.filter(created_at__gte=dt) + qs = qs.filter(created_at__date__gte=dt) except ValueError: pass + if created_before: try: dt = parse_date(created_before) if dt: - qs = qs.filter(created_at__lte=dt) + qs = qs.filter(created_at__date__lte=dt) except ValueError: pass + if request.GET.get("without_bonus"): + qs = qs.filter(Q(bonus__isnull=True) | Q(bonus=0)) + + if request.GET.get("without_fd"): + qs = qs.filter(Q(start_bonus__isnull=True) | Q(start_bonus=0)) + + # Обработка формы if request.method == "POST": form = AddParticipantsForm(request.POST) form.fields["invoices"].queryset = qs @@ -78,10 +91,13 @@ def add_participants_view(request): form = AddParticipantsForm() form.fields["invoices"].queryset = qs - context = {"form": form, "lottery": lottery} + context = { + "form": form, + "lottery": lottery, + "invoice_count": qs.count(), # Для отображения числа найденных + } return render(request, "admin/add_participants.html", context) - def get_client_by_invoice(invoice): try: return Client.objects.get(club_card_number=invoice.client_club_card_number) diff --git a/lottery/draw/forms.py b/lottery/draw/forms.py index 69edc69..fdc0c5f 100644 --- a/lottery/draw/forms.py +++ b/lottery/draw/forms.py @@ -6,7 +6,7 @@ class AddParticipantsForm(forms.Form): deposit_min = forms.DecimalField(label="Минимальный депозит", required=False) deposit_max = forms.DecimalField(label="Максимальный депозит", required=False) invoices = forms.ModelMultipleChoiceField( - queryset=Invoice.objects.none(), + queryset=Invoice.objects.none(), # устанавливается в представлении widget=forms.CheckboxSelectMultiple, required=False, label="Доступные счета" diff --git a/lottery/templates/admin/add_participants.html b/lottery/templates/admin/add_participants.html index d693b28..f29dfc2 100644 --- a/lottery/templates/admin/add_participants.html +++ b/lottery/templates/admin/add_participants.html @@ -48,6 +48,18 @@ + +
+
+ + +
+
+ + +
+
+
@@ -55,59 +67,51 @@

Найдено подходящих счетов: {{ invoice_count }}

- -
- {% csrf_token %} -
- - - - - - - - - - - - - - - - - - {% for invoice in form.fields.invoices.queryset %} + + + {% csrf_token %} +
+
СозданЗакрытСчетКлиентНомер клиентаСумма счетаБонусФДДепозитПримечание
+ - - - - - - - - - - - + + + + + + + + + + + - {% empty %} - - - - {% endfor %} - -
{{ invoice.created_at|date:"d.m.Y H:i" }} - {% if invoice.closed_at %} - {{ invoice.closed_at|date:"d.m.Y H:i" }} - {% else %} - — - {% endif %} - {{ invoice.ext_id|default:"—" }}{{ invoice.client.name|default:"Не указан" }}{{ invoice.client.club_card_number|default:"—" }}{{ invoice.sum|default:"—" }}{{ invoice.bonus|default:"—" }}{{ invoice.start_bonus|default:"—" }}{{ invoice.deposit_sum|default:"—" }}{{ invoice.notes|default:"—" }}СозданЗакрытСчётКлиентНомер клиентаСумма счетаБонусФДДепозитПримечание
Нет доступных счетов
-
- -
- - + + + {% for invoice in form.fields.invoices.queryset %} + + + {{ invoice.created_at|date:"d.m.Y H:i" }} + {% if invoice.closed_at %}{{ invoice.closed_at|date:"d.m.Y H:i" }}{% else %}—{% endif %} + {{ invoice.ext_id|default:"—" }} + {{ invoice.client.name|default:"Не указан" }} + {{ invoice.client.club_card_number|default:"—" }} + {{ invoice.sum|default:"—" }} + {{ invoice.bonus|default:"—" }} + {{ invoice.start_bonus|default:"—" }} + {{ invoice.deposit_sum|default:"—" }} + {{ invoice.notes|default:"—" }} + + {% empty %} + + Нет доступных счетов + + {% endfor %} + + + + + Вернуться к списку участников