Beispiel #1
0
def update_model(request):
    '''
        Returns Model object in JSON.
        - This method varies slightly from the common object method in that
          if we don't specify a model ID, we:
          - update the current active model if it exists or...
          - generate a 'Not Found' exception.
    '''
    ret = None
    try:
        json_request = json.loads(request.body)
    except:
        raise HTTPBadRequest()

    if not JSONImplementsOneOf(json_request, implemented_types):
        raise HTTPNotImplemented()

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    obj_id = obj_id_from_req_payload(json_request)
    if obj_id:
        my_model = get_session_object(obj_id, request.session)
    else:
        my_model = get_active_model(request.session)

    if my_model:
        if UpdateObject(my_model, json_request,
                        get_session_objects(request.session)):
            set_session_object(my_model, request.session)
        ret = my_model.serialize()
    else:
        gnome_sema.release()
        raise HTTPNotFound()

    gnome_sema.release()

    return ret
Beispiel #2
0
def create_model(request):
    '''
        Creates a new model
    '''
    log_prefix = 'req({0}): create_object():'.format(id(request))
    print '>>', log_prefix

    try:
        json_request = json.loads(request.body)
    except:
        json_request = None

    if json_request and not JSONImplementsOneOf(json_request,
                                                implemented_types):
        raise HTTPNotImplemented()

    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()
    print '  ', log_prefix, 'semaphore acquired...'

    try:
        init_session_objects(request.session, force=True)
        if json_request:
            new_model = CreateObject(json_request,
                                     get_session_objects(request.session))
        else:
            new_model = Model()
        set_session_object(new_model, request.session)
        set_session_object(new_model._map, request.session)
        set_active_model(request.session, new_model.id)
    finally:
        gnome_sema.release()
        print '  ', log_prefix, 'semaphore released...'

    print '<<', log_prefix
    return new_model.serialize()