def _json_to_section(section_data,
                     term=None,
                     include_instructor_not_on_time_schedule=True):
    """
    Returns a section model created from the passed json.
    """
    section = Section()
    if term is not None and (
            term.year == int(section_data["Course"]["Year"]) and
            term.quarter == section_data["Course"]["Quarter"]):
        section.term = term
    else:
        section.term = get_term_by_year_and_quarter(
            section_data["Course"]["Year"],
            section_data["Course"]["Quarter"])

    section.curriculum_abbr = section_data["Course"][
        "CurriculumAbbreviation"]
    section.course_number = section_data["Course"]["CourseNumber"]
    section.course_title = section_data["CourseTitle"]
    section.course_title_long = section_data["CourseTitleLong"]
    section.course_campus = section_data["CourseCampus"]
    section.course_description = section_data["CourseDescription"]
    section.section_id = section_data["SectionID"]
    section.eos_cid = section_data.get("EOS_CID", None)
    section.institute_name = section_data.get("InstituteName", "")
    section.metadata = section_data.get("Metadata", "")
    section.primary_lms = section_data.get("PrimaryLMS", None)
    section.lms_ownership = section_data.get("LMSOwnership", None)
    section.is_independent_start = section_data.get("IsIndependentStart",
                                                    False)
    section.section_type = section_data["SectionType"]
    if "independent study" == section.section_type or\
       "IS" == section.section_type:
        is_independent_study = True
    else:
        is_independent_study = False

    section.is_independent_study = section_data.get(
        "IndependentStudy", is_independent_study)

    section.credit_control = section_data.get("CreditControl", "")

    if "StartDate" in section_data and\
       len(section_data["StartDate"]) > 0:
        section.start_date = parse(section_data["StartDate"]).date()

    if "EndDate" in section_data and\
       len(section_data["EndDate"]) > 0:
        section.end_date = parse(section_data["EndDate"]).date()

    section.class_website_url = section_data["ClassWebsiteUrl"]

    if is_valid_sln(section_data["SLN"]):
        section.sln = int(section_data["SLN"])
    else:
        section.sln = 0

    if "SummerTerm" in section_data:
        section.summer_term = section_data["SummerTerm"]
    else:
        section.summer_term = ""

    section.delete_flag = section_data["DeleteFlag"]
    section.current_enrollment = int(section_data['CurrentEnrollment'])
    section.limit_estimate_enrollment = int(
        section_data['LimitEstimateEnrollment'])
    section.limit_estimate_enrollment_indicator = section_data[
        'LimitEstimateEnrollmentIndicator']

    section.auditors = int(section_data['Auditors'])
    section.allows_secondary_grading = section_data["SecondaryGradingOption"]

    primary_section = section_data["PrimarySection"]
    if (primary_section is not None and
            primary_section["SectionID"] != section.section_id):
        section.is_primary_section = False
        section.primary_section_href = primary_section["Href"]
        section.primary_section_id = primary_section["SectionID"]
        section.primary_section_curriculum_abbr = primary_section[
            "CurriculumAbbreviation"]
        section.primary_section_course_number = primary_section[
            "CourseNumber"]
    else:
        section.is_primary_section = True

    section.linked_section_urls = []
    for linked_section_type in section_data["LinkedSectionTypes"]:
        for linked_section_data in linked_section_type["LinkedSections"]:
            url = linked_section_data["Section"]["Href"]
            section.linked_section_urls.append(url)

    section.joint_section_urls = []
    for joint_section_data in section_data.get("JointSections", []):
        url = joint_section_data["Href"]
        section.joint_section_urls.append(url)

    section.grading_system = section_data['GradingSystem']
    section.grade_submission_delegates = []
    for del_data in section_data["GradeSubmissionDelegates"]:
        delegate = GradeSubmissionDelegate(
            person=UWPWS.get_person_by_regid(del_data["Person"]["RegID"]),
            delegate_level=del_data["DelegateLevel"])
        section.grade_submission_delegates.append(delegate)

    section.meetings = []
    for meeting_data in section_data["Meetings"]:
        meeting = SectionMeeting()
        meeting.section = section
        meeting.term = section.term
        meeting.meeting_index = meeting_data["MeetingIndex"]
        meeting.meeting_type = meeting_data["MeetingType"]

        meeting.building = meeting_data["Building"]
        if meeting_data["BuildingToBeArranged"]:
            meeting.building_to_be_arranged = True
        else:
            meeting.building_to_be_arranged = False

        meeting.room_number = meeting_data["RoomNumber"]
        if meeting_data["RoomToBeArranged"]:
            meeting.room_to_be_arranged = True
        else:
            meeting.room_to_be_arranged = False

        if meeting_data["DaysOfWeekToBeArranged"]:
            meeting.days_to_be_arranged = True
        else:
            meeting.days_to_be_arranged = False

        for day_data in meeting_data["DaysOfWeek"]["Days"]:
            attribute = "meets_{}".format(day_data["Name"].lower())
            setattr(meeting, attribute, True)

        if (len(meeting_data["StartTime"]) and
                meeting_data["StartTime"] != "00:00:00"):
            meeting.start_time = meeting_data["StartTime"]
            # in case of "18:00:00", only keep hh:mm
            if len(meeting.start_time) > 5:
                meeting.start_time = meeting.start_time[:5]

        if (len(meeting_data["EndTime"]) and
                meeting_data["EndTime"] != "00:00:00"):
            meeting.end_time = meeting_data["EndTime"]
            if len(meeting.end_time) > 5:
                meeting.end_time = meeting.end_time[:5]

        if (meeting_data.get("EOS_StartDate", None) and
                meeting_data.get("EOS_EndDate", None)):
            meeting.eos_start_date = parse(
                meeting_data["EOS_StartDate"]).date()
            meeting.eos_end_date = parse(
                meeting_data["EOS_EndDate"]).date()

        meeting.instructors = []
        for instructor_data in meeting_data["Instructors"]:
            # TSPrint: True
            # Instructor information currently listed on the Time Schedule
            if (instructor_data["TSPrint"] or
                    include_instructor_not_on_time_schedule):
                pdata = instructor_data["Person"]

                if "RegID" in pdata and pdata["RegID"] is not None:
                    try:
                        instructor = UWPWS.get_person_by_regid(pdata["RegID"])
                    except Exception:
                        instructor = Person(uwregid=pdata["RegID"],
                                            display_name=pdata["Name"])
                    instructor.TSPrint = instructor_data["TSPrint"]
                    meeting.instructors.append(instructor)

        section.meetings.append(meeting)

    section.final_exam = None
    if "FinalExam" in section_data and section_data["FinalExam"] is not None:
        if "MeetingStatus" in section_data["FinalExam"]:
            final_exam = FinalExam()
            final_data = section_data["FinalExam"]
            status = final_data["MeetingStatus"]
            # MeetingStatus values:
            # 0 - default final exam meeting date/time has not been confirmed
            # 1 - no final exam or no traditional final exam
            # 2 - confirmed, at the default final exam date/time/location
            # 3 - confirmed, but at a different date/time/location

            final_exam.no_exam_or_nontraditional = False
            final_exam.is_confirmed = False

            if (status == "2") or (status == "3"):
                final_exam.is_confirmed = True
            elif status == "1":
                final_exam.no_exam_or_nontraditional = True

            final_exam.building = final_data["Building"]
            final_exam.room_number = final_data["RoomNumber"]

            final_format = "%Y-%m-%d : %H:%M"

            strptime = datetime.strptime
            if final_data["Date"] and final_data["Date"] != "0000-00-00":
                if final_data["StartTime"]:
                    start_string = "{} : {}".format(final_data["Date"],
                                                    final_data["StartTime"])
                    final_exam.start_date = strptime(start_string,
                                                     final_format)

                if final_data["EndTime"]:
                    end_string = "{} : {}".format(final_data["Date"],
                                                  final_data["EndTime"])
                    try:
                        final_exam.end_date = strptime(
                            end_string, final_format)
                    except ValueError:
                        logger.info('bad final EndTime: {}'.format(end_string))
                        final_exam.end_date = None

            final_exam.clean_fields()
            section.final_exam = final_exam

    return section
