def handle_csv(request): """ Note: not a whole lot of error detection / correction going on here, if a bad csv comes in, it'll 500 """ if request.method == 'GET': data = {} data.update(csrf(request)) return render_to_response('csv.html', data) else: f = request.FILES['csv'] contents = f.read().replace('\r\n', '\n').replace('\r', '\n') fp = StringIO.StringIO(contents) # Check if we need to skip the first line if request.POST['skip_first_line'] == 'on': next(fp) teacher = Teacher.objects.get(user=request.user) reader = csv.reader(fp) for row in reader: student = Student( student_id = row[0].strip(), first_name = row[1].strip(), last_name = row[2].strip(), phone_number = row[3].strip(), email = row[4].strip(), sms_notification_ind = (row[5].strip() == 'True'), call_notification_ind = (row[6].strip() == 'True'), email_notification_ind = (row[7].strip() == 'True'), ) student.save() student.teachers.add(teacher) student.save() return render_to_response('csv-saved.html')
def handle_csv(request): """ Note: not a whole lot of error detection / correction going on here, if a bad csv comes in, it'll 500 """ if request.method == 'GET': data = {} data.update(csrf(request)) return render_to_response('csv.html', data) else: f = request.FILES['csv'] contents = f.read().replace('\r\n', '\n').replace('\r', '\n') fp = StringIO.StringIO(contents) # Check if we need to skip the first line if request.POST['skip_first_line'] == 'on': next(fp) teacher = Teacher.objects.get(user=request.user) reader = csv.reader(fp) for row in reader: student = Student( student_id=row[0].strip(), first_name=row[1].strip(), last_name=row[2].strip(), phone_number=row[3].strip(), email=row[4].strip(), sms_notification_ind=(row[5].strip() == 'True'), call_notification_ind=(row[6].strip() == 'True'), email_notification_ind=(row[7].strip() == 'True'), ) student.save() student.teachers.add(teacher) student.save() return render_to_response('csv-saved.html')
def handle_csv(request): if request.method == 'GET': data = {} data.update(csrf(request)) return render_to_response('csv.html', data) else: f = request.FILES['csv'] contents = f.read() fp = StringIO.StringIO(contents) reader = csv.reader(fp) for row in reader: student = Student( student_id=row[0].strip(), first_name=row[1].strip(), last_name=row[2].strip(), phone_number=row[3].strip(), email=row[4].strip(), sms_notification_ind=(row[5].strip() == 'True'), call_notification_ind=(row[6].strip() == 'True'), email_notification_ind=(row[7].strip() == 'True')) student.save() return render_to_response('csv-saved.html')
def handle_csv(request): if request.method == 'GET': data = {} data.update(csrf(request)) return render_to_response('csv.html', data) else: f = request.FILES['csv'] contents = f.read() fp = StringIO.StringIO(contents) reader = csv.reader(fp) for row in reader: student = Student( student_id = row[0].strip(), first_name = row[1].strip(), last_name = row[2].strip(), phone_number = row[3].strip(), email = row[4].strip(), sms_notification_ind = (row[5].strip() == 'True'), call_notification_ind = (row[6].strip() == 'True'), email_notification_ind = (row[7].strip() == 'True') ) student.save() return render_to_response('csv-saved.html')