~homeworkprod/byceps

1e544cc5b5d52208851a2bcb4c1031e7751c7516 — Jochen Kupperschmidt 8 months ago 5a15004
Turn `{Topic, Posting}.may_be_updated_by_user()` into board blueprint service functions
M byceps/blueprints/site/board/service.py => byceps/blueprints/site/board/service.py +22 -1
@@ 27,7 27,7 @@ from ....services.user_badge import awarding_service as badge_awarding_service
from ....services.user_badge.transfer.models import Badge
from ....typing import BrandID, PartyID, UserID

from .authorization import BoardPermission
from .authorization import BoardPermission, BoardPostingPermission
from .models import CategoryWithLastUpdateAndUnseenFlag, Creator, Ticket




@@ 192,3 192,24 @@ def _get_site_setting_int_value(key, default_value) -> int:
def may_current_user_view_hidden() -> bool:
    """Return `True' if the current user may view hidden items."""
    return g.user.has_permission(BoardPermission.view_hidden)


def may_topic_be_updated_by_current_user(topic: DbTopic) -> bool:
    """Return `True` if the topic may be updated by the current user."""
    return (
        (
            not topic.locked
                and g.user.id == topic.creator_id
                and g.user.has_permission(BoardTopicPermission.update)
        )
        or g.user.has_permission(BoardPermission.update_of_others)
    )


def may_posting_be_updated_by_current_user(posting: DbPosting) -> bool:
    """Return `True` if the posting may be updated by the current user."""
    return (
        not posting.topic.locked
        and g.user.id == posting.creator_id
        and g.user.has_permission(BoardPostingPermission.update)
    ) or g.user.has_permission(BoardPermission.update_of_others)

M byceps/blueprints/site/board/templates/site/board/_posting_view_actions.html => byceps/blueprints/site/board/templates/site/board/_posting_view_actions.html +1 -1
@@ 4,7 4,7 @@
{%- set is_initial_topic_posting = posting.is_initial_topic_posting(posting.topic) %}

{%- set user_may_reply = (not topic.locked and g.user.has_permission(BoardPostingPermission.create)) %}
{%- set user_may_update = (topic.may_be_updated_by_user(g.user) if is_initial_topic_posting else posting.may_be_updated_by_user(g.user)) %}
{%- set user_may_update = (may_topic_be_updated_by_current_user(topic) if is_initial_topic_posting else may_posting_be_updated_by_current_user(posting)) %}
{%- set user_may_hide = g.user.has_permission(BoardPermission.hide) %}

      {%- if user_may_reply or user_may_update or user_may_hide %}

M byceps/blueprints/site/board/views_posting.py => byceps/blueprints/site/board/views_posting.py +2 -2
@@ 139,7 139,7 @@ def posting_update_form(posting_id, erroneous_form=None):
    page = service.calculate_posting_page_number(posting)
    url = h.build_url_for_posting_in_topic_view(posting, page)

    user_may_update = posting.may_be_updated_by_user(g.user)
    user_may_update = service.may_posting_be_updated_by_current_user(posting)

    if posting.topic.locked and not user_may_update:
        flash_error(


@@ 176,7 176,7 @@ def posting_update(posting_id):
    page = service.calculate_posting_page_number(posting)
    url = h.build_url_for_posting_in_topic_view(posting, page)

    user_may_update = posting.may_be_updated_by_user(g.user)
    user_may_update = service.may_posting_be_updated_by_current_user(posting)

    if posting.topic.locked and not user_may_update:
        flash_error(

M byceps/blueprints/site/board/views_topic.py => byceps/blueprints/site/board/views_topic.py +4 -2
@@ 120,6 120,8 @@ def topic_view(topic_id, page):
        'topic': topic,
        'postings': postings,
        'is_last_page': is_last_page,
        'may_topic_be_updated_by_current_user': service.may_topic_be_updated_by_current_user,
        'may_posting_be_updated_by_current_user': service.may_posting_be_updated_by_current_user,
    }

    if is_last_page:


@@ 214,7 216,7 @@ def topic_update_form(topic_id, erroneous_form=None):
    topic = h.get_topic_or_404(topic_id)
    url = h.build_url_for_topic(topic.id)

    user_may_update = topic.may_be_updated_by_user(g.user)
    user_may_update = service.may_topic_be_updated_by_current_user(topic)

    if topic.locked and not user_may_update:
        flash_error(


@@ 250,7 252,7 @@ def topic_update(topic_id):
    topic = h.get_topic_or_404(topic_id)
    url = h.build_url_for_topic(topic.id)

    user_may_update = topic.may_be_updated_by_user(g.user)
    user_may_update = service.may_topic_be_updated_by_current_user(topic)

    if topic.locked and not user_may_update:
        flash_error(

M byceps/services/board/models/posting.py => byceps/services/board/models/posting.py +0 -12
@@ 8,15 8,10 @@ byceps.services.board.models.posting

from datetime import datetime

from ....blueprints.site.board.authorization import (
    BoardPermission,
    BoardPostingPermission,
)
from ....database import BaseQuery, db, generate_uuid
from ....typing import UserID
from ....util.instances import ReprBuilder

from ...authentication.session.models.current_user import CurrentUser
from ...user.models.user import User

from ..transfer.models import TopicID


@@ 69,13 64,6 @@ class Posting(db.Model):
    def is_initial_topic_posting(self, topic: Topic) -> bool:
        return self == topic.initial_posting

    def may_be_updated_by_user(self, user: CurrentUser) -> bool:
        return (
            not self.topic.locked
            and user.id == self.creator_id
            and user.has_permission(BoardPostingPermission.update)
        ) or user.has_permission(BoardPermission.update_of_others)

    def __eq__(self, other) -> bool:
        return self.id == other.id


M byceps/services/board/models/topic.py => byceps/services/board/models/topic.py +0 -15
@@ 10,15 10,10 @@ from datetime import datetime

from sqlalchemy.ext.associationproxy import association_proxy

from ....blueprints.site.board.authorization import (
    BoardPermission,
    BoardTopicPermission,
)
from ....database import BaseQuery, db, generate_uuid
from ....typing import UserID
from ....util.instances import ReprBuilder

from ...authentication.session.models.current_user import CurrentUser
from ...user.models.user import User

from ..transfer.models import CategoryID


@@ 75,16 70,6 @@ class Topic(db.Model):
        self.creator_id = creator_id
        self.title = title

    def may_be_updated_by_user(self, user: CurrentUser) -> bool:
        return (
            (
                not self.locked
                    and user.id == self.creator_id
                    and user.has_permission(BoardTopicPermission.update)
            )
            or user.has_permission(BoardPermission.update_of_others)
        )

    @property
    def reply_count(self) -> int:
        return self.posting_count - 1