work around the fact that ballot IDs are never actually saved in their raw form, by reworking the vote ID to be a composite of the saved hash and the issue ID. This relates to STEVE-38.

git-svn-id: https://svn.apache.org/repos/asf/steve/trunk/pysteve@1736805 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/backends/es.py b/lib/backends/es.py
index b56b4a4..760bb0e 100644
--- a/lib/backends/es.py
+++ b/lib/backends/es.py
@@ -167,10 +167,12 @@
             pass # THIS IS OKAY! On initial setup, this WILL fail until an election has been created
         return elections
     
-    def vote(self,electionID, issueID, uid, vote):
+    def vote(self,electionID, issueID, uid, vote, vhash = None):
         "Casts a vote on an issue"
         eid = hashlib.sha224(electionID + ":" + issueID + ":" + uid).hexdigest()
         now = time.time()
+        if vhash:
+            eid = vhash
         self.es.index(index="steve", doc_type="votes", id=eid, body =
             {
                 'issue': issueID,
@@ -248,17 +250,13 @@
         bid = self.voter_get_uid(election, xhash)
         if not bid:
             return None
-        res = self.es.search(index="steve", doc_type="votes", body = {
-            "query": {
-                "match": {
-                    "key": xhash
-                }
-            }
-        }, size = 999)
-        results = len(res['hits']['hits'])
-        if results > 0:
-            for entry in res['hits']['hits']:
-                self.es.delete(index="steve", doc_type="votes", id=entry['_id']);
+        issues = self.issue_list(election)
+        for issue in issues:
+            vhash = hashlib.sha224(xhash + issue).hexdigest()
+            try:
+                self.es.delete(index="steve", doc_type="votes", id=vhash);
+            except:
+                pass
         return True
     
     def voter_remove(self,election, UID):
diff --git a/lib/election.py b/lib/election.py
index 70641b1..16e17a5 100644
--- a/lib/election.py
+++ b/lib/election.py
@@ -122,6 +122,8 @@
     basedata = getBasedata(electionID)
     issueData = getIssue(electionID, issueID)
     if basedata and issueData:
+        xhash = hashlib.sha224(election + ":" + voterID).hexdigest()
+        vhash = hashlib.sha224(xhash + issueID).hexdigest()
         votehash = hashlib.sha224(basedata['hash'] + issueID + voterID + vote).hexdigest()
         
         # Vote verification
@@ -131,7 +133,7 @@
             uid = voter.get(electionID, basedata, voterID)
             voteType['vote_func'](basedata, issueID, voterID, vote, uid)
             
-        backend.vote(electionID, issueID, voterID, vote)
+        backend.vote(electionID, issueID, voterID, vote, vhash = vhash)
         
         # LURK on who voted :O :O :O
        # if config.has_option("general", "lurk") and config.get("general", "lurk") == "yes":