|
@ -5,12 +5,13 @@ |
|
|
|
|
|
|
|
|
from typing import List, Optional, Type, Union |
|
|
from typing import List, Optional, Type, Union |
|
|
|
|
|
|
|
|
|
|
|
from marshmallow.fields import String |
|
|
from pyramid.request import Request |
|
|
from pyramid.request import Request |
|
|
from pyramid.view import view_config |
|
|
from pyramid.view import view_config |
|
|
from sqlalchemy.sql.expression import desc |
|
|
from sqlalchemy.sql.expression import desc |
|
|
from webargs.pyramidparser import use_kwargs |
|
|
from webargs.pyramidparser import use_kwargs |
|
|
|
|
|
|
|
|
from tildes.enums import CommentLabelOption |
|
|
|
|
|
|
|
|
from tildes.enums import CommentLabelOption, CommentSortOption, TopicSortOption |
|
|
from tildes.models.comment import Comment |
|
|
from tildes.models.comment import Comment |
|
|
from tildes.models.pagination import MixedPaginatedResults |
|
|
from tildes.models.pagination import MixedPaginatedResults |
|
|
from tildes.models.topic import Topic |
|
|
from tildes.models.topic import Topic |
|
@ -19,15 +20,21 @@ from tildes.schemas.fields import PostType |
|
|
from tildes.schemas.listing import MixedListingSchema |
|
|
from tildes.schemas.listing import MixedListingSchema |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@view_config(route_name="user", renderer="user.jinja2") |
|
|
|
|
|
|
|
|
@view_config(route_name="user", renderer="user.jinja2") # noqa |
|
|
@use_kwargs(MixedListingSchema()) |
|
|
@use_kwargs(MixedListingSchema()) |
|
|
@use_kwargs({"post_type": PostType(load_from="type")}) |
|
|
|
|
|
|
|
|
@use_kwargs( |
|
|
|
|
|
{ |
|
|
|
|
|
"post_type": PostType(load_from="type"), |
|
|
|
|
|
"order_name": String(load_from="order", missing="new"), |
|
|
|
|
|
} # noqa |
|
|
|
|
|
) |
|
|
def get_user( |
|
|
def get_user( |
|
|
request: Request, |
|
|
request: Request, |
|
|
after: Optional[str], |
|
|
after: Optional[str], |
|
|
before: Optional[str], |
|
|
before: Optional[str], |
|
|
per_page: int, |
|
|
per_page: int, |
|
|
anchor_type: Optional[str], |
|
|
anchor_type: Optional[str], |
|
|
|
|
|
order_name: str, |
|
|
post_type: Optional[str] = None, |
|
|
post_type: Optional[str] = None, |
|
|
) -> dict: |
|
|
) -> dict: |
|
|
# pylint: disable=too-many-arguments |
|
|
# pylint: disable=too-many-arguments |
|
@ -44,14 +51,27 @@ def get_user( |
|
|
per_page = 20 |
|
|
per_page = 20 |
|
|
|
|
|
|
|
|
types_to_query: List[Union[Type[Topic], Type[Comment]]] |
|
|
types_to_query: List[Union[Type[Topic], Type[Comment]]] |
|
|
|
|
|
order_options: Optional[Union[Type[TopicSortOption], Type[CommentSortOption]]] |
|
|
|
|
|
|
|
|
if post_type == "topic": |
|
|
if post_type == "topic": |
|
|
types_to_query = [Topic] |
|
|
types_to_query = [Topic] |
|
|
|
|
|
order_options = TopicSortOption |
|
|
elif post_type == "comment": |
|
|
elif post_type == "comment": |
|
|
types_to_query = [Comment] |
|
|
types_to_query = [Comment] |
|
|
|
|
|
order_options = CommentSortOption |
|
|
else: |
|
|
else: |
|
|
# the order here is important so items are in the right order when the results |
|
|
# the order here is important so items are in the right order when the results |
|
|
# are merged at the end (we want topics to come first when times match) |
|
|
# are merged at the end (we want topics to come first when times match) |
|
|
types_to_query = [Comment, Topic] |
|
|
types_to_query = [Comment, Topic] |
|
|
|
|
|
order_options = None |
|
|
|
|
|
|
|
|
|
|
|
order = None |
|
|
|
|
|
if order_options: |
|
|
|
|
|
# try to get the specified order, but fall back to "newest" |
|
|
|
|
|
try: |
|
|
|
|
|
order = order_options[order_name.upper()] |
|
|
|
|
|
except KeyError: |
|
|
|
|
|
order = order_options["NEW"] |
|
|
|
|
|
|
|
|
result_sets = [] |
|
|
result_sets = [] |
|
|
for type_to_query in types_to_query: |
|
|
for type_to_query in types_to_query: |
|
@ -66,6 +86,9 @@ def get_user( |
|
|
if after: |
|
|
if after: |
|
|
query = query.after_id36(after) |
|
|
query = query.after_id36(after) |
|
|
|
|
|
|
|
|
|
|
|
if order: |
|
|
|
|
|
query = query.apply_sort_option(order) |
|
|
|
|
|
|
|
|
query = query.join_all_relationships() |
|
|
query = query.join_all_relationships() |
|
|
|
|
|
|
|
|
# include removed posts if the user's looking at their own page or is an admin |
|
|
# include removed posts if the user's looking at their own page or is an admin |
|
@ -83,6 +106,8 @@ def get_user( |
|
|
"user": user, |
|
|
"user": user, |
|
|
"posts": posts, |
|
|
"posts": posts, |
|
|
"post_type": post_type, |
|
|
"post_type": post_type, |
|
|
|
|
|
"order": order, |
|
|
|
|
|
"order_options": order_options, |
|
|
"comment_label_options": CommentLabelOption, |
|
|
"comment_label_options": CommentLabelOption, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|