Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/users/users.py: 90%

125 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 03:04 +0000

1import enum 

2from datetime import datetime 

3from typing import TYPE_CHECKING, Optional 

4 

5from pydantic import ConfigDict 

6from sqlalchemy import Boolean, Enum, ForeignKey, Integer, String, orm, select 

7from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy 

8from sqlalchemy.ext.hybrid import hybrid_property 

9from sqlalchemy.orm import Mapped, Session, mapped_column 

10 

11from mealie.core.config import get_app_settings 

12from mealie.db.models._model_utils.auto_init import auto_init 

13from mealie.db.models._model_utils.datetime import NaiveDateTime 

14from mealie.db.models._model_utils.guid import GUID 

15 

16from .._model_base import BaseMixins, SqlAlchemyBase 

17from .user_to_recipe import UserToRecipe 

18 

19if TYPE_CHECKING: 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true

20 from ..group import Group 

21 from ..household import Household 

22 from ..household.mealplan import GroupMealPlan 

23 from ..household.shopping_list import ShoppingList 

24 from ..recipe import RecipeComment, RecipeModel, RecipeTimelineEvent 

25 from .password_reset import PasswordResetModel 

26 

27 

28class LongLiveToken(SqlAlchemyBase, BaseMixins): 

29 __tablename__ = "long_live_tokens" 

30 name: Mapped[str] = mapped_column(String, nullable=False) 

31 token: Mapped[str] = mapped_column(String, nullable=False, index=True) 

32 

33 user_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("users.id"), index=True) 

34 user: Mapped[Optional["User"]] = orm.relationship("User") 

35 

36 group_id: AssociationProxy[GUID] = association_proxy("user", "group_id") 

37 household_id: AssociationProxy[GUID] = association_proxy("user", "household_id") 

38 

39 def __init__(self, name, token, user_id, **_) -> None: 

40 self.name = name 

41 self.token = token 

42 self.user_id = user_id 

43 

44 

45class AuthMethod(enum.Enum): 

46 MEALIE = "Mealie" 

47 LDAP = "LDAP" 

48 OIDC = "OIDC" 

49 

50 

51class User(SqlAlchemyBase, BaseMixins): 

52 __tablename__ = "users" 

53 id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate) 

54 full_name: Mapped[str | None] = mapped_column(String, index=True) 

55 username: Mapped[str | None] = mapped_column(String, index=True, unique=True) 

56 email: Mapped[str | None] = mapped_column(String, unique=True, index=True) 

57 password: Mapped[str | None] = mapped_column(String) 

58 auth_method: Mapped[Enum[AuthMethod]] = mapped_column(Enum(AuthMethod), default=AuthMethod.MEALIE) 

59 admin: Mapped[bool | None] = mapped_column(Boolean, default=False) 

60 advanced: Mapped[bool | None] = mapped_column(Boolean, default=False) 

61 

62 group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False, index=True) 

63 group: Mapped["Group"] = orm.relationship("Group", back_populates="users") 

64 household_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("households.id"), nullable=True, index=True) 

65 household: Mapped["Household"] = orm.relationship("Household", back_populates="users") 

66 

67 cache_key: Mapped[str | None] = mapped_column(String, default="1234") 

68 login_attemps: Mapped[int | None] = mapped_column(Integer, default=0) 

69 locked_at: Mapped[datetime | None] = mapped_column(NaiveDateTime, default=None) 

70 

71 # Group Permissions 

72 can_manage_household: Mapped[bool | None] = mapped_column(Boolean, default=False) 

73 can_manage: Mapped[bool | None] = mapped_column(Boolean, default=False) 

74 can_invite: Mapped[bool | None] = mapped_column(Boolean, default=False) 

75 can_organize: Mapped[bool | None] = mapped_column(Boolean, default=False) 

76 

77 sp_args = { 

78 "back_populates": "user", 

79 "cascade": "all, delete, delete-orphan", 

80 "single_parent": True, 

81 } 

82 

83 tokens: Mapped[list[LongLiveToken]] = orm.relationship(LongLiveToken, **sp_args) 

84 comments: Mapped[list["RecipeComment"]] = orm.relationship("RecipeComment", **sp_args) 

85 recipe_timeline_events: Mapped[list["RecipeTimelineEvent"]] = orm.relationship("RecipeTimelineEvent", **sp_args) 

86 password_reset_tokens: Mapped[list["PasswordResetModel"]] = orm.relationship("PasswordResetModel", **sp_args) 

87 

88 owned_recipes_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipes.id")) 

89 owned_recipes: Mapped[Optional["RecipeModel"]] = orm.relationship( 

90 "RecipeModel", single_parent=True, foreign_keys=[owned_recipes_id] 

91 ) 

