add_participants: Refactor admin interface for participant management

This commit is contained in:
2025-08-06 11:55:15 +09:00
parent becf4f5c99
commit 68caea5937
3 changed files with 76 additions and 56 deletions

View File

@@ -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)

View File

@@ -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="Доступные счета"

View File

@@ -48,6 +48,18 @@
<input type="date" name="created_before" value="{{ request.GET.created_before }}" class="form-control">
</div>
</div>
<div class="row mt-3">
<div class="col-md-3 form-check mt-2">
<input type="checkbox" class="form-check-input" name="without_bonus" id="without_bonus" {% if request.GET.without_bonus %}checked{% endif %}>
<label class="form-check-label" for="without_bonus">Только без бонуса</label>
</div>
<div class="col-md-3 form-check mt-2">
<input type="checkbox" class="form-check-input" name="without_fd" id="without_fd" {% if request.GET.without_fd %}checked{% endif %}>
<label class="form-check-label" for="without_fd">Только без ФД</label>
</div>
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">Применить фильтр</button>
</div>
@@ -55,59 +67,51 @@
<p><strong>Найдено подходящих счетов: {{ invoice_count }}</strong></p>
<!-- Форма добавления участников -->
<form method="post">
{% csrf_token %}
<div class="table-responsive">
<table class="table table-striped table-bordered table-sm">
<thead>
<tr>
<th><input type="checkbox" id="select-all" /></th>
<th>Создан</th>
<th>Закрыт</th>
<th>Счет</th>
<th>Клиент</th>
<th>Номер клиента</th>
<th>Сумма счета</th>
<th>Бонус</th>
<th>ФД</th>
<th>Депозит</th>
<th>Примечание</th>
</tr>
</thead>
<tbody>
{% for invoice in form.fields.invoices.queryset %}
<!-- Форма добавления участников -->
<form method="post">
{% csrf_token %}
<div class="table-responsive">
<table class="table table-striped table-bordered table-sm align-middle">
<thead>
<tr>
<input type="checkbox" name="invoices" value="{{ invoice.id }}">
<td>{{ invoice.created_at|date:"d.m.Y H:i" }}</td>
<td>
{% if invoice.closed_at %}
{{ invoice.closed_at|date:"d.m.Y H:i" }}
{% else %}
{% endif %}
</td>
<td>{{ invoice.ext_id|default:"—" }}</td>
<td>{{ invoice.client.name|default:"Не указан" }}</td>
<td>{{ invoice.client.club_card_number|default:"—" }}</td>
<td>{{ invoice.sum|default:"—" }}</td>
<td>{{ invoice.bonus|default:"—" }}</td>
<td>{{ invoice.start_bonus|default:"—" }}</td>
<td>{{ invoice.deposit_sum|default:"—" }}</td>
<td>{{ invoice.notes|default:"—" }}</td>
<th><input type="checkbox" id="select-all" /></th>
<th>Создан</th>
<th>Закрыт</th>
<th>Счёт</th>
<th>Клиент</th>
<th>Номер клиента</th>
<th>Сумма счета</th>
<th>Бонус</th>
<th>ФД</th>
<th>Депозит</th>
<th>Примечание</th>
</tr>
{% empty %}
<tr>
<td colspan="11" class="text-center">Нет доступных счетов</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<button type="submit" class="btn btn-primary">Добавить выбранные счета</button>
</form>
</thead>
<tbody>
{% for invoice in form.fields.invoices.queryset %}
<tr>
<td><input type="checkbox" name="invoices[]" value="{{ invoice.id }}"></td>
<td>{{ invoice.created_at|date:"d.m.Y H:i" }}</td>
<td>{% if invoice.closed_at %}{{ invoice.closed_at|date:"d.m.Y H:i" }}{% else %}—{% endif %}</td>
<td>{{ invoice.ext_id|default:"—" }}</td>
<td>{{ invoice.client.name|default:"Не указан" }}</td>
<td>{{ invoice.client.club_card_number|default:"—" }}</td>
<td>{{ invoice.sum|default:"—" }}</td>
<td>{{ invoice.bonus|default:"—" }}</td>
<td>{{ invoice.start_bonus|default:"—" }}</td>
<td>{{ invoice.deposit_sum|default:"—" }}</td>
<td>{{ invoice.notes|default:"—" }}</td>
</tr>
{% empty %}
<tr>
<td colspan="11" class="text-center">Нет доступных счетов</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<button type="submit" class="btn btn-primary">Добавить выбранные счета</button>
</form>
<a href="{% url 'admin:draw_lotteryparticipant_changelist' %}" class="btn btn-secondary mt-3">Вернуться к списку участников</a>
</div>