diff --git a/tildes/tildes/schemas/topic_listing.py b/tildes/tildes/schemas/listing.py similarity index 85% rename from tildes/tildes/schemas/topic_listing.py rename to tildes/tildes/schemas/listing.py index ca055b2..70c9bde 100644 --- a/tildes/tildes/schemas/topic_listing.py +++ b/tildes/tildes/schemas/listing.py @@ -11,19 +11,12 @@ from tildes.enums import TopicSortOption from tildes.schemas.fields import Enum, ID36, Ltree, ShortTimePeriod -class TopicListingSchema(Schema): - """Marshmallow schema to validate arguments for a topic listing page.""" - - DEFAULT_TOPICS_PER_PAGE = 50 +class PaginatedListingSchema(Schema): + """Marshmallow schema to validate arguments for a paginated listing page.""" - order = Enum(TopicSortOption) - period = ShortTimePeriod(allow_none=True) after = ID36() before = ID36() - per_page = Integer(validate=Range(min=1, max=100), missing=DEFAULT_TOPICS_PER_PAGE) - rank_start = Integer(load_from="n", validate=Range(min=1), missing=None) - tag = Ltree(missing=None) - unfiltered = Boolean(missing=False) + per_page = Integer(validate=Range(min=1, max=100), missing=50) @validates_schema def either_after_or_before(self, data: dict) -> None: @@ -31,6 +24,21 @@ class TopicListingSchema(Schema): if data.get("after") and data.get("before"): raise ValidationError("Can't specify both after and before.") + class Meta: + """Always use strict checking so error handlers are invoked.""" + + strict = True + + +class TopicListingSchema(PaginatedListingSchema): + """Marshmallow schema to validate arguments for a topic listing page.""" + + period = ShortTimePeriod(allow_none=True) + order = Enum(TopicSortOption) + tag = Ltree(missing=None) + unfiltered = Boolean(missing=False) + rank_start = Integer(load_from="n", validate=Range(min=1), missing=None) + @pre_load def reset_rank_start_on_first_page(self, data: dict) -> dict: """Reset rank_start to 1 if this is a first page (no before/after).""" @@ -38,8 +46,3 @@ class TopicListingSchema(Schema): data["rank_start"] = 1 return data - - class Meta: - """Always use strict checking so error handlers are invoked.""" - - strict = True diff --git a/tildes/tildes/views/notifications.py b/tildes/tildes/views/notifications.py index 7a84285..96da5d9 100644 --- a/tildes/tildes/views/notifications.py +++ b/tildes/tildes/views/notifications.py @@ -10,7 +10,7 @@ from webargs.pyramidparser import use_kwargs from tildes.enums import CommentLabelOption from tildes.models.comment import CommentNotification -from tildes.schemas.topic_listing import TopicListingSchema +from tildes.schemas.listing import PaginatedListingSchema @view_config(route_name="notifications_unread", renderer="notifications_unread.jinja2") @@ -37,7 +37,7 @@ def get_user_unread_notifications(request: Request) -> dict: @view_config(route_name="notifications", renderer="notifications.jinja2") -@use_kwargs(TopicListingSchema(only=("after", "before", "per_page"))) +@use_kwargs(PaginatedListingSchema()) def get_user_notifications( request: Request, after: str, before: str, per_page: int ) -> dict: diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index 446ce9f..4a320ca 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -33,8 +33,8 @@ from tildes.models.topic import Topic, TopicVisit from tildes.models.user import UserGroupSettings from tildes.schemas.comment import CommentSchema from tildes.schemas.fields import Enum, ShortTimePeriod +from tildes.schemas.listing import TopicListingSchema from tildes.schemas.topic import TopicSchema -from tildes.schemas.topic_listing import TopicListingSchema from tildes.views.decorators import rate_limit_view diff --git a/tildes/tildes/views/user.py b/tildes/tildes/views/user.py index 05c5520..730e246 100644 --- a/tildes/tildes/views/user.py +++ b/tildes/tildes/views/user.py @@ -16,7 +16,7 @@ from tildes.enums import CommentLabelOption from tildes.models.comment import Comment from tildes.models.topic import Topic from tildes.models.user import User, UserInviteCode -from tildes.schemas.topic_listing import TopicListingSchema +from tildes.schemas.listing import PaginatedListingSchema def _get_user_recent_activity( @@ -66,7 +66,7 @@ def _get_user_recent_activity( @view_config(route_name="user", renderer="user.jinja2") -@use_kwargs(TopicListingSchema(only=("after", "before", "per_page"))) +@use_kwargs(PaginatedListingSchema()) @use_kwargs( {"post_type": String(load_from="type", validate=OneOf(("topic", "comment")))} )