Browse Source

Add admin tool for removing comments

merge-requests/34/head
Chad Birch 6 years ago
parent
commit
51e8949b84
  1. 36
      tildes/alembic/versions/3fbddcba0e3b_add_comment_remove_and_comment_unremove_.py
  2. 2
      tildes/tildes/enums.py
  3. 3
      tildes/tildes/models/comment/comment.py
  4. 3
      tildes/tildes/routes.py
  5. 24
      tildes/tildes/templates/macros/comments.jinja2
  6. 24
      tildes/tildes/views/api/web/comment.py

36
tildes/alembic/versions/3fbddcba0e3b_add_comment_remove_and_comment_unremove_.py

@ -0,0 +1,36 @@
"""Add COMMENT_REMOVE and COMMENT_UNREMOVE to logeventtype
Revision ID: 3fbddcba0e3b
Revises: 6a635773de8f
Create Date: 2018-08-26 04:34:51.741972
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "3fbddcba0e3b"
down_revision = "6a635773de8f"
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_REMOVE'")
op.execute("ALTER TYPE logeventtype ADD VALUE IF NOT EXISTS 'COMMENT_UNREMOVE'")
# re-activate the transaction for any future migrations
if connection is not None:
connection.execution_options(isolation_level="READ_COMMITTED")
def downgrade():
# no way to remove enum values, just do nothing
pass

2
tildes/tildes/enums.py

@ -49,6 +49,8 @@ class LogEventType(enum.Enum):
USER_REGISTER = enum.auto() USER_REGISTER = enum.auto()
COMMENT_POST = enum.auto() COMMENT_POST = enum.auto()
COMMENT_REMOVE = enum.auto()
COMMENT_UNREMOVE = enum.auto()
TOPIC_LOCK = enum.auto() TOPIC_LOCK = enum.auto()
TOPIC_MOVE = enum.auto() TOPIC_MOVE = enum.auto()

3
tildes/tildes/models/comment/comment.py

@ -185,6 +185,9 @@ class Comment(DatabaseModel):
# - logged-in users can mark comments read # - logged-in users can mark comments read
acl.append((Allow, Authenticated, "mark_read")) acl.append((Allow, Authenticated, "mark_read"))
# tools that require specifically granted permissions
acl.append((Allow, "admin", "remove"))
acl.append(DENY_ALL) acl.append(DENY_ALL)
return acl return acl

3
tildes/tildes/routes.py

@ -121,6 +121,9 @@ def add_intercooler_routes(config: Configurator) -> None:
add_ic_route("topic_tags", "/topics/{topic_id36}/tags", factory=topic_by_id36) add_ic_route("topic_tags", "/topics/{topic_id36}/tags", factory=topic_by_id36)
add_ic_route("comment", "/comments/{comment_id36}", factory=comment_by_id36) add_ic_route("comment", "/comments/{comment_id36}", factory=comment_by_id36)
add_ic_route(
"comment_remove", "/comments/{comment_id36}/remove", factory=comment_by_id36
)
add_ic_route( add_ic_route(
"comment_replies", "/comments/{comment_id36}/replies", factory=comment_by_id36 "comment_replies", "/comments/{comment_id36}/replies", factory=comment_by_id36
) )

24
tildes/tildes/templates/macros/comments.jinja2

@ -160,6 +160,30 @@
>Delete</a></li> >Delete</a></li>
{% endif %} {% endif %}
{% if request.has_permission("remove", comment) %}
<li>
{% if not comment.is_removed %}
<a class="post-button"
data-ic-put-to="{{ request.route_url(
'ic_comment_remove',
comment_id36=comment.comment_id36,
) }}"
data-ic-replace-target="true"
data-ic-confirm="Remove this comment?"
>Remove</a>
{% else %}
<a class="post-button"
data-ic-delete-from="{{ request.route_url(
'ic_comment_remove',
comment_id36=comment.comment_id36,
) }}"
data-ic-replace-target="true"
data-ic-confirm="Un-remove this comment?"
>Un-remove</a>
{% endif %}
</li>
{% endif %}
{% if request.has_permission('reply', comment) %} {% if request.has_permission('reply', comment) %}
<li><a class="post-button" name="reply" data-js-comment-reply-button>Reply</a></li> <li><a class="post-button" name="reply" data-js-comment-reply-button>Reply</a></li>
{% endif %} {% endif %}

24
tildes/tildes/views/api/web/comment.py

@ -349,3 +349,27 @@ def put_mark_comments_read(request: Request, mark_all_previous: bool) -> Respons
_increment_topic_comments_seen(request, notification.comment) _increment_topic_comments_seen(request, notification.comment)
return IC_NOOP return IC_NOOP
@ic_view_config(route_name="comment_remove", request_method="PUT", permission="remove")
def put_comment_remove(request: Request) -> Response:
"""Remove a comment with Intercooler."""
comment = request.context
comment.is_removed = True
request.db_session.add(LogComment(LogEventType.COMMENT_REMOVE, request, comment))
return Response("Removed")
@ic_view_config(
route_name="comment_remove", request_method="DELETE", permission="remove"
)
def delete_comment_remove(request: Request) -> Response:
"""Un-remove a comment with Intercooler."""
comment = request.context
comment.is_removed = False
request.db_session.add(LogComment(LogEventType.COMMENT_UNREMOVE, request, comment))
return Response("Un-removed")
Loading…
Cancel
Save