enrollment_report.py 3.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 59
"""
Defines abstract class for the Enrollment Reports.
"""

from django.contrib.auth.models import User
from student.models import UserProfile
import collections
import json
import abc


class AbstractEnrollmentReportProvider(object):
    """
    Abstract interface for Detailed Enrollment Report Provider
    """
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def get_enrollment_info(self, user, course_id):
        """
        Returns the User Enrollment information.
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_user_profile(self, user_id):
        """
        Returns the UserProfile information.
        """
        raise NotImplementedError()

    @abc.abstractmethod
    def get_payment_info(self, user, course_id):
        """
        Returns the User Payment information.
        """
        raise NotImplementedError()


class BaseAbstractEnrollmentReportProvider(AbstractEnrollmentReportProvider):
    """
    The base abstract class for all Enrollment Reports that can support multiple
    backend such as MySQL/Django-ORM.

    # don't allow instantiation of this class, it must be subclassed
    """
    def get_user_profile(self, user_id):
        """
        Returns the UserProfile information.
        """
        user_info = User.objects.select_related('profile').get(id=user_id)
        # extended user profile fields are stored in the user_profile meta column
        meta = {}
        if user_info.profile.meta:
            meta = json.loads(user_info.profile.meta)

        user_data = collections.OrderedDict()
        user_data['User ID'] = user_info.id
        user_data['Username'] = user_info.username
60
        user_data['Email'] = user_info.email
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        user_data['Full Name'] = user_info.profile.name
        user_data['First Name'] = meta.get('first-name', '')
        user_data['Last Name'] = meta.get('last-name', '')
        user_data['Company Name'] = meta.get('company', '')
        user_data['Title'] = meta.get('title', '')
        user_data['Language'] = user_info.profile.language
        user_data['Country'] = user_info.profile.country
        user_data['Year of Birth'] = user_info.profile.year_of_birth

        user_data['Gender'] = None
        gender = user_info.profile.gender
        for _gender in UserProfile.GENDER_CHOICES:
            if gender == _gender[0]:
                user_data['Gender'] = _gender[1]
                break

        user_data['Level of Education'] = None
        level_of_education = user_info.profile.level_of_education
        for _loe in UserProfile.LEVEL_OF_EDUCATION_CHOICES:
            if level_of_education == _loe[0]:
                user_data['Level of Education'] = _loe[1]

        user_data['Mailing Address'] = user_info.profile.mailing_address
        user_data['Goals'] = user_info.profile.goals
        user_data['City'] = user_info.profile.city
        user_data['Country'] = user_info.profile.country
        return user_data

    def get_enrollment_info(self, user, course_id):
        """
        Returns the User Enrollment information.
        """
        raise NotImplementedError()

    def get_payment_info(self, user, course_id):
        """
        Returns the User Payment information.
        """
        raise NotImplementedError()