Пример #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()
		try:
			section_object.update_ratings()
		except:
			pass

		# 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)
Пример #2
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)
    def refresh_one_section(self, object, new_data):
        object.grading_style = _sanitize(new_data['GradingStyle'])
        object.description = _sanitize(new_data['Description'])
        object.note = BR_TAGS.sub('\n', _sanitize(new_data['Note'])).strip()
        object.credit = float(new_data['Credits'])
        object.requisites = new_data['Requisites'] == 'Y'

        # Check for fees or prerequisites
        object.fee = FEE_REGEX.findall(unicode(object.description))

        object.save()

        object.course.name = _sanitize(new_data['Name'])

        if new_data['Instructors']:
            for instructor in new_data['Instructors']:
                instructor_object, _ = Instructor.objects.get_or_create(name=instructor['Name'])
                object.instructors.add(instructor_object)

        # refresh meetings
        # Clear old meetings
        object.meeting_set.all().delete()

        if new_data['Schedules']:
            for mtg in new_data['Schedules']:
                if mtg['Weekdays'] != '':
                    # Parse weekdays

                    weekdays = mtg['Weekdays']
                    monday = True if weekdays.find('M') != -1 else False
                    tuesday = True if weekdays.find('T') != -1 else False
                    wednesday = True if weekdays.find('W') != -1 else False
                    thursday = True if weekdays.find('R') != -1 else False
                    friday = True if weekdays.find('F') != -1 else False

                    # Parse times
                    try:
                        start, start_pm, end, end_pm = TIME_REGEX.findall(mtg['MeetTime'])[0]
                    except IndexError:
                        continue

                    if end_pm == 'PM':
                        end_pm = True
                    else:
                        end_pm = False

                    if start_pm in ('AM', 'PM'):
                        start_pm = True if start_pm == 'PM' else False
                    else:
                        start_pm = end_pm

                    start_h, start_m = [int(a) for a in start.split(':')]
                    end_h, end_m = [int(a) for a in end.split(':')]

                    # Correct times to 24hr form

                    if end_pm and end_h != 12:
                        end_h += 12
                    if start_pm and start_h != 12:
                        start_h += 12
                    begin = time(start_h, start_m)
                    end = time(end_h, end_m)

                    # Get campus
                    try:
                        campus_code = mtg['Campus'].split(' ')[0]
                        campus = CAMPUSES_LOOKUP[campus_code]
                    except Exception:
                        campus = CAMPUSES_LOOKUP['?']

                    # Get location

                    if mtg['Room'] and mtg['Building']:
                        room_number = ROOM_REGEX.findall(mtg['MeetTime'])[0]
                        location = "{0}, {1}".format(mtg['Building'], room_number)
                        # special case for Keck building / CU campus
                        if mtg['Building'] == u'Keck Science Center':
                            campus = CAMPUSES_LOOKUP['KS']
                    else:
                        location = ''

                    meeting = Meeting(
                        section=object,
                        monday=monday,
                        tuesday=tuesday,
                        wednesday=wednesday,
                        thursday=thursday,
                        friday=friday,
                        begin=begin,
                        end=end,
                        campus=campus,
                        location=location
                    )
                    meeting.save()

        dcode = ''
        for x in object.course.code:
            if x.isalpha():
                dcode += x
            else:
                break
        try:
            object.course.primary_department = Department.objects.get(code=dcode)
            object.course.save()
        except Department.DoesNotExist:
            self.stdout.write(
                'unknown department "%s" for section "%s" - deleting...\n' % (dcode, object.code))
            object.delete()
            return

        object.save()

        self.stdout.write('section "%s" added\n' % object.code)