Commits

Benjamin Klüglein committed fbea7fd

Added forms and template files.
+ First version of working workflow

Comments (0)

Files changed (5)

     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
+    ''
 )
 
 INSTALLED_APPS = (

templates/upload/show_download.html

+<div>
+{{ download.id }}
+{{ download.name }}
+
+{{ download.description }}
+
+{{ download.filename }}
+{{ download.download_password }}
+
+{{ download.is_password_protected }}
+
+{{ download.created_at }}
+
+{% if form %}
+<form action="/download/{{ download.id }}/" method="post">
+
+{% if download.is_password_protected %}
+     {{ form.as_p }}
+{% endif %}
+<input type="submit" value="Download" />
+</form>
+{% endif %}
+</div>
+from django import forms
+
+class DownloadFileForm(forms.Form):
+    password = forms.CharField(required=False)
+
 from django.db import models
 from django.contrib.auth.models import User
 
+from os import path
 
 class File(models.Model):
     owner = models.ForeignKey(User)
     def is_password_protected(self):
         return any(self.download_password )
     
+    def filename(self):
+        return path.basename(self.file_on_fs.name)
+from mimetypes import guess_type
+
+from django.core.files import File as DjangoFile
+from django.http import HttpResponse, Http404, HttpResponseForbidden
+from django.shortcuts import render_to_response, get_object_or_404
+
 from Datenaustausch.upload.models import File
-from django.http import HttpResponse, Http404
-
-from os import path
+from Datenaustausch.upload.forms import DownloadFileForm
 
 def download(request, download_id):
-    try:
-        f = File.objects.get(pk=download_id)
-    except File.DoesNotExist:
-        raise Http404
-        
-    return HttpResponse(path.basename(f.file_on_fs.name))
+    f = get_object_or_404(File, pk=download_id)
+    if request.method == 'GET':
+        form = DownloadFileForm()
+        return render_to_response("templates/upload/show_download.html", {"download" : f, 
+            'form': form})
+    elif request.method == 'POST':
+        form = DownloadFileForm(request.POST)
+        if form.is_valid() and form.cleaned_data['password'] == f.download_password:
+            file_to_download = DjangoFile(f.file_on_fs)
+            response = HttpResponse(file_to_download, mimetype=guess_type(file_to_download.name)[0])
+            response['Content-Disposition'] = 'attachment; filename=%s' % f.filename()
+            return response
+        else:
+            return HttpResponseForbidden("Zugriff nicht gestattet. Passwort falsch!")