1
|
|
|
""" |
2
|
|
|
byceps.services.user_badge.badge_service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2020 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import Optional, Set |
10
|
|
|
|
11
|
1 |
|
from ...database import db |
12
|
1 |
|
from ...typing import BrandID |
13
|
|
|
|
14
|
1 |
|
from .models.badge import Badge as DbBadge |
15
|
1 |
|
from .transfer.models import Badge, BadgeID |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
def create_badge( |
19
|
|
|
slug: str, |
20
|
|
|
label: str, |
21
|
|
|
image_filename: str, |
22
|
|
|
*, |
23
|
|
|
brand_id: Optional[BrandID] = None, |
24
|
|
|
description: Optional[str] = None, |
25
|
|
|
featured: bool = False, |
26
|
|
|
) -> Badge: |
27
|
|
|
"""Create a badge.""" |
28
|
1 |
|
badge = DbBadge( |
29
|
|
|
slug, |
30
|
|
|
label, |
31
|
|
|
image_filename, |
32
|
|
|
brand_id=brand_id, |
33
|
|
|
description=description, |
34
|
|
|
featured=featured, |
35
|
|
|
) |
36
|
|
|
|
37
|
1 |
|
db.session.add(badge) |
38
|
1 |
|
db.session.commit() |
39
|
|
|
|
40
|
1 |
|
return _db_entity_to_badge(badge) |
41
|
|
|
|
42
|
|
|
|
43
|
1 |
|
def update_badge( |
44
|
|
|
badge_id: BadgeID, |
45
|
|
|
brand_id: Optional[BrandID], |
46
|
|
|
slug: str, |
47
|
|
|
label: str, |
48
|
|
|
description: Optional[str], |
49
|
|
|
image_filename: str, |
50
|
|
|
featured: bool, |
51
|
|
|
) -> Badge: |
52
|
|
|
"""Update a badge.""" |
53
|
|
|
badge = DbBadge.query.get(badge_id) |
54
|
|
|
if not badge: |
55
|
|
|
raise ValueError(f'Unknown badge ID: "{badge_id}"') |
56
|
|
|
|
57
|
|
|
badge.brand_id = brand_id |
58
|
|
|
badge.slug = slug |
59
|
|
|
badge.label = label |
60
|
|
|
badge.description = description |
61
|
|
|
badge.image_filename = image_filename |
62
|
|
|
badge.featured = featured |
63
|
|
|
|
64
|
|
|
db.session.commit() |
65
|
|
|
|
66
|
|
|
return _db_entity_to_badge(badge) |
67
|
|
|
|
68
|
|
|
|
69
|
1 |
|
def delete_badge(badge_id: BadgeID) -> None: |
70
|
|
|
"""Delete a badge.""" |
71
|
|
|
db.session.query(DbBadge) \ |
72
|
|
|
.filter_by(id=badge_id) \ |
73
|
|
|
.delete() |
74
|
|
|
|
75
|
|
|
db.session.commit() |
76
|
|
|
|
77
|
|
|
|
78
|
1 |
|
def find_badge(badge_id: BadgeID) -> Optional[Badge]: |
79
|
|
|
"""Return the badge with that id, or `None` if not found.""" |
80
|
1 |
|
badge = DbBadge.query.get(badge_id) |
81
|
|
|
|
82
|
1 |
|
if badge is None: |
83
|
|
|
return None |
84
|
|
|
|
85
|
1 |
|
return _db_entity_to_badge(badge) |
86
|
|
|
|
87
|
|
|
|
88
|
1 |
|
def find_badge_by_slug(slug: str) -> Optional[Badge]: |
89
|
|
|
"""Return the badge with that slug, or `None` if not found.""" |
90
|
1 |
|
badge = DbBadge.query \ |
91
|
|
|
.filter_by(slug=slug) \ |
92
|
|
|
.one_or_none() |
93
|
|
|
|
94
|
1 |
|
if badge is None: |
95
|
|
|
return None |
96
|
|
|
|
97
|
1 |
|
return _db_entity_to_badge(badge) |
98
|
|
|
|
99
|
|
|
|
100
|
1 |
|
def get_badges( |
101
|
|
|
badge_ids: Set[BadgeID], *, featured_only: bool = False |
102
|
|
|
) -> Set[Badge]: |
103
|
|
|
"""Return the badges with those IDs. |
104
|
|
|
|
105
|
|
|
If `featured_only` is `True`, only return featured badges. |
106
|
|
|
""" |
107
|
|
|
if not badge_ids: |
108
|
|
|
return set() |
109
|
|
|
|
110
|
|
|
query = DbBadge.query \ |
111
|
|
|
.filter(DbBadge.id.in_(badge_ids)) |
112
|
|
|
|
113
|
|
|
if featured_only: |
114
|
|
|
query = query.filter_by(featured=True) |
115
|
|
|
|
116
|
|
|
badges = query.all() |
117
|
|
|
|
118
|
|
|
return {_db_entity_to_badge(badge) for badge in badges} |
119
|
|
|
|
120
|
|
|
|
121
|
1 |
|
def get_all_badges() -> Set[Badge]: |
122
|
|
|
"""Return all badges.""" |
123
|
|
|
badges = DbBadge.query.all() |
124
|
|
|
|
125
|
|
|
return {_db_entity_to_badge(badge) for badge in badges} |
126
|
|
|
|
127
|
|
|
|
128
|
1 |
|
def _db_entity_to_badge(entity: DbBadge) -> Badge: |
129
|
1 |
|
image_url_path = f'/data/global/users/badges/{entity.image_filename}' |
130
|
|
|
|
131
|
1 |
|
return Badge( |
132
|
|
|
entity.id, |
133
|
|
|
entity.brand_id, |
134
|
|
|
entity.slug, |
135
|
|
|
entity.label, |
136
|
|
|
entity.description, |
137
|
|
|
image_url_path, |
138
|
|
|
entity.featured, |
139
|
|
|
) |
140
|
|
|
|