Exemple #1
0
    def fetch(self, request):
        """
        Gets an API request, and returns the formatted JSON array in the for of a Python dict.

        The returned dictionary is what has been set by the expect_request if the request matches the next expectation.

        Also pops the next element from the expectation queue.

        Normally URL, request method type, payload and slogan has to be equal when matching expectations, but if
        payload is equal to minicrm.crmrequestfactory._, payload and slogan is not contributing and only URL and
        method type has to be equal to be considered matching.

        Note: this function only adds printing to the match_expectation.

        :raises: AssertionError if expectation queue is empty or if the next element doesn't match the given request.

        :param request: the encapsulated API request
        :type request: ApiRequest

        :return: the JSON answer as Python dictionary, if the request matches the next expectation.
        :rtype: dict
        """

        print("COMMAND SENT TO ---MOCK--- API: {}".format(request.get_slogan()))
        formatted_output = self.match_expectation(request)
        trace("ANSWER RECEIVED:")
        pretty_print(formatted_output)
        return formatted_output
Exemple #2
0
def put_student_to_waiting_list(crm_facade, student_data):
    """
    This function sends the waiting list email to the given student.

    This mail is information about that the student got to waiting list.

    :param crm_facade: instance of the CrmFacade class this functionality will use to communicate with a MiniCRM system.
    :type crm_facade: CrmFacade

    :param student_data: full JSON array of a student as stored in the MiniCRM system.
    :type student_data: dict

    :return: None
    """
    update_data = {}

    update_data[MAILS_TO_SEND_FIELD] = add_element_to_commasep_list(
        student_data[MAILS_TO_SEND_FIELD],
        WAITING_LIST_MAIL_NAME
    )

    update_data[STATUS_ID_FIELD] = crm_facade.get_student_status_number_by_name(WAITING_LIST_STATE)

    trace("DATA TO UPDATE:")
    pretty_print(update_data)

    crm_facade.set_student_data(student_data[STUDENT_ID_FIELD], update_data)
Exemple #3
0
def handle_waiting_list(crm_facade):
    """
    Loops through the students in waiting list and checks whether they got into the courses.

    Loops through all of the students in the waiting list and if there is
    free space in their course, it sends them the INFO letter and changes their
    status to INFO sent. Also copies all necessary data from the course to the student, and sends
     an additional mail that one spot has freed up, and updates the headcounts of courses.

    Students will get to the courses in the order of original application.

    :param crm_facade: instance of the CrmFacade class this functionality will use to communicate with a MiniCRM system.

    :return: None
    """

    waiting_list_students = crm_facade.get_student_list_with_status(
        WAITING_LIST_STATE)
    trace("LOOPING THROUGH STUDENTS ON WAITING LIST")

    waiting_list_students_ordered = []

    for student in waiting_list_students:
        trace("GETTING DETAILED DATA FOR SORTING")
        student_data = crm_facade.get_student(student)
        waiting_list_students_ordered.append(student_data)

    waiting_list_students_ordered.sort(
        key=lambda student_instance: student_instance[CREATED_AT_FIELD])

    trace("ORDERED LIST IS")
    pretty_print(waiting_list_students_ordered)

    for student_data in waiting_list_students_ordered:
        trace(
            "LOOPING THROUGH OERDERED LIST OF WAITING STUDENTS, CURRENTLY PROCESSING [{}]([{}])"
            .format(student_data[STUDENT_ID_FIELD],
                    student_data[STUDENT_NAME_FILED]))
        crm_facade.update_headcounts()
        trace("COURSE FOR " + student_data[STUDENT_NAME_FILED] + " IS " +
              student_data[CHOSEN_COURSE_FIELD])
        course_code = student_data[CHOSEN_COURSE_FIELD]
        course_data = crm_facade.get_course_by_course_code(course_code)

        is_there_free_spot = (course_data[MAX_HEADCOUNT_FIELD] -
                              course_data[CURRENT_HEADCOUNT_FIELD]) > 0

        if is_there_free_spot:
            send_one_spot_freed_up_mail(crm_facade, student_data)
            enroll_student_to_course(crm_facade, student_data, course_data)

    crm_facade.update_headcounts()
