예제 #1
0
class MixedListingSchema(PaginatedListingSchema):
    """Marshmallow schema to validate arguments for a "mixed" listing page.

    By "mixed", this means that the listing can contain topics and/or comments, instead
    of just one or the other.
    """

    anchor_type = PostType(missing=None)

    @pre_load
    def set_anchor_type_from_before_or_after(self, data: dict, many: bool,
                                             partial: Any) -> dict:
        """Set the anchor_type if before or after has a special value indicating type.

        For example, if after or before looks like "t-123" that means it is referring
        to the topic with ID36 "123". "c-123" also works, for comments.
        """
        # pylint: disable=unused-argument
        new_data = data.copy()

        keys = ("after", "before")

        for key in keys:
            value = new_data.get(key)
            if not value:
                continue

            type_char, _, id36 = value.partition("-")
            if not id36:
                continue

            if type_char == "c":
                new_data["anchor_type"] = "comment"
            elif type_char == "t":
                new_data["anchor_type"] = "topic"
            else:
                continue

            new_data[key] = id36

        return new_data
예제 #2
0
from typing import Optional, Type, Union

from pyramid.request import Request
from pyramid.view import view_config
from sqlalchemy.sql import desc
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": PostType(data_key="type", missing="topic")})
def get_bookmarks(
    request: Request,
    after: Optional[str],
    before: Optional[str],
    per_page: int,
    post_type: str,
) -> dict:
    """Generate the bookmarks page."""
    # pylint: disable=unused-argument
    user = request.user

    bookmark_cls: Union[Type[CommentBookmark], Type[TopicBookmark]]

    if post_type == "comment":
        post_cls = Comment
예제 #3
0
파일: user.py 프로젝트: spectria/tildes
from tildes.enums import CommentLabelOption, CommentSortOption, TopicSortOption
from tildes.models.comment import Comment
from tildes.models.pagination import MixedPaginatedResults, PaginatedResults
from tildes.models.topic import Topic
from tildes.models.user import User, UserInviteCode
from tildes.schemas.fields import PostType
from tildes.schemas.listing import MixedListingSchema
from tildes.views.decorators import use_kwargs


@view_config(route_name="user", renderer="user.jinja2")
@use_kwargs(MixedListingSchema())
@use_kwargs(
    {
        "post_type": PostType(data_key="type", missing=None),
        "order_name": String(data_key="order", missing="new"),
    }
)
def get_user(
    request: Request,
    after: Optional[str],
    before: Optional[str],
    per_page: int,
    anchor_type: Optional[str],
    order_name: str,
    post_type: Optional[str],
) -> dict:
    """Generate the main user history page."""
    user = request.context
예제 #4
0
from sqlalchemy.sql.expression import desc
from webargs.pyramidparser import use_kwargs

from tildes.enums import CommentLabelOption, CommentSortOption, TopicSortOption
from tildes.models.comment import Comment
from tildes.models.pagination import MixedPaginatedResults, PaginatedResults
from tildes.models.topic import Topic
from tildes.models.user import User, UserInviteCode
from tildes.schemas.fields import PostType
from tildes.schemas.listing import MixedListingSchema


@view_config(route_name="user", renderer="user.jinja2")
@use_kwargs(MixedListingSchema())
@use_kwargs({
    "post_type": PostType(load_from="type"),
    "order_name": String(load_from="order", missing="new"),
})
def get_user(
    request: Request,
    after: Optional[str],
    before: Optional[str],
    per_page: int,
    anchor_type: Optional[str],
    order_name: str,
    post_type: Optional[str] = None,
) -> dict:
    # pylint: disable=too-many-arguments
    """Generate the main user history page."""
    user = request.context
예제 #5
0
from typing import Type, Union

from pyramid.request import Request
from pyramid.view import view_config
from sqlalchemy.sql import desc
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": PostType(load_from="type", missing="topic")})
def get_bookmarks(
    request: Request, after: str, before: str, per_page: int, post_type: str
) -> dict:
    """Generate the bookmarks page."""
    # pylint: disable=unused-argument
    user = request.user

    bookmark_cls: Union[Type[CommentBookmark], Type[TopicBookmark]]

    if post_type == "comment":
        post_cls = Comment
        bookmark_cls = CommentBookmark
    elif post_type == "topic":
        post_cls = Topic
        bookmark_cls = TopicBookmark