Changeset - 9a06def0e723
[Not reviewed]
0 1 4
Ben Sturmfels (bsturmfels) - 11 months ago 2021-12-03 00:38:21
ben@sturm.com.au
Add simple CCS upload (no JavaScript).
5 files changed with 80 insertions and 0 deletions:
0 comments (0 inline, 0 general)
www/conservancy/apps/ccs_upload/__init__.py
Show inline comments
 
new file 100644
www/conservancy/apps/ccs_upload/urls.py
Show inline comments
 
new file 100644
 
from django.conf.urls import url
 

	
 
from . import views
 

	
 
urlpatterns = [
 
    url(r'^$', views.upload, name='form')
 
]
www/conservancy/apps/ccs_upload/views.py
Show inline comments
 
new file 100644
 
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)
www/conservancy/templates/ccs_upload/upload.html
Show inline comments
 
new file 100644
 
{% extends "base_conservancy.html" %}
 
{% block category %}Copyright Assignment{% endblock %}
 
{% load static %}
 

	
 
{% block outercontent %}
 
  <style>
 
   label { display: block; }
 

	
 
   li.info {
 
     background: #F0FFB8;
 
     border: 1px solid #D0DD98;
 
   }
 
  </style>
 
  <h1>CCS Candidate Upload</h1>
 

	
 
  <div class="mw7 mb4">
 
    <p>Please note that a progress bar is not shown and that large uploads may take a significant time.</p>
 

	
 
    {% if messages %}
 
      <ul class="messages list mv2 ">
 
        {% for message in messages %}
 
          <li class="{{ message.tags }} pa2">{{ message }}</li>
 
        {% endfor %}
 
      </ul>
 
    {% endif %}
 

	
 
    <form action="." method="post" enctype="multipart/form-data" class="mw7">
 
      {% csrf_token %}
 
      {{ form.as_p }}
 

	
 
      <p><button type="submit" class="ph3 pv2">Next</button></p>
 
    </form>
 
  </div>
 
{% endblock %}
www/conservancy/urls.py
Show inline comments
...
 
@@ -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')),
 
]
0 comments (0 inline, 0 general)