Skip to content

Commit

Permalink
re-added email batching
Browse files Browse the repository at this point in the history
  • Loading branch information
benadida committed Sep 18, 2008
1 parent 14bd4f8 commit e1b9cb2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
18 changes: 14 additions & 4 deletions controllers/election.py
Expand Up @@ -595,15 +595,23 @@ def email_voters(self, election_id):

@web
@session.login_protect
def email_voters_2(self, election, introductory_message):
def email_voters_2(self, election, introductory_message, after=None, limit=None):
"""
Send email to voters of an election.
"""
user, api_client, election = self.check(election, True, True)

voters = election.get_voters()
if after:
after = str(after)
if limit:
limit = int(limit)

voters = election.get_voters(after=after, limit=limit)

last_id = None

for voter in voters:
logging.info("sending email to %s" % voter.email)
message_header = """
Dear %s,
Expand All @@ -622,10 +630,12 @@ def email_voters_2(self, election, introductory_message):

message = message_header + introductory_message + message_footer

mail.simple_send([voter.name],[voter.email],"Helios","ben@adida.net","An Invitation to Vote in %s" % election.name, message)
mail.simple_send([voter.name],[voter.email],"Helios","ben@adida.net","Your Vote in Election %s" % election.name, message)

last_id = voter.voter_id

# hack for now, no more batching
return "DONE"
return last_id or "DONE"

@web
@session.login_protect
Expand Down
6 changes: 3 additions & 3 deletions index.yaml
Expand Up @@ -10,14 +10,14 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

# Used 12 times in query history.
# Used 25 times in query history.
- kind: ElectionExponent
properties:
- name: election
- name: exponent
direction: desc

# Used 168 times in query history.
# Used 172 times in query history.
- kind: KeyShare
properties:
- name: election
Expand All @@ -43,7 +43,7 @@ indexes:
- name: tallied_at
- name: cast_id

# Used 267 times in query history.
# Used 301 times in query history.
- kind: Voter
properties:
- name: election
Expand Down
3 changes: 2 additions & 1 deletion models/modelsbase.py
Expand Up @@ -31,6 +31,7 @@ def toJSONDict(self):
if self.openreg_enabled:
self.openreg = True
else:
## FIXME: make this more efficient for large number of voters
self.voters_hash = self.get_voters_hash()

return DBObject.toJSONDict(self)
Expand Down Expand Up @@ -85,7 +86,7 @@ def get_cast_votes(self, after=None, limit=None):
def get_voters_hash(self):
voters = self.get_voters()
voters_json = utils.to_json([v.toJSONDict() for v in voters])
logging.info("json for voters is: " + voters_json)
# logging.info("json for voters is: " + voters_json)
return utils.hash_b64(voters_json)

def freeze(self):
Expand Down
22 changes: 18 additions & 4 deletions templates/election/email_voters.tmpl
Expand Up @@ -3,13 +3,24 @@
<script>
BATCH_SIZE = 10;

function send_email() {
function send_email(after) {
\$('#submit').hide();
\$('#processing').show();

var form = document.forms['email_form'];

\$.post("email_voters_2", {'introductory_message' : form.introductory_message.value}, function(data) {
params = {'introductory_message' : form.introductory_message.value};

if (after) {
params.after = after;
}

params.limit = "5";

\$.post("email_voters_2", params, function(data) {
// decide whether or not to keep going
if (data != "DONE") {
alert('oy unexpected: ' + data);
setTimeout("send_email('" + data + "')", 10000);
} else {
alert('done!');
document.location = "./view";
Expand Down Expand Up @@ -42,7 +53,10 @@ Your password: [VOTER_PASSWORD]

<br />
<br /><br />
<input type="submit" value="send now!" class="pretty" />
<input type="submit" value="send now!" class="pretty" id="submit" />
<div id="processing" style="display: none;">
Processing Email... this will take approximately 2 seconds per voter, as we have to batch emails and pause between batches.
</div>
</form>

</div>
Expand Down

0 comments on commit e1b9cb2

Please sign in to comment.