From 4948a7961f3849de772293dd83d2f70c4ee0c5e9 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 31 Jan 2014 15:45:02 -0500 Subject: [PATCH] Upload for T4U is fixed --- frontend/forms.py | 27 +++++++++++++++++++++++++-- frontend/templates/rh_tools.html | 32 ++++++++++++++++++++------------ requirements_versioned.pip | 1 + 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/frontend/forms.py b/frontend/forms.py index c7e59b74..70fb67d6 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -2,6 +2,7 @@ external library imports """ import logging +import zipfile from datetime import timedelta, datetime, date from decimal import Decimal as D @@ -26,6 +27,9 @@ from selectable.forms import ( AutoCompleteSelectField ) +from PyPDF2 import PdfFileReader + + """ regluit imports """ @@ -139,13 +143,15 @@ class EditionForm(forms.ModelForm): } class EbookFileForm(forms.ModelForm): - file = EpubFileField(max_length=16777216) + file = forms.FileField(max_length=16777216) def __init__(self, campaign_type=BUY2UNGLUE, *args, **kwargs): super(EbookFileForm, self).__init__(*args, **kwargs) self.campaign_type = campaign_type if campaign_type == BUY2UNGLUE: - self.fields['format'].widget=forms.HiddenInput + self.fields['format'].widget=forms.HiddenInput() + if campaign_type == THANKS: + self.fields['format'].widget=forms.Select(choices=(('pdf','PDF'),( 'epub','EPUB'), ('mobi','MOBI'))) def clean_format(self): if self.campaign_type is BUY2UNGLUE: @@ -153,6 +159,23 @@ class EbookFileForm(forms.ModelForm): else: logger.info("EbookFileForm "+self.cleaned_data.get('format','')) return self.cleaned_data.get('format','') + + def clean(self): + format = self.cleaned_data['format'] + the_file = self.cleaned_data.get('file',None) + if the_file and the_file.name: + if format == 'epub': + if not zipfile.is_zipfile(the_file.file): + raise forms.ValidationError(_('%s is not a valid EPUB file' % the_file.name) ) + elif format == 'mobi': + if not zipfile.is_zipfile(the_file.file): + raise forms.ValidationError(_('%s is not a valid MOBI file' % the_file.name) ) + elif format == 'pdf': + try: + doc = PdfFileReader(the_file.file) + except Exception, e: + raise forms.ValidationError(_('%s is not a valid PDF file' % the_file.name) ) + return self.cleaned_data class Meta: model = EbookFile diff --git a/frontend/templates/rh_tools.html b/frontend/templates/rh_tools.html index e3341429..2968c302 100644 --- a/frontend/templates/rh_tools.html +++ b/frontend/templates/rh_tools.html @@ -49,22 +49,30 @@ Any questions not covered here? Please email us at
{% ifequal campaign.type 1 %} - Pledge Campaign
- Campaign status: {{ campaign.status }}
- Created: {{ campaign.created }}
- ${{ campaign.current_total }} pledged of ${{ campaign.target }}, {{ campaign.supporters_count }} supporters + Pledge Campaign
+ Campaign status: {{ campaign.status }}
+ Created: {{ campaign.created }}
+ ${{ campaign.current_total }} pledged of ${{ campaign.target }}, {{ campaign.supporters_count }} supporters {% endifequal %} {% ifequal campaign.type 2 %} - Buy-to-Unglue Campaign
- Campaign status: {{ campaign.status }}
- Created: {{ campaign.created }}
- ${{ campaign.current_total }} sold. ${{ campaign.target }} to go. Ungluing Date: {{ campaign.cc_date }} + Buy-to-Unglue Campaign
+ Campaign status: {{ campaign.status }}
+ Created: {{ campaign.created }}
+ ${{ campaign.current_total }} sold. ${{ campaign.target }} to go. Ungluing Date: {{ campaign.cc_date }}
+ {% with campaign.work.preferred_edition as edition %} +
Edit the preferred edition
+ You can also Load a file for this edition.
+ {% endwith %} {% endifequal %} {% ifequal campaign.type 3 %} - Thanks-for-Ungluing Campaign
- Campaign status: {{ campaign.status }}
- Created: {{ campaign.created }}
- ${{ campaign.current_total }} raised from {{ campaign.supporters_count }} supporters. + Thanks-for-Ungluing Campaign
+ Campaign status: {{ campaign.status }}
+ Created: {{ campaign.created }}
+ ${{ campaign.current_total }} raised from {{ campaign.supporters_count }} supporters. + {% with campaign.work.preferred_edition as edition %} + Edit the preferred edition
+ You can also Load a file for this edition.
+ {% endwith %} {% endifequal %}
{% if campaign.status = 'ACTIVE' or campaign.status = 'INITIALIZED' %} diff --git a/requirements_versioned.pip b/requirements_versioned.pip index 01768386..e1a0d032 100644 --- a/requirements_versioned.pip +++ b/requirements_versioned.pip @@ -2,6 +2,7 @@ Django==1.4.5 Fabric==1.4.3 MySQL-python==1.2.3 Pillow==1.7.7 +PyPDF2==1.20 Pyzotero==0.9.51 South==0.7.6 WebOb==1.2.3