From e2a6645a48c9abf9c77630449fb898df4dfae615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Robles?= Date: Sat, 22 Jun 2024 15:08:57 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=F0=9F=90=9E=20Improve/rethink/fix=20L?= =?UTF-8?q?ive=20Preview=20(#348)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parent issue: https://github.com/sequentech/meta/issues/752 --- iam/api/urls.py | 2 ++ iam/api/views.py | 43 ++++++++++++++++++++++++++++++++++++++++ iam/iam/settings.py | 1 + iam/iam/test_settings.py | 1 + 4 files changed, 47 insertions(+) diff --git a/iam/api/urls.py b/iam/api/urls.py index db3ca253..e796192c 100644 --- a/iam/api/urls.py +++ b/iam/api/urls.py @@ -61,6 +61,8 @@ url(r'^auth-event/(?P\d+)/census/reset-voter/$', views.census_reset_voter, name='census_reset_voter'), url(r'^auth-event/(?P\d+)/(?P(notstarted|started|stopped|suspended|resumed))/$', views.ae_status, name='ae_status'), url(r'^auth-event/(?P\d+)/turnout/$', views.turnout, name='turnout'), + url(r'^auth-event/(?P[-\w]+)/live-preview/$', views.live_preview, name='live-preview'), + url(r'^auth-event/live-preview/$', views.live_preview, name='live-preview'), url(r'^auth-event/module/$', views.authevent_module, name='authevent_module'), url(r'^auth-event/module/(?P[-\w]+)/$', views.authevent_module, name='authevent_module'), diff --git a/iam/api/views.py b/iam/api/views.py index 3055b15d..253a3013 100644 --- a/iam/api/views.py +++ b/iam/api/views.py @@ -18,6 +18,7 @@ import json import requests import mimetypes +import uuid from datetime import datetime from django import forms from django.core import serializers @@ -1502,6 +1503,48 @@ def post(request, pk): edit_children_parent = EditChildrenParentView.as_view() +class LivePreviewView(View): + @login_required + def post(request, pk=None): + ''' + Uploads the configuration for a live preview + ''' + try: + election_config = parse_json_request(request) + except: + return json_response( + status=400, + error_codename=ErrorCodes.BAD_REQUEST) + + preview_id = str(uuid.uuid4()) + preview_dir = settings.STATIC_PREVIEW_PATH + + os.makedirs(preview_dir, exist_ok=True) + + preview_file = os.path.join(preview_dir, f"{preview_id}.json") + with open(preview_file, "w") as file: + json.dump(election_config, file) + + data = {'status': 'ok', 'id': preview_id} + return json_response(data) + + def get(self, request, pk): + """ + Gets the configuration for a live preview by ID + """ + preview_dir = settings.STATIC_PREVIEW_PATH + preview_file = os.path.join(preview_dir, f"{pk}.json") + + if not os.path.exists(preview_file): + raise Http404("Configuration not found.") + + with open(preview_file, "r") as file: + election_config = json.load(file) + + return json_response(election_config) + +live_preview = LivePreviewView.as_view() + class AuthEventView(View): @login_required def post(request, pk=None): diff --git a/iam/iam/settings.py b/iam/iam/settings.py index 8f465f2d..e4efc438 100644 --- a/iam/iam/settings.py +++ b/iam/iam/settings.py @@ -177,6 +177,7 @@ class CeleryConfig: STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') +STATIC_PREVIEW_PATH = os.path.join(BASE_DIR, 'static', 'preview') # cors CORS_ORIGIN_ALLOW_ALL = False diff --git a/iam/iam/test_settings.py b/iam/iam/test_settings.py index bd6a30fe..0562b018 100644 --- a/iam/iam/test_settings.py +++ b/iam/iam/test_settings.py @@ -198,6 +198,7 @@ class CeleryConfig: STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') +STATIC_PREVIEW_PATH = os.path.join(BASE_DIR, 'static', 'preview') # cors CORS_ORIGIN_ALLOW_ALL = False