-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
85 lines (68 loc) · 3.02 KB
/
views.py
File metadata and controls
85 lines (68 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import io
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.core.management import call_command
from django.http import Http404, HttpResponse
from django.shortcuts import redirect
from django.template import loader
from rest_framework.exceptions import PermissionDenied
from hat.dashboard import views as dashboard_views
from iaso.models import Account
from plugins.snt_malaria.permissions import SNT_SETTINGS_WRITE_PERMISSION
def public_account_setup_spa(request):
"""Serve the anonymous account-setup SPA only when explicitly enabled.
Authenticated users are sent to the dashboard so the creation form is never
shown to logged-in users (e.g. bookmarks to the public URL after signup).
"""
if not getattr(settings, "ENABLE_PUBLIC_ACCOUNT_SETUP", False):
raise Http404()
if request.user.is_authenticated:
return redirect("/dashboard/")
return dashboard_views.public_iaso(request)
@staff_member_required
def import_openhexa_metrics(request):
"""
Admin-only view to import OpenHEXA metrics data via web interface.
Provides a form to execute the import_openhexa_metrics management command
with account_id parameter. The workspace slug and dataset slug are automatically
retrieved from the OpenHEXAWorkspace model associated with the account.
"""
user = request.user
if not user.has_perm(SNT_SETTINGS_WRITE_PERMISSION.full_name()):
raise PermissionDenied("User does not have permission to edit settings")
if request.method == "POST":
account_id = request.POST.get("account_id", "")
if not account_id:
return HttpResponse("Error: account_id is required", status=400)
try:
account_id = int(account_id)
except ValueError:
return HttpResponse("Error: account_id must be a valid integer", status=400)
try:
out = io.StringIO()
call_command(
"import_openhexa_metrics",
account_id=account_id,
stdout=out,
stderr=out,
)
output = out.getvalue()
# Render success template
template = loader.get_template("import_openhexa_metrics_success.html")
context = {
"account_id": account_id,
"output": output,
}
return HttpResponse(template.render(context, request))
except Exception as e:
template = loader.get_template("import_openhexa_metrics_error.html")
context = {
"account_id": account_id,
"error_message": str(e),
}
return HttpResponse(template.render(context, request), status=500)
# GET request - display the form
# Get all accounts to populate the select dropdown
accounts = Account.objects.all().order_by("name")
template = loader.get_template("import_openhexa_metrics.html")
return HttpResponse(template.render({"accounts": accounts}, request))