Changeset View
Changeset View
Standalone View
Standalone View
profiles/signals.py
| from typing import Dict | from typing import Dict | ||||
| import logging | import logging | ||||
| from actstream.models import Action | from actstream.models import Action | ||||
| from django.contrib.auth import get_user_model | from django.contrib.auth import get_user_model | ||||
| from django.db.models.signals import post_save | from django.db.models.signals import post_save, pre_save | ||||
| from django.dispatch import receiver | from django.dispatch import receiver | ||||
| from blender_id_oauth_client import signals as bid_signals | from blender_id_oauth_client import signals as bid_signals | ||||
| from profiles.models import Profile, Notification | from profiles.models import Profile, Notification | ||||
| from profiles.queries import set_groups_from_roles | from profiles.queries import set_groups_from_roles | ||||
| User = get_user_model() | User = get_user_model() | ||||
| Show All 29 Lines | ) -> None: | ||||
| instance.profile.save() | instance.profile.save() | ||||
| group_names = oauth_info.get('roles') or [] | group_names = oauth_info.get('roles') or [] | ||||
| set_groups_from_roles(instance, group_names=group_names) | set_groups_from_roles(instance, group_names=group_names) | ||||
| instance.profile.copy_avatar_from_blender_id() | instance.profile.copy_avatar_from_blender_id() | ||||
| @receiver(pre_save, sender=Profile) | |||||
| def _sync_is_subscribed_to_newsletter(sender: object, instance: Profile, **kwargs): | |||||
| if not instance.pk: | |||||
| return | |||||
| try: | |||||
| obj = sender.objects.get(pk=instance.pk) | |||||
| except sender.DoesNotExist: | |||||
| pass | |||||
| else: | |||||
| if obj.is_subscribed_to_newsletter != instance.is_subscribed_to_newsletter: | |||||
| # State of newsletter subscription has changed | |||||
| tasks.handle_is_subscribed_to_newsletter(pk=instance.pk) | |||||
| @receiver(post_save, sender=User) | @receiver(post_save, sender=User) | ||||
| def create_profile(sender: object, instance: User, created: bool, **kwargs: object) -> None: | def create_profile(sender: object, instance: User, created: bool, **kwargs: object) -> None: | ||||
| """Create a Profile record for a newly created User.""" | """Create a Profile record for a newly created User.""" | ||||
| if not created: | if not created: | ||||
| return | return | ||||
| if not getattr(instance, 'profile', None): | if not getattr(instance, 'profile', None): | ||||
| logger.info(f'Creating new Profile for user pk={instance.pk}') | logger.info(f'Creating new Profile for user pk={instance.pk}') | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | if not users: | ||||
| return | return | ||||
| for user in users: | for user in users: | ||||
| # Don't notify yourself about your own actions: they can be viewed in activity | # Don't notify yourself about your own actions: they can be viewed in activity | ||||
| if user == instance.actor: | if user == instance.actor: | ||||
| continue | continue | ||||
| notification = Notification(user=user, action=instance) | notification = Notification(user=user, action=instance) | ||||
| notification.save() | notification.save() | ||||
| import profiles.tasks as tasks # noqa: E402 | |||||