Passed
Push — master ( 6fa740...307301 )
by Jochen
02:39
created

byceps.services.tourney.match_comment_service   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 14

8 Functions

Rating   Name   Duplication   Size   Complexity  
A get_comment() 0 11 2
A find_comment() 0 3 1
B get_comments() 0 39 5
A create_comment() 0 10 1
A _get_users_by_id() 0 8 1
A update_comment() 0 13 1
A hide_comment() 0 9 1
A unhide_comment() 0 9 1
1
"""
2
byceps.services.tourney.match_comment_service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2019 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9
from datetime import datetime
10
from typing import Dict, Optional, Sequence, Set
11
12
from ...database import db
13
from ...services.text_markup import service as text_markup_service
14
from ...services.user import service as user_service
15
from ...services.user.transfer.models import User
16
from ...typing import PartyID, UserID
17
18
from .models.match_comment import MatchComment as DbMatchComment
19
from .transfer.models import MatchID, MatchCommentID
20
21
22
def find_comment(comment_id: MatchCommentID) -> DbMatchComment:
23
    """Return the comment, or `None` if not found."""
24
    return DbMatchComment.query.get(comment_id)
25
26
27
def get_comment(comment_id: MatchCommentID) -> DbMatchComment:
28
    """Return the comment.
29
30
    Raise exception if comment ID is unknown.
31
    """
32
    comment = find_comment(comment_id)
33
34
    if comment is None:
35
        raise ValueError('Unknown match comment ID')
36
37
    return comment
38
39
40
def get_comments(
41
    match_id: MatchID,
42
    *,
43
    party_id: Optional[PartyID] = None,
44
    include_hidden: bool = False,
45
) -> Sequence[DbMatchComment]:
46
    """Return comments on the match, ordered chronologically."""
47
    query = DbMatchComment.query \
48
        .for_match(match_id)
49
50
    if not include_hidden:
51
        query = query.filter_by(hidden=False)
52
53
    comments = query \
54
        .for_match(match_id) \
55
        .order_by(DbMatchComment.created_at) \
56
        .all()
57
58
    # Add creator objects.
59
    creator_ids = {comment.created_by_id for comment in comments}
60
    creators_by_id = _get_users_by_id(creator_ids, party_id=party_id)
61
    for comment in comments:
62
        comment.creator = creators_by_id[comment.created_by_id]
63
64
    # Add last editor objects.
65
    last_editor_ids = {
66
        comment.last_edited_by_id
67
        for comment in comments
68
        if comment.last_edited_by_id
69
    }
70
    last_editors_by_id = _get_users_by_id(last_editor_ids, party_id=party_id)
71
    for comment in comments:
72
        comment.last_editor = last_editors_by_id.get(comment.last_edited_by_id)
73
74
    # Add rendered bodies.
75
    for comment in comments:
76
        comment.body_rendered = text_markup_service.render_html(comment.body)
77
78
    return comments
79
80
81
def _get_users_by_id(
82
    user_ids: Set[UserID], *, party_id: Optional[PartyID] = None
83
) -> Dict[UserID, User]:
84
    users = user_service.find_users(
85
        user_ids, include_avatars=True, include_orga_flags_for_party_id=party_id
86
    )
87
88
    return user_service.index_users_by_id(users)
89
90
91
def find_comment(comment_id: MatchCommentID) -> DbMatchComment:
92
    """Return match comment."""
93
    return DbMatchComment.query.get(comment_id)
94
95
96
def create_comment(
97
    match_id: MatchID, creator_id: UserID, body: str
98
) -> DbMatchComment:
99
    """Create a comment on a match."""
100
    comment = DbMatchComment(match_id, creator_id, body)
101
102
    db.session.add(comment)
103
    db.session.commit()
104
105
    return comment
106
107
108
def update_comment(
109
    comment_id: MatchCommentID, editor_id: UserID, body: str
110
) -> DbMatchComment:
111
    """Update a comment on a match."""
112
    comment = get_comment(comment_id)
113
114
    comment.body = body
115
    comment.last_edited_at = datetime.utcnow()
116
    comment.last_edited_by_id = editor_id
117
118
    db.session.commit()
119
120
    return comment
121
122
123
def hide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
124
    """Hide the match comment."""
125
    comment = get_comment(comment_id)
126
127
    comment.hidden = True
128
    comment.hidden_at = datetime.utcnow()
129
    comment.hidden_by_id = initiator_id
130
131
    db.session.commit()
132
133
134
def unhide_comment(comment_id: MatchCommentID, initiator_id: UserID) -> None:
135
    """Un-hide the match comment."""
136
    comment = get_comment(comment_id)
137
138
    comment.hidden = False
139
    comment.hidden_at = None
140
    comment.hidden_by_id = None
141
142
    db.session.commit()
143