aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorFulgen301 <tokmajigeorge@gmail.com>2019-07-26 13:50:12 +0200
committerFulgen301 <tokmajigeorge@gmail.com>2019-09-08 21:48:46 +0200
commit38e0af00dc06f0d4c5d9672b2c786ae7bf9be860 (patch)
tree05e72985557e79ec4089ed1d364f0e3cc9acfbc8 /routes
parentd123729cc3e33d00f8b46c315cc22ca98fe3f068 (diff)
downloadparry-38e0af00dc06f0d4c5d9672b2c786ae7bf9be860.tar.gz
parry-38e0af00dc06f0d4c5d9672b2c786ae7bf9be860.zip
Add comment voting
Diffstat (limited to 'routes')
-rw-r--r--routes/uploads.py68
1 files changed, 58 insertions, 10 deletions
diff --git a/routes/uploads.py b/routes/uploads.py
index d75c4f2..615ae2a 100644
--- a/routes/uploads.py
+++ b/routes/uploads.py
@@ -19,20 +19,26 @@ from bottle import delete, put
from itertools import chain
import string
-_vote_dummy = {
- "voting" : {
- "sum" : 0,
- "count" : 0,
- "votes" : None
- }
- }
-
def _add_upload(entry : Upload, session : DBSession):
e = entry.json()
- #e.update(_vote_dummy)
+ e["comments"] = []
+ for comment in session.query(Comment).filter_by(upload=entry):
+ c = comment.json()
+ votes = session.query(CommentVote).filter_by(comment=comment)
+ c["voting"] = {
+ "sum" : 0,
+ "count" : 0,
+ "votes" : []
+ }
+
+ for vote in votes:
+ c["voting"]["sum"] += vote.impact
+ c["voting"]["count"] += 1
+ c["voting"]["votes"].append(vote.json())
+
+ e["comments"].append(c)
- e["comments"] = [{**(comment.json()), **_vote_dummy} for comment in session.query(Comment).filter_by(upload=entry)]
e["files"] = [file.json() for file in session.query(File).filter_by(upload=entry)]
e["dependencies"] = [] #TODO
@@ -206,6 +212,48 @@ def delete_comments(id, comment_id):
session.commit()
return HTTPResponse(status=204)
+@get("/api/uploads/<id>/comments/<comment_id>/vote")
+@jwt_auth_required
+def get_comment_vote(id, comment_id):
+ session = DBSession()
+ upload = session.query(Upload).get(id)
+ if upload is None:
+ raise HTTPResponse("Invalid upload id", 404)
+
+ try:
+ comment = session.query(Comment).filter_by(id=comment_id, author=get_user(session), upload=session.query(Upload).get(id)).one()
+ except db.orm.exc.NoResultFound:
+ raise HTTPResponse("Invalid comment id", status=404)
+
+ try:
+ return session.query(CommentVote).filter_by(comment=comment, author=get_user(session)).one().json()
+ except db.orm.exc.NoResultFound:
+ raise HTTPResponse(status=404)
+
+@post("/api/uploads/<id>/comments/<comment_id>/vote")
+@jwt_auth_required
+def post_comment_vote(id, comment_id):
+ session = DBSession()
+ upload = session.query(Upload).get(id)
+ if upload is None:
+ raise HTTPResponse("Invalid upload id", 404)
+
+ try:
+ comment = session.query(Comment).filter_by(id=comment_id, author=get_user(session), upload=session.query(Upload).get(id)).one()
+ except db.orm.exc.NoResultFound:
+ raise HTTPResponse("Invalid comment id", status=404)
+
+ author = get_user(session)
+ try:
+ vote = session.query(CommentVote).filter_by(comment=comment, author=author).one()
+ except db.orm.exc.NoResultFound:
+ vote = CommentVote(author=author, comment=comment)
+
+ vote.impact = request_data()["impact"]
+ session.add(vote)
+ session.commit()
+ return vote.json()
+
@get("/api/uploads/<id>/vote")
@jwt_auth_required
def get_vote(id):