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}))
Exemplo n.º 2
0
def district_submit(request):
    if not request.user.is_authenticated:
        raise Http404
    state_id = request.POST["state_id"]
    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 = request.POST["state_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))