<commit_before>""" 
This module encapsulates the access of the term data
(including registered summer terms, registered future terms).
"""

from datetime import date
import logging
import traceback
from django.conf import settings
from myuw_mobile.logger.timer import Timer
from myuw_mobile.logger.logback import log_resp_time, log_exception
from myuw_mobile.dao.term import is_a_term, is_b_term, is_full_summer_term
from myuw_mobile.dao.term import get_current_summer_term
from myuw_mobile.dao.schedule import get_next_quarter_schedule
from myuw_mobile.dao.schedule import get_next_autumn_quarter_schedule
from myuw_mobile.dao.schedule import has_summer_quarter_section


logger =  logging.getLogger(__name__)
FULL_TERM = "F"
A_TERM = "A"
B_TERM = "B"


def _get_registered_summer_terms(registered_summer_sections):
    """
    Return all the summer terms in the registered summer sections
    """
    data = {
        FULL_TERM : False,
        A_TERM : False,
        B_TERM : False,
        }
    for section in registered_summer_sections:
        if is_full_summer_term(section.summer_term):
            data[FULL_TERM] = True
        elif is_a_term(section.summer_term):
            data[A_TERM] = True
        elif is_b_term(section.summer_term):
            data[B_TERM] = True
        else:
            pass
    return data


def _must_displayed_separately (schedule):
    """
    Return True if the summer terms in the schedule sections
    need to be displayed separately
    """
    sumr_tms = _get_registered_summer_terms(schedule.sections)
    return sumr_tms[A_TERM] and sumr_tms[B_TERM] and sumr_tms[FULL_TERM] or sumr_tms[A_TERM] and sumr_tms[FULL_TERM] or sumr_tms[B_TERM] and sumr_tms[FULL_TERM] or sumr_tms[A_TERM] and sumr_tms[B_TERM]
    

def get_current_summer_term_in_schedule (schedule):
    """
    If the summer terms needs to be displayed separately,
    return the current summer term
    """
    summer_term = ""
    if has_summer_quarter_section(schedule) and _must_displayed_separately(schedule):
        summer_term = get_current_summer_term()
    return summer_term


def get_registered_future_quarters():
    """ 
    Return the list of future quarters that 
    has actively enrolled sections
    @param next_quar_sche: ClassSchedule object of a future quarter
    @param next_autumn_quar_sche: 
    """
    terms = []
    next_quar_sche = get_next_quarter_schedule()
    next_quarter = next_quar_sche.term
    if next_quar_sche is not None and len(next_quar_sche.sections) > 0:

        if next_quarter.quarter == "summer":
            if _must_displayed_separately(next_quar_sche):
                sumr_tms = _get_registered_summer_terms(next_quar_sche.sections)

                if sumr_tms[A_TERM]:
                    terms.append(_get_future_term_json(next_quarter,
                                                       "A-Term"))

                if sumr_tms[FULL_TERM] and not sumr_tms[A_TERM]:
                    terms.append(_get_future_term_json(next_quarter,
                                                       "A-Term"))

                if sumr_tms[B_TERM]:
                    terms.append(_get_future_term_json(next_quarter,
                                                       "B-Term"))

                if sumr_tms[FULL_TERM] and not sumr_tms[B_TERM]:
                    terms.append(_get_future_term_json(next_quarter,
                                                       "B-Term"))
            else:
                # summer full-term
                terms.append(_get_future_term_json(next_quarter,""))

            next_autumn_sche = get_next_autumn_quarter_schedule()
            if next_autumn_sche is not None and len(next_autumn_sche.sections) > 0:
                terms.append(_get_future_term_json(next_autumn_sche.term, ""))
        else:
            # non-summer quarter
            terms.append(_get_future_term_json(next_quarter,""))
    return terms


def _get_future_term_json(term, summer_term):
    return_json = term.json_data()
    return_json["summer_term"] = summer_term
    url = "/" + str(term.year) + "," + term.quarter
    if summer_term:
        url = url + "," + summer_term.lower()
    return_json["url"] = url
    return return_json

<commit_msg>Revised get_registered_future_quarters so it can be unti-tested.<commit_after>""" 
This module encapsulates the access of the term data
(including registered summer terms, registered future terms).
"""

