diff --git a/tildes/tildes/schemas/fields.py b/tildes/tildes/schemas/fields.py index d7fa9b7..909b44e 100644 --- a/tildes/tildes/schemas/fields.py +++ b/tildes/tildes/schemas/fields.py @@ -8,7 +8,7 @@ from typing import Any, Optional, Type from marshmallow.exceptions import ValidationError from marshmallow.fields import Field, String -from marshmallow.validate import Length, Regexp +from marshmallow.validate import Length, OneOf, Regexp import sqlalchemy_utils from tildes.lib.datetime import SimpleHoursPeriod @@ -148,3 +148,11 @@ class Ltree(Field): return sqlalchemy_utils.Ltree(value.lower()) except (TypeError, ValueError): raise ValidationError("Invalid path") + + +class PostType(String): + """Field for selecting a type of post (topic or comment).""" + + def __init__(self, **kwargs: Any): + """Initialize the field with a "one of" validator.""" + super().__init__(validate=OneOf(("topic", "comment")), **kwargs) diff --git a/tildes/tildes/views/bookmarks.py b/tildes/tildes/views/bookmarks.py index 22eeb0b..3c4f313 100644 --- a/tildes/tildes/views/bookmarks.py +++ b/tildes/tildes/views/bookmarks.py @@ -1,9 +1,7 @@ """Views relating to bookmarks.""" -from typing import Optional, Type, Union +from typing import Type, Union -from marshmallow.fields import String -from marshmallow.validate import OneOf from pyramid.request import Request from pyramid.view import view_config from sqlalchemy.sql import desc @@ -11,24 +9,15 @@ from webargs.pyramidparser import use_kwargs from tildes.models.comment import Comment, CommentBookmark from tildes.models.topic import Topic, TopicBookmark +from tildes.schemas.fields import PostType from tildes.schemas.listing import PaginatedListingSchema @view_config(route_name="bookmarks", renderer="bookmarks.jinja2") @use_kwargs(PaginatedListingSchema) -@use_kwargs( - { - "post_type": String( - load_from="type", validate=OneOf(("topic", "comment")), missing="topic" - ) - } -) +@use_kwargs({"post_type": PostType(load_from="type", missing="topic")}) def get_bookmarks( - request: Request, - after: str, - before: str, - per_page: int, - post_type: Optional[str] = None, + request: Request, after: str, before: str, per_page: int, post_type: str ) -> dict: """Generate the bookmarks page.""" # pylint: disable=unused-argument diff --git a/tildes/tildes/views/user.py b/tildes/tildes/views/user.py index 730e246..b4bd7ab 100644 --- a/tildes/tildes/views/user.py +++ b/tildes/tildes/views/user.py @@ -5,8 +5,6 @@ from typing import List, Optional, Union -from marshmallow.fields import String -from marshmallow.validate import OneOf from pyramid.request import Request from pyramid.view import view_config from sqlalchemy.sql.expression import desc @@ -16,6 +14,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.fields import PostType from tildes.schemas.listing import PaginatedListingSchema @@ -67,9 +66,7 @@ def _get_user_recent_activity( @view_config(route_name="user", renderer="user.jinja2") @use_kwargs(PaginatedListingSchema()) -@use_kwargs( - {"post_type": String(load_from="type", validate=OneOf(("topic", "comment")))} -) +@use_kwargs({"post_type": PostType(load_from="type")}) def get_user( request: Request, after: str,