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 stockstert. 

5 

6stockstert 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 

11stockstert 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 stockstert. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20 

21from typing import Dict, Any 

22from stockstert.flask import db 

23from stockstert.db.ModelMixin import ModelMixin 

24from puffotter.crypto import verify_password 

25 

26 

27class User(ModelMixin, db.Model): 

28 """ 

29 Model that describes the 'users' SQL table 

30 A User stores a user's information, including their email address, username 

31 and password hash 

32 """ 

33 

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

35 """ 

36 Initializes the Model 

37 :param args: The constructor arguments 

38 :param kwargs: The constructor keyword arguments 

39 """ 

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

41 

42 __tablename__ = "users" 

43 """ 

44 The name of the table 

45 """ 

46 

47 username = db.Column(db.String(12), nullable=False, unique=True) 

48 """ 

49 The user's username 

50 """ 

51 

52 email = db.Column(db.String(150), nullable=False, unique=True) 

53 """ 

54 The user's email address 

55 """ 

56 

57 password_hash = db.Column(db.String(255), nullable=False) 

58 """ 

59 The user's hashed password, salted and hashed. 

60 """ 

61 

62 confirmed = db.Column(db.Boolean, nullable=False, default=False) 

63 """ 

64 The account's confirmation status. Logins should be impossible as long as 

65 this value is False. 

66 """ 

67 

68 confirmation_hash = db.Column(db.String(255), nullable=False) 

69 """ 

70 The account's confirmation hash. This is the hash of a key emailed to 

71 the user. Only once the user follows the link in the email containing the 

72 key will their account be activated 

73 """ 

74 

75 @property 

76 def is_authenticated(self) -> bool: 

77 """ 

78 Property required by flask-login 

79 :return: True if the user is confirmed, False otherwise 

80 """ 

81 return True 

82 

83 @property 

84 def is_anonymous(self) -> bool: 

85 """ 

86 Property required by flask-login 

87 :return: True if the user is not confirmed, False otherwise 

88 """ 

89 return not self.is_authenticated # pragma: no cover 

90 

91 @property 

92 def is_active(self) -> bool: 

93 """ 

94 Property required by flask-login 

95 :return: True 

96 """ 

97 return self.confirmed 

98 

99 def get_id(self) -> str: 

100 """ 

101 Method required by flask-login 

102 :return: The user's ID as a unicode string 

103 """ 

104 return str(self.id) 

105 

106 def verify_password(self, password: str) -> bool: 

107 """ 

108 Verifies a password against the password hash 

109 :param password: The password to check 

110 :return: True if the password matches, False otherwise 

111 """ 

112 return verify_password(password, self.password_hash) 

113 

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

115 """ 

116 Generates a dictionary containing the information of this model 

117 :param include_children: Specifies if children data models will be 

118 included or if they're limited to IDs 

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

120 """ 

121 data = { 

122 "id": self.id, 

123 "username": self.username, 

124 "email": self.email, 

125 "confirmed": self.confirmed 

126 } 

127 if include_children: 

128 pass 

129 return data