92 mealplans: Mapped[Optional["GroupMealPlan"]] = orm.relationship( 

93 "GroupMealPlan", order_by="GroupMealPlan.date", **sp_args 

94 ) 

95 shopping_lists: Mapped[Optional["ShoppingList"]] = orm.relationship("ShoppingList", **sp_args) 

96 rated_recipes: Mapped[list["RecipeModel"]] = orm.relationship( 

97 "RecipeModel", 

98 secondary=UserToRecipe.__tablename__, 

99 back_populates="rated_by", 

100 overlaps="recipe,favorited_by,favorited_recipes", 

101 ) 

102 favorite_recipes: Mapped[list["RecipeModel"]] = orm.relationship( 

103 "RecipeModel", 

104 secondary=UserToRecipe.__tablename__, 

105 primaryjoin="and_(User.id==UserToRecipe.user_id, UserToRecipe.is_favorite==True)", 

106 back_populates="favorited_by", 

107 overlaps="recipe,rated_by,rated_recipes", 

108 ) 

109 model_config = ConfigDict( 

110 exclude={ 

111 "password", 

112 "admin", 

113 "can_manage_household", 

114 "can_manage", 

115 "can_invite", 

116 "can_organize", 

117 "group", 

118 "household", 

119 } 

120 ) 

121 

122 @hybrid_property 

123 def group_slug(self) -> str: 

124 return self.group.slug 

125 

126 @hybrid_property 

127 def household_slug(self) -> str: 

128 return self.household.slug 

129 

130 @auto_init() 

131 def __init__( 

132 self, session: Session, full_name, password, group: str | None = None, household: str | None = None, **kwargs 

133 ) -> None: 

134 if group is None or household is None: 

135 settings = get_app_settings() 

136 group = group or settings.DEFAULT_GROUP 

137 household = household or settings.DEFAULT_HOUSEHOLD 

138 

139 from mealie.db.models.group import Group 

140 from mealie.db.models.household import Household 

141 

142 self.group = session.execute(select(Group).filter(Group.name == group)).scalars().one_or_none() 

143 if self.group: 

144 self.household = ( 

145 session.execute( 

146 select(Household).filter(Household.name == household, Household.group_id == self.group.id) 

147 ) 

148 .scalars() 

149 .one_or_none() 

150 ) 

151 else: 

152 self.household = None 

153 

154 if self.group is None: 

155 raise ValueError(f"Group {group} does not exist; cannot create user") 

156 if self.household is None: 

157 raise ValueError( 

158 f'Household "{household}" does not exist on group ' 

159 f'"{self.group.name}" ({self.group.id}); cannot create user' 

160 ) 

161 

162 self.rated_recipes = [] 

163 

164 self.password = password 

165 

166 if self.username is None: 166 ↛ 167line 166 didn't jump to line 167 because the condition on line 166 was never true

167 self.username = full_name 

168 

169 self._set_permissions(**kwargs) 

170 

171 @auto_init() 

172 def update(self, session: Session, full_name, email, group, household, username, **kwargs): 

173 self.username = username 

174 self.full_name = full_name 

175 self.email = email 

176 

177 from mealie.db.models.group import Group 

178 from mealie.db.models.household import Household 

179 

180 self.group = session.execute(select(Group).filter(Group.name == group)).scalars().one_or_none() 

181 if self.group: 181 ↛ 190line 181 didn't jump to line 190 because the condition on line 181 was always true

182 self.household = ( 

183 session.execute( 

184 select(Household).filter(Household.name == household, Household.group_id == self.group.id) 

185 ) 

186 .scalars() 

187 .one_or_none() 

188 ) 

189 else: 

190 self.household = None 

191 

192 if self.username is None: 192 ↛ 193line 192 didn't jump to line 193 because the condition on line 192 was never true

193 self.username = full_name 

194 

195 self._set_permissions(**kwargs) 

196 

197 def update_password(self, password): 

198 self.password = password 

199 

200 def _set_permissions( 

201 self, admin, can_manage_household=False, can_manage=False, can_invite=False, can_organize=False, **_ 

202 ): 

203 """Set user permissions based on the admin flag and the passed in kwargs 

204 

205 Args: 

206 admin (bool): 

207 can_manage_household (bool): 

208 can_manage (bool): 

209 can_invite (bool): 

210 can_organize (bool): 

211 """ 

212 self.admin = admin 

213 if self.admin: 

214 self.can_manage_household = True 

215 self.can_manage = True 

216 self.can_invite = True 

217 self.can_organize = True 

218 self.advanced = True 

219 else: 

220 self.can_manage_household = can_manage_household 

221 self.can_manage = can_manage 

222 self.can_invite = can_invite 

223 self.can_organize = can_organize