~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/snippet/service.py -rw-r--r-- 8.1 KiB
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
byceps.services.snippet.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime
from typing import List, Optional, Sequence, Set, Tuple

from ...database import db
from ...events.snippet import SnippetCreated, SnippetDeleted, SnippetUpdated
from ...typing import UserID

from .models.snippet import (
    CurrentVersionAssociation as DbCurrentVersionAssociation,
    Snippet as DbSnippet,
    SnippetVersion as DbSnippetVersion,
)
from .transfer.models import Scope, SnippetID, SnippetType, SnippetVersionID


# -------------------------------------------------------------------- #
# document


def create_document(
    scope: Scope,
    name: str,
    creator_id: UserID,
    title: str,
    body: str,
    *,
    head: Optional[str] = None,
    image_url_path: Optional[str] = None,
) -> Tuple[DbSnippetVersion, SnippetCreated]:
    """Create a document and its initial version, and return that version."""
    return _create_snippet(
        scope,
        name,
        SnippetType.document,
        creator_id,
        body,
        title=title,
        head=head,
        image_url_path=image_url_path,
    )


def update_document(
    snippet_id: SnippetID,
    creator_id: UserID,
    title: str,
    body: str,
    *,
    head: Optional[str] = None,
    image_url_path: Optional[str] = None,
) -> Tuple[DbSnippetVersion, SnippetUpdated]:
    """Update document with a new version, and return that version."""
    return _update_snippet(
        snippet_id, creator_id, title, head, body, image_url_path
    )


# -------------------------------------------------------------------- #
# fragment


def create_fragment(
    scope: Scope, name: str, creator_id: UserID, body: str
) -> Tuple[DbSnippetVersion, SnippetCreated]:
    """Create a fragment and its initial version, and return that version."""
    return _create_snippet(scope, name, SnippetType.fragment, creator_id, body)


def update_fragment(
    snippet_id: SnippetID, creator_id: UserID, body: str
) -> Tuple[DbSnippetVersion, SnippetUpdated]:
    """Update fragment with a new version, and return that version."""
    title = None
    head = None
    image_url_path = None

    return _update_snippet(
        snippet_id, creator_id, title, head, body, image_url_path
    )


# -------------------------------------------------------------------- #
# snippet


def _create_snippet(
    scope: Scope,
    name: str,
    type_: SnippetType,
    creator_id: UserID,
    body: str,
    *,
    title: Optional[str] = None,
    head: Optional[str] = None,
    image_url_path: Optional[str] = None,
) -> Tuple[DbSnippetVersion, SnippetCreated]:
    """Create a snippet and its initial version, and return that version."""
    snippet = DbSnippet(scope, name, type_)
    db.session.add(snippet)

    version = DbSnippetVersion(
        snippet, creator_id, title, head, body, image_url_path
    )
    db.session.add(version)

    current_version_association = DbCurrentVersionAssociation(snippet, version)
    db.session.add(current_version_association)

    db.session.commit()

    event = SnippetCreated(
        occurred_at=version.created_at,
        initiator_id=creator_id,
        snippet_id=snippet.id,
        scope=snippet.scope,
        snippet_name=snippet.name,
        snippet_type=snippet.type_,
        snippet_version_id=version.id,
    )

    return version, event


def _update_snippet(
    snippet_id: SnippetID,
    creator_id: UserID,
    title: Optional[str],
    head: Optional[str],
    body: str,
    image_url_path: Optional[str],
) -> Tuple[DbSnippetVersion, SnippetUpdated]:
    """Update snippet with a new version, and return that version."""
    snippet = find_snippet(snippet_id)
    if snippet is None:
        raise ValueError('Unknown snippet ID')

    version = DbSnippetVersion(
        snippet, creator_id, title, head, body, image_url_path
    )
    db.session.add(version)

    snippet.current_version = version

    db.session.commit()

    event = SnippetUpdated(
        occurred_at=version.created_at,
        initiator_id=creator_id,
        snippet_id=snippet.id,
        scope=snippet.scope,
        snippet_name=snippet.name,
        snippet_type=snippet.type_,
        snippet_version_id=version.id,
    )

    return version, event


