예제 #1
0
 def activation_link(self):
     """
     Returns the activation link displayed in the activation email sent to a learner.
     """
     return get_license_activation_link(
         self.subscription_plan.customer_agreement.enterprise_customer_slug,
         self.activation_key,
     )
예제 #2
0
    def csv(self, request, subscription_uuid):  # pylint: disable=unused-argument
        """
        Returns license data for a given subscription in CSV format.

        Only includes licenses with a status of ACTIVATED, ASSIGNED, or REVOKED.
        """
        subscription = self._get_subscription_plan()
        enterprise_slug = subscription.customer_agreement.enterprise_customer_slug
        licenses = subscription.licenses.filter(status__in=[
            constants.ACTIVATED, constants.ASSIGNED, constants.REVOKED
        ], ).values('status', 'user_email', 'activation_date',
                    'last_remind_date', 'activation_key')
        for lic in licenses:
            # We want to expose the full activation link rather than just the activation key
            lic['activation_link'] = get_license_activation_link(
                enterprise_slug, lic['activation_key'])
            lic.pop('activation_key', None)
        csv_data = CSVRenderer().render(list(licenses))
        return Response(csv_data,
                        status=status.HTTP_200_OK,
                        content_type='text/csv')
예제 #3
0
def _send_email_with_activation(email_activation_key_map, context,
                                enterprise_slug, sender_alias):
    """
    Helper that sends emails with the given template with an activation link to the the given list of emails.

    Args:
        email_activation_key_map (dict): Dictionary containing the emails of each recipient and the activation key that
            is unique to the email. Recipient emails are the keys in the dictionary.
        context (dict): Dictionary of context variables for template rendering. Context takes an optional `REMINDER`
            key. If `REMINDER` is provided, a reminder message is added to the email.
        enterprise_slug (str): The slug associated with an enterprise to uniquely identify it.
        sender_alias (str): The alias to use in from email for sending the email.
    """
    # Construct each message to be sent and appended onto the email list
    email_recipient_list = email_activation_key_map.keys()
    emails = []
    for email_address in email_recipient_list:
        # Construct user specific context for each message
        context.update({
            'LICENSE_ACTIVATION_LINK':
            get_license_activation_link(
                enterprise_slug, email_activation_key_map.get(email_address)),
            'RECIPIENT_EMAIL':
            email_address,
            'SOCIAL_MEDIA_FOOTER_URLS':
            settings.SOCIAL_MEDIA_FOOTER_URLS,
            'MOBILE_STORE_URLS':
            settings.MOBILE_STORE_URLS,
        })
        emails.append(_message_from_context_and_template(
            context, sender_alias))

    # Send out the emails in batches
    email_chunks = chunks(emails, LICENSE_BULK_OPERATION_BATCH_SIZE)
    for email_chunk in email_chunks:
        # Renew the email connection for each chunk of emails sent
        with mail.get_connection() as connection:
            connection.send_messages(email_chunk)
예제 #4
0
def check_missing_licenses(customer_agreement, user_emails, course_run_keys, subscription_uuid=None):
    """
    Helper function to check that each of the provided learners has a valid subscriptions license for the provided
    courses.

    Uses a map to track:
        <plan_key>: <plan_contains_course>
    where, plan_key = {subscription_plan.uuid}_{course_key}
    This will help us memoize the value of the subscription_plan.contains_content([course_key])
    to avoid repeated requests to the enterprise catalog endpoint for the same information
    """
    missing_subscriptions = {}
    licensed_enrollment_info = []

    subscription_plan_course_map = {}

    enterprise_slug = customer_agreement.enterprise_customer_slug

    subscription_plan_filter = [subscription_uuid] if subscription_uuid else customer_agreement.subscriptions.all()
    for email in set(user_emails):
        filtered_licenses = License.objects.filter(
            subscription_plan__in=subscription_plan_filter,
            user_email=email,
        )

        # order licenses by their associated subscription plan expiration date
        ordered_licenses_by_expiration = sorted(
            filtered_licenses,
            key=lambda user_license: user_license.subscription_plan.expiration_date,
            reverse=True,
        )
        for course_key in set(course_run_keys):
            plan_found = False
            for user_license in ordered_licenses_by_expiration:
                subscription_plan = user_license.subscription_plan
                plan_key = f'{subscription_plan.uuid}_{course_key}'
                if plan_key in subscription_plan_course_map:
                    plan_contains_content = subscription_plan_course_map.get(plan_key)
                else:
                    plan_contains_content = subscription_plan.contains_content([course_key])
                    subscription_plan_course_map[plan_key] = plan_contains_content

                if plan_contains_content:
                    this_enrollment = {
                        'email': email,
                        'course_run_key': course_key,
                        'license_uuid': str(user_license.uuid)
                    }
                    # assigned, not yet activated, incliude activation URL
                    if user_license.status == constants.ASSIGNED:
                        this_enrollment['activation_link'] = get_license_activation_link(
                            enterprise_slug,
                            user_license.activation_key,
                        )
                    licensed_enrollment_info.append(this_enrollment)
                    plan_found = True
            if not plan_found:
                if missing_subscriptions.get(email):
                    missing_subscriptions[email].append(course_key)
                else:
                    missing_subscriptions[email] = [course_key]

    return missing_subscriptions, licensed_enrollment_info