righto, I'll remember that for next commit :)

With regards,
Daniel

On 2015-03-25 17:26, Pierre Smits wrote:
I have created an issue STEVE-19
<https://issues.apache.org/jira/browse/STEVE-19> for this kind of future
work.

Best regards,

Pierre Smits

*ORRTIZ.COM <http://www.orrtiz.com>*
Services & Solutions for Cloud-
Based Manufacturing, Professional
Services and Retail & Trade
http://www.orrtiz.com

On Wed, Mar 25, 2015 at 5:17 PM, <[email protected]> wrote:

Author: humbedooh
Date: Wed Mar 25 16:17:30 2015
New Revision: 1669142

URL: http://svn.apache.org/r1669142
Log:
Allow for other ways of fetching vote data in the future (ElasticSearch
anyone??)

Modified:
     steve/trunk/pysteve/lib/election.py
     steve/trunk/pysteve/lib/voter.py
     steve/trunk/pysteve/steve.cfg

Modified: steve/trunk/pysteve/lib/election.py
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/election.py?rev=1669142&r1=1669141&r2=1669142&view=diff

==============================================================================
--- steve/trunk/pysteve/lib/election.py (original)
+++ steve/trunk/pysteve/lib/election.py Wed Mar 25 16:17:30 2015
@@ -27,114 +27,125 @@ from plugins import *

  def exists(election, *issue):
      "Returns True if an election/issue exists, False otherwise"
-    elpath = os.path.join(homedir, "issues", election)
-    if issue:
-        elpath += "/" + issue[0] + ".json"
-        return os.path.isfile(elpath)
-    else:
-        return os.path.isdir(elpath)
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        if issue:
+            elpath += "/" + issue[0] + ".json"
+            return os.path.isfile(elpath)
+        else:
+            return os.path.isdir(elpath)


  def getBasedata(election, hideHash=False):
      "Get base data from an election"
-    elpath = os.path.join(homedir, "issues", election)
-    if os.path.isdir(elpath):
-        with open(elpath + "/basedata.json", "r") as f:
-            data = f.read()
-            f.close()
-            basedata = json.loads(data)
-            if hideHash and 'hash' in basedata:
-                del basedata['hash']
-            basedata['id'] = election
-            return basedata
+
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        if os.path.isdir(elpath):
+            with open(elpath + "/basedata.json", "r") as f:
+                data = f.read()
+                f.close()
+                basedata = json.loads(data)
+                if hideHash and 'hash' in basedata:
+                    del basedata['hash']
+                basedata['id'] = election
+                return basedata
      return None

  def close(election, reopen = False):
      "Mark an election as closed"
-    elpath = os.path.join(homedir, "issues", election)
-    if os.path.isdir(elpath):
-        basedata = {}
-        with open(elpath + "/basedata.json", "r") as f:
-            data = f.read()
-            f.close()
-            basedata = json.loads(data)
-        if reopen:
-            basedata['closed'] = False
-        else:
-            basedata['closed'] = True
-        with open(elpath + "/basedata.json", "w") as f:
-            f.write(json.dumps(basedata))
-            f.close()
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        if os.path.isdir(elpath):
+            basedata = {}
+            with open(elpath + "/basedata.json", "r") as f:
+                data = f.read()
+                f.close()
+                basedata = json.loads(data)
+            if reopen:
+                basedata['closed'] = False
+            else:
+                basedata['closed'] = True
+            with open(elpath + "/basedata.json", "w") as f:
+                f.write(json.dumps(basedata))
+                f.close()

  def getIssue(electionID, issueID):
      "Get JSON data from an issue"
-    issuepath = os.path.join(homedir, "issues", electionID, issueID) +
".json"
      issuedata = None
