-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
59 lines (47 loc) · 1.98 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8
import codecs
import csv
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.template import RequestContext
from rapidsms.webui.utils import render_to_response, paginated
from ussd.models import *
from utilities.export import export
def index(req):
return render_to_response(req,
"ussd/index.html", {
"sims" : SIM.objects.all(),
"transfers" : AirtimeTransfer.objects.all()
})
def bulk_airtime(req):
file = req.FILES['file']
amount = req.POST['amount']
sim = SIM.objects.get(pk=int(req.POST['sim']))
# use codecs.open() instead of open() so all characters are utf-8 encoded
# BEFORE we start dealing with them (just in case)
# rU option is for universal-newline mode which takes care of \n or \r etc
#csvee = codecs.open(file, "rU", encoding='utf-8', errors='ignore')
csvee = file
# sniffer attempts to guess the file's dialect e.g., excel, etc
dialect = csv.Sniffer().sniff(csvee.read(1024))
csvee.seek(0)
# DictReader uses first row of csv as key for data in corresponding column
#reader = csv.DictReader(csvee, dialect=dialect)
reader = csv.DictReader(csvee, quoting=csv.QUOTE_ALL, dialect=dialect)
try:
for row in reader:
if row.has_key('NUMERO'):
if row['NUMERO'] != "":
print(row['NUMERO'])
# cast as strings
trans = AirtimeTransfer(destination=str(row['NUMERO']),\
amount=amount, status='Q', sim=sim)
trans.save()
continue
except csv.Error, e:
# TODO handle this error?
print('%d : %s' % (reader.reader.line_num, e))
return HttpResponseRedirect("/ussd")
def csv_transfers(req, format='csv'):
return export(AirtimeTransfer.objects.all())