Hide keyboard shortcuts

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> 

3 

4This file is part of otaku-info-web. 

5 

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. 

10 

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. 

15 

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""" 

19 

20from typing import Dict, Any 

21from puffotter.flask.base import db 

22from puffotter.flask.db.ModelMixin import ModelMixin 

23from otaku_info_web.db.MediaList import MediaList 

24from otaku_info_web.db.MediaUserState import MediaUserState 

25 

26 

27class MediaListItem(ModelMixin, db.Model): 

28 """ 

29 Database model for media list items. 

30 This model maps MediaLists and MediaUserStates 

31 """ 

32 

33 __tablename__ = "media_list_items" 

34 """ 

35 The name of the database table 

36 """ 

37 

38 __table_args__ = ( 

39 db.UniqueConstraint( 

40 "media_list_id", 

41 "media_user_state_id", 

42 name="unique_media_list_item" 

43 ), 

44 ) 

45 """ 

46 Makes sure that objects that should be unique are unique 

47 """ 

48 

49 def __init__(self, *args, **kwargs): 

50 """ 

51 Initializes the Model 

52 :param args: The constructor arguments 

53 :param kwargs: The constructor keyword arguments 

54 """ 

55 super().__init__(*args, **kwargs) 

56 

57 media_list_id: int = db.Column( 

58 db.Integer, 

59 db.ForeignKey( 

60 "media_lists.id", ondelete="CASCADE", onupdate="CASCADE" 

61 ), 

62 nullable=False 

63 ) 

64 """ 

65 The ID of the media list this list item is a part of 

66 """ 

67 

68 media_list: MediaList = db.relationship( 

69 "MediaList", 

70 backref=db.backref("media_list_items", lazy=True, cascade="all,delete") 

71 ) 

72 """ 

73 The media list this list item is a part of 

74 """ 

75 

76 media_user_state_id: int = db.Column( 

77 db.Integer, 

78 db.ForeignKey( 

79 "media_user_states.id", ondelete="CASCADE", onupdate="CASCADE" 

80 ), 

81 nullable=False 

82 ) 

83 """ 

84 The ID of the media user state this list item references 

85 """ 

86 

87 media_user_state: MediaUserState = db.relationship( 

88 "MediaUserState", 

89 backref=db.backref("media_list_items", lazy=True, cascade="all,delete") 

90 ) 

91 """ 

92 The media user state this list item references 

93 """ 

94 

95 def __json__(self, include_children: bool = False) -> Dict[str, Any]: 

96 """ 

97 Generates a dictionary containing the information of this model 

98 :param include_children: Specifies if children data models 

99 will be included or if they're limited to IDs 

100 :return: A dictionary representing the model's values 

101 """ 

102 data = { 

103 "id": self.id, 

104 "media_list_id": self.media_list_id, 

105 "media_user_state_id": self.media_user_state_id 

106 } 

107 if include_children: 

108 data["media_list"] = self.media_list.__json__(include_children) 

109 data["media_user_state"] = \ 

110 self.media_user_state.__json__(include_children) 

111 return data