Changeset View
Changeset View
Standalone View
Standalone View
profiles/signals.py
- This file was added.
| from typing import Dict | |||||
| import logging | |||||
| from django.contrib.auth.models import User | |||||
| from django.db.models.signals import post_save | |||||
| from django.dispatch import receiver | |||||
| from blender_id_oauth_client import signals as bid_signals | |||||
| from profiles.models import Profile | |||||
| logger = logging.getLogger(__name__) | |||||
| @receiver(bid_signals.user_created) | |||||
| def fill_profile( | |||||
| sender: object, instance: User, oauth_info: Dict[str, str], **kwargs: object | |||||
| ) -> None: | |||||
| """Create a Profile when a new User is created via OAuth""" | |||||
| if not hasattr(instance, 'profile'): | |||||
| logger.warning(f'User {instance.pk} is missing profile') | |||||
sybren: Logging `'User pk={instance.pk} ...` will make it clear what the logged number is. Blender ID… | |||||
| return | |||||
| instance.profile.full_name = oauth_info['full_name'] | |||||
sybrenUnsubmitted Done Inline ActionsUse oauth_info.get('full_name') or '' for safety. Also put the fact that 'full_name' is used in the docstring. sybren: Use `oauth_info.get('full_name') or ''` for safety. Also put the fact that `'full_name'` is… | |||||
| instance.profile.set_avatar() | |||||
sybrenUnsubmitted Done Inline ActionsHow are communication errors handled here? And disk I/O errors saving the avatar? sybren: How are communication errors handled here? And disk I/O errors saving the avatar? | |||||
| @receiver(post_save, sender=User) | |||||
| def create_profile(sender: object, instance: User, created: bool, **kwargs: object) -> None: | |||||
| if not created: | |||||
sybrenUnsubmitted Done Inline ActionsAdd a docstring that explains what this function is for. sybren: Add a docstring that explains what this function is for. | |||||
| return | |||||
| if not hasattr(instance, 'profile'): | |||||
sybrenUnsubmitted Done Inline Actionsif not getattr(instance, 'profile', None) will cover both the case that it just isn't there, or the hypothetical case that the property is there but set to None. sybren: `if not getattr(instance, 'profile', None)` will cover both the case that it just isn't there… | |||||
| logger.info('Creating new Profile for user {instance.pk}') | |||||
| Profile.objects.create(user=instance) | |||||
Logging 'User pk={instance.pk} ... will make it clear what the logged number is. Blender ID account numbers are also numerical.