From 9a06def0e72395a693ce84e4453eab23b064a09e Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Fri, 26 Nov 2021 18:14:22 +1100 Subject: [PATCH] Add simple CCS upload (no JavaScript). --- www/conservancy/apps/ccs_upload/__init__.py | 0 www/conservancy/apps/ccs_upload/urls.py | 7 ++++ www/conservancy/apps/ccs_upload/views.py | 38 +++++++++++++++++++ .../templates/ccs_upload/upload.html | 34 +++++++++++++++++ www/conservancy/urls.py | 1 + 5 files changed, 80 insertions(+) create mode 100644 www/conservancy/apps/ccs_upload/__init__.py create mode 100644 www/conservancy/apps/ccs_upload/urls.py create mode 100644 www/conservancy/apps/ccs_upload/views.py create mode 100644 www/conservancy/templates/ccs_upload/upload.html diff --git a/www/conservancy/apps/ccs_upload/__init__.py b/www/conservancy/apps/ccs_upload/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/www/conservancy/apps/ccs_upload/urls.py b/www/conservancy/apps/ccs_upload/urls.py new file mode 100644 index 00000000..66ee72b6 --- /dev/null +++ b/www/conservancy/apps/ccs_upload/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^$', views.upload, name='form') +] diff --git a/www/conservancy/apps/ccs_upload/views.py b/www/conservancy/apps/ccs_upload/views.py new file mode 100644 index 00000000..965c8b0f --- /dev/null +++ b/www/conservancy/apps/ccs_upload/views.py @@ -0,0 +1,38 @@ +import logging +import os +import random + +from django.contrib import messages +from django.shortcuts import redirect, render + +from .forms import UploadForm + +CCS_UPLOAD_PATH = '/tmp/' + +logger = logging.getLogger(__name__) + + +def upload(request): + if request.method == 'POST': + form = UploadForm(request.POST, request.FILES) + if form.is_valid(): + file = request.FILES['file'] + _handle_uploaded_file(file) + messages.add_message(request, messages.INFO, '"{}" was successfully uploaded.'.format(file.name)) + return redirect('ccs_upload:form') + else: + form = UploadForm() + return render(request, 'ccs_upload/upload.html', {'form': form}) + + +def _handle_uploaded_file(f): + filename = _hash_prefixed_filename(f.name) + path = os.path.join(CCS_UPLOAD_PATH, filename) + with open(path, 'wb+') as destination: + for chunk in f.chunks(): + destination.write(chunk) + logger.info('Wrote uploaded file to {}.'.format(path)) + + +def _hash_prefixed_filename(name: str) -> str: + return '{hash:x} {name}'.format(hash=random.getrandbits(32), name=name) diff --git a/www/conservancy/templates/ccs_upload/upload.html b/www/conservancy/templates/ccs_upload/upload.html new file mode 100644 index 00000000..57c9bf1d --- /dev/null +++ b/www/conservancy/templates/ccs_upload/upload.html @@ -0,0 +1,34 @@ +{% extends "base_conservancy.html" %} +{% block category %}Copyright Assignment{% endblock %} +{% load static %} + +{% block outercontent %} + +

CCS Candidate Upload

+ +
+

Please note that a progress bar is not shown and that large uploads may take a significant time.

+ + {% if messages %} + + {% endif %} + +
+ {% csrf_token %} + {{ form.as_p }} + +

+
+
+{% endblock %} diff --git a/www/conservancy/urls.py b/www/conservancy/urls.py index 3a94533e..0cd95c48 100644 --- a/www/conservancy/urls.py +++ b/www/conservancy/urls.py @@ -58,4 +58,5 @@ urlpatterns = [ url(r'^sustainer', include('conservancy.apps.supporter.urls')), url(r'^coming-soon.html', static_views.index), url(r'^fundraiser_data', fundgoal_views.view), + url(r'^ccs-upload/', include('conservancy.apps.ccs_upload.urls', namespace='ccs_upload')), ]