Example #1
0
File: views.py Project: hj91/markup
def makemark(request):
    mark_form = MarkForm(request.POST)
    if mark_form.is_valid():
        mark_data = {'points_obj': mark_form.cleaned_data['points_obj'], 'country_code': mark_form.cleaned_data['country_code']}
        common.save_new_mark_with_data(mark_data)
    else:
        mark_form = MarkForm()
    return render_response(request, 'makemark.html', {'form': mark_form})
Example #2
0
def makemark(request):
    mark_form = MarkForm(request.POST)
    if mark_form.is_valid():
        mark_data = {
            'points_obj': mark_form.cleaned_data['points_obj'],
            'country_code': mark_form.cleaned_data['country_code']
        }
        common.save_new_mark_with_data(mark_data)
    else:
        mark_form = MarkForm()
    return render_response(request, 'makemark.html', {'form': mark_form})
def save_mark(request):
    #    Default response
    response = {'success': False}

    #    Check for mandatory POST data
    if 'points_obj_simplified' in request.POST:
        #    Construct mark data
        mark_data = {
            'points_obj_simplified': request.POST['points_obj_simplified']
        }
        if 'points_obj' in request.POST:
            mark_data['points_obj'] = request.POST['points_obj']
        if 'country_code' in request.POST:
            mark_data['country_code'] = request.POST['country_code']
            if 'invite' in request.POST:
                mark_data['invite'] = request.POST['invite']
                if 'contributor_locale' in request.POST:
                    mark_data['contributor_locale'] = request.POST[
                        'contributor_locale']
            if 'contributor' in request.POST:
                mark_data['contributor'] = request.POST['contributor']

        try:
            #    Confirm that what we're getting is in fact JSON
            if 'points_obj' in request.POST:
                json = simplejson.loads(request.POST['points_obj'])
                validate_json(json, False)
            json_simplified = simplejson.loads(
                request.POST['points_obj_simplified'])
            validate_json(json_simplified, True)
            #    Save new mark, handled by common.py
            new_mark_reference = common.save_new_mark_with_data(
                mark_data, request.META['REMOTE_ADDR'])
            #    Successful response, returning new mark reference
            response['mark_reference'] = new_mark_reference
        except ValueError:
            response['error'] = _(
                'mark was too large. Please try drawing a smaller mark. Thanks!'
            )
            return HttpResponseBadRequest(simplejson.dumps(response),
                                          'application/json')
        except KeyError:
            response['error'] = _('invalid JSON in the POST request')
            return HttpResponseBadRequest(simplejson.dumps(response),
                                          'application/json')
    else:
        #    Error response
        response['error'] = _('missing data in POST request')
        json_response = simplejson.dumps(response)
        return HttpResponseServerError(json_response, 'application/json')

    #    Return response as json
    json_response = simplejson.dumps(response)
    return HttpResponse(json_response, 'application/json')
Example #4
0
def save_mark(request):
    #    Default response
    response = {'success': False}

    #    Check for mandatory POST data
    if 'points_obj_simplified' in request.POST:
    #    Cosntruct mark data
        mark_data = {'points_obj_simplified': request.POST['points_obj_simplified']}
        if 'points_obj' in request.POST:
            mark_data['points_obj'] = request.POST['points_obj']
        if 'country_code' in request.POST:
            mark_data['country_code'] = request.POST['country_code']
            if 'invite' in request.POST:
                mark_data['invite'] = request.POST['invite']
                if 'contributor_locale' in request.POST:
                    mark_data['contributor_locale'] = request.POST['contributor_locale']
            if 'contributor' in request.POST:
                mark_data['contributor'] = request.POST['contributor']

        try:
            #    Confirm that what we're getting is in fact JSON
            if 'points_obj' in request.POST:
                json = simplejson.loads(request.POST['points_obj'])
                validate_json(json, False)
            json_simplified = simplejson.loads(request.POST['points_obj_simplified'])
            validate_json(json_simplified, True)
            #    Save new mark, handled by common.py
            new_mark_reference = common.save_new_mark_with_data(mark_data, request.META['REMOTE_ADDR'])
            #    Successful response, returning new mark reference
            response['mark_reference'] = new_mark_reference
        except ValueError:
            response['error'] = _('mark was too large. Please try drawing a smaller mark. Thanks!')
            return HttpResponseBadRequest(simplejson.dumps(response), 'application/json')
        except KeyError:
            response['error'] = _('invalid JSON in the POST request')
            return HttpResponseBadRequest(simplejson.dumps(response), 'application/json')
    else:
        #    Error response
        response['error'] = _('missing data in POST request')
        json_response = simplejson.dumps(response)
        return HttpResponseServerError(json_response, 'application/json')

    #    Return response as json
    json_response = simplejson.dumps(response)
    return HttpResponse(json_response, 'application/json')