From 246364de1d1ab8f1388d6ec418c907ca36febd08 Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 13 May 2019 17:52:40 -0600 Subject: [PATCH] Refactor bookmark toggle button some more This should (mostly) work for also applying this to some of the other toggle buttons, but will probably still need a bit of work. --- .../post_action_toggle_button.jinja2 | 10 +-- tildes/tildes/templates/macros/buttons.jinja2 | 61 +++++++++---------- .../tildes/templates/macros/comments.jinja2 | 10 +-- tildes/tildes/templates/topic.jinja2 | 10 +-- tildes/tildes/views/api/web/comment.py | 26 ++------ tildes/tildes/views/api/web/topic.py | 37 ++++------- 6 files changed, 49 insertions(+), 105 deletions(-) diff --git a/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 b/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 index 050d818..48d0f36 100644 --- a/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 +++ b/tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2 @@ -3,12 +3,4 @@ {% from 'macros/buttons.jinja2' import post_action_toggle_button with context %} -{{ post_action_toggle_button( - subject=subject, - is_toggled=is_toggled, - normal_name=normal_name, - normal_label=normal_label, - toggled_name=toggled_name, - toggled_label=toggled_label, - route_name=route_name, -) }} +{{ post_action_toggle_button("bookmark", subject, is_toggled) }} diff --git a/tildes/tildes/templates/macros/buttons.jinja2 b/tildes/tildes/templates/macros/buttons.jinja2 index 75f5b7e..a847057 100644 --- a/tildes/tildes/templates/macros/buttons.jinja2 +++ b/tildes/tildes/templates/macros/buttons.jinja2 @@ -1,39 +1,38 @@ {# Copyright (c) 2019 Tildes contributors #} {# SPDX-License-Identifier: AGPL-3.0-or-later #} -{% macro post_action_toggle_button(subject, is_toggled, normal_name, normal_label, toggled_name, toggled_label, route_name) %} - {% if is_toggled %} -
  • {% else %} -
  • {% endif %} -
  • + +{% endmacro %} + +{% macro _post_action_toggle_button_url(route_name, subject) %} + {% if subject is comment -%} + {{ request.route_url(route_name, comment_id36=subject.comment_id36) }} + {% elif subject is topic -%} + {{ request.route_url(route_name, topic_id36=subject.topic_id36) }} + {%- endif %} {% endmacro %} diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index 75275b8..f234e33 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -208,15 +208,7 @@ {% endif %} {% if request.has_permission('bookmark', comment) %} - {{ post_action_toggle_button( - subject=comment, - is_toggled=comment.bookmark_created_time, - normal_name="bookmark", - normal_label="Bookmark", - toggled_name="unbookmark", - toggled_label="Unbookmark", - route_name="ic_comment_bookmark", - ) }} + {{ post_action_toggle_button("bookmark", comment, is_toggled=comment.bookmark_created_time) }} {% endif %} {% if request.has_permission("remove", comment) %} diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index 7800706..66a256b 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -165,15 +165,7 @@ {% endif %} {% if request.has_permission('bookmark', topic) %} - {{ post_action_toggle_button( - subject=topic, - is_toggled=topic.bookmark_created_time, - normal_name="bookmark", - normal_label="Bookmark", - toggled_name="unbookmark", - toggled_label="Unbookmark", - route_name="ic_topic_bookmark", - ) }} + {{ post_action_toggle_button("bookmark", topic, is_toggled=topic.bookmark_created_time) }} {% endif %} {% if request.has_permission("remove", topic) %} diff --git a/tildes/tildes/views/api/web/comment.py b/tildes/tildes/views/api/web/comment.py index c3948e2..aded654 100644 --- a/tildes/tildes/views/api/web/comment.py +++ b/tildes/tildes/views/api/web/comment.py @@ -426,7 +426,7 @@ def delete_comment_remove(request: Request) -> Response: route_name="comment_bookmark", request_method="PUT", permission="bookmark", - renderer="comment_contents.jinja2", + renderer="post_action_toggle_button.jinja2", ) def put_comment_bookmark(request: Request) -> dict: """Bookmark a comment with Intercooler.""" @@ -448,22 +448,14 @@ def put_comment_bookmark(request: Request) -> dict: # the user has already bookmarked this comment savepoint.rollback() - # re-query the comment to get complete data - comment = ( - request.query(Comment) - .join_all_relationships() - .filter_by(comment_id=comment.comment_id) - .one() - ) - - return {"comment": comment} + return {"name": "bookmark", "subject": comment, "is_toggled": True} @ic_view_config( route_name="comment_bookmark", request_method="DELETE", permission="bookmark", - renderer="comment_contents.jinja2", + renderer="post_action_toggle_button.jinja2", ) def delete_comment_bookmark(request: Request) -> dict: """Unbookmark a comment with Intercooler.""" @@ -475,14 +467,4 @@ def delete_comment_bookmark(request: Request) -> dict: _mark_comment_read_from_interaction(request, comment) - # commit and then re-query the comment to get complete data - request.tm.commit() - - comment = ( - request.query(Comment) - .join_all_relationships() - .filter_by(comment_id=comment.comment_id) - .one() - ) - - return {"comment": comment} + return {"name": "bookmark", "subject": comment, "is_toggled": False} diff --git a/tildes/tildes/views/api/web/topic.py b/tildes/tildes/views/api/web/topic.py index 2832dde..962cfba 100644 --- a/tildes/tildes/views/api/web/topic.py +++ b/tildes/tildes/views/api/web/topic.py @@ -6,7 +6,6 @@ from marshmallow import ValidationError from marshmallow.fields import String from pyramid.httpexceptions import HTTPNotFound -from pyramid.renderers import render_to_response from pyramid.response import Response from pyramid.request import Request from sqlalchemy import cast, Text @@ -375,9 +374,12 @@ def patch_topic_link(request: Request, link: str) -> dict: @ic_view_config( - route_name="topic_bookmark", request_method="PUT", permission="bookmark" + route_name="topic_bookmark", + request_method="PUT", + permission="bookmark", + renderer="post_action_toggle_button.jinja2", ) -def put_topic_bookmark(request: Request) -> Response: +def put_topic_bookmark(request: Request) -> dict: """Bookmark a topic with Intercooler.""" topic = request.context @@ -395,13 +397,16 @@ def put_topic_bookmark(request: Request) -> Response: # the user has already bookmarked this topic savepoint.rollback() - return _bookmark_toggle_button(topic, request, is_toggled=True) + return {"name": "bookmark", "subject": topic, "is_toggled": True} @ic_view_config( - route_name="topic_bookmark", request_method="DELETE", permission="bookmark" + route_name="topic_bookmark", + request_method="DELETE", + permission="bookmark", + renderer="post_action_toggle_button.jinja2", ) -def delete_topic_bookmark(request: Request) -> Response: +def delete_topic_bookmark(request: Request) -> dict: """Unbookmark a topic with Intercooler.""" topic = request.context @@ -409,22 +414,4 @@ def delete_topic_bookmark(request: Request) -> Response: TopicBookmark.user == request.user, TopicBookmark.topic == topic ).delete(synchronize_session=False) - return _bookmark_toggle_button(topic, request, is_toggled=False) - - -def _bookmark_toggle_button( - topic: Topic, request: Request, is_toggled: bool -) -> Response: - return render_to_response( - "tildes:templates/intercooler/post_action_toggle_button.jinja2", - value={ - "subject": topic, - "is_toggled": is_toggled, - "normal_name": "bookmark", - "normal_label": "Bookmark", - "toggled_name": "unbookmark", - "toggled_label": "Unbookmark", - "route_name": "ic_topic_bookmark", - }, - request=request, - ) + return {"name": "bookmark", "subject": topic, "is_toggled": False}