You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@steve.apache.org by hu...@apache.org on 2015/03/20 22:13:49 UTC
svn commit: r1668150 - in /steve/trunk/pytest/www/cgi-bin: lib/voter.py
rest_voter.py
Author: humbedooh
Date: Fri Mar 20 21:13:49 2015
New Revision: 1668150
URL: http://svn.apache.org/r1668150
Log:
- don't duplicate var names
- add receipt emails
Modified:
steve/trunk/pytest/www/cgi-bin/lib/voter.py
steve/trunk/pytest/www/cgi-bin/rest_voter.py
Modified: steve/trunk/pytest/www/cgi-bin/lib/voter.py
URL: http://svn.apache.org/viewvc/steve/trunk/pytest/www/cgi-bin/lib/voter.py?rev=1668150&r1=1668149&r2=1668150&view=diff
==============================================================================
--- steve/trunk/pytest/www/cgi-bin/lib/voter.py (original)
+++ steve/trunk/pytest/www/cgi-bin/lib/voter.py Fri Mar 20 21:13:49 2015
@@ -1,5 +1,13 @@
import hashlib, json, random, os, sys
-from __main__ import homedir
+from __main__ import homedir, config
+
+# SMTP Lib
+import smtplib
+from email.mime.text import MIMEText
+from email.mime.multipart import MIMEMultipart
+
+
+
def get(election, basedata, uid):
elpath = os.path.join(homedir, "issues", election)
@@ -38,3 +46,25 @@ def remove(election, basedata, email):
f.close()
return uid, xhash
+def email(rcpt, subject, message):
+ sender = config.get("email", "sender")
+ signature = config.get("email", "signature")
+ receivers = [rcpt]
+ msg = """From: %s
+To: %s
+Subject: %s
+
+%s
+
+With regards,
+%s
+--
+Powered by Apache STeVe - https://steve.apache.org
+""" % (sender, rcpt, subject, message, signature)
+
+ try:
+ smtpObj = smtplib.SMTP(config.get("email", "mta"))
+ smtpObj.sendmail(sender, receivers, msg)
+ except SMTPException:
+ raise Exception("Could not send email - SMTP server down?")
+
\ No newline at end of file
Modified: steve/trunk/pytest/www/cgi-bin/rest_voter.py
URL: http://svn.apache.org/viewvc/steve/trunk/pytest/www/cgi-bin/rest_voter.py?rev=1668150&r1=1668149&r2=1668150&view=diff
==============================================================================
--- steve/trunk/pytest/www/cgi-bin/rest_voter.py (original)
+++ steve/trunk/pytest/www/cgi-bin/rest_voter.py Fri Mar 20 21:13:49 2015
@@ -23,6 +23,7 @@ if sys.hexversion < 0x03000000:
else:
import configparser
version = 3
+
path = os.path.abspath(os.getcwd())
@@ -70,7 +71,7 @@ if pathinfo:
if 'hash' in basedata:
del basedata['hash']
- issues = [ f for f in listdir(elpath) if os.path.isfile(os.path.join(elpath,f)) and f != "basedata.json" ]
+ issues = [ f for f in listdir(elpath) if os.path.isfile(os.path.join(elpath,f)) and f != "basedata.json" and f != "voters.json" and f.endswith(".json")]
for issue in issues:
try:
with open(elpath + "/" + issue, "r") as f:
@@ -113,12 +114,12 @@ if pathinfo:
issuepath = os.path.join(homedir, "issues", election, issue) + ".json"
if os.path.isdir(elpath) and os.path.isfile(issuepath):
basedata = {}
- issue = {}
+ issuedata = {}
with open(elpath + "/basedata.json", "r") as f:
basedata = json.loads(f.read())
f.close()
with open(issuepath, "r") as f:
- issue = json.loads(f.read())
+ issuedata = json.loads(f.read())
f.close()
email = voter.get(election, basedata, voterid)
if not email:
@@ -131,8 +132,8 @@ if pathinfo:
double = False
invalid = False
letters = ['y','n','a']
- if issue['type'].find("stv") == 0:
- letters = [chr(i) for i in range(ord('a'),ord('a') + len(issue['candidates'])-1)]
+ if issuedata['type'].find("stv") == 0:
+ letters = [chr(i) for i in range(ord('a'),ord('a') + len(issuedata['candidates'])-1)]
for char in letters:
if vote.count(char) > 1:
double = True
@@ -149,12 +150,13 @@ if pathinfo:
votes = {}
if os.path.isfile(issuepath + ".votes"):
with open(issuepath + ".votes", "r") as f:
- votes = json.reads(f.read())
+ votes = json.loads(f.read())
f.close()
votes[voterid] = vote
with open(issuepath + ".votes", "w") as f:
f.write(json.dumps(votes))
f.close()
+ voter.email(email, "Vote registered: %s (%s)" % (issue, issuedata['title']), "This is a receipt that your vote was registered for issue #%s:\n\nElection: %s (%s)\nIssue: %s (%s)" % (issue, basedata['title'], election, issuedata['title'], issue))
response.respond(200, {'message': 'Vote saved!'})
else:
Re: svn commit: r1668150 - in /steve/trunk/pytest/www/cgi-bin: lib/voter.py rest_voter.py
Posted by "Alan D. Cabrera" <li...@toolazydogs.com>.
You are a coding machine.
> On Mar 20, 2015, at 2:13 PM, humbedooh@apache.org wrote:
>
> +def email(rcpt, subject, message):
> + sender = config.get("email", "sender")
> + signature = config.get("email", "signature")
> + receivers = [rcpt]
> + msg = """From: %s
> +To: %s
> +Subject: %s
> +
> +%s
> +
> +With regards,
> +%s
> +--
> +Powered by Apache STeVe - https://steve.apache.org <https://steve.apache.org/>
> +""" % (sender, rcpt, subject, message, signature)
> +
> + try:
> + smtpObj = smtplib.SMTP(config.get("email", "mta"))
> + smtpObj.sendmail(sender, receivers, msg)
> + except SMTPException:
> + raise Exception("Could not send email - SMTP server down?")
> +
> \ No newline at end of file
Consider using jinja2 templates.
Regards,
Alan