Browse Source

Log voting events

merge-requests/72/head
Deimos 6 years ago
parent
commit
694ca411dc
  1. 38
      tildes/alembic/versions/a2fda5d4e058_add_logeventtype_values_for_voting.py
  2. 4
      tildes/tildes/enums.py
  3. 4
      tildes/tildes/views/api/web/comment.py
  4. 4
      tildes/tildes/views/api/web/topic.py

38
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

4
tildes/tildes/enums.py

@ -93,6 +93,8 @@ class LogEventType(enum.Enum):
COMMENT_POST = enum.auto() COMMENT_POST = enum.auto()
COMMENT_REMOVE = enum.auto() COMMENT_REMOVE = enum.auto()
COMMENT_UNREMOVE = enum.auto() COMMENT_UNREMOVE = enum.auto()
COMMENT_UNVOTE = enum.auto()
COMMENT_VOTE = enum.auto()
TOPIC_LINK_EDIT = enum.auto() TOPIC_LINK_EDIT = enum.auto()
TOPIC_LOCK = enum.auto() TOPIC_LOCK = enum.auto()
@ -103,6 +105,8 @@ class LogEventType(enum.Enum):
TOPIC_TITLE_EDIT = enum.auto() TOPIC_TITLE_EDIT = enum.auto()
TOPIC_UNLOCK = enum.auto() TOPIC_UNLOCK = enum.auto()
TOPIC_UNREMOVE = enum.auto() TOPIC_UNREMOVE = enum.auto()
TOPIC_UNVOTE = enum.auto()
TOPIC_VOTE = enum.auto()
class ScraperType(enum.Enum): class ScraperType(enum.Enum):

4
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) _mark_comment_read_from_interaction(request, comment)
request.db_session.add(LogComment(LogEventType.COMMENT_VOTE, request, comment))
try: try:
# manually flush before attempting to commit, to avoid having all objects # manually flush before attempting to commit, to avoid having all objects
# detached from the session in case of an error # 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) _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 # manually commit the transaction so triggers will execute
request.tm.commit() request.tm.commit()

4
tildes/tildes/views/api/web/topic.py

@ -92,6 +92,8 @@ def put_topic_vote(request: Request) -> Response:
new_vote = TopicVote(request.user, topic) new_vote = TopicVote(request.user, topic)
request.db_session.add(new_vote) request.db_session.add(new_vote)
request.db_session.add(LogTopic(LogEventType.TOPIC_VOTE, request, topic))
try: try:
# manually flush before attempting to commit, to avoid having all objects # manually flush before attempting to commit, to avoid having all objects
# detached from the session in case of an error # 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 TopicVote.topic == topic, TopicVote.user == request.user
).delete(synchronize_session=False) ).delete(synchronize_session=False)
request.db_session.add(LogTopic(LogEventType.TOPIC_UNVOTE, request, topic))
# manually commit the transaction so triggers will execute # manually commit the transaction so triggers will execute
request.tm.commit() request.tm.commit()

Loading…
Cancel
Save