def delete_snippet(
    snippet_id: SnippetID, *, initiator_id: Optional[UserID] = None
) -> Tuple[bool, Optional[SnippetDeleted]]:
    """Delete the snippet and its versions.

    It is expected that no database records (mountpoints, consents,
    etc.) refer to the snippet anymore.

    Return `True` on success, or `False` if an error occurred.
    """
    snippet = find_snippet(snippet_id)
    if snippet is None:
        raise ValueError('Unknown snippet ID')

    # Keep values for use after snippet is deleted.
    snippet_name = snippet.name
    scope = snippet.scope

    db.session.delete(snippet.current_version_association)

    versions = get_versions(snippet_id)
    for version in versions:
        db.session.delete(version)

    db.session.delete(snippet)

    try:
        db.session.commit()
    except Exception:
        db.session.rollback()
        return False, None

    event = SnippetDeleted(
        occurred_at=datetime.utcnow(),
        initiator_id=initiator_id,
        snippet_id=snippet_id,
        scope=scope,
        snippet_name=snippet_name,
        snippet_type=snippet.type_,
    )

    return True, event


def find_snippet(snippet_id: SnippetID) -> Optional[DbSnippet]:
    """Return the snippet with that id, or `None` if not found."""
    return DbSnippet.query.get(snippet_id)


def get_snippets(snippet_ids: Set[SnippetID]) -> Sequence[DbSnippet]:
    """Return these snippets."""
    return DbSnippet.query \
        .filter(DbSnippet.id.in_(snippet_ids)) \
        .all()


def get_snippets_for_scope_with_current_versions(
    scope: Scope,
) -> Sequence[DbSnippet]:
    """Return all snippets with their current versions for that scope."""
    return DbSnippet.query \
        .filter_by(scope_type=scope.type_) \
        .filter_by(scope_name=scope.name) \
        .options(
            db.joinedload('current_version_association').joinedload('version')
        ) \
        .all()


def find_snippet_version(
    version_id: SnippetVersionID,
) -> Optional[DbSnippetVersion]:
    """Return the snippet version with that id, or `None` if not found."""
    return DbSnippetVersion.query.get(version_id)


def find_current_version_of_snippet_with_name(
    scope: Scope, name: str
) -> DbSnippetVersion:
    """Return the current version of the snippet with that name in that
    scope, or `None` if not found.
    """
    return DbSnippetVersion.query \
        .join(DbCurrentVersionAssociation) \
        .join(DbSnippet) \
            .filter(DbSnippet.scope_type == scope.type_) \
            .filter(DbSnippet.scope_name == scope.name) \
            .filter(DbSnippet.name == name) \
        .one_or_none()


def get_versions(snippet_id: SnippetID) -> Sequence[DbSnippetVersion]:
    """Return all versions of that snippet, sorted from most recent to
    oldest.
    """
    return DbSnippetVersion.query \
        .filter_by(snippet_id=snippet_id) \
        .latest_first() \
        .all()


def search_snippets(
    search_term: str, scope: Optional[Scope]
) -> List[DbSnippetVersion]:
    """Search in (the latest versions of) snippets."""
    q = DbSnippetVersion.query \
        .join(DbCurrentVersionAssociation) \
        .join(DbSnippet)

    if scope is not None:
        q = q \
            .filter(DbSnippet.scope_type == scope.type_) \
            .filter(DbSnippet.scope_name == scope.name)

    return q \
            .filter(
                db.or_(
                    DbSnippetVersion.title.contains(search_term),
                    DbSnippetVersion.head.contains(search_term),
                    DbSnippetVersion.body.contains(search_term),
                    DbSnippetVersion.image_url_path.contains(search_term),
                )
            ) \
        .all()


class SnippetNotFound(Exception):

    def __init__(self, scope: Scope, name: str) -> None:
        self.scope = scope
        self.name = name