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 

25 

26 

27class ServiceUsername(ModelMixin, db.Model): 

28 """ 

29 Database model that stores an external service username for a user 

30 """ 

31 

32 __tablename__ = "service_usernames" 

33 """ 

34 The name of the database table 

35 """ 

36 

37 __table_args__ = ( 

38 db.UniqueConstraint( 

39 "user_id", 

40 "username", 

41 "service", 

42 name="unique_service_username" 

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 user_id: int = db.Column( 

58 db.Integer, 

59 db.ForeignKey( 

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

61 ), 

62 nullable=False 

63 ) 

64 """ 

65 The ID of the user associated with this service username 

66 """ 

67 

68 user: User = db.relationship( 

69 "User", 

70 backref=db.backref( 

71 "service_usernames", lazy=True, cascade="all,delete" 

72 ) 

73 ) 

74 """ 

75 The user associated with this service username 

76 """ 

77 

78 username: str = db.Column(db.String(255), nullable=False) 

79 """ 

80 The service username 

81 """ 

82 

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

84 """ 

85 The external service this item is a username for 

86 """ 

87 

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

89 """ 

90 Generates a dictionary containing the information of this model 

91 :param include_children: Specifies if children data models 

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

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

94 """ 

95 data = { 

96 "id": self.id, 

97 "user_id": self.user_id, 

98 "username": self.username, 

99 "service": self.service.value 

100 } 

101 if include_children: 

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

103 return data