diff options
| -rw-r--r-- | database.py | 18 | ||||
| -rw-r--r-- | routes/uploads.py | 68 |
2 files changed, 76 insertions, 10 deletions
diff --git a/database.py b/database.py index 2559b0f..746078e 100644 --- a/database.py +++ b/database.py @@ -142,6 +142,24 @@ class Vote(Base): "impact" : self.impact } +class CommentVote(Base): + __tablename__ = "commentvotes" + id = db.Column(Types.ObjectId, primary_key=True, default=ObjectId) + author_id = db.Column(Types.ObjectId, db.ForeignKey("users.id")) + comment_id = db.Column(Types.ObjectId, db.ForeignKey("comments.id")) + impact = db.Column(db.Integer, nullable=False) + + author = relationship("User") + comment = relationship("Comment") + + def json(self): + return { + "id" : self.id, + "author" : self.author.json(), + "comment" : self.comment.id, + "impact" : self.impact + } + #class Dependency(Base): #__tablename__ = "dependencies" #id = db.Column(db.Integer, primary_key=True, default=ObjectId) 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): |
