Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info-web.
6otaku-info-web is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11otaku-info-web is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with otaku-info-web. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from flask import url_for
21from typing import Dict, Any
22from puffotter.flask.base import db
23from puffotter.flask.db.ModelMixin import ModelMixin
24from otaku_info_web.utils.enums import ListService
25from otaku_info_web.utils.db_model_helper import build_service_url
26from otaku_info_web.db.MediaItem import MediaItem
29class MediaId(ModelMixin, db.Model):
30 """
31 Database model for media IDs.
32 These are used to map media items to their corresponding external
33 IDS on external sites.
34 """
36 __tablename__ = "media_ids"
37 """
38 The name of the database table
39 """
41 __table_args__ = (
42 db.UniqueConstraint(
43 "media_item_id",
44 "service",
45 name="unique_media_id"
46 ),
47 )
48 """
49 Makes sure that objects that should be unique are unique
50 """
52 def __init__(self, *args, **kwargs):
53 """
54 Initializes the Model
55 :param args: The constructor arguments
56 :param kwargs: The constructor keyword arguments
57 """
58 super().__init__(*args, **kwargs)
60 media_item_id: int = db.Column(
61 db.Integer,
62 db.ForeignKey(
63 "media_items.id", ondelete="CASCADE", onupdate="CASCADE"
64 ),
65 nullable=False
66 )
67 """
68 The ID of the media item referenced by this ID
69 """
71 media_item: MediaItem = db.relationship(
72 "MediaItem",
73 backref=db.backref("media_ids", lazy=True, cascade="all,delete")
74 )
75 """
76 The media item referenced by this ID
77 """
79 service_id: str = db.Column(db.String(255), nullable=False)
80 """
81 The ID of the media item on the external service
82 """
84 service: ListService = db.Column(db.Enum(ListService), nullable=False)
85 """
86 The service for which this object represents an ID
87 """
89 @property
90 def service_url(self) -> str:
91 """
92 :return: The URL to the series for the given service
93 """
94 return build_service_url(
95 self.media_item.media_type, self.service, self.service_id
96 )
98 @property
99 def service_icon(self) -> str:
100 """
101 :return: The path to the service's icon file
102 """
103 return url_for(
104 "static", filename="service_logos/" + self.service.value + ".png"
105 )
107 def __json__(self, include_children: bool = False) -> Dict[str, Any]:
108 """
109 Generates a dictionary containing the information of this model
110 :param include_children: Specifies if children data models
111 will be included or if they're limited to IDs
112 :return: A dictionary representing the model's values
113 """
114 data = {
115 "id": self.id,
116 "media_item_id": self.media_item_id,
117 "service_id": self.service_id,
118 "service": self.service.value
119 }
120 if include_children:
121 data["media_item"] = self.media_item.__json__(include_children)
122 return data