-    if os.path.isfile(issuepath):
-        ihash = ""
-        with open(issuepath, "r") as f:
-            data = f.read()
-            ihash = hashlib.sha224(data).hexdigest()
-            f.close()
-            issuedata = json.loads(data)
-        issuedata['hash'] = ihash
-        issuedata['id'] = issueID
-        issuedata['APIURL'] = "https://%s/steve/voter/view/%s/%s"; %
(config.get("general", "rooturl"), electionID, issueID)
-        issuedata['prettyURL'] = "https://%s/steve/ballot?%s/%s"; %
(config.get("general", "rooturl"), electionID, issueID)
-
-        # Add vote category for JS magic
-        for vtype in constants.VOTE_TYPES:
-            if vtype['key'] == issuedata['type']:
-                issuedata['category'] = vtype['category']
-                break
+    if config.get("database", "dbsys") == "file":
+        issuepath = os.path.join(homedir, "issues", electionID, issueID)
+ ".json"
+        if os.path.isfile(issuepath):
+            ihash = ""
+            with open(issuepath, "r") as f:
+                data = f.read()
+                ihash = hashlib.sha224(data).hexdigest()
+                f.close()
+                issuedata = json.loads(data)
+            issuedata['hash'] = ihash
+            issuedata['id'] = issueID
+            issuedata['APIURL'] = "https://%s/steve/voter/view/%s/%s"; %
(config.get("general", "rooturl"), electionID, issueID)
+            issuedata['prettyURL'] = "https://%s/steve/ballot?%s/%s"; %
(config.get("general", "rooturl"), electionID, issueID)

+            # Add vote category for JS magic
+            for vtype in constants.VOTE_TYPES:
+                if vtype['key'] == issuedata['type']:
+                    issuedata['category'] = vtype['category']
+                    break
+
      return issuedata


  def getVotes(electionID, issueID):
      "Read votes from the vote file"
-    issuepath = os.path.join(homedir, "issues", electionID, issueID) +
".json.votes"
-    issuedata = {}
-    if os.path.isfile(issuepath):
-        with open(issuepath, "r") as f:
-            data = f.read()
-            f.close()
-            issuedata = json.loads(data)
-    return issuedata
+    if config.get("database", "dbsys") == "file":
+        issuepath = os.path.join(homedir, "issues", electionID, issueID)
+ ".json.votes"
+        issuedata = {}
+        if os.path.isfile(issuepath):
+            with open(issuepath, "r") as f:
+                data = f.read()
+                f.close()
+                issuedata = json.loads(data)
+        return issuedata
+    return {}

  def createElection(eid, title, owner, monitors, starts, ends, isopen):
-    elpath = os.path.join(homedir, "issues", eid)
-    os.mkdir(elpath)
-    with open(elpath  + "/basedata.json", "w") as f:
-        f.write(json.dumps({
-            'title': title,
-            'owner': owner,
-            'monitors': monitors,
-            'starts': starts,
-            'ends': ends,
-            'hash': hashlib.sha512("%f-stv-%s" % (time.time(),
os.environ['REMOTE_ADDR'] if 'REMOTE_ADDR' in os.environ else
random.randint(1,99999999999))).hexdigest(),
-            'open': isopen
-        }))
-        f.close()
-    with open(elpath  + "/voters.json", "w") as f:
-        f.write("{}")
-        f.close()
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", eid)
+        os.mkdir(elpath)
+        with open(elpath  + "/basedata.json", "w") as f:
+            f.write(json.dumps({
+                'title': title,
+                'owner': owner,
+                'monitors': monitors,
+                'starts': starts,
+                'ends': ends,
+                'hash': hashlib.sha512("%f-stv-%s" % (time.time(),
os.environ['REMOTE_ADDR'] if 'REMOTE_ADDR' in os.environ else
random.randint(1,99999999999))).hexdigest(),
+                'open': isopen
+            }))
+            f.close()
+        with open(elpath  + "/voters.json", "w") as f:
+            f.write("{}")
+            f.close()


  def listIssues(election):
      "List all issues in an election"
      issues = []
-    elpath = os.path.join(homedir, "issues", election)
-    if os.path.isdir(elpath):
-        issues = [f.strip(".json") for f in os.listdir(elpath) if
os.path.isfile(os.path.join(elpath, f)) and f != "basedata.json" and f !=
"voters.json" and f.endswith(".json")]
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        if os.path.isdir(elpath):
+            issues = [f.strip(".json") for f in os.listdir(elpath) if
os.path.isfile(os.path.join(elpath, f)) and f != "basedata.json" and f !=
"voters.json" and f.endswith(".json")]
      return issues

  def listElections():
      "List all elections"
      elections = []
