예제 #1
0
    def handle(self, **options):
        # Get sent applications with an available partner and editor. We're sorting by ascending date to back-fill
        # authorizations from oldest to newest.
        sent_applications = Application.objects.filter(
            status=Application.SENT,
            editor__isnull=False,
            partner__isnull=False,
            partner__status=0).order_by('date_created', 'editor', 'partner')
        for application in sent_applications:
            # Check if an authorization already exists.
            if application.specific_stream:
                existing_authorization = Authorization.objects.filter(
                    user=application.user,
                    partner=application.partner,
                    stream=application.specific_stream)
            else:
                existing_authorization = Authorization.objects.filter(
                    user=application.user, partner=application.partner)
            # In the case that there is no existing authorization, create a new one
            if existing_authorization.count() == 0:
                authorization = Authorization()
                # You can't set the date_authorized on creation, but you can modify it afterwards. So save immediately.
                authorization.save()
                # We set the authorization date to the date the application was closed.
                authorization.date_authorized = application.date_closed
                if application.specific_stream:
                    authorization.stream = application.specific_stream

                authorization.user = application.user
                authorization.authorizer = application.sent_by
                authorization.partner = application.partner
                # If this is a proxy partner, and the requested_access_duration
                # field is set to false, set (or reset) the expiry date
                # to one year from authorization.
                if application.partner.authorization_method == Partner.PROXY and application.requested_access_duration is None:
                    one_year_from_auth = authorization.date_authorized + timedelta(
                        days=365)
                    authorization.date_expires = one_year_from_auth
                # If this is a proxy partner, and the requested_access_duration
                # field is set to true, set (or reset) the expiry date
                # to 1, 3, 6 or 12 months from authorization based on user input
                elif application.partner.authorization_method == Partner.PROXY and application.partner.requested_access_duration is True:
                    custom_expiry_date = authorization.date_authorized + relativedelta(
                        months=+application.requested_access_duration)
                    authorization.date_expires = custom_expiry_date
                # Alternatively, if this partner has a specified account_length,
                # we'll use that to set the expiry.
                elif application.partner.account_length:
                    # account_length should be a timedelta
                    authorization.date_expires = authorization.date_authorized + application.partner.account_length

                authorization.save()
                logger.info("authorization created: {authorization}".format(
                    authorization=authorization))
예제 #2
0
def post_revision_commit(sender, instance, **kwargs):
    if reversion.is_active() and (instance.status == 2 or instance.status == 4) and not instance.imported:

        authorized_user = instance.user
        authorizer = reversion.get_user()

        authorization = Authorization()

        if instance.specific_stream:
            authorization.stream = instance.specific_stream

        authorization.authorized_user = authorized_user
        authorization.authorizer = authorizer
        authorization.partner = instance.partner
        authorization.save()
예제 #3
0
def post_revision_commit(sender, instance, **kwargs):

    # For some authorization methods, we can skip the manual Approved->Sent
    # step and just immediately take an Approved application and give it
    # a finalised status.
    skip_approved = (instance.status == Application.APPROVED
                     and instance.is_instantly_finalized())

    if skip_approved:
        instance.status = Application.SENT
        instance.save()

    # Renewals are for applications that are approved/sent.
    # Having a parent for NOT_APPROVED apps hinders us from
    # correctly renewing the parent. So, we unset the parent
    # if the status is NOT_APPROVED and the app already has
    # a parent.
    if instance.status == Application.NOT_APPROVED and instance.parent:
        instance.parent = None
        instance.save()

    # Authorize editor to access resource after an application is saved as sent.

    if instance.status == Application.SENT:
        # Check if an authorization already exists.
        if instance.specific_stream:
            existing_authorization = Authorization.objects.filter(
                user=instance.user,
                partner=instance.partner,
                stream=instance.specific_stream,
            )
        else:
            existing_authorization = Authorization.objects.filter(
                user=instance.user, partner=instance.partner)

        authorized_user = instance.user
        authorizer = instance.sent_by

        # In the case that there is no existing authorization, create a new one
        if existing_authorization.count() == 0:
            authorization = Authorization()
        # If an authorization already existed (such as in the case of a
        # renewal), we'll simply update that one.
        elif existing_authorization.count() == 1:
            authorization = existing_authorization[0]
        else:
            logger.error("Found more than one authorization object for "
                         "{user} - {partner}".format(user=instance.user,
                                                     partner=instance.partner))
            return

        if instance.specific_stream:
            authorization.stream = instance.specific_stream

        authorization.user = authorized_user
        authorization.authorizer = authorizer
        authorization.partner = instance.partner

        # If this is a proxy partner, and the requested_access_duration
        # field is set to false, set (or reset) the expiry date
        # to one year from now
        if (instance.partner.authorization_method == Partner.PROXY
                and instance.requested_access_duration is None):
            one_year_from_now = date.today() + timedelta(days=365)
            authorization.date_expires = one_year_from_now
        # If this is a proxy partner, and the requested_access_duration
        # field is set to true, set (or reset) the expiry date
        # to 1, 3, 6 or 12 months from today based on user input
        elif (instance.partner.authorization_method == Partner.PROXY
              and instance.partner.requested_access_duration is True):
            custom_expiry_date = date.today() + relativedelta(
                months=instance.requested_access_duration)
            authorization.date_expires = custom_expiry_date
        # Alternatively, if this partner has a specified account_length,
        # we'll use that to set the expiry.
        elif instance.partner.account_length:
            # account_length should be a timedelta
            authorization.date_expires = date.today(
            ) + instance.partner.account_length

        authorization.save()