From e0ca24b203011b28d4fe07fb3c1c76f00fdea959 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 4 Nov 2013 10:34:27 +0100 Subject: [PATCH] wip: saving submission with file attachments --- wtem/forms.py | 32 +++++++++ wtem/migrations/0001_initial.py | 70 +++++++++++++++++++ wtem/migrations/__init__.py | 0 wtem/models.py | 52 ++++++++++++++ wtem/static/wtem/wtem.js | 4 +- .../templates/wtem/exercises/file_upload.html | 2 +- wtem/templates/wtem/main.html | 5 +- wtem/templates/wtem/thanks.html | 7 ++ wtem/urls.py | 8 ++- wtem/views.py | 26 ++++++- 10 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 wtem/forms.py create mode 100644 wtem/migrations/0001_initial.py create mode 100644 wtem/migrations/__init__.py create mode 100644 wtem/models.py create mode 100644 wtem/templates/wtem/thanks.html diff --git a/wtem/forms.py b/wtem/forms.py new file mode 100644 index 0000000..61e1d7c --- /dev/null +++ b/wtem/forms.py @@ -0,0 +1,32 @@ +import os + +from django import forms +from django.utils import simplejson + +from .models import Submission, Attachment + + +class WTEMForm(forms.ModelForm): + class Meta: + model = Submission + fields = ('answers',) + + def __init__(self, *args, **kwargs): + super(WTEMForm, self).__init__(*args, **kwargs) + + ## @@ move this one level up + f = file(os.path.dirname(__file__) + '/fixtures/exercises.json') + exercises = simplejson.loads(f.read()) + f.close() + + for exercise in exercises: + if exercise['type'] != 'file_upload': + continue + self.fields['attachment_' + exercise['name']] = forms.FileField(required = False) + + def save(self): + submission = super(WTEMForm, self).save() + for file in self.files.values(): + attachment = Attachment(file = file, submission = submission) + attachment.save() + diff --git a/wtem/migrations/0001_initial.py b/wtem/migrations/0001_initial.py new file mode 100644 index 0000000..6d22934 --- /dev/null +++ b/wtem/migrations/0001_initial.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Submission' + db.create_table(u'wtem_submission', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('contact', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contact.Contact'], null=True)), + ('key', self.gf('django.db.models.fields.CharField')(unique=True, max_length=30)), + ('first_name', self.gf('django.db.models.fields.CharField')(max_length=100)), + ('last_name', self.gf('django.db.models.fields.CharField')(max_length=100)), + ('email', self.gf('django.db.models.fields.EmailField')(unique=True, max_length=100)), + ('answers', self.gf('django.db.models.fields.CharField')(max_length=65536, null=True, blank=True)), + )) + db.send_create_signal(u'wtem', ['Submission']) + + # Adding model 'Attachment' + db.create_table(u'wtem_attachment', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('submission', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['wtem.Submission'])), + ('name', self.gf('django.db.models.fields.CharField')(max_length=100)), + ('file', self.gf('django.db.models.fields.files.FileField')(max_length=100)), + )) + db.send_create_signal(u'wtem', ['Attachment']) + + + def backwards(self, orm): + # Deleting model 'Submission' + db.delete_table(u'wtem_submission') + + # Deleting model 'Attachment' + db.delete_table(u'wtem_attachment') + + + models = { + u'contact.contact': { + 'Meta': {'ordering': "('-created_at',)", 'object_name': 'Contact'}, + 'body': ('jsonfield.fields.JSONField', [], {'default': '{}'}), + 'contact': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'created_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'form_tag': ('django.db.models.fields.CharField', [], {'max_length': '32', 'db_index': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'ip': ('django.db.models.fields.IPAddressField', [], {'max_length': '15'}) + }, + u'wtem.attachment': { + 'Meta': {'object_name': 'Attachment'}, + 'file': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'submission': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['wtem.Submission']"}) + }, + u'wtem.submission': { + 'Meta': {'object_name': 'Submission'}, + 'answers': ('django.db.models.fields.CharField', [], {'max_length': '65536', 'null': 'True', 'blank': 'True'}), + 'contact': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contact.Contact']", 'null': 'True'}), + 'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '100'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + } + } + + complete_apps = ['wtem'] \ No newline at end of file diff --git a/wtem/migrations/__init__.py b/wtem/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wtem/models.py b/wtem/models.py new file mode 100644 index 0000000..b6858cc --- /dev/null +++ b/wtem/models.py @@ -0,0 +1,52 @@ +import random +import string + +from django.db import models + +from contact.models import Contact + + +class Submission(models.Model): + contact = models.ForeignKey(Contact, null = True) + key = models.CharField(max_length = 30, unique = True) + first_name = models.CharField(max_length = 100) + last_name = models.CharField(max_length = 100) + email = models.EmailField(max_length = 100, unique = True) + answers = models.CharField(max_length = 65536, null = True, blank = True) + + @classmethod + def generate_key(cls): + key = '' + while not key and key in [record['key'] for record in cls.objects.values('key')]: + key = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(30)) + return key + + @classmethod + def create(cls, first_name, last_name, email, contact = None, key = None): + submissions = [] + + if contact: + students = contact['students'] + else: + students = [dict(first_name = first_name, last_name = last_name, email = email)] + + if key is None: + key = Submission.generate_key() + + for student in students: + submission = cls( + contact = contact, + key = key, + first_name = student['first_name'], + last_name = student['last_name'], + email = student['email'] + ) + submission.save() + submissions.append(submission) + return submissions + + +class Attachment(models.Model): + submission = models.ForeignKey(Submission) + name = models.CharField(max_length=100) + file = models.FileField(upload_to = 'wtem/attachment') \ No newline at end of file diff --git a/wtem/static/wtem/wtem.js b/wtem/static/wtem/wtem.js index 209108f..3f9bfad 100644 --- a/wtem/static/wtem/wtem.js +++ b/wtem/static/wtem/wtem.js @@ -3,7 +3,7 @@ $(function() { var to_submit; $('#submit_answers').click(function(e) { - e.preventDefault(); + //e.preventDefault(); to_submit = []; $('.exercise-wtem').each(function() { @@ -18,7 +18,7 @@ $(function() { } }); console.log(JSON.stringify(to_submit)); - $('input[name=wtem_answers]').val(JSON.stringify(to_submit)); + $('input[name=answers]').val(JSON.stringify(to_submit)); }); var handlers = { diff --git a/wtem/templates/wtem/exercises/file_upload.html b/wtem/templates/wtem/exercises/file_upload.html index e4be9a1..df6f64b 100644 --- a/wtem/templates/wtem/exercises/file_upload.html +++ b/wtem/templates/wtem/exercises/file_upload.html @@ -10,6 +10,6 @@ {% endfor %} - + \ No newline at end of file diff --git a/wtem/templates/wtem/main.html b/wtem/templates/wtem/main.html index 4536cfb..b3360b9 100644 --- a/wtem/templates/wtem/main.html +++ b/wtem/templates/wtem/main.html @@ -9,7 +9,8 @@ {% block body %} -
+ +{% csrf_token %} {% for exercise in exercises %} {% with 'wtem/exercises/'|add:exercise.type|add:'.html' as template_name %} @@ -19,7 +20,7 @@
- +
diff --git a/wtem/templates/wtem/thanks.html b/wtem/templates/wtem/thanks.html new file mode 100644 index 0000000..ca9463c --- /dev/null +++ b/wtem/templates/wtem/thanks.html @@ -0,0 +1,7 @@ +{% extends 'base_super.html' %} + +{% block body %} + +