-    path = os.path.join(homedir, "issues")
-    if os.path.isdir(path):
-        elections = [f for f in os.listdir(path) if
os.path.isdir(os.path.join(path, f))]
+    if config.get("database", "dbsys") == "file":
+        path = os.path.join(homedir, "issues")
+        if os.path.isdir(path):
+            elections = [f for f in os.listdir(path) if
os.path.isdir(os.path.join(path, f))]
+
      return elections

  def getVoteType(issue):
@@ -148,43 +159,45 @@ def vote(electionID, issueID, voterID, v
      votes = {}
      basedata = getBasedata(electionID)
      if basedata:
-        issuepath = os.path.join(homedir, "issues", electionID, issueID)
+ ".json"
-        if os.path.isfile(issuepath + ".votes"):
-            with open(issuepath + ".votes", "r") as f:
-                votes = json.loads(f.read())
-                f.close()
-        votes[voterID] = vote
-        with open(issuepath + ".votes", "w") as f:
-            f.write(json.dumps(votes))
-            f.close()
          votehash = hashlib.sha224(basedata['hash'] + issueID + voterID +
vote).hexdigest()

-        # LURK on who voted :O :O :O
-        if config.has_option("general", "lurk") and config.get("general",
"lurk") == "yes":
-            email = voter.get(electionID, basedata, voterID)
-            lurks = {}
-            lurkpath = os.path.join(homedir, "issues", electionID,
"who.voted")
-            if os.path.isfile(lurkpath):
-                with open(lurkpath, "r") as f:
-                    lurks = json.loads(f.read())
+        if config.get("database", "dbsys") == "file":
+            issuepath = os.path.join(homedir, "issues", electionID,
issueID) + ".json"
+            if os.path.isfile(issuepath + ".votes"):
+                with open(issuepath + ".votes", "r") as f:
+                    votes = json.loads(f.read())
                      f.close()
-            lurks[email] = True
-            with open(lurkpath, "w") as f:
-                f.write(json.dumps(lurks))
+            votes[voterID] = vote
+            with open(issuepath + ".votes", "w") as f:
+                f.write(json.dumps(votes))
                  f.close()
+
+            # LURK on who voted :O :O :O
+            if config.has_option("general", "lurk") and
config.get("general", "lurk") == "yes":
+                email = voter.get(electionID, basedata, voterID)
+                lurks = {}
+                lurkpath = os.path.join(homedir, "issues", electionID,
"who.voted")
+                if os.path.isfile(lurkpath):
+                    with open(lurkpath, "r") as f:
+                        lurks = json.loads(f.read())
+                        f.close()
+                lurks[email] = True
+                with open(lurkpath, "w") as f:
+                    f.write(json.dumps(lurks))
+                    f.close()
          return votehash
      else:
          raise Exception("No such election")

  def getVotes(electionID, issueID):
-    issuepath = os.path.join(homedir, "issues", electionID, issueID) +
".json.votes"
-    if os.path.isfile(issuepath):
-        with open(issuepath, "r") as f:
-            votes = json.loads(f.read())
-            f.close()
-            return votes
-    else:
-        return {}
+    if config.get("database", "dbsys") == "file":
+        issuepath = os.path.join(homedir, "issues", electionID, issueID)
+ ".json.votes"
+        if os.path.isfile(issuepath):
+            with open(issuepath, "r") as f:
+                votes = json.loads(f.read())
+                f.close()
+                return votes
+    return {}

  def validType(issueType):
      for voteType in constants.VOTE_TYPES:
@@ -206,12 +219,14 @@ def tally(votes, issue):

  def deleteIssue(electionID, issueID):
      "Deletes an issue if it exists"
+
      if exists(electionID):
-        issuepath = os.path.join(homedir, "issues", electionID, issueID)
+ ".json"
-        if os.path.isfile(issuepath):
-            os.unlink(issuepath)
-        if os.path.isfile(issuepath + ".votes"):
-            os.unlink(issuepath + ".votes")
+        if config.get("database", "dbsys") == "file":
+            issuepath = os.path.join(homedir, "issues", electionID,
issueID) + ".json"
+            if os.path.isfile(issuepath):
+                os.unlink(issuepath)
+            if os.path.isfile(issuepath + ".votes"):
+                os.unlink(issuepath + ".votes")
          return True
      else:
          raise Exception("No such election")

Modified: steve/trunk/pysteve/lib/voter.py
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/voter.py?rev=1669142&r1=1669141&r2=1669142&view=diff

==============================================================================
--- steve/trunk/pysteve/lib/voter.py (original)
+++ steve/trunk/pysteve/lib/voter.py Wed Mar 25 16:17:30 2015
@@ -26,49 +26,54 @@ from smtplib import SMTPException


  def get(election, basedata, uid):
-    elpath = os.path.join(homedir, "issues", election)
-    with open(elpath + "/voters.json", "r") as f:
-        voters = json.loads(f.read())
-        f.close()
-        xhash = hashlib.sha512(basedata['hash'] + uid).hexdigest()
-        for voter in voters:
-            if voters[voter] == xhash:
-                return voter
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        with open(elpath + "/voters.json", "r") as f:
+            voters = json.loads(f.read())
+            f.close()
+            xhash = hashlib.sha512(basedata['hash'] + uid).hexdigest()
+            for voter in voters:
+                if voters[voter] == xhash:
+                    return voter
      return None

  def add(election, basedata, email):
      uid = hashlib.sha224("%s%s%s%s" % (email, basedata['hash'],
time.time(), random.randint(1,99999999))).hexdigest()
      xhash = hashlib.sha512(basedata['hash'] + uid).hexdigest()
-    elpath = os.path.join(homedir, "issues", election)
-    with open(elpath + "/voters.json", "r") as f:
-        voters = json.loads(f.read())
-        f.close()
-    voters[email] = xhash
-    with open(elpath + "/voters.json", "w") as f:
-        f.write(json.dumps(voters))
-        f.close()
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        with open(elpath + "/voters.json", "r") as f:
+            voters = json.loads(f.read())
+            f.close()
+        voters[email] = xhash
+        with open(elpath + "/voters.json", "w") as f:
+            f.write(json.dumps(voters))
+            f.close()
      return uid, xhash

  def remove(election, basedata, email):
-    elpath = os.path.join(homedir, "issues", election)
-    with open(elpath + "/voters.json", "r") as f:
-        voters = json.loads(f.read())
-        f.close()
-    if email in voters:
-        del voters[email]
-    with open(elpath + "/voters.json", "w") as f:
-        f.write(json.dumps(voters))
-        f.close()
+    if config.get("database", "dbsys") == "file":
+        elpath = os.path.join(homedir, "issues", election)
+        with open(elpath + "/voters.json", "r") as f:
+            voters = json.loads(f.read())
+            f.close()
+        if email in voters:
+            del voters[email]
+        with open(elpath + "/voters.json", "w") as f:
+            f.write(json.dumps(voters))
+            f.close()

  def hasVoted(election, issue, uid):
      issue = issue.strip(".json")
-    path = os.path.join(homedir, "issues", election, issue)
-    votes = {}
-    if os.path.isfile(path + ".json.votes"):
-        with open(path + ".json.votes", "r") as f:
-            votes = json.loads(f.read())
-            f.close()
-    return True if uid in votes else False
+    if config.get("database", "dbsys") == "file":
+        path = os.path.join(homedir, "issues", election, issue)
+        votes = {}
+        if os.path.isfile(path + ".json.votes"):
+            with open(path + ".json.votes", "r") as f:
+                votes = json.loads(f.read())
+                f.close()
+        return True if uid in votes else False
+    return False

  def email(rcpt, subject, message):
      sender = config.get("email", "sender")

Modified: steve/trunk/pysteve/steve.cfg
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/steve.cfg?rev=1669142&r1=1669141&r2=1669142&view=diff

==============================================================================
--- steve/trunk/pysteve/steve.cfg (original)
+++ steve/trunk/pysteve/steve.cfg Wed Mar 25 16:17:30 2015
@@ -4,6 +4,11 @@ homedir:            /home/voter
  rooturl:            http://demo.stv.website
  lurk:               yes

+
+[database]
+dbsys:              file
+
+
  [karma]
  admin:            5
  chairman:         4




Reply via email to