def _request_csv_tas_upload(request, f): count = 0 invalid_users = [] reader = csv.reader(f.read().splitlines()) next(reader, None) # skip the headers for r in reader: if len(r) != 1: raise ValueError odin = r[0].strip() try: user = User.objects.get(username=odin) user.is_ta = 1 user.is_active = 1 user.save() except User.DoesNotExist: first_name, last_name = get_ldap_user_data(odin) if first_name is not None: user = User() user.username = odin user.first_name = first_name user.last_name = last_name user.is_ta = 1 user.is_active = 1 user.save() count = count + 1 else: invalid_users.append(odin) messages.success(request, "CSV file uploaded. %s new TAs added!" % count) if len(invalid_users) > 0: messages.warning(request, "There are some invalid Odin usernames: %s. They were not created." % ", ".join(map(str, invalid_users)))
def _request_csv_courses_upload(request, f): new_course = 0 invalid_users = [] reader = csv.reader(f.read().splitlines()) next(reader, None) # skip the headers for r in reader: user = None course = None course_no, section_no, course_name, max_tas, odin = [item.strip() for item in r] # IndexError or PackingError try: user = User.objects.get(username=odin) except User.DoesNotExist: first_name, last_name = get_ldap_user_data(odin) if first_name is not None: user = User() user.username = odin user.first_name = first_name user.last_name = last_name else: invalid_users.append(odin + " for course " + course_no + "-" + section_no) try: course = Course.objects.get(course_no=course_no, section_no=section_no) except Course.DoesNotExist: course = Course() course.course_no = course_no course.section_no = section_no course.course_name = course_name course.max_tas = max_tas new_course = new_course + 1 if user is not None and course is not None: user.is_faculty = 1 user.is_active = 1 user.save() course.save() course.faculties.add(user) course.save() if len(invalid_users) > 0: messages.warning(request, "There were invalid Odin usernames: %s." % ", ".join(map(str, invalid_users))) messages.success(request, "CSV file uploaded. %s new courses added!" % new_course)