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'))