Browse Source

Add setting to choose default comment sort order

merge-requests/110/head
Bauke 5 years ago
committed by Deimos
parent
commit
2c7572016d
  1. 37
      tildes/alembic/versions/51a1012f4f63_add_comment_sort_order_account_setting.py
  2. 10
      tildes/tildes/models/user/user.py
  3. 25
      tildes/tildes/templates/settings.jinja2
  4. 16
      tildes/tildes/views/api/web/user.py
  5. 12
      tildes/tildes/views/settings.py
  6. 13
      tildes/tildes/views/topic.py

37
tildes/alembic/versions/51a1012f4f63_add_comment_sort_order_account_setting.py

@ -0,0 +1,37 @@
"""Add comment sort order account setting
Revision ID: 51a1012f4f63
Revises: 9b7a7b906956
Create Date: 2020-02-07 22:38:08.826608
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = "51a1012f4f63"
down_revision = "9b7a7b906956"
branch_labels = None
depends_on = None
def upgrade():
op.execute(
"create type commenttreesortoption as enum('VOTES', 'NEWEST', 'POSTED', 'RELEVANCE')"
)
op.add_column(
"users",
sa.Column(
"comment_sort_order_default",
postgresql.ENUM(
"VOTES", "NEWEST", "POSTED", "RELEVANCE", name="commenttreesortoption"
),
nullable=True,
),
)
def downgrade():
op.drop_column("users", "comment_sort_order_default")
op.execute("drop type commenttreesortoption")

10
tildes/tildes/models/user/user.py

@ -30,7 +30,12 @@ from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import deferred
from sqlalchemy.sql.expression import text
from tildes.enums import CommentLabelOption, HTMLSanitizationContext, TopicSortOption
from tildes.enums import (
CommentLabelOption,
CommentTreeSortOption,
HTMLSanitizationContext,
TopicSortOption,
)
from tildes.lib.database import CIText, TagList
from tildes.lib.datetime import utc_now
from tildes.lib.hash import hash_string, is_match_for_hash
@ -90,6 +95,9 @@ class User(DatabaseModel):
inviter_id: int = Column(Integer, ForeignKey("users.user_id"))
invite_codes_remaining: int = Column(Integer, nullable=False, server_default="0")
collapse_old_comments: bool = Column(Boolean, nullable=False, server_default="true")
comment_sort_order_default: Optional[CommentTreeSortOption] = Column(
ENUM(CommentTreeSortOption)
)
auto_mark_notifications_read: bool = Column(
Boolean, nullable=False, server_default="false"
)

25
tildes/tildes/templates/settings.jinja2

@ -73,6 +73,31 @@
<h2>Site behavior settings</h2>
<ul class="settings-list">
<li>
<form
name="account-default-comment-sort-order"
data-ic-patch-to="{{ request.route_url(
'ic_user',
username=request.user.username
) }}"
>
<label class="form-label" for="comment-sort-order">Choose a default comment sort order:</label>
<select
class="form-select"
name="comment-sort-order"
id="comment-sort-order"
data-js-autosubmit-on-change
>
{% for sort_option in comment_sort_order_options %}
<option
value="{{ sort_option.name }}"
{{ 'selected' if current_comment_sort_order == sort_option else '' }}
>{{ sort_option.description|capitalize }}</option>
{% endfor %}
</select>
</form>
</li>
<li>
<form
name="collapse-old-comments"

16
tildes/tildes/views/api/web/user.py

@ -184,6 +184,22 @@ def patch_change_show_tags_in_listings(request: Request) -> Response:
return IC_NOOP
@ic_view_config(
route_name="user",
request_method="PATCH",
request_param="ic-trigger-name=account-default-comment-sort-order",
permission="change_comment_sort_order_setting",
)
def patch_change_comment_sort_order(request: Request) -> Response:
"""Change the user's default comment sort order setting."""
user = request.context
comment_sort_order = request.params.get("comment-sort-order")
user.comment_sort_order_default = comment_sort_order
return IC_NOOP
@ic_view_config(
route_name="user",
request_method="PATCH",

12
tildes/tildes/views/settings.py

@ -62,7 +62,17 @@ def get_settings(request: Request) -> dict:
theme_options[user_default_theme] += " (account default)"
theme_options[site_default_theme] += " (site default)"
return {"current_theme": current_theme, "theme_options": theme_options}
if request.user.comment_sort_order_default:
current_comment_sort_order = request.user.comment_sort_order_default
else:
current_comment_sort_order = CommentTreeSortOption.RELEVANCE
return {
"current_comment_sort_order": current_comment_sort_order,
"comment_sort_order_options": CommentTreeSortOption,
"current_theme": current_theme,
"theme_options": theme_options,
}
@view_config(

13
tildes/tildes/views/topic.py

@ -380,16 +380,15 @@ def get_new_topic_form(request: Request) -> dict:
@view_config(route_name="topic", renderer="topic.jinja2")
@view_config(route_name="topic_no_title", renderer="topic.jinja2")
@use_kwargs(
{
"comment_order": Enum(
CommentTreeSortOption, missing=CommentTreeSortOption.RELEVANCE
)
}
)
@use_kwargs({"comment_order": Enum(CommentTreeSortOption, missing=None)})
def get_topic(request: Request, comment_order: CommentTreeSortOption) -> dict:
"""View a single topic."""
topic = request.context
if comment_order is None:
if request.user and request.user.comment_sort_order_default:
comment_order = request.user.comment_sort_order_default
else:
comment_order = CommentTreeSortOption.RELEVANCE
# deleted and removed comments need to be included since they're necessary for
# building the tree if they have replies

Loading…
Cancel
Save