arthexis 0.1.22__py3-none-any.whl → 0.1.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of arthexis might be problematic. Click here for more details.

pages/views.py CHANGED
@@ -16,6 +16,7 @@ from django.contrib import admin
16
16
  from django.contrib import messages
17
17
  from django.contrib.admin.views.decorators import staff_member_required
18
18
  from django.contrib.auth import get_user_model, login
19
+ from django.contrib.auth.decorators import login_required
19
20
  from django.contrib.auth.tokens import default_token_generator
20
21
  from django.contrib.auth.views import LoginView
21
22
  from django import forms
@@ -23,7 +24,14 @@ from django.apps import apps as django_apps
23
24
  from utils.decorators import security_group_required
24
25
  from utils.sites import get_site
25
26
  from django.contrib.staticfiles import finders
26
- from django.http import FileResponse, Http404, HttpResponse, JsonResponse
27
+ from django.http import (
28
+ FileResponse,
29
+ Http404,
30
+ HttpResponse,
31
+ HttpResponseForbidden,
32
+ HttpResponseRedirect,
33
+ JsonResponse,
34
+ )
27
35
  from django.shortcuts import get_object_or_404, redirect, render
28
36
  from nodes.models import Node
29
37
  from nodes.utils import capture_screenshot, save_screenshot
@@ -1280,10 +1288,10 @@ class ClientReportForm(forms.Form):
1280
1288
  initial=ClientReportSchedule.PERIODICITY_NONE,
1281
1289
  help_text=_("Defines how often the report should be generated automatically."),
1282
1290
  )
1283
- disable_emails = forms.BooleanField(
1284
- label=_("Disable email delivery"),
1291
+ enable_emails = forms.BooleanField(
1292
+ label=_("Enable email delivery"),
1285
1293
  required=False,
1286
- help_text=_("Generate files without sending emails."),
1294
+ help_text=_("Send the report via email to the recipients listed above."),
1287
1295
  )
1288
1296
 
1289
1297
  def __init__(self, *args, request=None, **kwargs):
@@ -1386,12 +1394,17 @@ def client_report(request):
1386
1394
  owner = form.cleaned_data.get("owner")
1387
1395
  if not owner and request.user.is_authenticated:
1388
1396
  owner = request.user
1397
+ enable_emails = form.cleaned_data.get("enable_emails", False)
1398
+ disable_emails = not enable_emails
1399
+ recipients = (
1400
+ form.cleaned_data.get("destinations") if enable_emails else []
1401
+ )
1389
1402
  report = ClientReport.generate(
1390
1403
  form.cleaned_data["start"],
1391
1404
  form.cleaned_data["end"],
1392
1405
  owner=owner,
1393
- recipients=form.cleaned_data.get("destinations"),
1394
- disable_emails=form.cleaned_data.get("disable_emails", False),
1406
+ recipients=recipients,
1407
+ disable_emails=disable_emails,
1395
1408
  )
1396
1409
  report.store_local_copy()
1397
1410
  recurrence = form.cleaned_data.get("recurrence")
@@ -1400,8 +1413,8 @@ def client_report(request):
1400
1413
  owner=owner,
1401
1414
  created_by=request.user if request.user.is_authenticated else None,
1402
1415
  periodicity=recurrence,
1403
- email_recipients=form.cleaned_data.get("destinations", []),
1404
- disable_emails=form.cleaned_data.get("disable_emails", False),
1416
+ email_recipients=recipients,
1417
+ disable_emails=disable_emails,
1405
1418
  )
1406
1419
  report.schedule = schedule
1407
1420
  report.save(update_fields=["schedule"])
@@ -1411,6 +1424,27 @@ def client_report(request):
1411
1424
  "Client report schedule created; future reports will be generated automatically."
1412
1425
  ),
1413
1426
  )
1427
+ if disable_emails:
1428
+ messages.success(
1429
+ request,
1430
+ _(
1431
+ "Consumer report generated. The download will begin automatically."
1432
+ ),
1433
+ )
1434
+ redirect_url = f"{reverse('pages:client-report')}?download={report.pk}"
1435
+ return HttpResponseRedirect(redirect_url)
1436
+ download_url = None
1437
+ download_param = request.GET.get("download")
1438
+ if download_param and request.user.is_authenticated:
1439
+ try:
1440
+ download_id = int(download_param)
1441
+ except (TypeError, ValueError):
1442
+ download_id = None
1443
+ if download_id:
1444
+ download_url = reverse(
1445
+ "pages:client-report-download", args=[download_id]
1446
+ )
1447
+
1414
1448
  try:
1415
1449
  login_url = reverse("pages:login")
1416
1450
  except NoReverseMatch:
@@ -1424,10 +1458,52 @@ def client_report(request):
1424
1458
  "report": report,
1425
1459
  "schedule": schedule,
1426
1460
  "login_url": login_url,
1461
+ "download_url": download_url,
1462
+ "previous_reports": _client_report_history(request),
1427
1463
  }
1428
1464
  return render(request, "pages/client_report.html", context)
1429
1465
 
1430
1466
 
1467
+ @login_required
1468
+ def client_report_download(request, report_id: int):
1469
+ report = get_object_or_404(ClientReport, pk=report_id)
1470
+ if not request.user.is_staff and report.owner_id != request.user.pk:
1471
+ return HttpResponseForbidden(
1472
+ _("You do not have permission to download this report.")
1473
+ )
1474
+ pdf_path = report.ensure_pdf()
1475
+ if not pdf_path.exists():
1476
+ raise Http404(_("Report file unavailable."))
1477
+ filename = f"consumer-report-{report.start_date}-{report.end_date}.pdf"
1478
+ response = FileResponse(pdf_path.open("rb"), content_type="application/pdf")
1479
+ response["Content-Disposition"] = f'attachment; filename="{filename}"'
1480
+ return response
1481
+
1482
+
1483
+ def _client_report_history(request, limit: int = 20):
1484
+ if not request.user.is_authenticated:
1485
+ return []
1486
+ qs = ClientReport.objects.order_by("-created_on")
1487
+ if not request.user.is_staff:
1488
+ qs = qs.filter(owner=request.user)
1489
+ history = []
1490
+ for report in qs[:limit]:
1491
+ totals = report.rows_for_display.get("totals", {})
1492
+ history.append(
1493
+ {
1494
+ "instance": report,
1495
+ "download_url": reverse("pages:client-report-download", args=[report.pk]),
1496
+ "email_enabled": not report.disable_emails,
1497
+ "recipients": report.recipients or [],
1498
+ "totals": {
1499
+ "total_kw": totals.get("total_kw", 0.0),
1500
+ "total_kw_period": totals.get("total_kw_period", 0.0),
1501
+ },
1502
+ }
1503
+ )
1504
+ return history
1505
+
1506
+
1431
1507
  def _get_request_language_code(request) -> str:
1432
1508
  language_code = ""
1433
1509
  if hasattr(request, "session"):