<commit_before>"""
This module will provide a method to retrieve all user majors, class standings
and minors along with current and rollup variables to facilitate
determinations about user context in the case that a user is not
enrolled for the current quarter
"""
from myuw.dao.enrollment import enrollment_search
from uw_sws.term import get_current_term


def get_student_status(request):
    """
    This will return a dictionary with the student status over time alongside
    various rollup variables
    :param request:
    :return: dict
    """
    enrollments = enrollment_search(request)

    return {
        'majors': _get_majors(enrollments),
        'class_level': _get_class_standings(enrollments)
    }


def get_majors(request):
    """
    Returns a dictionary with a dict of majors by terms in the 'majors'
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_majors(enrollments)


def get_rollup_and_future_majors(majors):
    return _get_rollup_and_future(majors['majors'], majors['rollup'])


def _get_majors(enrollments):
    return _process_fields(enrollments, "majors")


def get_minors(request):
    """
    Returns a dictionary with a dict of majors by terms in the 'majors'
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_minors(enrollments)


def get_rollup_and_future_minors(minors):
    return _get_rollup_and_future(minors['minors'], minors['rollup'])


def _get_minors(enrollments):
    return _process_fields(enrollments, "minors")


def get_class_standings(request):
    """
    Returns a dictionary with a dict of majors by terms in the majors
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_class_standings(enrollments)


def _get_class_standings(enrollments):
    return _process_fields(enrollments, "class_level")


def _process_fields(enrollments, attribute):
    obj = {}

    current_term = get_current_term()

    if current_term in enrollments:
        obj['current'] = getattr(enrollments[current_term], attribute)

    obj[attribute] = {term: getattr(enrollments[term], attribute)
                      for term in enrollments}

    if 'current' not in obj:
        sorted_terms = sorted(enrollments.keys())

        has_future_entry = False

        for term in sorted_terms:
            if term > current_term:
                obj['rollup'] = getattr(enrollments[term], attribute)
                has_future_entry = True
                break

        most_recent_term = sorted_terms[0]

        if not has_future_entry:
            for term in sorted_terms:
                if term > most_recent_term:
                    most_recent_term = term

            obj['rollup'] = getattr(enrollments[most_recent_term], attribute)
    else:
        obj['rollup'] = obj['current']

    return obj
<commit_msg>Refactoring to avoid redundant method calls<commit_after>"""
This module will provide a method to retrieve all user majors, class standings
and minors along with current and rollup variables to facilitate
determinations about user context in the case that a user is not
enrolled for the current quarter
"""
from myuw.dao.enrollment import enrollment_search
from uw_sws.term import get_current_term


def get_student_status(request):
    """
    This will return a dictionary with the student status over time alongside
    various rollup variables
    :param request:
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _process_fields(enrollments, ['majors', 'minors', 'class_level'])


def get_majors(request):
    """
    Returns a dictionary with a dict of majors by terms in the 'majors'
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_majors(enrollments)


def get_rollup_and_future_majors(majors):
    return _get_rollup_and_future(majors['majors'], majors['rollup'])


def _get_majors(enrollments):
    return _process_fields(enrollments, ["majors"])['majors']


def get_minors(request):
    """
    Returns a dictionary with a dict of majors by terms in the 'majors'
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_minors(enrollments)


def get_rollup_and_future_minors(minors):
    return _get_rollup_and_future(minors['minors'], minors['rollup'])


def _get_minors(enrollments):
    return _process_fields(enrollments, ["minors"])['minors']


def get_class_standings(request):
    """
    Returns a dictionary with a dict of majors by terms in the majors
    attribute, and a 'current' and 'rollup' major field
    :param request
    :return: dict
    """
    enrollments = enrollment_search(request)

    return _get_class_standings(enrollments)


def _get_class_standings(enrollments):
    return _process_fields(enrollments, ["class_level"])['class_level']


def _process_fields(enrollments, attributes):
    obj = {}
    for attribute in attributes:
        obj[attribute] = {}

    current_term = get_current_term()

    if current_term in enrollments:
        for attribute in attributes:
            obj[attribute]['current'] = getattr(enrollments[current_term],
                                                attribute)

    for attribute in attributes:
        obj[attribute][attribute] = {term: getattr(enrollments[term],
                                                   attribute)
                                     for term in enrollments}

    if 'current' not in obj:
        sorted_terms = sorted(enrollments.keys())

        has_future_entry = False

        for term in sorted_terms:
            if term > current_term:
                for attribute in attributes:
                    obj[attribute]['rollup'] = getattr(enrollments[term],
                                                       attribute)
                has_future_entry = True
                break

        recent_term = sorted_terms[0]

        if not has_future_entry:
            for term in sorted_terms:
                if term > recent_term:
                    recent_term = term

            for attribute in attributes:
                obj[attribute]['rollup'] = getattr(enrollments[recent_term],
                                                   attribute)
    else:
        for attribute in attributes:
            obj[attribute]['rollup'] = obj[attribute]['current']

    return obj

{'fraction_non_alphanumeric': 0.07956734331012001, 'fraction_numerical': 0.000296340198547933, 'mean_word_length': 2.8461538461538463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6569919', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1879, 'n_words': 550}