"""Module containing core app signals."""
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in
from django.db.models.signals import post_save
from django.dispatch import receiver
from core.models import Profile
[docs]
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
"""Create profile instance if user is successfully created.
Method is called by signal sent from django infrastructure
after sender instance is saved.
:param sender: class responsible for signal sending
:type sender: type
:param instance: instance of the sender class
:type instance: :class:`User`
:param created: value that determines is sender is created or not
:type created: boolean
"""
if created:
Profile.objects.create(user=instance)
[docs]
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
"""Update profile instance after related user is updated.
Method is called by signal sent from django infrastructure
after sender instance is updated.
:param sender: class responsible for signal sending
:type sender: :class:`User`
:param instance: instance of the sender class
:type instance: object of :class:`User`
"""
instance.profile.save()
[docs]
@receiver(user_logged_in)
def post_login(sender, user, request, **kwargs):
"""Check user's votes and permission values upon log in.
Method is called by signal sent from django infrastructure
after user logs in.
:param sender: class responsible for signal sending
:type sender: :class:`User`
:param user: instance of the user that just logged in
:type user: :class:`User`
"""
if user.profile.authorized:
user.profile.check_votes_and_permission()