Skip to content

Commit

Permalink
Brian's csv upload code. Not functional yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
U-rotop\rowena committed May 6, 2010
1 parent dbd3382 commit b4888c9
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apps/messaging/templates/messaging/bulk.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% load i18n %}
{% block title %}{% trans "Send SMS via CSV upload" %}{% endblock %}
{% block buildmanager_content %}
{% extends base_template %}

<h2>{% trans "CSV file to upload for bulk SMS sending" %}</h2>
<h3>{{msg}}</h3>
<form action="" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="csv_file_upload" id="id_csv_file_upload" />
<input type="submit" value="Submit" />
</form>
{{errors}}

{%endblock%}
31 changes: 31 additions & 0 deletions apps/messaging/templates/messaging/bulk_confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% load i18n %}
{% block title %}{% trans "Send SMS via CSV upload" %}{% endblock %}
{% block buildmanager_content %}
{% extends base_template %}

<h2>{% trans "CSV file to upload for bulk SMS sending" %}</h2>
<h3>{{msg}}</h3>
<p>The following SMS will be sent:
<table>
<tr>
<td>Recipient ID</td>
<td>Number</td>
<td>Message</td>
</tr>
{% for row in msglist %}
{% for key,value in row.items %}
<tr>
<td>{{ value }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
</p>
<form action="" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
<input type="hidden" name="temp-file" id="id_csv_file_upload" value="{{temp-file}}"/>
<input type="submit" value="Send SMS" />
</form>

{{errors}}

{%endblock%}
73 changes: 73 additions & 0 deletions apps/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,76 @@ def clear(req):
resp.delete_cookie("recip-error")
resp.delete_cookie("recip-sent")
return resp





def _send_message(req, id, text):
# also send the message, by hitting the ajax url of the messaging app
data = {"uid": id, "text": text}
encoded = urllib.urlencode(data)
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection(req.META["HTTP_HOST"])
conn.request("POST", "/ajax/messaging/send_message", encoded, headers)
response = conn.getresponse()

def parse_csv(filename=""):
'''helper function to parse phone_number, message CSV files'''
to_return = []
with open(filename) as f:
msglist = csv.reader(f)
for row in msglist:
obj = PersistantConnection.objects.get(identity=row[0])
to_return.append({"id":obj.reporter, "number":obj.identity, "msg":row[1]})
return to_return

@login_required
def confirm_csv_sms(request,template_name="confirm_csv.html"):
'''Chance to view what SMS will be sent before sending'''
context = {}

if request.method == 'POST':
try:
if request.POST["confirmed"] == 1:
# send them
msglist = parse_csv(request.POST["temp-file"])
for m in msglist:
_send_message(request, m["id"], m["msg"])
return HttpResponseRedirect(reverse("reports.views.upload_csv_sms")
else:
# get them all
msglist = parse_csv(request.FILES["csv_file_upload"], True)
context['msglist'] = msglist
tf = tempfile.NamedTemporaryFile(delete=False)
with open(request.FILES["csv_file_upload"]) as f:
tf.write(f.read())
context['temp-file'] = tf.name
except Exception, e:
logging.error("Error importing csv file.",
extra={'exception':e,
'request.POST': request.POST,
'request.FILES': request.FILES,
'form':form})
context['errors'] = "Could not commit build: " + str(e)
return render_to_response(request, template_name, context)

@login_required
def upload_csv_for_sms(request,template_name="upload_csv.html"):
'''A view for bulk sending SMS from a CSV file upload'''
context = {}

if request.method == 'POST':
try:
msglist = parse_csv(request.FILES["csv_file_upload"])
except Exception, e:
logging.error("Error importing csv file.",
extra={'exception':e,
'request.POST': request.POST,
'request.FILES': request.FILES,
'form':form})
context['errors'] = "Could not commit build: " + str(e)
return HttpResponseRedirect(reverse("reports.views.confirm_csv_sms")
return render_to_response(request, template_name, context)

0 comments on commit b4888c9

Please sign in to comment.