Пример #1
0
    def _create_or_update_section(self, section_data, course_object):
        section_code = section_data[
            'CourseCode']  # E.g. 'ANTH088 PZ-01' not 'ANTH088'

        # First, create the skeleton Section object, or get it if it already exists
        try:
            section_object, was_created = Section.objects.get_or_create(
                term=self.current_term,
                course=course_object,
                code=section_code,
                code_slug=slugify(section_code).upper())
        except IntegrityError as e:
            # The get_or_create call should prevent this from happening most of the time, but it's still possible
            logger.error('{0} duplicate section: {1}\n'.format(
                e.message, section_code))
            return

        # Set the data for all the Section properties
        try:
            section_object.description = sanitize(section_data['Description'])
            section_object.note = BR_TAGS_REGEX.sub(
                '\n', sanitize(section_data['Note'])).strip()
            section_object.credit = float(section_data['Credits'])
            section_object.requisites = section_data['Requisites'] == 'Y'
            section_object.fee = FEE_REGEX.findall(
                unicode(section_data['Description']))
            section_object.grading_style = sanitize(
                section_data['GradingStyle'])
        except Exception as e:
            logger.error('{0} corrupted data for section {1}: {2}\n'.format(
                e.message, section_code, section_data))

        # Link the Section with its Instructor
        if section_data['Instructors']:
            for instructor in section_data['Instructors']:
                instructor_object, _ = Instructor.objects.get_or_create(
                    name=instructor['Name'])
                section_object.instructors.add(instructor_object)

        # Save the fully-formed Section
        section_object.save()

        # Create the Meeting objects for this Section
        if section_data['Schedules']:
            section_object.meeting_set.all().delete(
            )  # Delete all the old Meeting records for this Section first
            for meeting_data in section_data['Schedules']:
                if meeting_data['Weekdays'] != '':
                    parsed_meeting_data = self._parse_meeting_data(
                        meeting_data=meeting_data)
                    if parsed_meeting_data:
                        meeting_object = Meeting(
                            section=section_object,
                            monday=parsed_meeting_data['monday'],
                            tuesday=parsed_meeting_data['tuesday'],
                            wednesday=parsed_meeting_data['wednesday'],
                            thursday=parsed_meeting_data['thursday'],
                            friday=parsed_meeting_data['friday'],
                            begin=parsed_meeting_data['begin'],
                            end=parsed_meeting_data['end'],
                            campus=parsed_meeting_data['campus'],
                            location=parsed_meeting_data['location'])
                        meeting_object.save()

        if was_created:
            logger.info('ADDED: section ' + section_code)
        else:
            logger.info('UPDATED: section ' + section_code)