Permalink
Browse files

adding a address csv import

  • Loading branch information...
arneb committed Mar 30, 2009
1 parent 487ee96 commit 6e72482d15c50d45baa7fed42b8c20974fe7f833
Showing with 34 additions and 8 deletions.
  1. +22 −6 campaign/admin.py
  2. +7 −2 campaign/forms.py
  3. +5 −0 campaign/templates/admin/campaign/subscriber/import.html
View
@@ -1,3 +1,4 @@
+import csv
from django import forms
from django.shortcuts import render_to_response
from django import template
@@ -39,14 +40,29 @@ def import_view(self, request, extra_context=None):
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
- data = json.loads(form.cleaned_data['file'].read())
num_import = 0
- for entry in data:
+
+ try: # try json
+ data = json.loads(form.cleaned_data['file'].read())
+
+ for entry in data:
+ try:
+ Subscriber.objects.create(email=entry['email'], salutation=entry['name'])
+ num_import += 1
+ except Exception, e:
+ pass
+ except: # may be csv data
try:
- Subscriber.objects.create(email=entry['email'], salutation=entry['name'])
- num_import += 1
- except Exception, e:
- pass
+ reader = csv.reader(form.cleaned_data['file'].readlines(), delimiter=',')
+ for entry in reader:
+ try:
+ Subscriber.objects.create(email=entry[0], salutation=entry[1])
+ num_import += 1
+ except Exception, e:
+ pass
+ except:
+ raise
+
request.user.message_set.create(message=_(u'Successfully imported %(num_import)s %(name)s.' % {'name': force_unicode(opts.verbose_name_plural), 'num_import': num_import,}))
return HttpResponseRedirect('../')
View
@@ -1,3 +1,4 @@
+import csv
from django import forms
from django.utils.simplejson import simplejson as json
from django.utils.translation import ugettext as _
@@ -14,6 +15,10 @@ def clean_file(self):
self.cleaned_data['file'].seek(0)
return self.cleaned_data['file']
except Exception, e:
- raise forms.ValidationError(_(u"uploaded file must contain json data"))
+ try:
+ reader = csv.reader(self.cleaned_data['file'].readlines())
+ self.cleaned_data['file'].seek(0)
+ return self.cleaned_data['file']
+ except Exception, e:
+ raise forms.ValidationError(_(u"uploaded file must contain json or csv data"))
-
@@ -26,6 +26,11 @@
{% endblocktrans %}
<pre>[{"email": "info@example.com", "name": "Peter"},
{"email": "news@example.de", "name": "Hans"}]</pre>
+ {% blocktrans %}or in csv format with ',' as delimiter:
+ {% endblocktrans %}
+ <pre>"info@example.com", "Peter"
+"news@example.de", "Hans"</pre>
+
</div>
<div class="form-row">
{{ form }}

0 comments on commit 6e72482

Please sign in to comment.