def handle(self, *args, **options): Course.objects.all().delete() # do NOT want this any more, we should merge gracefully. from aspc.data.scraped_courses import courses for k, scraped_course in courses.items(): code_slug = slugify(scraped_course['code']).upper() dept_code = self.deptrx.match(code_slug).groups()[0] try: course = Course.objects.get(code_slug=code_slug) self.stdout.write('found existing for code: "%s", dept_code: "%s"\n' % (course.code, dept_code)) except Course.DoesNotExist: course = Course(code=scraped_course['code'], code_slug=code_slug) self.stdout.write('adding new for code: "%s", dept_code: "%s"\n' % (course.code, dept_code)) course.name = scraped_course['name'] course.instructor = scraped_course['instructor'] course.grading_style = scraped_course['grading_style'] course.description = scraped_course['description'] course.note = scraped_course['note'] course.credit = float(scraped_course['credit']) course.spots = int(scraped_course['slots']) try: course.primary_department = Department.objects.get(code=dept_code) except Department.DoesNotExist: course.primary_department = None course.save() # Save first then run m2m course.departments.clear() # On the off chance that a course has been # removed from one subject area / dept between imports # we don't want to keep stale dept relationships around for dcode in scraped_course['depts']: course.departments.add(Department.objects.get(code=dcode)) if not course.primary_department: if course.departments.count() == 0: course.delete() self.stdout.write('Failed to add %s because it wasn\'t in a department' % course.code) else: smallest_dept = course.departments.annotate(num_courses=Count('primary_course_set')).distinct().order_by('-num_courses')[0] course.primary_department = smallest_dept course.save() course.meeting_set.all().delete() # don't want to keep stale meetings, can safely re-create all because schedule m2ms to course for mtg in scraped_course['mtgs']: meeting_breakout = parse_meeting(mtg) if not meeting_breakout: continue m, t, w, r, f, begin, end, loc = meeting_breakout new_meeting = Meeting(course=course) new_meeting.monday = m new_meeting.tuesday = t new_meeting.wednesday = w new_meeting.thursday = r new_meeting.friday = f new_meeting.begin = begin new_meeting.end = end campus_code = keyword_regex.findall(loc)[0] if campus_code not in CAMPUSES_LOOKUP.keys(): continue new_meeting.campus = CAMPUSES_LOOKUP[campus_code] new_meeting.location = loc[11:] new_meeting.save() self.stdout.write('Successfully added course "%s"\n' % course.name.encode('utf-8'))
def refresh_meetings(cursor, course): # Create Meeting objects for the course's meetings meetings = cursor.execute("""SELECT DISTINCT(MeetTime), Weekdays, Campus, Building, Room FROM pom.Courses AS pc JOIN pom.Meetings AS pm ON (pc.CourseCode = pm.CourseCode) WHERE pm.CourseCode = ? AND Weekdays IS NOT NULL AND MeetTime NOT LIKE '%00:00-00:00AM. %';""", course.cx_code.encode('utf8')).fetchall() # Query explanation: Null weekdays can't be displayed on the schedule, so # they don't make any sense to store (or try to parse). Non-existent # meeting times likewise shouldn't be parsed. # # The DISTINCT() is to eliminate duplicate rows, at least until ITS # fixes the bug in their updater. # Clear old meetings course.meeting_set.all().delete() for mtg in meetings: # 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( course=course, monday=monday, tuesday=tuesday, wednesday=wednesday, thursday=thursday, friday=friday, begin=begin, end=end, campus=campus, location=location ) try: meeting.save() except Exception: code.interact(local=locals())