Test Failed
Push — main ( 2ac75e...34ff35 )
by Jochen
08:37
created

DbSnippetVersion.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
"""
2
byceps.services.snippet.dbmodels
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
Snippets of database-stored content. Can contain HTML and template
6
engine syntax. Can be embedded in other templates or mounted as full
7
page.
8
9
:Copyright: 2014-2022 Jochen Kupperschmidt
10
:License: Revised BSD (see `LICENSE` file for details)
11
"""
12
13
from datetime import datetime
14
15
from sqlalchemy.ext.associationproxy import association_proxy
16
17
from ...database import db, generate_uuid
18
from ...typing import UserID
19
from ...util.instances import ReprBuilder
20
21
from ..user.dbmodels.user import DbUser
22
23
from .transfer.models import Scope
24
25
26
class DbSnippet(db.Model):
27
    """A snippet.
28
29
    Each snippet is expected to have at least one version (the initial
30
    one).
31
    """
32
33
    __tablename__ = 'snippets'
34
    __table_args__ = (
35
        db.Index('ix_snippets_scope', 'scope_type', 'scope_name'),
36
        db.UniqueConstraint('scope_type', 'scope_name', 'name'),
37
    )
38
39
    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
40
    scope_type = db.Column(db.UnicodeText, nullable=False)
41
    scope_name = db.Column(db.UnicodeText, nullable=False)
42
    name = db.Column(db.UnicodeText, index=True, nullable=False)
43
    current_version = association_proxy(
44
        'current_version_association', 'version'
45
    )
46
47
    def __init__(self, scope: Scope, name: str) -> None:
48
        self.scope_type = scope.type_
49
        self.scope_name = scope.name
50
        self.name = name
51
52
    @property
53
    def scope(self) -> Scope:
54
        return Scope(self.scope_type, self.scope_name)
55
56
    def __repr__(self) -> str:
57
        return (
58
            ReprBuilder(self)
59
            .add_with_lookup('id')
60
            .add_with_lookup('scope_type')
61
            .add_with_lookup('scope_name')
62
            .add_with_lookup('name')
63
            .build()
64
        )
65
66
67
class DbSnippetVersion(db.Model):
68
    """A snapshot of a snippet at a certain time."""
69
70
    __tablename__ = 'snippet_versions'
71
72
    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
73
    snippet_id = db.Column(
74
        db.Uuid, db.ForeignKey('snippets.id'), index=True, nullable=False
75
    )
76
    snippet = db.relationship(DbSnippet)
77
    created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
78
    creator_id = db.Column(db.Uuid, db.ForeignKey('users.id'), nullable=False)
79
    creator = db.relationship(DbUser)
80
    body = db.Column(db.UnicodeText, nullable=False)
81
82
    def __init__(
83
        self, snippet: DbSnippet, creator_id: UserID, body: str
84
    ) -> None:
85
        self.snippet = snippet
86
        self.creator_id = creator_id
87
        self.body = body
88
89
    @property
90
    def is_current(self) -> bool:
91
        """Return `True` if this version is the current version of the
92
        snippet it belongs to.
93
        """
94
        return self.id == self.snippet.current_version.id
95
96
    def __repr__(self) -> str:
97
        return (
98
            ReprBuilder(self)
99
            .add_with_lookup('id')
100
            .add_with_lookup('snippet')
101
            .add_with_lookup('created_at')
102
            .build()
103
        )
104
105
106
class DbCurrentSnippetVersionAssociation(db.Model):
107
    __tablename__ = 'snippet_current_versions'
108
109
    snippet_id = db.Column(
110
        db.Uuid, db.ForeignKey('snippets.id'), primary_key=True
111
    )
112
    snippet = db.relationship(
113
        DbSnippet,
114
        backref=db.backref('current_version_association', uselist=False),
115
    )
116
    version_id = db.Column(
117
        db.Uuid,
118
        db.ForeignKey('snippet_versions.id'),
119
        unique=True,
120
        nullable=False,
121
    )
122
    version = db.relationship(DbSnippetVersion)
123
124
    def __init__(self, snippet: DbSnippet, version: DbSnippetVersion) -> None:
125
        self.snippet = snippet
126
        self.version = version
127