Changeset View
Standalone View
profiles/queries.py
- This file was added.
| from blender_id_oauth_client.models import OAuthUserInfo, OAuthToken | |||||
| def get_oauth_user_info(oauth_user_id: str) -> OAuthUserInfo: | |||||
sybren: This module should have a docstring that explains what it's for. What kind of code is expected… | |||||
| return OAuthUserInfo.objects.get(oauth_user_id=oauth_user_id) | |||||
Not Done Inline ActionsThis is an indication that this functionality should actually be in a custom model manager of OAuthUserInfo. sybren: This is an indication that this functionality should actually be in a custom model manager of… | |||||
Done Inline ActionsI think a lot of what's implemented here should be part of blender_id_oauth_client (@Francesco Siddi (fsiddi) disagrees afaik), but I'd like to avoid changing it now, and instead implement profiles in a way that this functionality could be "hotswapped" later if it gets implemented in blender_id_oauth_client. railla: I think a lot of what's implemented here should be part of `blender_id_oauth_client` (@fsiddi… | |||||
Not Done Inline Actions👍 sybren: :+1: | |||||
| def get_oauth_token(oauth_user_id) -> OAuthToken: | |||||
| return OAuthToken.objects.filter(oauth_user_id=oauth_user_id).last() | |||||
Not Done Inline ActionsThis is of quadratic complexity (len(group_names) × len(current_groups)). How about something like this? def set_groups(user: User, group_names: Set[str]) -> None:
current_group_names = {group.name for group in current_groups}
names_to_add_to = group_names - current_group_names
groups_to_add_to = [Group.objects.get_or_create(name=group_name) for group_name in names_to_add_to]
user.groups.add(*groups_to_add_to)
names_to_remove_from = current_group_names - group_names
groups_to_remove_from = [
group := Group.objects.filter(name=group_name).first
for group_name in names_to_remove_from
if group is not None]
user.groups.remove(*groups_to_remove_from)Disclaimer: I haven't tested this code ;-) sybren: This is of quadratic complexity (`len(group_names)` × `len(current_groups)`). How about… | |||||
Done Inline ActionsYes, it does indeed look more readable with sets. Updated the code, with few adjustments: get_or_create doesn't "fit" into list comprehension and an extra Group.object.filter query can be avoided. railla: Yes, it does indeed look more readable with `set`s.
Updated the code, with few adjustments… | |||||
This module should have a docstring that explains what it's for. What kind of code is expected to call these functions?