Changeset View
Changeset View
Standalone View
Standalone View
profiles/queries.py
| from typing import List, Set, Union | from typing import List, Set, Union | ||||
| import logging | import logging | ||||
| import re | |||||
| from django.contrib.auth.models import User, Group | from django.contrib.auth.models import User, Group | ||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
| re_cloud_role_name_cleanup = re.compile('^cloud_') | |||||
| def clean_role_names(names: Union[List[str], Set[str]]) -> Set[str]: | |||||
| """Remove Blender Cloud prefixes from given Blender ID roles. | |||||
| Blender Cloud strips "cloud_" prefix from the role names before storing them, | |||||
| so to keep group/role naming consistent, Blender Studio shall do the same. | |||||
| """ | |||||
| return {re_cloud_role_name_cleanup.sub('', name) for name in names} | |||||
| def set_groups(user: User, group_names: Union[List[str], Set[str]]) -> None: | def set_groups(user: User, group_names: Union[List[str], Set[str]]) -> None: | ||||
| """Set user groups to match the given list of `group_names`. | """Set user groups to match the given list of `group_names`. | ||||
| If a group with a particular name doesn't exist, create one. | If a group with a particular name doesn't exist, create one. | ||||
| """ | """ | ||||
sybren: This comment doesn't quite make sense. To me it reads like "Keep role names the same, which… | |||||
| group_names = set(group_names) | group_names = clean_role_names(group_names) | ||||
| current_groups = user.groups.all() | current_groups = user.groups.all() | ||||
Done Inline ActionsThere is no need to use sub-expressions here. This should do the same: re.sub(r'^cloud_', '', name) sybren: There is no need to use sub-expressions here. This should do the same:
```
re.sub(r'^cloud_'… | |||||
Done Inline ActionsPrecompile the regular expression. Right now it's compiled for every iteration of the loop. Having a global declaration like this would help: _cloud_role_name_cleanup_re = re.compile('^cloud_')sybren: Precompile the regular expression. Right now it's compiled for every iteration of the loop. | |||||
Done Inline ActionsYou can also use a set comprehension here: return {_cloud_role_name_cleanup_re.sub('', name) for name in names}This is slightly more efficient than creating a generator expression and passing that to the set() function. sybren: You can also use a set comprehension here:
```
return {_cloud_role_name_cleanup_re.sub(''… | |||||
| current_group_names = {group.name for group in current_groups} | current_group_names = {group.name for group in current_groups} | ||||
| names_to_add_to = group_names - current_group_names | names_to_add_to = group_names - current_group_names | ||||
| # Look up all groups this user is now being added to and make sure they actually exist | # Look up all groups this user is now being added to and make sure they actually exist | ||||
| groups_to_add_to = [] | groups_to_add_to = [] | ||||
| for group_name in names_to_add_to: | for group_name in names_to_add_to: | ||||
| group, _ = Group.objects.get_or_create(name=group_name) | group, _ = Group.objects.get_or_create(name=group_name) | ||||
| groups_to_add_to.append(group) | groups_to_add_to.append(group) | ||||
| Show All 13 Lines | |||||
This comment doesn't quite make sense. To me it reads like "Keep role names the same, which means changing the role names".