Author: humbedooh
Date: Fri Mar 27 17:40:25 2015
New Revision: 1669630
URL: http://svn.apache.org/r1669630
Log:
- fix some ES issues
- pull out add/edit/delete candidate REST, not really used
- Add updateElection and updateIssue to abstract rest_admin completely
Modified:
steve/trunk/pysteve/lib/election.py
steve/trunk/pysteve/www/cgi-bin/rest_admin.py
Modified: steve/trunk/pysteve/lib/election.py
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/lib/election.py?rev=1669630&r1=1669629&r2=1669630&view=diff
==============================================================================
--- steve/trunk/pysteve/lib/election.py (original)
+++ steve/trunk/pysteve/lib/election.py Fri Mar 27 17:40:25 2015
@@ -63,7 +63,7 @@ def exists(election, *issue):
doc = "elections"
if issue and issue[0]:
doc = "issues"
- s = "id:%s" % issue[0]
+ s = "election:%s AND id:%s" % (election, issue[0])
res = es.search(index="steve", doc_type=doc, q = s, size = 1)
if len(res['hits']['hits']) > 0:
return True
@@ -130,7 +130,7 @@ def getIssue(electionID, issueID):
f.close()
issuedata = json.loads(data)
elif dbtype == "elasticsearch":
- res = es.search(index="steve", doc_type="issues", q = "id:%s" %
issueID, size = 1)
+ res = es.search(index="steve", doc_type="issues", q = "election:%s AND
id:%s" % (electionID, issueID), size = 1)
results = len(res['hits']['hits'])
if results > 0:
issuedata = res['hits']['hits'][0]['_source']
@@ -227,6 +227,26 @@ def createElection(eid, title, owner, mo
}
);
+def updateElection(electionID, basedata):
+ dbtype = config.get("database", "dbsys")
+ if dbtype == "file":
+ elpath = os.path.join(homedir, "issues", electionID)
+ with open(elpath + "/basedata.json", "w") as f:
+ f.write(json.dumps(basedata))
+ f.close()
+ elif dbtype == "elasticsearh":
+ es.index(index = "steve", doc_type = "elections", id=electionID, body
= basedata)
+
+def updateIssue(electionID, issueID, issueData):
+ dbtype = config.get("database", "dbsys")
+ if dbtype == "file":
+ issuepath = os.path.join(homedir, "issues", electionID, issueID) +
".json"
+ with open(issuepath, "w") as f:
+ f.write(json.dumps(issueData))
+ f.close()
+ elif dbtype == "elasticsearch":
+ es.index(index = "steve", doc_type = "issues",
id=hashlib.sha224(electionID + "/" + issueID).hexdigest(), body = issueData)
+
def listIssues(election):
"List all issues in an election"
@@ -362,7 +382,7 @@ def deleteIssue(electionID, issueID):
if os.path.isfile(issuepath + ".votes"):
os.unlink(issuepath + ".votes")
elif dbtype == "elasticsearch":
- es.delete(index="steve", doc_type="votes", id=votehash);
+ es.delete(index="steve", doc_type="votes",
id=hashlib.sha224(electionID + "/" + issueID).hexdigest());
return True
else:
raise Exception("No such election")
Modified: steve/trunk/pysteve/www/cgi-bin/rest_admin.py
URL:
http://svn.apache.org/viewvc/steve/trunk/pysteve/www/cgi-bin/rest_admin.py?rev=1669630&r1=1669629&r2=1669630&view=diff
==============================================================================
--- steve/trunk/pysteve/www/cgi-bin/rest_admin.py (original)
+++ steve/trunk/pysteve/www/cgi-bin/rest_admin.py Fri Mar 27 17:40:25 2015
@@ -206,38 +206,29 @@ else:
if (issue and karma >= 4) or (karma >= 5 and electionID):
if electionID:
if not issue:
- elpath = os.path.join(homedir, "issues", electionID)
- if not os.path.isdir(elpath) or not
os.path.isfile(elpath+"/basedata.json"):
- response.respond(404, {'message': 'No such issue'})
+ if not election.exists(electionID,):
+ response.respond(404, {'message': 'No such
election'})
else:
try:
js = {}
- with open(elpath + "/basedata.json", "r") as f:
- js = json.loads(f.read())
- f.close()
+ basedata = election.getBasedata(electionID)
fields =
['title','owner','monitors','starts','ends']
for field in fields:
val = form.getvalue(field)
if val:
if field == "monitors":
val = [x.strip() for x in
val.split(",")]
- js[field] = val
- with open(elpath + "/basedata.json", "w") as f:
- f.write(json.dumps(js))
- f.close()
+ basedata[field] = val
+ election.updateElection(electionID, basedata)
response.respond(200, {'message': "Changed
saved"})
except Exception as err:
response.respond(500, {'message': "Could not
edit election: %s" % err})
else:
- issuepath = os.path.join(homedir, "issues",
electionID, issue)
- if not os.path.isfile(issuepath + ".json"):
+ if not election.exists(electionID, issue):
response.respond(404, {'message': 'No such issue'})
else:
try:
- js = {}
- with open(issuepath + ".json", "r") as f:
- js = json.loads(f.read())
- f.close()
+ issuedata = election.getIssue(electionID,
issue)
fields =
['title','description','type','statements','candidates','seconds','nominatedby']
statements = []
for field in fields:
@@ -265,14 +256,12 @@ else:
val = [x.strip() for x in
val.split("\n")]
# HACK: If field parsing is
outsourced, let's do that instead (primarily for COP)
- voteType = election.getVoteType(js)
+ voteType =
election.getVoteType(issuedata)
if 'parsers' in voteType and field in
voteType['parsers']:
val =
voteType['parsers'][field](form.getvalue(field))
- js[field] = val
- with open(issuepath + ".json", "w") as f:
- f.write(json.dumps(js))
- f.close()
+ issuedata[field] = val
+ election.updateIssue(electionID, issue,
issuedata)
response.respond(200, {'message': "Changed
saved"})
except Exception as err:
response.respond(500, {'message': "Could not
edit issue: %s" % err})
@@ -281,107 +270,6 @@ else:
else:
response.respond(403, {'message': 'You do not have enough
karma for this'})
- # Edit/add a statement
- elif action == "statement":
- issue = l[2] if len(l) > 2 else None
- if (issue and karma >= 4):
- issuepath = os.path.join(homedir, "issues", electionID, issue)
- if not os.path.isfile(issuepath + ".json"):
- response.respond(404, {'message': 'No such issue'})
- else:
- try:
- js = {}
- with open(issuepath + ".json", "r") as f:
- js = json.loads(f.read())
- f.close()
-
- cand = form.getvalue('candidate')
- stat = form.getvalue('statement')
- found = False
- for entry in js['candidates']:
- if entry['name'] == cand:
- found = True
- entry['statement'] = stat
- break
- if not found:
- raise Exception("No such candidate: " + cand)
- with open(issuepath + ".json", "w") as f:
- f.write(json.dumps(js))
- f.close()
- response.respond(200, {'message': "Changed saved"})
- except Exception as err:
- response.respond(500, {'message': "Could not edit
issue: %s" % err})
- else:
- response.respond(403, {'message': 'You do not have enough
karma for this'})
-
- # Edit/add a statement
- elif action == "addcandidate":
- issue = l[2] if len(l) > 2 else None
- if (issue and karma >= 4):
- issuepath = os.path.join(homedir, "issues", electionID, issue)
- if not os.path.isfile(issuepath + ".json"):
- response.respond(404, {'message': 'No such issue'})
- else:
- try:
- js = {}
- with open(issuepath + ".json", "r") as f:
- js = json.loads(f.read())
- f.close()
-
- cand = form.getvalue('candidate')
- stat = form.getvalue('statement')
- found = False
- for entry in js['candidates']:
- if entry['name'] == cand:
- found = True
- break
- if found:
- raise Exception("Candidate already exists: " +
cand)
- else:
- js['candidates'].append( {
- 'name': cand,
- 'statement': stat
- })
- with open(issuepath + ".json", "w") as f:
- f.write(json.dumps(js))
- f.close()
- response.respond(200, {'message': "Changed saved"})
- except Exception as err:
- response.respond(500, {'message': "Could not edit
issue: %s" % err})
- else:
- response.respond(403, {'message': 'You do not have enough
karma for this'})
- elif action == "delcandidate":
- issue = l[2] if len(l) > 2 else None
- if (issue and karma >= 4):
- issuepath = os.path.join(homedir, "issues", electionID, issue)
- if not os.path.isfile(issuepath + ".json"):
- response.respond(404, {'message': 'No such issue'})
- else:
- try:
- js = {}
- with open(issuepath + ".json", "r") as f:
- js = json.loads(f.read())
- f.close()
-
- cand = form.getvalue('candidate')
- found = False
- i = 0
- for entry in js['candidates']:
- if entry['name'] == cand:
- js['candidates'].pop(i)
- found = True
- break
- i += 1
- if not found:
- raise Exception("Candidate does nost exist: " +
cand)
- with open(issuepath + ".json", "w") as f:
- f.write(json.dumps(js))
- f.close()
- response.respond(200, {'message': "Changed saved"})
- except Exception as err:
- response.respond(500, {'message': "Could not edit
issue: %s" % err})
- else:
- response.respond(403, {'message': 'You do not have enough
karma for this'})
elif action == "view" and karma >= 3:
# View a list of issues for an election
if electionID: