Browse Source

Add PaginatedListingSchema for basic listings

Previously, TopicListingSchema was being used for things that weren't
listings of topics. So this creates a more generic
PaginatedListingSchema and makes TopicListingSchema a subclass of it
that adds the additional fields it needs.
merge-requests/40/head
Deimos 6 years ago
parent
commit
afbaab9804
  1. 33
      tildes/tildes/schemas/listing.py
  2. 4
      tildes/tildes/views/notifications.py
  3. 2
      tildes/tildes/views/topic.py
  4. 4
      tildes/tildes/views/user.py

33
tildes/tildes/schemas/topic_listing.py → 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

4
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:

2
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

4
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")))}
)

Loading…
Cancel
Save