From b4402d32e9887a5af20bdcbd73e084e07f7f4603 Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Sat, 25 Aug 2018 22:12:40 -0600 Subject: [PATCH] Log comment post events --- ...73de8f_add_comment_post_to_logeventtype.py | 35 +++++++++++++++++++ tildes/tildes/enums.py | 2 ++ tildes/tildes/views/api/web/comment.py | 7 +++- tildes/tildes/views/topic.py | 8 ++++- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tildes/alembic/versions/6a635773de8f_add_comment_post_to_logeventtype.py diff --git a/tildes/alembic/versions/6a635773de8f_add_comment_post_to_logeventtype.py b/tildes/alembic/versions/6a635773de8f_add_comment_post_to_logeventtype.py new file mode 100644 index 0000000..5218b69 --- /dev/null +++ b/tildes/alembic/versions/6a635773de8f_add_comment_post_to_logeventtype.py @@ -0,0 +1,35 @@ +"""Add COMMENT_POST to logeventtype + +Revision ID: 6a635773de8f +Revises: b3be50625592 +Create Date: 2018-08-26 01:56:13.511360 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "6a635773de8f" +down_revision = "b3be50625592" +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_POST'") + + # 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 a7bfc92..c69ece6 100644 --- a/tildes/tildes/enums.py +++ b/tildes/tildes/enums.py @@ -48,6 +48,8 @@ class LogEventType(enum.Enum): USER_LOG_OUT = enum.auto() USER_REGISTER = enum.auto() + COMMENT_POST = enum.auto() + TOPIC_LOCK = enum.auto() TOPIC_MOVE = enum.auto() TOPIC_POST = enum.auto() diff --git a/tildes/tildes/views/api/web/comment.py b/tildes/tildes/views/api/web/comment.py index e17fc41..f46f2d2 100644 --- a/tildes/tildes/views/api/web/comment.py +++ b/tildes/tildes/views/api/web/comment.py @@ -10,9 +10,10 @@ from sqlalchemy.orm.exc import FlushError from webargs.pyramidparser import use_kwargs from zope.sqlalchemy import mark_changed -from tildes.enums import CommentNotificationType, CommentTagOption +from tildes.enums import CommentNotificationType, CommentTagOption, LogEventType from tildes.lib.datetime import utc_now from tildes.models.comment import Comment, CommentNotification, CommentTag, CommentVote +from tildes.models.log import LogComment from tildes.models.topic import TopicVisit from tildes.schemas.comment import CommentSchema, CommentTagSchema from tildes.views import IC_NOOP @@ -65,6 +66,8 @@ def post_toplevel_comment(request: Request, markdown: str) -> dict: new_comment = Comment(topic=topic, author=request.user, markdown=markdown) request.db_session.add(new_comment) + request.db_session.add(LogComment(LogEventType.COMMENT_POST, request, new_comment)) + if topic.user != request.user and not topic.is_deleted: notification = CommentNotification( topic.user, new_comment, CommentNotificationType.TOPIC_REPLY @@ -103,6 +106,8 @@ def post_comment_reply(request: Request, markdown: str) -> dict: ) request.db_session.add(new_comment) + request.db_session.add(LogComment(LogEventType.COMMENT_POST, request, new_comment)) + if parent_comment.user != request.user: notification = CommentNotification( parent_comment.user, new_comment, CommentNotificationType.COMMENT_REPLY diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index 2728b12..7f2e53f 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -23,7 +23,7 @@ from tildes.enums import ( from tildes.lib.datetime import SimpleHoursPeriod from tildes.models.comment import Comment, CommentNotification, CommentTree from tildes.models.group import Group -from tildes.models.log import LogTopic +from tildes.models.log import LogComment, LogTopic from tildes.models.topic import Topic, TopicVisit from tildes.models.user import UserGroupSettings from tildes.schemas.comment import CommentSchema @@ -55,6 +55,10 @@ def post_group_topics( topic=new_topic, author=request.user, markdown=markdown ) request.db_session.add(new_comment) + + request.db_session.add( + LogComment(LogEventType.COMMENT_POST, request, new_comment) + ) else: new_topic = Topic.create_text_topic( group=request.context, author=request.user, title=title, markdown=markdown @@ -295,6 +299,8 @@ def post_comment_on_topic(request: Request, markdown: str) -> HTTPFound: new_comment = Comment(topic=topic, author=request.user, markdown=markdown) request.db_session.add(new_comment) + request.db_session.add(LogComment(LogEventType.COMMENT_POST, request, new_comment)) + if topic.user != request.user and not topic.is_deleted: notification = CommentNotification( topic.user, new_comment, CommentNotificationType.TOPIC_REPLY