Skip to content

Commit

Permalink
✨🐞 Improve/rethink/fix Live Preview (#348)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#752
  • Loading branch information
Findeton committed Jun 22, 2024
1 parent ba660f0 commit e2a6645
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions iam/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
url(r'^auth-event/(?P<pk>\d+)/census/reset-voter/$', views.census_reset_voter, name='census_reset_voter'),
url(r'^auth-event/(?P<pk>\d+)/(?P<status>(notstarted|started|stopped|suspended|resumed))/$', views.ae_status, name='ae_status'),
url(r'^auth-event/(?P<pk>\d+)/turnout/$', views.turnout, name='turnout'),
url(r'^auth-event/(?P<pk>[-\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<name>[-\w]+)/$', views.authevent_module, name='authevent_module'),

Expand Down
43 changes: 43 additions & 0 deletions iam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions iam/iam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions iam/iam/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e2a6645

Please sign in to comment.