Browse Source

Add tool for editing topic links

merge-requests/64/head
Deimos 6 years ago
parent
commit
6b2afd6889
  1. 35
      tildes/alembic/versions/24014adda7c3_add_topic_link_edit_to_logeventtype.py
  2. 1
      tildes/tildes/enums.py
  3. 4
      tildes/tildes/models/log/log.py
  4. 4
      tildes/tildes/models/topic/topic.py
  5. 1
      tildes/tildes/routes.py
  6. 19
      tildes/tildes/templates/intercooler/topic_link_edit.jinja2
  7. 13
      tildes/tildes/templates/topic.jinja2
  8. 39
      tildes/tildes/views/api/web/topic.py
  9. 1
      tildes/tildes/views/topic.py

35
tildes/alembic/versions/24014adda7c3_add_topic_link_edit_to_logeventtype.py

@ -0,0 +1,35 @@
"""Add TOPIC_LINK_EDIT to logeventtype
Revision ID: 24014adda7c3
Revises: 7ac1aad64144
Create Date: 2019-03-14 21:57:27.057187
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "24014adda7c3"
down_revision = "7ac1aad64144"
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 'TOPIC_LINK_EDIT'")
# 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

1
tildes/tildes/enums.py

@ -74,6 +74,7 @@ class LogEventType(enum.Enum):
COMMENT_REMOVE = enum.auto() COMMENT_REMOVE = enum.auto()
COMMENT_UNREMOVE = enum.auto() COMMENT_UNREMOVE = enum.auto()
TOPIC_LINK_EDIT = enum.auto()
TOPIC_LOCK = enum.auto() TOPIC_LOCK = enum.auto()
TOPIC_MOVE = enum.auto() TOPIC_MOVE = enum.auto()
TOPIC_POST = enum.auto() TOPIC_POST = enum.auto()

4
tildes/tildes/models/log/log.py

@ -164,6 +164,10 @@ class LogTopic(DatabaseModel, BaseLog):
old_title = self.info["old"] old_title = self.info["old"]
new_title = self.info["new"] new_title = self.info["new"]
return f'changed title from "{old_title}" to "{new_title}"' return f'changed title from "{old_title}" to "{new_title}"'
elif self.event_type == LogEventType.TOPIC_LINK_EDIT:
old_link = self.info["old"]
new_link = self.info["new"]
return f"changed link from {old_link} to {new_link}"
return f"performed action {self.event_type.name}" return f"performed action {self.event_type.name}"

4
tildes/tildes/models/topic/topic.py

@ -304,6 +304,10 @@ class Topic(DatabaseModel):
acl.append((Allow, "admin", "edit_title")) acl.append((Allow, "admin", "edit_title"))
acl.append((Allow, "topic.edit_title", "edit_title")) acl.append((Allow, "topic.edit_title", "edit_title"))
if self.is_link_type:
acl.append((Allow, "admin", "edit_link"))
acl.append((Allow, "topic.edit_link", "edit_link"))
# bookmark: # bookmark:
# - logged-in users can bookmark topics # - logged-in users can bookmark topics
acl.append((Allow, Authenticated, "bookmark")) acl.append((Allow, Authenticated, "bookmark"))

1
tildes/tildes/routes.py

@ -115,6 +115,7 @@ def add_intercooler_routes(config: Configurator) -> None:
with config.route_prefix_context("/topics/{topic_id36}"): with config.route_prefix_context("/topics/{topic_id36}"):
add_ic_route("topic_comments", "/comments", factory=topic_by_id36) add_ic_route("topic_comments", "/comments", factory=topic_by_id36)
add_ic_route("topic_group", "/group", factory=topic_by_id36) add_ic_route("topic_group", "/group", factory=topic_by_id36)
add_ic_route("topic_link", "/link", factory=topic_by_id36)
add_ic_route("topic_lock", "/lock", factory=topic_by_id36) add_ic_route("topic_lock", "/lock", factory=topic_by_id36)
add_ic_route("topic_remove", "/remove", factory=topic_by_id36) add_ic_route("topic_remove", "/remove", factory=topic_by_id36)
add_ic_route("topic_title", "/title", factory=topic_by_id36) add_ic_route("topic_title", "/title", factory=topic_by_id36)

19
tildes/tildes/templates/intercooler/topic_link_edit.jinja2

@ -0,0 +1,19 @@
{# Copyright (c) 2019 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
<form method="post" name="topic-link-edit"
autocomplete="off"
data-ic-patch-to="{{ request.route_url(
'ic_topic',
topic_id36=topic.topic_id36,
) }}"
data-ic-target=".topic-full-link:first"
data-js-remove-on-success
>
<label class="form-label" for="link">Link:</label>
<input class="form-input" type="URL" name="link" id="link" value="{{ topic.link }}" data-js-auto-focus>
<div class="form-buttons">
<button class="btn btn-primary" type="submit">Update link</button>
<button type="button" class="btn btn-link" data-js-cancel-button>Cancel</button>
</div>
</form>

13
tildes/tildes/templates/topic.jinja2

@ -66,7 +66,7 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if request.has_any_permission(('edit', 'delete', 'tag', 'lock', 'move', 'edit_title', 'remove', 'bookmark'), topic) %}
{% if request.has_any_permission(('edit', 'delete', 'tag', 'lock', 'move', 'edit_link', 'edit_title', 'remove', 'bookmark'), topic) %}
<menu class="btn-post"> <menu class="btn-post">
{% if request.has_permission('edit', topic) %} {% if request.has_permission('edit', topic) %}
<li><button class="btn-post-action" name="edit" <li><button class="btn-post-action" name="edit"
@ -122,6 +122,17 @@
>Edit title</button> >Edit title</button>
{% endif %} {% endif %}
{% if request.has_permission('edit_link', topic) %}
<li><button class="btn-post-action" name="edit-link"
data-ic-get-from="{{ request.route_url(
'ic_topic_link',
topic_id36=topic.topic_id36,
) }}"
data-ic-swap-style="replace"
data-ic-target=".topic-full .btn-post:first + .btn-post-settings"
>Edit link</button>
{% endif %}
{% if request.has_permission('lock', topic) %} {% if request.has_permission('lock', topic) %}
<li> <li>
{% if not topic.is_locked %} {% if not topic.is_locked %}

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

@ -316,6 +316,45 @@ def patch_topic_title(request: Request, title: str) -> dict:
return Response(topic.title) return Response(topic.title)
@ic_view_config(
route_name="topic_link",
request_method="GET",
renderer="topic_link_edit.jinja2",
permission="edit_link",
)
def get_topic_link(request: Request) -> dict:
"""Get the form for editing a topic's link with Intercooler."""
return {"topic": request.context}
@ic_view_config(
route_name="topic",
request_param="ic-trigger-name=topic-link-edit",
request_method="PATCH",
permission="edit_link",
)
@use_kwargs(TopicSchema(only=("link",)))
def patch_topic_link(request: Request, link: str) -> dict:
"""Edit a topic's link with Intercooler."""
topic = request.context
if link == topic.link:
return IC_NOOP
request.db_session.add(
LogTopic(
LogEventType.TOPIC_LINK_EDIT,
request,
topic,
info={"old": topic.link, "new": link},
)
)
topic.link = link
return Response(f'<a href="{topic.link}">{topic.link}</a>')
@ic_view_config( @ic_view_config(
route_name="topic_bookmark", request_method="PUT", permission="bookmark" route_name="topic_bookmark", request_method="PUT", permission="bookmark"
) )

1
tildes/tildes/views/topic.py

@ -269,6 +269,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict:
# check if there are any items in the log to show # check if there are any items in the log to show
visible_events = ( visible_events = (
LogEventType.TOPIC_LINK_EDIT,
LogEventType.TOPIC_LOCK, LogEventType.TOPIC_LOCK,
LogEventType.TOPIC_MOVE, LogEventType.TOPIC_MOVE,
LogEventType.TOPIC_REMOVE, LogEventType.TOPIC_REMOVE,

Loading…
Cancel
Save