async def ab(): await tempdb() usermod = await UserMod.get(email=VERIFIED_EMAIL_DEMO).only('id') partialkey = s.CACHE_USERNAME.format(usermod.id) await usermod.get_and_cache(usermod.id) groups = await usermod.get_groups() assert Counter(groups) == Counter(s.USER_GROUPS) cached_groups = UserDBComplete( **cache.restoreuser_dict(red.get(partialkey))).groups assert Counter(groups) == Counter(cached_groups) for i in param: addgroups, out = i newgroups = await usermod.add_group(*listify(addgroups)) if newgroups: assert Counter(newgroups) == Counter(out) updatedgroups = await usermod.get_groups() if updatedgroups: assert Counter(updatedgroups) == Counter(out) cached_groups = userdb.usercomplete( **cache.restoreuser_dict(red.get(partialkey))).groups if cached_groups: assert Counter(cached_groups) == Counter(out)
def test_restoreuser_dict(tempdb, loop): async def ab(): await tempdb() usermod_temp = await UserMod.get(email=VERIFIED_EMAIL_DEMO).only('id') await usermod_temp.get_and_cache(usermod_temp.id, model=True) partialkey = s.CACHE_USERNAME.format(usermod_temp.id) return red.get(partialkey) red_dict = loop.run_until_complete(ab()) for k, v in red_dict.items(): if k in ['is_active', 'is_superuser', 'is_verified']: assert isinstance(v, int) else: assert isinstance(v, str) restored = cache.restoreuser_dict(red_dict) for k, v in restored.items(): if k in ['is_active', 'is_superuser', 'is_verified']: assert isinstance(v, bool) else: if k in ['groups', 'permissions']: assert isinstance(v, list) elif k in ['options']: assert isinstance(v, dict) else: assert isinstance(v, str)
async def update_groups(self, new_groups: list): from app.auth import userdb # new_groups = set(filter(valid_str_only, new_groups)) valid_groups = set(await Group.filter(name__in=new_groups ).values_list('name', flat=True)) if not valid_groups: return existing_groups = set(await self.get_groups()) toadd: set = valid_groups - existing_groups toremove: set = existing_groups - valid_groups if toadd: toadd_obj = await Group.filter(name__in=toadd).only('id', 'name') if toadd_obj: await self.groups.add(*toadd_obj) if toremove: toremove_obj = await Group.filter(name__in=toremove ).only('id', 'name') if toremove_obj: await self.groups.remove(*toremove_obj) partialkey = s.CACHE_USERNAME.format(self.id) if user_dict := red.get(partialkey): user_dict = cache.restoreuser_dict(user_dict) user = userdb.usercomplete(**user_dict)
async def add_group(self, *groups) -> Optional[list]: """ Add groups to a user and update redis :param groups: Groups to add :return: list The user's groups """ from app.auth import userdb groups = list(filter(None, groups)) # groups = list(filter(valid_str_only, groups)) if not groups: return groups = await Group.filter(name__in=groups).only('id', 'name') if not groups: return await self.groups.add(*groups) names = await Group.filter(group_users__id=self.id) \ .values_list('name', flat=True) partialkey = s.CACHE_USERNAME.format(self.id) if user_dict := red.get(partialkey): user_dict = cache.restoreuser_dict(user_dict) user = userdb.usercomplete(**user_dict)
async def ab(): await tempdb() usermod = await UserMod.get(email=VERIFIED_EMAIL_DEMO).only('id') partialkey = s.CACHE_USERNAME.format(usermod.id) red.delete(partialkey) query_data = await usermod.get_and_cache(usermod.id) assert red.exists(partialkey) cache_data = UserDBComplete( **cache.restoreuser_dict(red.get(partialkey))) return query_data, cache_data
async def ab(): await tempdb() for i in param: groups, out = i user = await UserMod.get(email=VERIFIED_EMAIL_DEMO).only('id') partialkey = s.CACHE_USERNAME.format(user.id) queried = await user.get_groups(force_query=True) cached = UserDBComplete( **cache.restoreuser_dict(red.get(partialkey))).groups assert Counter(queried) == Counter(cached) updated = await user.update_groups(groups) if updated: assert Counter(updated) == Counter(out)
async def ab(): await tempdb() usermod = await UserMod.get(email=VERIFIED_EMAIL_DEMO).only('id') if usermod: groups = await usermod.add_group('StaffGroup', 'AdminGroup', 'NoaddGroup') assert Counter(groups) == Counter([ 'StaffGroup', 'AdminGroup', 'NoaddGroup', 'ContentGroup', 'AccountGroup' ]) for i in param: removegroup, out = i afterremove = await usermod.remove_group(*listify(removegroup)) assert Counter(afterremove) == Counter(out) partialkey = s.CACHE_USERNAME.format(usermod.id) cached_groups = userdb.usercomplete( **cache.restoreuser_dict(red.get(partialkey))).groups assert Counter(cached_groups) == Counter(out)
async def get_data(cls, id, force_query=False, debug=False): """ Get the UserDBComplete data whether it be via cache or query. Checks cache first else query. :param force_query: Force use query instead of checking the cache :param id: User id :param debug: Debug data for tests :return: UserDBComplete/tuple or None """ from app.auth import userdb debug = debug if s.DEBUG else False partialkey = s.CACHE_USERNAME.format(id) if not force_query and red.exists(partialkey): source = 'CACHE' user_data = cache.restoreuser_dict(red.get(partialkey)) user = userdb.usercomplete(**user_data) else: source = 'QUERY' user = await UserMod.get_and_cache(id) if debug: return user, source return user
async def get_groups(self, force_query=False, debug=False) -> Union[list, tuple]: """ Return a user's groups as a list from the cache or not. Uses cache else query. :param force_query: Don't use cache :param debug: Return debug data for tests :return: List of groups if not debug """ from app.auth import userdb debug = debug if s.DEBUG else False partialkey = s.CACHE_USERNAME.format(self.id) if not force_query and red.exists(partialkey): user_dict = red.get(partialkey) source = 'CACHE' user_dict = cache.restoreuser_dict(user_dict) user = userdb.usercomplete(**user_dict) else: source = 'QUERY' user = await UserMod.get_and_cache(self.id) if debug: return user.groups, source return user.groups
async def get(self, id: UUID4) -> Optional[UD]: # noqa partialkey = s.CACHE_USERNAME.format(str(id)) if user_dict := red.get(partialkey): # ic('CACHE') user_dict = cache.restoreuser_dict(user_dict)
class UserMod(DTMixin, TortoiseBaseUserModel): username = fields.CharField(max_length=50, null=True) first_name = fields.CharField(max_length=191, default='') middle_name = fields.CharField(max_length=191, default='') last_name = fields.CharField(max_length=191, default='') civil = fields.CharField(max_length=20, default='') bday = fields.DateField(null=True) mobile = fields.CharField(max_length=50, default='') telephone = fields.CharField(max_length=50, default='') avatar = fields.CharField(max_length=191, default='') status = fields.CharField(max_length=20, default='') bio = fields.CharField(max_length=191, default='') address1 = fields.CharField(max_length=191, default='') address2 = fields.CharField(max_length=191, default='') country = fields.CharField(max_length=2, default='') zipcode = fields.CharField(max_length=20, default='') timezone = fields.CharField(max_length=10, default='+00:00') website = fields.CharField(max_length=20, default='') last_login = fields.DatetimeField(null=True) groups = fields.ManyToManyField('models.Group', related_name='group_users', through='auth_user_groups', backward_key='user_id') permissions = fields.ManyToManyField('models.Permission', related_name='permission_users', through='auth_user_permissions', backward_key='user_id') full = Manager() class Meta: table = 'auth_user' manager = ActiveManager() def __str__(self): return modstr(self, 'id') @property def fullname(self): return f'{self.first_name} {self.last_name}'.strip() @property async def display_name(self): if self.username: return self.username elif self.fullname: return self.fullname.split()[0] else: emailname = self.email.split('@')[0] return ' '.join(emailname.split('.')) # @classmethod # def has_perm(cls, id: str, *perms): # partialkey = s.CACHE_USERNAME.format('id') # if red.exists(partialkey): # groups = red.get(partialkey).get('groups') async def to_dict(self, exclude: Optional[List[str]] = None, prefetch=False) -> dict: """ Converts a UserMod instance into UserModComplete. Included fields are based on UserDB + groups, options, and permissions. :param exclude: Fields not to explicitly include :param prefetch: Query used prefetch_related to save on db hits :return: UserDBComplete """ d = {} exclude = ['created_at', 'deleted_at', 'updated_at' ] if exclude is None else exclude for field in self._meta.db_fields: if hasattr(self, field) and field not in exclude: d[field] = getattr(self, field) if field == 'id': d[field] = str(d[field]) if hasattr(self, 'groups'): if prefetch: d['groups'] = [i.name for i in self.groups] else: d['groups'] = await self.groups.all().values_list('name', flat=True) if hasattr(self, 'options'): if prefetch: d['options'] = {i.name: i.value for i in self.options} else: d['options'] = { i.name: i.value for i in await self.options.all().only( 'id', 'name', 'value', 'is_active') if i.is_active } if hasattr(self, 'permissions'): if prefetch: d['permissions'] = [i.code for i in self.permissions] else: d['permissions'] = await self.permissions.all().values_list( 'code', flat=True) # ic(d) return d @classmethod async def get_and_cache(cls, id, model=False): """ Get a user's cachable data and cache it for future use. Replaces data if exists. Similar to the dependency current_user. :param id: User id as str :param model: Also return the UserMod instance :return: DOESN'T NEED cache.restoreuser() since data is from the db not redis. The id key in the hash is already formatted to a str from UUID. Can be None if user doesn't exist. """ from app.auth import userdb query = UserMod.get_or_none(pk=id) \ .prefetch_related( Prefetch('groups', queryset=Group.all().only('id', 'name')), Prefetch('options', queryset=Option.all().only('user_id', 'name', 'value')), Prefetch('permissions', queryset=Permission.filter(deleted_at=None).only('id', 'code')) ) if userdb.oauth_account_model is not None: query = query.prefetch_related("oauth_accounts") usermod = await query.only(*userdb.select_fields) if usermod: user_dict = await usermod.to_dict(prefetch=True) partialkey = s.CACHE_USERNAME.format(id) red.set(partialkey, cache.prepareuser_dict(user_dict), clear=True) if model: return userdb.usercomplete(**user_dict), usermod return userdb.usercomplete(**user_dict) @classmethod async def get_data(cls, id, force_query=False, debug=False): """ Get the UserDBComplete data whether it be via cache or query. Checks cache first else query. :param force_query: Force use query instead of checking the cache :param id: User id :param debug: Debug data for tests :return: UserDBComplete/tuple or None """ from app.auth import userdb debug = debug if s.DEBUG else False partialkey = s.CACHE_USERNAME.format(id) if not force_query and red.exists(partialkey): source = 'CACHE' user_data = cache.restoreuser_dict(red.get(partialkey)) user = userdb.usercomplete(**user_data) else: source = 'QUERY' user = await UserMod.get_and_cache(id) if debug: return user, source return user async def get_permissions(self, perm_type: Optional[str] = None) -> list: """ Collate all the permissions a user has from groups + user :param perm_type: user or group :return: List of permission codes to match data with """ group_perms, user_perms = [], [] groups = await self.get_groups() if perm_type is None or perm_type == 'group': for group in groups: partialkey = s.CACHE_GROUPNAME.format(group) if perms := red.get(partialkey): group_perms += perms else: perms = Group.get_and_cache(group) group_perms += perms red.set(partialkey, perms) if perm_type is None or perm_type == 'user': partialkey = s.CACHE_USERNAME.format(self.id) if user_dict := red.get(partialkey): user_dict = cache.restoreuser_dict(user_dict) user_perms = user_dict.get('permissions') else: user = await UserMod.get_and_cache(self.id) user_perms = user.permissions