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, Optional 

21from puffotter.flask.base import db 

22from puffotter.flask.db.User import User 

23from puffotter.flask.db.ModelMixin import ModelMixin 

24from otaku_info_web.db.MediaId import MediaId 

25from otaku_info_web.utils.enums import ConsumingState 

26 

27 

28class MediaUserState(ModelMixin, db.Model): 

29 """ 

30 Database model that keeps track of a user's entries on external services 

31 for a media item 

32 """ 

33 

34 __tablename__ = "media_user_states" 

35 """ 

36 The name of the database table 

37 """ 

38 

39 __table_args__ = ( 

40 db.UniqueConstraint( 

41 "media_id_id", 

42 "user_id", 

43 name="unique_media_user_state" 

44 ), 

45 ) 

46 """ 

47 Makes sure that objects that should be unique are unique 

48 """ 

49 

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

51 """ 

52 Initializes the Model 

53 :param args: The constructor arguments 

54 :param kwargs: The constructor keyword arguments 

55 """ 

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

57 

58 media_id_id: int = db.Column( 

59 db.Integer, 

60 db.ForeignKey( 

61 "media_ids.id", ondelete="CASCADE", onupdate="CASCADE" 

62 ), 

63 nullable=False 

64 ) 

65 """ 

66 The ID of the media ID referenced by this user state 

67 """ 

68 

69 media_id: MediaId = db.relationship( 

70 "MediaId", 

71 backref=db.backref( 

72 "media_user_states", lazy=True, cascade="all,delete" 

73 ) 

74 ) 

75 """ 

76 The media ID referenced by this user state 

77 """ 

78 

79 user_id: int = db.Column( 

80 db.Integer, 

81 db.ForeignKey( 

82 "users.id", ondelete="CASCADE", onupdate="CASCADE" 

83 ), 

84 nullable=False 

85 ) 

86 """ 

87 The ID of the user associated with this user state 

88 """ 

89 

90 user: User = db.relationship( 

91 "User", 

92 backref=db.backref( 

93 "media_user_states", lazy=True, cascade="all,delete" 

94 ) 

95 ) 

96 """ 

97 The user associated with this user state 

98 """ 

99 

100 progress: Optional[int] = db.Column(db.Integer, nullable=True) 

101 """ 

102 The user's current progress consuming the media item 

103 """ 

104 

105 score: Optional[int] = db.Column(db.Integer, nullable=True) 

106 """ 

107 The user's score for the references media item 

108 """ 

109 

110 consuming_state: ConsumingState \ 

111 = db.Column(db.Enum(ConsumingState), nullable=False) 

112 """ 

113 The current consuming state of the user for this media item 

114 """ 

115 

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

117 """ 

118 Generates a dictionary containing the information of this model 

119 :param include_children: Specifies if children data models 

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

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

122 """ 

123 data = { 

124 "id": self.id, 

125 "media_id_id": self.media_id_id, 

126 "user_id": self.user_id, 

127 "progress": self.progress, 

128 "score": self.score, 

129 "consuming_state": self.consuming_state.value 

130 } 

131 if include_children: 

132 data["media_id"] = self.media_id.__json__(include_children) 

133 data["user"] = self.user.__json__(include_children) 

134 return data