Browse Source

Add a PostType field for choosing topic/comment

merge-requests/51/head
Deimos 6 years ago
parent
commit
5aedc8b959
  1. 10
      tildes/tildes/schemas/fields.py
  2. 19
      tildes/tildes/views/bookmarks.py
  3. 7
      tildes/tildes/views/user.py

10
tildes/tildes/schemas/fields.py

@ -8,7 +8,7 @@ from typing import Any, Optional, Type
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
from marshmallow.fields import Field, String from marshmallow.fields import Field, String
from marshmallow.validate import Length, Regexp
from marshmallow.validate import Length, OneOf, Regexp
import sqlalchemy_utils import sqlalchemy_utils
from tildes.lib.datetime import SimpleHoursPeriod from tildes.lib.datetime import SimpleHoursPeriod
@ -148,3 +148,11 @@ class Ltree(Field):
return sqlalchemy_utils.Ltree(value.lower()) return sqlalchemy_utils.Ltree(value.lower())
except (TypeError, ValueError): except (TypeError, ValueError):
raise ValidationError("Invalid path") 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)

19
tildes/tildes/views/bookmarks.py

@ -1,9 +1,7 @@
"""Views relating to bookmarks.""" """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.request import Request
from pyramid.view import view_config from pyramid.view import view_config
from sqlalchemy.sql import desc 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.comment import Comment, CommentBookmark
from tildes.models.topic import Topic, TopicBookmark from tildes.models.topic import Topic, TopicBookmark
from tildes.schemas.fields import PostType
from tildes.schemas.listing import PaginatedListingSchema from tildes.schemas.listing import PaginatedListingSchema
@view_config(route_name="bookmarks", renderer="bookmarks.jinja2") @view_config(route_name="bookmarks", renderer="bookmarks.jinja2")
@use_kwargs(PaginatedListingSchema) @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( 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: ) -> dict:
"""Generate the bookmarks page.""" """Generate the bookmarks page."""
# pylint: disable=unused-argument # pylint: disable=unused-argument

7
tildes/tildes/views/user.py

@ -5,8 +5,6 @@
from typing import List, Optional, Union from typing import List, Optional, Union
from marshmallow.fields import String
from marshmallow.validate import OneOf
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
@ -16,6 +14,7 @@ from tildes.enums import CommentLabelOption
from tildes.models.comment import Comment from tildes.models.comment import Comment
from tildes.models.topic import Topic from tildes.models.topic import Topic
from tildes.models.user import User, UserInviteCode from tildes.models.user import User, UserInviteCode
from tildes.schemas.fields import PostType
from tildes.schemas.listing import PaginatedListingSchema from tildes.schemas.listing import PaginatedListingSchema
@ -67,9 +66,7 @@ def _get_user_recent_activity(
@view_config(route_name="user", renderer="user.jinja2") @view_config(route_name="user", renderer="user.jinja2")
@use_kwargs(PaginatedListingSchema()) @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( def get_user(
request: Request, request: Request,
after: str, after: str,

Loading…
Cancel
Save