def __init__(self, notifier): self.semesters = {} # semester.ref: semester obj self.notifier = notifier for semester in Semester.objects.filter(ref__startswith='http://sis.rpi.edu/reg/rocs/'): self.semesters[semester.ref] = semester self.latest_semester = None if len(self.semesters) > 0: self.latest_semester = max(self.semesters.values()) self.sections_changed = False self.SectionPeriod = Synchronizer(SectionPeriod, SectionPeriod.objects.values_list('id', flat=True))
def cache_conflicts(semester_year=None, semester_month=None, semester=None, sql=True, stdout=False): assert ( semester_year and semester_month ) or semester, "Semester year & month must be provided or the semester object." import sys # trash existing conflict data... if not semester: semester = courses.Semester.objects.get(year=semester_year, month=semester_month) with transaction.atomic(): # we don't want to increment IDs too quickly (ev 25 minutes) Syncer = Synchronizer( SectionConflict, SectionConflict.objects.values_list('id', flat=True)) sections = courses.Section.objects.select_related('course', 'semester') \ .by_semester(semester).prefetch_periods() section_courses = dict_by_attr(sections, 'course') mapping = {} for id, sid1, sid2 in SectionConflict.objects.filter( semester=semester).values_list('id', 'section1', 'section2'): mapping[(sid1, sid2)] = id conflicts = [] def log(msg): sys.stdout.write(msg) sys.stdout.flush() def perform_insert(conflicts): SectionConflict.objects.bulk_create(conflicts) count = 0 for course1, course2 in itertools.combinations(section_courses.keys(), 2): for section1, section2 in itertools.product( section_courses[course1], section_courses[course2]): if section1.conflicts_with(section2): if section1.id > section2.id: section1, section2 = section2, section1 count += 1 if sql: if count % 500 == 0: perform_insert(conflicts) conflicts = [] log('.') if (section1.id, section2.id) not in mapping: log('C') conflicts.append( SectionConflict(section1=section1, section2=section2, semester=semester)) else: Syncer.exclude_id(mapping[(section1.id, section2.id)]) else: log('C') Syncer.get_or_create( section1=section1, section2=section2, semester=semester, ) if sql and conflicts: log('C') perform_insert(conflicts) log('\n') Syncer.trim(semester=semester) log('\n')