def import_cohort_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):
                district_code=line[COHORT_CSV_COL_DISTRICT_CODE]
                district_id=District.objects.get(code=district_code).id

                validate_cohort_cvs_line(line)
                cohort=Cohort()
                cohort.code=line[COHORT_CSV_COL_CODE]
                cohort.district_id=district_id
                cohort.licences=int(line[COHORT_CSV_COL_LICENSES])
                cohort.term_months=int(line[COHORT_CSV_COL_TERM_MONTHS])
                cohort.start_date=line[COHORT_CSV_COL_START_DATE]
                cohort.save()
                count_success=count_success+1

            db.transaction.commit()
            message={"success": True,
                "message":"Success! %s cohort(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 impored.' % (e,count_success+1)}

    return HttpResponse(json.dumps(message))
def cohort_submit(request):
    if not request.user.is_authenticated:
        raise Http404
    try:
        if request.POST.get('id'):
            d=Cohort(request.POST['id'])
        else:
            d=Cohort()
        d.id==request.POST['id']
        d.code=request.POST['code']
        d.licences=request.POST['licences']
        d.term_months=request.POST['term_months']
        d.start_date=request.POST['start_date']
        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}))
Esempio n. 3
0
def cohort_submit(request):
    if not request.user.is_authenticated:
        raise Http404
    try:
        if request.POST.get("id"):
            d = Cohort(request.POST["id"])
        else:
            d = Cohort()
        d.id == request.POST["id"]
        d.code = request.POST["code"]
        d.licences = request.POST["licences"]
        d.term_months = request.POST["term_months"]
        d.start_date = request.POST["start_date"]
        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}))