From 694ca411dcba986c15c795569cd9f2a251fd8372 Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 10 Jun 2019 12:05:57 -0600 Subject: [PATCH] Log voting events --- ...e058_add_logeventtype_values_for_voting.py | 38 +++++++++++++++++++ tildes/tildes/enums.py | 4 ++ tildes/tildes/views/api/web/comment.py | 4 ++ tildes/tildes/views/api/web/topic.py | 4 ++ 4 files changed, 50 insertions(+) create mode 100644 tildes/alembic/versions/a2fda5d4e058_add_logeventtype_values_for_voting.py diff --git a/tildes/alembic/versions/a2fda5d4e058_add_logeventtype_values_for_voting.py b/tildes/alembic/versions/a2fda5d4e058_add_logeventtype_values_for_voting.py new file mode 100644 index 0000000..1d8646f --- /dev/null +++ b/tildes/alembic/versions/a2fda5d4e058_add_logeventtype_values_for_voting.py @@ -0,0 +1,38 @@ +"""Add logeventtype values for voting + +Revision ID: a2fda5d4e058 +Revises: e9bbc2929d9c +Create Date: 2019-06-10 17:56:11.892793 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "a2fda5d4e058" +down_revision = "e9bbc2929d9c" +branch_labels = None +depends_on = None + + +def upgrade(): + # ALTER TYPE doesn't work from inside a transaction, disable it + connection = None + if not op.get_context().as_sql: + connection = op.get_bind() + connection.execution_options(isolation_level="AUTOCOMMIT") + + op.execute("ALTER TYPE logeventtype ADD VALUE IF NOT EXISTS 'COMMENT_VOTE'") + op.execute("ALTER TYPE logeventtype ADD VALUE IF NOT EXISTS 'COMMENT_UNVOTE'") + op.execute("ALTER TYPE logeventtype ADD VALUE IF NOT EXISTS 'TOPIC_VOTE'") + op.execute("ALTER TYPE logeventtype ADD VALUE IF NOT EXISTS 'TOPIC_UNVOTE'") + + # re-activate the transaction for any future migrations + if connection is not None: + connection.execution_options(isolation_level="READ_COMMITTED") + + +def downgrade(): + # can't remove from enums, do nothing + pass diff --git a/tildes/tildes/enums.py b/tildes/tildes/enums.py index c1150b1..fda19bd 100644 --- a/tildes/tildes/enums.py +++ b/tildes/tildes/enums.py @@ -93,6 +93,8 @@ class LogEventType(enum.Enum): COMMENT_POST = enum.auto() COMMENT_REMOVE = enum.auto() COMMENT_UNREMOVE = enum.auto() + COMMENT_UNVOTE = enum.auto() + COMMENT_VOTE = enum.auto() TOPIC_LINK_EDIT = enum.auto() TOPIC_LOCK = enum.auto() @@ -103,6 +105,8 @@ class LogEventType(enum.Enum): TOPIC_TITLE_EDIT = enum.auto() TOPIC_UNLOCK = enum.auto() TOPIC_UNREMOVE = enum.auto() + TOPIC_UNVOTE = enum.auto() + TOPIC_VOTE = enum.auto() class ScraperType(enum.Enum): diff --git a/tildes/tildes/views/api/web/comment.py b/tildes/tildes/views/api/web/comment.py index 45e1020..7db91fd 100644 --- a/tildes/tildes/views/api/web/comment.py +++ b/tildes/tildes/views/api/web/comment.py @@ -226,6 +226,8 @@ def put_vote_comment(request: Request) -> dict: _mark_comment_read_from_interaction(request, comment) + request.db_session.add(LogComment(LogEventType.COMMENT_VOTE, request, comment)) + try: # manually flush before attempting to commit, to avoid having all objects # detached from the session in case of an error @@ -262,6 +264,8 @@ def delete_vote_comment(request: Request) -> dict: _mark_comment_read_from_interaction(request, comment) + request.db_session.add(LogComment(LogEventType.COMMENT_UNVOTE, request, comment)) + # manually commit the transaction so triggers will execute request.tm.commit() diff --git a/tildes/tildes/views/api/web/topic.py b/tildes/tildes/views/api/web/topic.py index 98bcd88..1880424 100644 --- a/tildes/tildes/views/api/web/topic.py +++ b/tildes/tildes/views/api/web/topic.py @@ -92,6 +92,8 @@ def put_topic_vote(request: Request) -> Response: new_vote = TopicVote(request.user, topic) request.db_session.add(new_vote) + request.db_session.add(LogTopic(LogEventType.TOPIC_VOTE, request, topic)) + try: # manually flush before attempting to commit, to avoid having all objects # detached from the session in case of an error @@ -126,6 +128,8 @@ def delete_topic_vote(request: Request) -> Response: TopicVote.topic == topic, TopicVote.user == request.user ).delete(synchronize_session=False) + request.db_session.add(LogTopic(LogEventType.TOPIC_UNVOTE, request, topic)) + # manually commit the transaction so triggers will execute request.tm.commit()