utils.py 3.91 KB
Newer Older
1
"""Utilities to assist with commerce tasks."""
2
from urllib import urlencode
vkaracic committed
3
from urlparse import urljoin
4

5
import waffle
6
from django.conf import settings
7 8
from django.core.urlresolvers import reverse
from student.models import CourseEnrollment
vkaracic committed
9 10

from commerce.models import CommerceConfiguration
11
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
12

vkaracic committed
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27
def is_account_activation_requirement_disabled():
    """
    Checks to see if the django-waffle switch for disabling the account activation requirement is active

    Returns:
        Boolean value representing switch status
    """
    switch_name = configuration_helpers.get_value(
        'DISABLE_ACCOUNT_ACTIVATION_REQUIREMENT_SWITCH',
        settings.DISABLE_ACCOUNT_ACTIVATION_REQUIREMENT_SWITCH
    )
    return waffle.switch_is_active(switch_name)


vkaracic committed
28 29 30 31 32
class EcommerceService(object):
    """ Helper class for ecommerce service integration. """
    def __init__(self):
        self.config = CommerceConfiguration.current()

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    @property
    def ecommerce_url_root(self):
        """ Retrieve Ecommerce service public url root. """
        return configuration_helpers.get_value('ECOMMERCE_PUBLIC_URL_ROOT', settings.ECOMMERCE_PUBLIC_URL_ROOT)

    def get_absolute_ecommerce_url(self, ecommerce_page_url):
        """ Return the absolute URL to the ecommerce page.

        Args:
            ecommerce_page_url (str): Relative path to the ecommerce page.

        Returns:
            Absolute path to the ecommerce page.
        """
        return urljoin(self.ecommerce_url_root, ecommerce_page_url)

    def get_receipt_page_url(self, order_number):
        """
        Gets the URL for the Order Receipt page hosted by the ecommerce service.

        Args:
            order_number (str): Order number.

        Returns:
            Receipt page for the specified Order.
        """
59

60
        return self.get_absolute_ecommerce_url(CommerceConfiguration.DEFAULT_RECEIPT_PAGE_URL + order_number)
61

62
    def is_enabled(self, user):
63
        """
64 65 66 67 68
        Determines the availability of the EcommerceService based on user activation and service configuration.
        Note: If the user is anonymous we bypass the user activation gate and only look at the service config.

        Returns:
            Boolean
69
        """
70 71
        user_is_active = user.is_active or is_account_activation_requirement_disabled()
        allow_user = user_is_active or user.is_anonymous()
72
        return allow_user and self.config.checkout_on_ecommerce_service
vkaracic committed
73 74 75 76 77 78 79

    def payment_page_url(self):
        """ Return the URL for the checkout page.

        Example:
            http://localhost:8002/basket/single_item/
        """
80
        return self.get_absolute_ecommerce_url(self.config.single_course_checkout_page)
vkaracic committed
81

82 83 84 85 86 87 88 89
    def get_checkout_page_url(self, *skus):
        """ Construct the URL to the ecommerce checkout page and include products.

        Args:
            skus (list): List of SKUs associated with products to be added to basket

        Returns:
            Absolute path to the ecommerce checkout page showing basket that contains specified products.
vkaracic committed
90 91

        Example:
92
            http://localhost:8002/basket/add/?sku=5H3HG5&sku=57FHHD
vkaracic committed
93
        """
94
        return '{checkout_page_path}?{skus}'.format(
95
            checkout_page_path=self.get_absolute_ecommerce_url(self.config.MULTIPLE_ITEMS_BASKET_PAGE_URL),
96 97
            skus=urlencode({'sku': skus}, doseq=True),
        )
98 99 100 101 102 103 104 105 106 107 108 109 110

    def upgrade_url(self, user, course_key):
        """
        Returns the URL for the user to upgrade, or None if not applicable.
        """
        enrollment = CourseEnrollment.get_enrollment(user, course_key)
        verified_mode = enrollment.verified_mode if enrollment else None
        if verified_mode:
            if self.is_enabled(user):
                return self.get_checkout_page_url(verified_mode.sku)
            else:
                return reverse('verify_student_upgrade_and_verify', args=(course_key,))
        return None