def set_course_states(crm_facade):
    """
    Loops through the courses and sets their statuses according to first and last date.

    - If current date is not less than course's 1st occasion, function sets the state to "In progress" ("Kurzus
      folyamatban").
    - If current date is not less than course's last occasion, function sets the state to "Recently finished" ("Frissen
      vegzett"). This state if to make it possible to make certain actions (make statistics / calculate salaries)
      monthly and the monthly script has to loop through the in progress and recently finished courses.
    - If current date is not less than course's last occasion plus 35 days, function sets the state to "Finished"
      ("Befejezett").

    :param crm_facade: instance of the CrmFacade class this functionality will use to communicate with a MiniCRM system.

    :return: None
    """
    open_courses = crm_facade.get_course_list_with_status(APPLICATION_OPEN_STATE)
    ongoing_courses = crm_facade.get_course_list_with_status(IN_PROGRESS_STATE)
    freshly_finished_courses = crm_facade.get_course_list_with_status(RECENTLY_FINISHED_STATE)

    courses = merge_dicts(open_courses, ongoing_courses, freshly_finished_courses)

    for course in courses:
        course_data = crm_facade.get_course(course)

        pretty_print(course_data)

        update_data = {}

        try:
            if date_is_not_less_than(crm_facade, course_data[FIRST_OCCASION_FIELD_NAME]):
                trace("Set: " + FIRST_OCCASION_FIELD_NAME)
                update_data[STATUS_ID_FIELD] = crm_facade.get_course_status_number_by_name(IN_PROGRESS_STATE)
            if date_is_not_less_than(crm_facade, course_data[LAST_OCCASION_FIELD_NAME]):
                trace("Set: " + LAST_OCCASION_FIELD_NAME)
                update_data[STATUS_ID_FIELD] = crm_facade.get_course_status_number_by_name(RECENTLY_FINISHED_STATE)
            if date_is_not_less_than(crm_facade, course_data[LAST_OCCASION_FIELD_NAME], +35):
                trace("Set: " + LAST_OCCASION_FIELD_NAME + " + 35 nap")
                update_data[STATUS_ID_FIELD] = crm_facade.get_course_status_number_by_name(FINISHED_STATE)
        except:
            trace("Missing date")

        if update_data:
            pretty_print(update_data)
            crm_facade.set_course_data(course, update_data)
        else:
            trace("NO DATA TO UPDATE")
