2017-10-19 01:12:52 +02:00
|
|
|
from allauth.socialaccount.models import SocialAccount
|
|
|
|
from django.db.models.signals import post_delete, post_save
|
2017-05-30 20:44:30 +02:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
@receiver(post_save, sender=SocialAccount)
|
|
|
|
def sync_clipper_on_saving_connection(sender, instance, **kwargs):
|
|
|
|
if instance.provider != "clipper":
|
|
|
|
return
|
2017-05-30 20:44:30 +02:00
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
# Saving instance makes it the most recently used clipper connection.
|
|
|
|
# Always update login_clipper, if value is not the used identifier.
|
2017-05-30 20:44:30 +02:00
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
profile = instance.user.profile
|
|
|
|
|
|
|
|
if profile.login_clipper != instance.uid:
|
|
|
|
profile.login_clipper = instance.uid
|
|
|
|
profile.save(update_fields=["login_clipper"])
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=SocialAccount)
|
|
|
|
def sync_clipper_on_deleting_connection(sender, instance, **kwargs):
|
|
|
|
if instance.provider != "clipper":
|
|
|
|
return
|
|
|
|
|
|
|
|
profile = instance.user.profile
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Get the most recently used clipper connection. Set its identifier as
|
|
|
|
# login_clipper value of the related CofProfile.
|
|
|
|
conn = SocialAccount.objects.filter(
|
|
|
|
provider="clipper", user=profile.user
|
|
|
|
).latest("last_login")
|
|
|
|
if profile.login_clipper != conn.uid:
|
|
|
|
profile.login_clipper = conn.uid
|
|
|
|
profile.save(update_fields=["login_clipper"])
|
|
|
|
except SocialAccount.DoesNotExist:
|
|
|
|
# If none lefts, flush it.
|
|
|
|
profile.login_clipper = ""
|
|
|
|
profile.save(update_fields=["login_clipper"])
|