import logging
from myuw_mobile.dao.term import is_a_term, is_b_term, is_full_summer_term
from myuw_mobile.dao.term import get_current_summer_term
from myuw_mobile.dao.schedule import get_next_quarter_schedule
from myuw_mobile.dao.schedule import get_next_autumn_quarter_schedule
from myuw_mobile.dao.schedule import has_summer_quarter_section


logger =  logging.getLogger(__name__)
FULL_TERM = "F"
A_TERM = "A"
B_TERM = "B"


def get_current_summer_term_in_schedule(schedule):
    """
    If the summer terms needs to be displayed separately,
    return the current summer term
    """
    summer_term = ""
    if schedule.term.quarter == 'summer':
        if has_summer_quarter_section(schedule):
            if _must_displayed_separately(schedule):
                summer_term = get_current_summer_term()
            else:
                summer_term = "Full-term"
    return summer_term


def get_registered_future_quarters():
    """ 
    Return the list of future quarters that 
    has actively enrolled sections
    """
    next_quar_sche = get_next_quarter_schedule()
    next_autumn_sche = None
    if next_quar_sche.term.quarter == 'summer':
        next_autumn_sche = get_next_autumn_quarter_schedule()
    return _get_registered_future_quarters(next_quar_sche,
                                           next_autumn_sche)


def _get_registered_future_quarters(next_quar_sche,
                                    next_autumn_sche):
    """ 
    Return the list of future quarters that 
    has actively enrolled sections

    Summer terms that must be displayed separately if exist:
    a-term, b-term and full-term or 
    a-term and full-term or 
    b-term and full-term or 
    a-term and b-term

    @param next_quar_sche: ClassSchedule object of a future quarter
    @param next_autumn_quar_sche: ClassSchedule object of the future
           autumn quarter if the next_quar_sche is of summer quarter 
    """
    terms = []
    next_quarter = next_quar_sche.term
    if next_quar_sche is not None and len(next_quar_sche.sections) > 0:

        if next_quarter.quarter == "summer":
            sumr_tms = _get_registered_summer_terms(next_quar_sche.sections)
            
            if sumr_tms[A_TERM] or sumr_tms[FULL_TERM] and sumr_tms[B_TERM]:
                terms.append(_get_future_term_json(next_quarter,
                                                   "A-term"))

            if sumr_tms[B_TERM] or sumr_tms[FULL_TERM] and sumr_tms[A_TERM]:
                terms.append(_get_future_term_json(next_quarter,
                                                   "B-term"))

            if sumr_tms[FULL_TERM] and not sumr_tms[A_TERM] and not sumr_tms[B_TERM]:
                terms.append(_get_future_term_json(next_quarter,
                                                   "Full-term"))
        else:
            terms.append(_get_future_term_json(next_quarter,""))

    if next_autumn_sche is not None and len(next_autumn_sche.sections) > 0:
        terms.append(_get_future_term_json(next_autumn_sche.term, ""))
    return terms


def _get_future_term_json(term, summer_term):
    return_json = term.json_data()
    return_json["summer_term"] = summer_term
    url = "/" + str(term.year) + "," + term.quarter
    if len(summer_term) > 0:
        url = url + "," + summer_term.lower()
    return_json["url"] = url
    return return_json


def _get_registered_summer_terms(registered_summer_sections):
    """
    Return all the summer terms in the registered summer sections
    """
    data = {
        FULL_TERM : False,
        A_TERM : False,
        B_TERM : False,
        }
    for section in registered_summer_sections:
        if is_full_summer_term(section.summer_term):
            data[FULL_TERM] = True
        elif is_a_term(section.summer_term):
            data[A_TERM] = True
        elif is_b_term(section.summer_term):
            data[B_TERM] = True
        else:
            pass
    return data


def _must_displayed_separately (schedule):
    """
    Return True if the summer terms in the schedule sections
    need to be displayed separately
    """
    sumr_tms = _get_registered_summer_terms(schedule.sections)
    return sumr_tms[A_TERM] and sumr_tms[B_TERM] and sumr_tms[FULL_TERM] or sumr_tms[A_TERM] and sumr_tms[FULL_TERM] or sumr_tms[B_TERM] and sumr_tms[FULL_TERM] or sumr_tms[A_TERM] and sumr_tms[B_TERM]
  

{'fraction_non_alphanumeric': 0.07279736931624901, 'fraction_numerical': 0.0005669577049552103, 'mean_word_length': 2.625154130702836, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3378997', 'n_tokens_mistral': 2834, 'n_tokens_neox': 2688, 'n_words': 602}