def _json_to_section ( section_data , term = None , include_instructor_not_on_time_schedule = True ) : section = Section ( ) if term is not None and ( term . year == int ( section_data [ "Course" ] [ "Year" ] ) and term . quarter == section_data [ "Course" ] [ "Quarter" ] ) : section . term = term else : section . term = get_term_by_year_and_quarter ( section_data [ "Course" ] [ "Year" ] , section_data [ "Course" ] [ "Quarter" ] ) section . curriculum_abbr = section_data [ "Course" ] [ "CurriculumAbbreviation" ] section . course_number = section_data [ "Course" ] [ "CourseNumber" ] section . course_title = section_data [ "CourseTitle" ] section . course_title_long = section_data [ "CourseTitleLong" ] section . course_campus = section_data [ "CourseCampus" ] section . course_description = section_data [ "CourseDescription" ] section . section_id = section_data [ "SectionID" ] section . eos_cid = section_data . get ( "EOS_CID" , None ) section . institute_name = section_data . get ( "InstituteName" , "" ) section . metadata = section_data . get ( "Metadata" , "" ) section . primary_lms = section_data . get ( "PrimaryLMS" , None ) section . lms_ownership = section_data . get ( "LMSOwnership" , None ) section . is_independent_start = section_data . get ( "IsIndependentStart" , False ) section . section_type = section_data [ "SectionType" ] if "independent study" == section . section_type or "IS" == section . section_type : is_independent_study = True else : is_independent_study = False section . is_independent_study = section_data . get ( "IndependentStudy" , is_independent_study ) section . credit_control = section_data . get ( "CreditControl" , "" ) if "StartDate" in section_data and len ( section_data [ "StartDate" ] ) > 0 : section . start_date = parse ( section_data [ "StartDate" ] ) . date ( ) if "EndDate" in section_data and len ( section_data [ "EndDate" ] ) > 0 : section . end_date = parse ( section_data [ "EndDate" ] ) . date ( ) section . class_website_url = section_data [ "ClassWebsiteUrl" ] if is_valid_sln ( section_data [ "SLN" ] ) : section . sln = int ( section_data [ "SLN" ] ) else : section . sln = 0 if "SummerTerm" in section_data : section . summer_term = section_data [ "SummerTerm" ] else : section . summer_term = "" section . delete_flag = section_data [ "DeleteFlag" ] section . current_enrollment = int ( section_data [ 'CurrentEnrollment' ] ) section . limit_estimate_enrollment = int ( section_data [ 'LimitEstimateEnrollment' ] ) section . limit_estimate_enrollment_indicator = section_data [ 'LimitEstimateEnrollmentIndicator' ] section . auditors = int ( section_data [ 'Auditors' ] ) section . allows_secondary_grading = section_data [ "SecondaryGradingOption" ] primary_section = section_data [ "PrimarySection" ] if ( primary_section is not None and primary_section [ "SectionID" ] != section . section_id ) : section . is_primary_section = False section . primary_section_href = primary_section [ "Href" ] section . primary_section_id = primary_section [ "SectionID" ] section . primary_section_curriculum_abbr = primary_section [ "CurriculumAbbreviation" ] section . primary_section_course_number = primary_section [ "CourseNumber" ] else : section . is_primary_section = True section . linked_section_urls = [ ] for linked_section_type in section_data [ "LinkedSectionTypes" ] : for linked_section_data in linked_section_type [ "LinkedSections" ] : url = linked_section_data [ "Section" ] [ "Href" ] section . linked_section_urls . append ( url ) section . joint_section_urls = [ ] for joint_section_data in section_data . get ( "JointSections" , [ ] ) : url = joint_section_data [ "Href" ] section . joint_section_urls . append ( url ) section . grading_system = section_data [ 'GradingSystem' ] section . grade_submission_delegates = [ ] for del_data in section_data [ "GradeSubmissionDelegates" ] : delegate = GradeSubmissionDelegate ( person = UWPWS . get_person_by_regid ( del_data [ "Person" ] [ "RegID" ] ) , delegate_level = del_data [ "DelegateLevel" ] ) section . grade_submission_delegates . append ( delegate ) section . meetings = [ ] for meeting_data in section_data [ "Meetings" ] : meeting = SectionMeeting ( ) meeting . section = section meeting . term = section . term meeting . meeting_index = meeting_data [ "MeetingIndex" ] meeting . meeting_type = meeting_data [ "MeetingType" ] meeting . building = meeting_data [ "Building" ] if meeting_data [ "BuildingToBeArranged" ] : meeting . building_to_be_arranged = True else : meeting . building_to_be_arranged = False meeting . room_number = meeting_data [ "RoomNumber" ] if meeting_data [ "RoomToBeArranged" ] : meeting . room_to_be_arranged = True else : meeting . room_to_be_arranged = False if meeting_data [ "DaysOfWeekToBeArranged" ] : meeting . days_to_be_arranged = True else : meeting . days_to_be_arranged = False for day_data in meeting_data [ "DaysOfWeek" ] [ "Days" ] : attribute = "meets_{}" . format ( day_data [ "Name" ] . lower ( ) ) setattr ( meeting , attribute , True ) if ( len ( meeting_data [ "StartTime" ] ) and meeting_data [ "StartTime" ] != "00:00:00" ) : meeting . start_time = meeting_data [ "StartTime" ] if len ( meeting . start_time ) > 5 : meeting . start_time = meeting . start_time [ : 5 ] if ( len ( meeting_data [ "EndTime" ] ) and meeting_data [ "EndTime" ] != "00:00:00" ) : meeting . end_time = meeting_data [ "EndTime" ] if len ( meeting . end_time ) > 5 : meeting . end_time = meeting . end_time [ : 5 ] if ( meeting_data . get ( "EOS_StartDate" , None ) and meeting_data . get ( "EOS_EndDate" , None ) ) : meeting . eos_start_date = parse ( meeting_data [ "EOS_StartDate" ] ) . date ( ) meeting . eos_end_date = parse ( meeting_data [ "EOS_EndDate" ] ) . date ( ) meeting . instructors = [ ] for instructor_data in meeting_data [ "Instructors" ] : if ( instructor_data [ "TSPrint" ] or include_instructor_not_on_time_schedule ) : pdata = instructor_data [ "Person" ] if "RegID" in pdata and pdata [ "RegID" ] is not None : try : instructor = UWPWS . get_person_by_regid ( pdata [ "RegID" ] ) except Exception : instructor = Person ( uwregid = pdata [ "RegID" ] , display_name = pdata [ "Name" ] ) instructor . TSPrint = instructor_data [ "TSPrint" ] meeting . instructors . append ( instructor ) section . meetings . append ( meeting ) section . final_exam = None if "FinalExam" in section_data and section_data [ "FinalExam" ] is not None : if "MeetingStatus" in section_data [ "FinalExam" ] : final_exam = FinalExam ( ) final_data = section_data [ "FinalExam" ] status = final_data [ "MeetingStatus" ] final_exam . no_exam_or_nontraditional = False final_exam . is_confirmed = False if ( status == "2" ) or ( status == "3" ) : final_exam . is_confirmed = True elif status == "1" : final_exam . no_exam_or_nontraditional = True final_exam . building = final_data [ "Building" ] final_exam . room_number = final_data [ "RoomNumber" ] final_format = "%Y-%m-%d : %H:%M" strptime = datetime . strptime if final_data [ "Date" ] and final_data [ "Date" ] != "0000-00-00" : if final_data [ "StartTime" ] : start_string = "{} : {}" . format ( final_data [ "Date" ] , final_data [ "StartTime" ] ) final_exam . start_date = strptime ( start_string , final_format ) if final_data [ "EndTime" ] : end_string = "{} : {}" . format ( final_data [ "Date" ] , final_data [ "EndTime" ] ) try : final_exam . end_date = strptime ( end_string , final_format ) except ValueError : logger . info ( 'bad final EndTime: {}' . format ( end_string ) ) final_exam . end_date = None final_exam . clean_fields ( ) section . final_exam = final_exam return section
Returns a section model created from the passed json.