Close #116 - adding in a view to export user data as a CSV #119

Merged
merged 1 commit into from Feb 18, 2013

Projects

None yet

3 participants

@rossbruniges
Member

No description provided.

@alfredo alfredo commented on the diff Feb 18, 2013
gameon/submissions/views.py
@@ -107,3 +109,41 @@ def single(request, slug, template='submissions/single.html'):
'entry': entry,
}
return render(request, template, data)
+
+
+def export_csv(request, template='submissions/table.html'):
+ if not request.user.is_staff:
+ return action_unavailable_response(request, case='not_staff')
+
+ response = HttpResponse(content_type='text/csv')
+ response['Content-Disposition'] = 'attachment; filename="gameon_entries.csv"'
@alfredo
alfredo Feb 18, 2013 Mozilla member

Maybe add a date and time to the file name to be able to differentiate between different exports?

Although this may not be that important.

@rossbruniges
rossbruniges Feb 18, 2013 Mozilla member

Nice idea

@alfredo
Member
alfredo commented Feb 18, 2013

+1 looks good to me!

@alfredo alfredo commented on the diff Feb 18, 2013
gameon/submissions/views.py
+
+ writer = csv.writer(response)
+ # Write out the header row
+ writer.writerow([
+ 'GAME NAME',
+ 'CATEGORY',
+ 'GAME WEBSITE',
+ 'GAME DESCRIPTION',
+ 'GAME SUBMISSION URL',
+ 'GAME VIDEO URL',
+ 'SUBMITTED BY',
+ 'SUBMITTED BIO',
+ 'SUBMIT TO MARKETPLACE?'
+ ])
+ for e in entry_set:
+ writer.writerow([
@alfredo
alfredo Feb 18, 2013 Mozilla member

As a word of advice, the writer doesn't support unicode by default. If unicode characters are expected on the title an different writer may need to be considered.

@rossbruniges
rossbruniges Feb 18, 2013 Mozilla member

If the worst we get is a dodgy looking char here and there then I think that's fine. Is that what will happen or will the export fail?

@alfredo
alfredo Feb 18, 2013 Mozilla member

It would rise a 500 with a UnicodeEncoding error.

@rossbruniges
rossbruniges Feb 18, 2013 Mozilla member

Hmm

@andrewhayward
andrewhayward Feb 18, 2013 Mozilla member

How concerned are you with data integrity? If you don't mind losing the odd non-translatable character, you can encode it yourself before hand.

e.title.encode('ascii', 'ignore')

You can encode('ascii', 'replace') if you'd rather see ?s than nothing at all.

For extra Brownie points, you could normalise it before hand, which would go some way to dealing with characters that are ASCII-like (such as ä, é, etc).

import unicodedata
unicodedata.normalize('NFKD', e.title).encode('ascii', 'ignore')
@rossbruniges
rossbruniges Feb 18, 2013 Mozilla member

@andrewhayward I've got a fix ready to land - just prepping the pull request, thanks for piling the feedback in!

@rossbruniges
Member

Updated with a fix for potential unicode errors on export

@alfredo alfredo commented on the diff Feb 18, 2013
gameon/base/utils.py
+# In case we get unicode in the DB we use a custom reader able to handle it
+class UTF8Recoder:
+ """
+ Iterator that reads an encoded stream and reencodes the input to UTF-8
+ """
+ def __init__(self, f, encoding):
+ self.reader = codecs.getreader(encoding)(f)
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ return self.reader.next().encode("utf-8")
+
+
+class UnicodeWriter:
@alfredo
alfredo Feb 18, 2013 Mozilla member

Lovely!

@alfredo
Member
alfredo commented Feb 18, 2013

Looks awesome +1

@rossbruniges rossbruniges merged commit c6170bc into mozilla:master Feb 18, 2013
@rossbruniges rossbruniges deleted the rossbruniges:data-export branch Feb 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment