~homeworkprod/byceps

983f664bdfd3c8a3d54cf09a5ef4276f174bff20 — Jochen Kupperschmidt 2 years ago 82ca0f5
Only pass board topic's ID to services
M byceps/blueprints/board/views_posting.py => byceps/blueprints/board/views_posting.py +1 -1
@@ 108,7 108,7 @@ def posting_create(topic_id):
        return redirect(h.build_url_for_topic(topic.id))

    posting, event = board_posting_command_service.create_posting(
        topic, creator.id, body
        topic.id, creator.id, body
    )

    if not g.current_user.is_anonymous:

M byceps/blueprints/board/views_topic.py => byceps/blueprints/board/views_topic.py +10 -10
@@ 236,7 236,7 @@ def topic_update(topic_id):
        return topic_update_form(topic_id, form)

    board_topic_command_service.update_topic(
        topic, g.current_user.id, form.title.data, form.body.data
        topic.id, g.current_user.id, form.title.data, form.body.data
    )

    flash_success(f'Das Thema "{topic.title}" wurde aktualisiert.')


@@ 271,7 271,7 @@ def topic_hide(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.hide_topic(topic, moderator_id)
    event = board_topic_command_service.hide_topic(topic.id, moderator_id)

    flash_success(f'Das Thema "{topic.title}" wurde versteckt.', icon='hidden')



@@ 291,7 291,7 @@ def topic_unhide(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.unhide_topic(topic, moderator_id)
    event = board_topic_command_service.unhide_topic(topic.id, moderator_id)

    flash_success(
        f'Das Thema "{topic.title}" wurde wieder sichtbar gemacht.', icon='view'


@@ 313,7 313,7 @@ def topic_lock(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.lock_topic(topic, moderator_id)
    event = board_topic_command_service.lock_topic(topic.id, moderator_id)

    flash_success(f'Das Thema "{topic.title}" wurde geschlossen.', icon='lock')



@@ 333,7 333,7 @@ def topic_unlock(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.unlock_topic(topic, moderator_id)
    event = board_topic_command_service.unlock_topic(topic.id, moderator_id)

    flash_success(
        f'Das Thema "{topic.title}" wurde wieder geöffnet.', icon='unlock'


@@ 355,7 355,7 @@ def topic_pin(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.pin_topic(topic, moderator_id)
    event = board_topic_command_service.pin_topic(topic.id, moderator_id)

    flash_success(f'Das Thema "{topic.title}" wurde angepinnt.', icon='pin')



@@ 375,7 375,7 @@ def topic_unpin(topic_id):
    topic = h.get_topic_or_404(topic_id)
    moderator_id = g.current_user.id

    event = board_topic_command_service.unpin_topic(topic, moderator_id)
    event = board_topic_command_service.unpin_topic(topic.id, moderator_id)

    flash_success(f'Das Thema "{topic.title}" wurde wieder gelöst.')



@@ 403,7 403,7 @@ def topic_move(topic_id):
    old_category = topic.category

    event = board_topic_command_service.move_topic(
        topic, new_category.id, moderator_id
        topic.id, new_category.id, moderator_id
    )

    flash_success(


@@ 430,7 430,7 @@ def topic_limit_to_announcements(topic_id):
    """Limit posting in the topic to moderators."""
    topic = h.get_topic_or_404(topic_id)

    board_topic_command_service.limit_topic_to_announcements(topic)
    board_topic_command_service.limit_topic_to_announcements(topic.id)

    flash_success(
        f'Das Thema "{topic.title}" wurde auf Ankündigungen beschränkt.',


@@ 449,7 449,7 @@ def topic_remove_limit_to_announcements(topic_id):
    """Allow non-moderators to post in the topic again."""
    topic = h.get_topic_or_404(topic_id)

    board_topic_command_service.remove_limit_of_topic_to_announcements(topic)
    board_topic_command_service.remove_limit_of_topic_to_announcements(topic.id)

    flash_success(
        f'Das Thema "{topic.title}" wurde für normale Beiträge geöffnet.'

M byceps/services/board/posting_command_service.py => byceps/services/board/posting_command_service.py +5 -1
@@ 21,12 21,16 @@ from ...typing import UserID
from .aggregation_service import aggregate_topic
from .models.posting import Posting as DbPosting
from .models.topic import Topic as DbTopic
from . import topic_query_service
from .transfer.models import TopicID


def create_posting(
    topic: DbTopic, creator_id: UserID, body: str
    topic_id: TopicID, creator_id: UserID, body: str
) -> Tuple[DbPosting, BoardPostingCreated]:
    """Create a posting in that topic."""
    topic = topic_query_service.get_topic(topic_id)

    posting = DbPosting(topic, creator_id, body)
    db.session.add(posting)
    db.session.commit()

M byceps/services/board/topic_command_service.py => byceps/services/board/topic_command_service.py +36 -11
@@ 28,7 28,8 @@ from .models.category import Category as DbCategory
from .models.posting import InitialTopicPostingAssociation, Posting as DbPosting
from .models.topic import Topic as DbTopic
from .posting_command_service import update_posting
from .transfer.models import CategoryID
from . import topic_query_service
from .transfer.models import CategoryID, TopicID


def create_topic(


@@ 56,9 57,11 @@ def create_topic(


def update_topic(
    topic: DbTopic, editor_id: UserID, title: str, body: str
    topic_id: TopicID, editor_id: UserID, title: str, body: str
) -> BoardTopicUpdated:
    """Update the topic (and its initial posting)."""
    topic = _get_topic(topic_id)

    topic.title = title.strip()

    posting_event = update_posting(


@@ 75,8 78,10 @@ def update_topic(
    )


def hide_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicHidden:
def hide_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicHidden:
    """Hide the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    topic.hidden = True


@@ 91,8 96,10 @@ def hide_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicHidden:
    )


def unhide_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnhidden:
def unhide_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicUnhidden:
    """Un-hide the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    # TODO: Store who un-hid the topic.


@@ 108,8 115,10 @@ def unhide_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnhidden:
    )


def lock_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicLocked:
def lock_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicLocked:
    """Lock the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    topic.locked = True


@@ 122,8 131,10 @@ def lock_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicLocked:
    )


def unlock_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnlocked:
def unlock_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicUnlocked:
    """Unlock the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    # TODO: Store who unlocked the topic.


@@ 137,8 148,10 @@ def unlock_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnlocked:
    )


def pin_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicPinned:
def pin_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicPinned:
    """Pin the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    topic.pinned = True


@@ 151,8 164,10 @@ def pin_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicPinned:
    )


def unpin_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnpinned:
def unpin_topic(topic_id: TopicID, moderator_id: UserID) -> BoardTopicUnpinned:
    """Unpin the topic."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    # TODO: Store who unpinned the topic.


@@ 167,9 182,11 @@ def unpin_topic(topic: DbTopic, moderator_id: UserID) -> BoardTopicUnpinned:


def move_topic(
    topic: DbTopic, new_category_id: CategoryID, moderator_id: UserID
    topic_id: TopicID, new_category_id: CategoryID, moderator_id: UserID
) -> BoardTopicMoved:
    """Move the topic to another category."""
    topic = _get_topic(topic_id)

    now = datetime.utcnow()

    old_category = topic.category


@@ 191,13 208,21 @@ def move_topic(
    )


def limit_topic_to_announcements(topic: DbTopic) -> None:
def limit_topic_to_announcements(topic_id: TopicID) -> None:
    """Limit posting in the topic to moderators."""
    topic = _get_topic(topic_id)

    topic.posting_limited_to_moderators = True
    db.session.commit()


def remove_limit_of_topic_to_announcements(topic: DbTopic) -> None:
def remove_limit_of_topic_to_announcements(topic_id: TopicID) -> None:
    """Allow non-moderators to post in the topic again."""
    topic = _get_topic(topic_id)

    topic.posting_limited_to_moderators = False
    db.session.commit()


def _get_topic(topic_id: TopicID) -> DbTopic:
    return topic_query_service.get_topic(topic_id)

M byceps/services/board/topic_query_service.py => byceps/services/board/topic_query_service.py +10 -0
@@ 32,6 32,16 @@ def find_topic_by_id(topic_id: TopicID) -> Optional[DbTopic]:
    return DbTopic.query.get(topic_id)


def get_topic(topic_id: TopicID) -> DbTopic:
    """Return the topic with that id."""
    topic = find_topic_by_id(topic_id)

    if topic is None:
        raise ValueError(f'Unknown topic ID "{topic_id}"')

    return topic


def find_topic_visible_for_user(
    topic_id: TopicID, user: CurrentUser
) -> Optional[DbTopic]:

M testfixtures/board.py => testfixtures/board.py +2 -2
@@ 49,12 49,12 @@ def create_topic(category_id, creator_id, *, number=1, title=None, body=None):
    return topic


def create_posting(topic, creator_id, *, number=1, body=None):
def create_posting(topic_id, creator_id, *, number=1, body=None):
    if body is None:
        body = f'Inhalt von Beitrag {number}.'

    posting, event = posting_command_service.create_posting(
        topic, creator_id, body
        topic_id, creator_id, body
    )

    return posting

M tests/blueprints/board/test_topic_hide_unhide.py => tests/blueprints/board/test_topic_hide_unhide.py +1 -1
@@ 36,7 36,7 @@ def test_hide_topic(party_app_with_db, moderator, topic):
def test_unhide_topic(party_app_with_db, moderator, topic):
    topic_before = topic

    board_topic_command_service.hide_topic(topic_before, moderator.id)
    board_topic_command_service.hide_topic(topic_before.id, moderator.id)

    assert_topic_is_hidden(topic_before, moderator.id)


M tests/blueprints/board/test_topic_lock_unlock.py => tests/blueprints/board/test_topic_lock_unlock.py +1 -1
@@ 36,7 36,7 @@ def test_lock_topic(party_app_with_db, moderator, topic):
def test_unlock_topic(party_app_with_db, moderator, topic):
    topic_before = topic

    board_topic_command_service.lock_topic(topic_before, moderator.id)
    board_topic_command_service.lock_topic(topic_before.id, moderator.id)

    assert_topic_is_locked(topic_before, moderator.id)


M tests/blueprints/board/test_topic_pin_unpin.py => tests/blueprints/board/test_topic_pin_unpin.py +1 -1
@@ 35,7 35,7 @@ def test_pin_topic(party_app_with_db, moderator, topic):

def test_unpin_topic(party_app_with_db, moderator, topic):
    topic_before = topic
    board_topic_command_service.pin_topic(topic_before, moderator.id)
    board_topic_command_service.pin_topic(topic_before.id, moderator.id)

    assert_topic_is_pinned(topic_before, moderator.id)