예제 #1
0
def transportation_data_save(request, tid):
    _e = log_extra(request)

    tid = int(tid)
    try:
        t = Transportation.objects.get(pk=tid)
    except Transportation.DoesNotExist:
        log.error('save transportation: invalid id: tid=%s', tid, extra=_e)
        return Fail(http_code=404, error_code=404,
                    error_msg='Unknown transportation id: {}'.format(tid))

    raw = request.POST['geojson']
    parent_id = request.POST.get('parent_id', None)

    log.info('save transportation: received data: parent=%s geojson=%s',
             parent_id, raw, extra=_e)

    try:
        parent = Submission.objects.get(submission_id=parent_id)
    except Submission.DoesNotExist:
        parent = None

    s = Submission()
    s.parent = parent
    s.ip_address = request.META['REMOTE_ADDR']
    s.user_agent = request.META['HTTP_USER_AGENT']
    s.user = request.user
    s.raw_geojson = raw
    s.source = 'web'
    s.save()

    processSubmission(s)
    if s.parsed_ok:
        s.transportation = t
        s.city = utils.capwords(s.city)
        if s.company is not None:
            s.company = utils.capwords(s.company)
    s.save()

    if s.parsed_ok:
        # Update transportation
        t.province = s.province
        t.city = s.city
        t.company = s.company
        t.number = s.number
        t.origin = s.origin
        t.destination = s.destination
        t.route = s.route
        t.submission = s
        t.save()
        log.info('save transportation: saved: sid=%d tid=%d', s.id, t.id, extra=_e)

    else:
        log.info('save transportation: invalid data: sid=%d', s.id, extra=_e)

    data = dict(submission_id=s.submission_id)
    if s.parsed_ok:
        res = OK(data, http_code=201)
    else:
        res = Fail(data, http_code=400,
                   error_code=400, error_msg='Invalid data')
    return res