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.User import User 

23from puffotter.flask.db.ModelMixin import ModelMixin 

24from otaku_info_web.utils.enums import ListService, MediaType 

25 

26 

27class MediaList(ModelMixin, db.Model): 

28 """ 

29 Database model for user-specific media lists. 

30 """ 

31 

32 __tablename__ = "media_lists" 

33 """ 

34 The name of the database table 

35 """ 

36 

37 __table_args__ = ( 

38 db.UniqueConstraint( 

39 "name", 

40 "user_id", 

41 "service", 

42 "media_type", 

43 name="unique_media_list" 

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 name: str = db.Column(db.Unicode(255), nullable=False) 

59 """ 

60 The name of this list 

61 """ 

62 

63 user_id: int = db.Column( 

64 db.Integer, 

65 db.ForeignKey( 

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

67 ), 

68 nullable=False 

69 ) 

70 """ 

71 The ID of the user associated with this list 

72 """ 

73 

74 user: User = db.relationship( 

75 "User", 

76 backref=db.backref("media_lists", lazy=True, cascade="all,delete") 

77 ) 

78 """ 

79 The user associated with this list 

80 """ 

81 

82 service: ListService = db.Column(db.Enum(ListService), nullable=False) 

83 """ 

84 The service for which this list applies to 

85 """ 

86 

87 media_type: MediaType = db.Column(db.Enum(MediaType), nullable=False) 

88 """ 

89 The media type for this list 

90 """ 

91 

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

93 """ 

94 Generates a dictionary containing the information of this model 

95 :param include_children: Specifies if children data models 

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

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

98 """ 

99 data = { 

100 "id": self.id, 

101 "name": self.name, 

102 "user_id": self.user_id, 

103 "service": self.service.value, 

104 "media_type": self.media_type.value 

105 } 

106 if include_children: 

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

108 return data