Exemple #1
0
    def validate_tags(self, value: list[str]) -> None:
        """Validate the tags field, raising an error if an issue exists.

        Note that tags are validated by ensuring that each tag would be a valid group
        path. This is definitely mixing concerns, but it's deliberate in this case. It
        will allow for some interesting possibilities by ensuring naming "compatibility"
        between groups and tags. For example, a popular tag in a group could be
        converted into a sub-group easily.
        """
        group_schema = GroupSchema(partial=True)
        for tag in value:
            try:
                group_schema.load({"path": tag})
            except ValidationError as exc:
                raise ValidationError("Tag %s is invalid" % tag) from exc
Exemple #2
0
    request_method="GET",
    renderer="topic_group_edit.jinja2",
    permission="move",
)
def get_topic_group(request: Request) -> dict:
    """Get the form for moving a topic with Intercooler."""
    return {"topic": request.context}


@ic_view_config(
    route_name="topic",
    request_param="ic-trigger-name=topic-move",
    request_method="PATCH",
    permission="move",
)
@use_kwargs(GroupSchema(only=("path", )))
def patch_move_topic(request: Request, path: str) -> dict:
    """Move a topic to a different group with Intercooler."""
    topic = request.context

    new_group = request.query(Group).filter(Group.path == path).one_or_none()
    if not new_group:
        raise HTTPNotFound("Group not found")

    old_group = topic.group

    if new_group == old_group:
        return IC_NOOP

    topic.group = new_group
Exemple #3
0
    request_method='GET',
    renderer='topic_group_edit.jinja2',
    permission='move',
)
def get_topic_group(request: Request) -> dict:
    """Get the form for moving a topic with Intercooler."""
    return {'topic': request.context}


@ic_view_config(
    route_name='topic',
    request_param='ic-trigger-name=topic-move',
    request_method='PATCH',
    permission='move',
)
@use_kwargs(GroupSchema(only=('path', )))
def move_topic(request: Request, path: str) -> dict:
    """Move a topic to a different group with Intercooler."""
    topic = request.context

    new_group = (request.query(Group).filter(Group.path == path).one_or_none())
    if not new_group:
        raise HTTPNotFound('Group not found')

    old_group = topic.group

    if new_group == old_group:
        return IC_NOOP

    topic.group = new_group
Exemple #4
0
"""Root factories for groups."""

from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.request import Request
from sqlalchemy_utils import Ltree
from webargs.pyramidparser import use_kwargs

from tildes.models.group import Group
from tildes.resources import get_resource
from tildes.schemas.group import GroupSchema


@use_kwargs(
    GroupSchema(only=('path', ), context={'fix_path_capitalization': True}),
    locations=('matchdict', ),
)
def group_by_path(request: Request, path: str) -> Group:
    """Get a group specified by {group_path} in the route (or 404)."""
    # If loading the specified group path into the GroupSchema changed it, do a
    # 301 redirect to the resulting group path. This will happen in cases like
    # the original url including capital letters in the group path, where we
    # want to redirect to the proper all-lowercase path instead.
    if path != request.matchdict['group_path']:
        request.matchdict['group_path'] = path
        proper_url = request.route_url(request.matched_route.name,
                                       **request.matchdict)

        raise HTTPMovedPermanently(location=proper_url)

    query = request.query(Group).filter(Group.path == Ltree(path))
Exemple #5
0
    request_method="GET",
    renderer="topic_group_edit.jinja2",
    permission="move",
)
def get_topic_group(request: Request) -> dict:
    """Get the form for moving a topic with Intercooler."""
    return {"topic": request.context}


@ic_view_config(
    route_name="topic",
    request_param="ic-trigger-name=topic-move",
    request_method="PATCH",
    permission="move",
)
@use_kwargs(GroupSchema(only=("path", )), location="form")
def patch_move_topic(request: Request, path: str) -> dict:
    """Move a topic to a different group with Intercooler."""
    topic = request.context

    new_group = request.query(Group).filter(Group.path == path).one_or_none()
    if not new_group:
        raise HTTPNotFound("Group not found")

    old_group = topic.group

    if new_group == old_group:
        return IC_NOOP

    topic.group = new_group
Exemple #6
0
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Root factories for groups."""

from marshmallow.fields import String
from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.request import Request
from sqlalchemy_utils import Ltree
from webargs.pyramidparser import use_kwargs

from tildes.models.group import Group, GroupWikiPage
from tildes.resources import get_resource
from tildes.schemas.group import GroupSchema


@use_kwargs(
    GroupSchema(only=("path", ), context={"fix_path_capitalization": True}),
    locations=("matchdict", ),
)
def group_by_path(request: Request, path: str) -> Group:
    """Get a group specified by {group_path} in the route (or 404)."""
    # If loading the specified group path into the GroupSchema changed it, do a 301
    # redirect to the resulting group path. This will happen in cases like the original
    # url including capital letters in the group path, where we want to redirect to the
    # proper all-lowercase path instead.
    if path != request.matchdict["group_path"]:
        request.matchdict["group_path"] = path
        proper_url = request.route_url(request.matched_route.name,
                                       **request.matchdict)

        raise HTTPMovedPermanently(location=proper_url)