Twoje rozwiązania zostały wysłane

+ +{% endblock %} \ No newline at end of file diff --git a/wtem/urls.py b/wtem/urls.py index 4fc156e..1dded94 100644 --- a/wtem/urls.py +++ b/wtem/urls.py @@ -1,6 +1,8 @@ from django.conf.urls import patterns, url -from .views import main +from django.conf import settings +from .views import main, form urlpatterns = patterns('', - url(r'^$', main, name = 'wtem_main') -) \ No newline at end of file + url(r'^$', main, name = 'wtem_main'), + url(r'^(?P.*)/$', form, name = 'wtem_form') +) diff --git a/wtem/views.py b/wtem/views.py index 6578e44..127710a 100644 --- a/wtem/views.py +++ b/wtem/views.py @@ -2,12 +2,36 @@ import os from django.shortcuts import render from django.utils import simplejson +from django.conf import settings +from django.http import Http404 + +from .models import Submission +from .forms import WTEMForm def main(request): + pass + +def form(request, key): + try: + submission = Submission.objects.get(key = key) + except Submission.DoesNotExist: + if settings.DEBUG and key == '12345': + submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = '12345')[0] + else: + raise Http404 + ## @@ move this out of the view f = file(os.path.dirname(__file__) + '/fixtures/exercises.json') exercises = simplejson.loads(f.read()) f.close() - return render(request, 'wtem/main.html', dict(exercises = exercises)) + if request.method == 'GET': + return render(request, 'wtem/main.html', dict(exercises = exercises)) + elif request.method == 'POST': + form = WTEMForm(request.POST, request.FILES, instance = submission) + if form.is_valid(): + form.save() + return render(request, 'wtem/thanks.html') + else: + raise Exception -- 2.20.1