def send_scheduled_emails(crm_facade):
    """
    Loops through active students and spectators and sends them scheduled mails based on the dates and other conditions.

    The following rules apply:

    - All students in "In Progress" ("Kurzus folyamatban") and "Spectator" ("Megfigyelo") are affected.

    - All mails of the occasions are sent if the date is not less than the occasion's date minus 3 days.

    - There are three types of "regular" mails (i.e. sent for every single occasion): beginner, advanded and frontend.
     There are 10 mails of each type for the 10 occasions.

    - Company advanced is counted as advanced and company beginner is counted as beginner in these regular mails.

    - On the day of the last occasion a goodbye mail is sent, also based on student is beginner, advanced or frontend.

    - This goodbye mail is not sent to company beginners, but is sent to company advanced. (So the courses can be
      accumulated to one long course and they only get goodbye mail at the very end.)

    - If the date is not less then the last date plus one day, student is put to "Finished" ("Elvegezte") state.

    - On the same day beginner/advanced/frontend certification is sent if the student is not attending a company course
      and is eligible.

    - On the same day advanced certification is sent if the student is attending a company advanced course.

    - Mail about free day is sent 2 days before each dayoff. If dayoff is added later, it is not sent.

    :param crm_facade: instance of the CrmFacade class this functionality will use to communicate with a MiniCRM system.

    :return: None
    """

    trace("ACTIVE STUDENTS")
    active_students = crm_facade.get_student_list_with_status(
        COURSE_IN_PROGRESS_STATE)

    trace("SPECTATORS: ")
    spectators = crm_facade.get_student_list_with_status(SPECTATOR_STATE)

    students = merge_dicts(active_students, spectators)

    trace("STUDENT LIST: ")
    pretty_print(students)

    for student in students:
        student_data = crm_facade.get_student(student)

        update_data = {}

        mails_to_send = student_data[MAILS_TO_SEND_FIELD]
        mails_to_send_old = mails_to_send

        student_is_beginner = (student_data[COURSE_TYPE_FIELD_IN_STUDENT] == BEGINNER_COURSE_TYPE) or \
                              (student_data[COURSE_TYPE_FIELD_IN_STUDENT] == COMPANY_BEGINNER_COURSE_TYPE)

        student_is_beginner_not_company = student_data[
            COURSE_TYPE_FIELD_IN_STUDENT] == BEGINNER_COURSE_TYPE

        student_is_advanced = (student_data[COURSE_TYPE_FIELD_IN_STUDENT] == ADVANCED_COURSE_TYPE) or \
                              (student_data[COURSE_TYPE_FIELD_IN_STUDENT] == COMPANY_ADVANCED_COURSE_TYPE)

        student_is_company_advanced = student_data[
            COURSE_TYPE_FIELD_IN_STUDENT] == COMPANY_ADVANCED_COURSE_TYPE

        student_is_advanced_not_company = student_data[
            COURSE_TYPE_FIELD_IN_STUDENT] == ADVANCED_COURSE_TYPE

        student_is_frontend = student_data[
            COURSE_TYPE_FIELD_IN_STUDENT] == FRONTEND_COURSE_TYPE

        if date_is_not_less_than(crm_facade,
                                 student_data[FIRST_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIRST_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIRST_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIRST_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[SECOND_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SECOND_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SECOND_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SECOND_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[THIRD_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, THIRD_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, THIRD_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, THIRD_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[FOURTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FOURTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FOURTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FOURTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[FIFTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIFTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIFTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIFTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[SIXTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SIXTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SIXTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SIXTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[SEVENTH_OCCASION_DATE_FIELD],
                                 -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SEVENTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SEVENTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SEVENTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[EIGTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, EIGHT_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, EIGHT_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, EIGHT_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[NINTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, NINTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, NINTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, NINTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[TENTH_OCCASION_DATE_FIELD], -3):
            if student_is_beginner:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, TENTH_OCCASION_BEGINNER_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, TENTH_OCCASION_ADVANCED_MAIL_NAME)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, TENTH_OCCASION_FRONTEND_MAIL_NAME)

        if date_is_not_less_than(crm_facade,
                                 student_data[TENTH_OCCASION_DATE_FIELD], +0):
            if student_is_beginner_not_company:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FINAL_MAIL_NAME)
            elif student_is_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FINAL_MAIL_NAME_ADVANCED)
            elif student_is_frontend:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FINAL_MAIL_NAME_FRONTEND)

        if date_is_not_less_than(crm_facade,
                                 student_data[TENTH_OCCASION_DATE_FIELD], +1):
            update_data[
                STATUS_ID_FIELD] = crm_facade.get_student_status_number_by_name(
                    FINISHED_STATE)
            if student_is_company_advanced:
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, CERTIFICATION_MAIL_NAME_ADVANCED)
            if ok_for_certification(student_data):
                if student_is_beginner_not_company:
                    mails_to_send = add_element_to_commasep_list(
                        mails_to_send, CERTIFICATION_MAIL_NAME)
                elif student_is_advanced_not_company:
                    mails_to_send = add_element_to_commasep_list(
                        mails_to_send, CERTIFICATION_MAIL_NAME_ADVANCED)
                elif student_is_frontend:
                    mails_to_send = add_element_to_commasep_list(
                        mails_to_send, CERTIFICATION_MAIL_NAME_FRONTEND)

        if student_data[FIRST_DAYOFF_FIELD] != "":
            if date_is_not_less_than(crm_facade, student_data[FIRST_DAYOFF_FIELD], -2) and \
                    date_is_not_more_than(crm_facade, student_data[FIRST_DAYOFF_FIELD]):
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, FIRST_DAYOFF_MAIL_NAME)
        if student_data[SECOND_DAYOFF_FIELD] != "":
            if date_is_not_less_than(crm_facade, student_data[SECOND_DAYOFF_FIELD], -2) and \
                    date_is_not_more_than(crm_facade, student_data[SECOND_DAYOFF_FIELD]):
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, SECOND_DAYOFF_MAIL_NAME)
        if student_data[THIRD_DAYOFF_FIELD] != "":
            if date_is_not_less_than(crm_facade, student_data[THIRD_DAYOFF_FIELD], -2) and \
                    date_is_not_more_than(crm_facade, student_data[THIRD_DAYOFF_FIELD]):
                mails_to_send = add_element_to_commasep_list(
                    mails_to_send, THIRD_DAYOFF_MAIL_NAME)

        if mails_to_send != mails_to_send_old:
            trace("CHANGE IN LEVELKULDESEK")
            update_data[MAILS_TO_SEND_FIELD] = mails_to_send
        else:
            trace("NO CHANGE IN LEVELKULDESEK")

        if update_data:
            crm_facade.set_student_data(student, update_data)
