1
|
|
|
""" |
2
|
|
|
byceps.services.news.dbmodels.image |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2014-2023 Jochen Kupperschmidt |
6
|
|
|
:License: Revised BSD (see `LICENSE` file for details) |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from datetime import datetime |
10
|
1 |
|
from typing import Optional |
11
|
|
|
|
12
|
1 |
|
from ....database import db |
13
|
1 |
|
from ....typing import UserID |
14
|
1 |
|
from ....util.instances import ReprBuilder |
15
|
|
|
|
16
|
1 |
|
from ..transfer.models import NewsImageID, NewsItemID |
17
|
|
|
|
18
|
1 |
|
from .item import DbNewsItem |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
class DbNewsImage(db.Model): |
22
|
|
|
"""An image to illustrate a news item.""" |
23
|
|
|
|
24
|
1 |
|
__tablename__ = 'news_images' |
25
|
1 |
|
__table_args__ = ( |
26
|
|
|
db.UniqueConstraint('item_id', 'number'), |
27
|
|
|
) |
28
|
|
|
|
29
|
1 |
|
id = db.Column(db.Uuid, primary_key=True) |
30
|
1 |
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) |
31
|
1 |
|
creator_id = db.Column(db.Uuid, db.ForeignKey('users.id'), nullable=False) |
32
|
1 |
|
item_id = db.Column( |
33
|
|
|
db.Uuid, db.ForeignKey('news_items.id'), index=True, nullable=False |
34
|
|
|
) |
35
|
1 |
|
item = db.relationship(DbNewsItem, backref='images') |
36
|
1 |
|
number = db.Column(db.Integer, nullable=False) |
37
|
1 |
|
filename = db.Column(db.UnicodeText, nullable=False) |
38
|
1 |
|
alt_text = db.Column(db.UnicodeText, nullable=True) |
39
|
1 |
|
caption = db.Column(db.UnicodeText, nullable=True) |
40
|
1 |
|
attribution = db.Column(db.UnicodeText, nullable=True) |
41
|
|
|
|
42
|
1 |
|
def __init__( |
43
|
|
|
self, |
44
|
|
|
image_id: NewsImageID, |
45
|
|
|
creator_id: UserID, |
46
|
|
|
item_id: NewsItemID, |
47
|
|
|
number: int, |
48
|
|
|
filename: str, |
49
|
|
|
*, |
50
|
|
|
alt_text: Optional[str] = None, |
51
|
|
|
caption: Optional[str] = None, |
52
|
|
|
attribution: Optional[str] = None, |
53
|
|
|
) -> None: |
54
|
|
|
self.id = image_id |
55
|
|
|
self.creator_id = creator_id |
56
|
|
|
self.item_id = item_id |
57
|
|
|
self.number = number |
58
|
|
|
self.filename = filename |
59
|
|
|
self.alt_text = alt_text |
60
|
|
|
self.caption = caption |
61
|
|
|
self.attribution = attribution |
62
|
|
|
|
63
|
1 |
|
def __repr__(self) -> str: |
64
|
|
|
return ( |
65
|
|
|
ReprBuilder(self) |
66
|
|
|
.add_with_lookup('id') |
67
|
|
|
.add_with_lookup('item_id') |
68
|
|
|
.add_with_lookup('number') |
69
|
|
|
.build() |
70
|
|
|
) |
71
|
|
|
|