コード例 #1
0
def kv_update(request, kv_class, kv_pk):
    """
    POST to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/update/

    with parameters like:

    {
        'key': 'key_string'
        'value': 'value_string'
        'obj_pk': 1
    }
    Status Codes:
        * 200 - Object updated
        * 400 - Issues during update
    """
    key, value, errors = get_kv(request)
    if errors:
        return HttpResponse(status=400,
                            content=json.dumps({
                                'success': False,
                                'message': errors
                            }))
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(status=404, content=json.dumps({'success': False}))
    try:
        kv.key = key
        kv.value = value
        kv.clean()
        kv.save()
        resp = {
            'status':
            200,
            'content':
            json.dumps({
                'success': True,
                'key': kv.key,
                'value': kv.value
            })
        }
    except ValidationError, e:
        resp = {
            'status': 400,
            'content': json.dumps({
                'success': False,
                'message': str(e)
            })
        }
コード例 #2
0
def kv_detail(request, kv_class, kv_pk):
    """
    GET to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/detail/

    Returns a single KV instance.
    """
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(status=404, content=json.dumps({'success': False}))
    return HttpResponse(json.dumps(kv.get_bundle()))
コード例 #3
0
def kv_delete(request, kv_class, kv_pk):
    """
    POST to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/delete/
    Status Codes:
        * 204 - Requests Fulfilled
        * 400 - Issues during delete
    """
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(status=404, content=json.dumps({'success': False}))
    kv.delete()
    return HttpResponse(status=204, content=json.dumps({'success': True}))
コード例 #4
0
ファイル: api.py プロジェクト: Marlburo/inventory
def kv_update(request, kv_class, kv_pk):
    """
    POST to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/update/

    with parameters like:

    {
        'key': 'key_string'
        'value': 'value_string'
        'obj_pk': 1
    }
    Status Codes:
        * 200 - Object updated
        * 400 - Issues during update
    """
    key, value, errors = get_kv(request)
    if errors:
        return HttpResponse(
            status=400, content=json.dumps(
                {'success': False, 'message': errors}
            )
        )
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(
            status=404, content=json.dumps({'success': False})
        )
    try:
        kv.key = key
        kv.value = value
        kv.clean()
        kv.save()
        resp = {
            'status': 200,
            'content': json.dumps(
                {'success': True, 'key': kv.key, 'value': kv.value}
            )
        }
    except ValidationError, e:
        resp = {
            'status': 400,
            'content': json.dumps({'success': False, 'message': str(e)})
        }
コード例 #5
0
ファイル: api.py プロジェクト: Marlburo/inventory
def kv_detail(request, kv_class, kv_pk):
    """
    GET to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/detail/

    Returns a single KV instance.
    """
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(
            status=404, content=json.dumps({'success': False})
        )
    return HttpResponse(
        json.dumps(kv.get_bundle())
    )
コード例 #6
0
ファイル: api.py プロジェクト: Marlburo/inventory
def kv_delete(request, kv_class, kv_pk):
    """
    POST to:
    /core/keyvalue/api/<kv_class>/<kv_pk>/delete/
    Status Codes:
        * 204 - Requests Fulfilled
        * 400 - Issues during delete
    """
    Klass = resolve_class(kv_class)
    KVKlass = Klass.keyvalue_set.related.model
    try:
        kv = KVKlass.objects.get(pk=kv_pk)
    except KVKlass.DoesNotExist:
        return HttpResponse(
            status=404, content=json.dumps({'success': False})
        )
    kv.delete()
    return HttpResponse(
        status=204,
        content=json.dumps({'success': True})
    )