def district_submit(request): if not request.user.is_authenticated: raise Http404 state_id = request.POST['state_id'] state = State.objects.get(id=state_id) safe_state = re.sub(' ', '', state.name) try: if request.POST.get('id'): d = District(request.POST['id']) else: d = District() d.code = request.POST['code'] d.name = request.POST['name'] d.state_id = state_id d.save() s = School() s.name = 'Multiple Schools' s.district = d s.code = 'pepper' + safe_state + str(request.POST['code']) s.save() except Exception as e: db.transaction.rollback() return HttpResponse(json.dumps({'success': False,'error':'%s' % e})) return HttpResponse(json.dumps({'success': True}))
def import_school_submit(request): if request.method == 'POST': f=request.FILES['file'] district_id=request.POST.get('district_id') count_success=0 try: # --- THIS FAILS ON SING COLUMN CVS --- # dialect = csv.Sniffer().sniff(f.read(1024), delimiters=";,\t",quotechar='\n') # f.seek(0) # r=csv.reader(f,dialect) r=csv.reader(f,delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i,line in enumerate(r): exist=validate_school_cvs_line(line,district_id) school_name=line[SCHOOL_CSV_COL_NAME] school_code=line[SCHOOL_CSV_COL_CODE] school = School() school.name=school_name school.code=school_code school.district_id=district_id school.save() count_success=count_success+1 # commit when all lines imported db.transaction.commit() # success information message={"success": True, "message":"Success! %s school(s) imported." % (count_success), "count_success":count_success } except Exception as e: db.transaction.rollback() # failure information message={'success': False,'error':'Import error: %s. At cvs line: %s, Nothing impored.' % (e,count_success+1)} return HttpResponse(json.dumps(message))
def school_submit(request): if not request.user.is_authenticated: raise Http404 try: if request.POST.get('id'): d=School(request.POST['id']) else: d=School() d.name=request.POST['name'] d.code=request.POST['code'] d.district_id=request.POST['district_id'] d.save() except Exception as e: db.transaction.rollback() return HttpResponse(json.dumps({'success': False,'error':'%s' % e})) return HttpResponse(json.dumps({'success': True}))
def school_submit(request): if not request.user.is_authenticated: raise Http404 try: if request.POST.get("id"): d = School(request.POST["id"]) else: d = School() d.name = request.POST["name"] d.district_id = request.POST["district_id"] d.save() except Exception as e: db.transaction.rollback() return HttpResponse(json.dumps({"success": False, "error": "%s" % e})) return HttpResponse(json.dumps({"success": True}))
def import_district_submit(request): message = {"success": True} if request.method == 'POST': f = request.FILES['file'] count_success = 0 try: r = csv.reader(f, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i, line in enumerate(r): state_name = line[DISTRICT_CSV_COL_STATE_NAME] state_id = State.objects.get(name=state_name).id safe_state = re.sub(' ', '', state_name) validate_district_cvs_line(line) d = District() d.code = line[DISTRICT_CSV_COL_CODE] d.name = line[DISTRICT_CSV_COL_NAME] d.state_id = state_id d.save() s = School() s.name = 'Multiple Schools' s.district = d s.code = 'pepper' + safe_state + str(line[DISTRICT_CSV_COL_CODE]) s.save() count_success += 1 db.transaction.commit() message = {"success": True, "message": "Success! %s district(s) imported." % count_success, "count_success": count_success } except Exception as e: db.transaction.rollback() message = {'success': False, 'error': 'Import error: %s. At cvs line: %s, Nothing imported.' % (e, count_success + 1) } return HttpResponse(json.dumps(message))