Browse Source

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.
merge-requests/69/head
Deimos 6 years ago
parent
commit
246364de1d
  1. 10
      tildes/tildes/templates/intercooler/post_action_toggle_button.jinja2
  2. 61
      tildes/tildes/templates/macros/buttons.jinja2
  3. 10
      tildes/tildes/templates/macros/comments.jinja2
  4. 10
      tildes/tildes/templates/topic.jinja2
  5. 26
      tildes/tildes/views/api/web/comment.py
  6. 37
      tildes/tildes/views/api/web/topic.py

10
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) }}

61
tildes/tildes/templates/macros/buttons.jinja2

@ -1,39 +1,38 @@
{# Copyright (c) 2019 Tildes contributors <code@tildes.net> #}
{# 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 %}
<li><button class="btn-post-action btn-post-action-used" name="{{ toggled_name }}"
{% if subject is comment %}
data-ic-delete-from="{{ request.route_url(
route_name,
comment_id36=subject.comment_id36,
) }}"
data-ic-target="#comment-{{ subject.comment_id36 }} .comment-itself:first"
{% elif subject is topic %}
data-ic-delete-from="{{ request.route_url(
route_name,
topic_id36=subject.topic_id36,
) }}"
{% endif %}
{% macro post_action_toggle_button(name, subject, is_toggled) %}
{% if subject is comment %}
{% set type_name = "comment" %}
{% elif subject is topic %}
{% set type_name = "topic" %}
{% endif %}
{% set route_name = "ic_" + type_name + "_" + name %}
{% if name == "bookmark" %}
{% set normal_label = "Bookmark" %}
{% set toggled_label = "Unbookmark" %}
{% endif %}
<li>
{% if not is_toggled %}
<button class="btn-post-action"
data-ic-put-to="{{ _post_action_toggle_button_url(route_name, subject) }}"
data-ic-replace-target="true"
>{{ toggled_label }}
>{{ normal_label }}</button>
{% else %}
<li><button class="btn-post-action" name="{{ normal_name }}"
{% if subject is comment %}
data-ic-put-to="{{ request.route_url(
route_name,
comment_id36=subject.comment_id36,
) }}"
data-ic-target="#comment-{{ subject.comment_id36 }} .comment-itself:first"
{% elif subject is topic %}
data-ic-put-to="{{ request.route_url(
route_name,
topic_id36=subject.topic_id36,
) }}"
{% endif %}
<button class="btn-post-action btn-post-action-used"
data-ic-delete-from="{{ _post_action_toggle_button_url(route_name, subject) }}"
data-ic-replace-target="true"
>{{ normal_label }}
>{{ toggled_label }}</button>
{% endif %}
</button></li>
</li>
{% 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 %}

10
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) %}

10
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) %}

26
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}

37
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}
Loading…
Cancel
Save