from datetime import timedelta
from django.dispatch import receiver, Signal
from django.db.models.signals import post_save
from TWLight.users.models import Authorization
from .models import Partner, Stream


class Notice(object):
    user_renewal_notice = Signal(
        providing_args=[
            "user_wp_username",
            "user_email",
            "user_lang",
            "partner_name",
            "partner_link",
        ]
    )


@receiver(post_save, sender=Partner)
@receiver(post_save, sender=Stream)
def update_partner_authorization_expiry(sender, instance, **kwargs):
    """
    Updates missing expiration dates upon resource updates.
    Mostly cribbed from
    TWLight.applications.models.post_receive_commit
    and
    TWLight.users.management.commands.authorization_backfill
    Could factor out the common code.
    """

    if sender == Partner:
        partner = instance
    elif sender == Stream:
        partner = instance.partner

    if partner.account_length or partner.authorization_method == Partner.PROXY:
        authorizations = Authorization.objects.filter(
            partner=partner, date_expires=None
        )
        for authorization in authorizations:
            if authorization.is_valid:
                if (
                    partner.authorization_method == Partner.PROXY
                    and partner.requested_access_duration is True
                ):
                    one_year_from_auth = authorization.date_authorized + timedelta(
                        days=365
                    )
                    authorization.date_expires = one_year_from_auth
                    authorization.save()
                elif partner.account_length:
                    authorization.date_expires = (
                        authorization.date_authorized + partner.account_length
                    )
                    authorization.save()

@@ -24,16 +24,60 @@
 medelta%0A
+from django.contrib.auth.models import User%0A
 from dja
@@ -159,16 +159,29 @@
 ost_save
+, post_delete
 %0Afrom TW
@@ -1928,28 +1928,812 @@
        authorization.save()%0A
+%0A%0A@receiver(post_delete, sender=Stream)%0Adef delete_all_but_latest_partner_authorizations(sender, instance, **kwargs):%0A    %22%22%22%0A    Deletes any duplicate partner auths left after a stream is deleted.%0A    %22%22%22%0A%0A    partner = instance.partner%0A    authorizations = Authorization.objects.filter(partner=partner)%0A    users = User.objects.filter(authorization__in=authorizations)%0A    for user in users:%0A        partner_authorizations = Authorization.objects.filter(%0A            user=user, partner=partner, stream__isnull=True%0A        )%0A        if partner_authorizations.count() %3E 1:%0A            latest_partner_authorization = partner_authorizations.latest(%0A                %22date_authorized%22%0A            )%0A            partner_authorizations.exclude(pk=latest_partner_authorization.pk).delete()%0A

add signal to cleanup duplicate auths after stream delete