def enroll_student_to_course(crm_facade, student_data, course_data):
    """
    This function enrolls the student to the given course.

    - It copies all needed information from the course's data to the student's data in the system.
    - It sends the first response to the given student.
    - It puts the student to "INFO sent" (INFO levél kiment) state.

    This mail is information about the course. The function assembles
    the initial mail by fetching information about the course and the location. Based on the course data this function
    also decides whether it has to be a beginner or an advanced INFO mail.

    :param crm_facade: instance of the CrmFacade class this functionality will use to communicate with a MiniCRM system.
    :type crm_facade: CrmFacade

    :param student_data: full JSON array of a student as stored in the MiniCRM system.
    :type student_data: dict

    :param course_data: full JSON array of a course as stored in the MiniCRM system. The student will be enrolled to
    this course.
    :type course_data: dict

    :return: None
    """

    crm_facade.fill_student_data(student_data, course_data)

    update_data = {}

    trace(
        "ACTUAL HEADCOUNT: [{}], MAXIMAL: [{}]. STUDENT GOT TO COURSE.".format(
            course_data[CURRENT_HEADCOUNT_FIELD],
            course_data[MAX_HEADCOUNT_FIELD]))

    trace("TYPE OF COURSE IS: [{}] ".format(course_data[COURSE_TYPE_FIELD]))

    if course_data[COURSE_TYPE_FIELD] == BEGINNER_COURSE_TYPE:
        update_data[MAILS_TO_SEND_FIELD] = add_element_to_commasep_list(
            student_data[MAILS_TO_SEND_FIELD], BEGINNER_INFO_MAIL_NAME)

    elif course_data[COURSE_TYPE_FIELD] == ADVANCED_COURSE_TYPE:
        update_data[MAILS_TO_SEND_FIELD] = add_element_to_commasep_list(
            student_data[MAILS_TO_SEND_FIELD], ADVANCED_INFO_MAIL_NAME)

    elif course_data[COURSE_TYPE_FIELD] == FRONTEND_COURSE_TYPE:
        update_data[MAILS_TO_SEND_FIELD] = add_element_to_commasep_list(
            student_data[MAILS_TO_SEND_FIELD], FRONTEND_COURSE_MAIL_NAME)

    elif course_data[COURSE_TYPE_FIELD] == ONLINE_FRONTEND_COURSE_TYPE:
        update_data[MAILS_TO_SEND_FIELD] = add_element_to_commasep_list(
            student_data[MAILS_TO_SEND_FIELD],
            ONLINE_FRONTEND_COURSE_MAIL_NAME)

    update_data[
        STATUS_ID_FIELD] = crm_facade.get_student_status_number_by_name(
            INFO_SENT_STATE)

    trace("DATA TO UPDATE:")
    pretty_print(update_data)

    crm_facade.set_student_data(student_data[STUDENT_ID_FIELD], update_data)