Ejemplo n.º 1
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    try:
        if not obj_id:
            my_model = get_active_model(request)
            if my_model:
                ret = my_model.serialize()
            else:
                ret = get_specifications(request, implemented_types)
        else:
            obj = get_session_object(obj_id, request)
            if obj:
                if ObjectImplementsOneOf(obj, implemented_types):
                    set_active_model(request, obj.id)
                    ret = obj.serialize()
                else:
                    # we refer to an object, but it is not a Model
                    raise cors_exception(request, HTTPBadRequest)
            else:
                raise cors_exception(request, HTTPNotFound)
    finally:
        gnome_sema.release()

    return ret
Ejemplo n.º 2
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)

    session_lock = acquire_session_lock(request)
    log.info('  session lock acquired (sess:{}, thr_id: {})'.format(
        id(session_lock),
        current_thread().ident))

    try:
        if obj_id is None:
            my_model = get_active_model(request)
            if my_model is not None:
                ret = my_model.serialize(options=web_ser_opts)

        if ret is None:
            ret = get_object(request, implemented_types)
    finally:
        session_lock.release()
        log.info('  session lock released (sess:{}, thr_id: {})'.format(
            id(session_lock),
            current_thread().ident))

    return ret
Ejemplo n.º 3
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    try:
        if not obj_id:
            my_model = get_active_model(request)
            if my_model:
                ret = my_model.serialize()
            else:
                ret = get_specifications(request, implemented_types)
        else:
            obj = get_session_object(obj_id, request)
            if obj:
                if ObjectImplementsOneOf(obj, implemented_types):
                    set_active_model(request, obj.id)
                    ret = obj.serialize()
                else:
                    # we refer to an object, but it is not a Model
                    raise cors_exception(request, HTTPBadRequest)
            else:
                raise cors_exception(request, HTTPNotFound)
    finally:
        gnome_sema.release()

    return ret
Ejemplo n.º 4
0
def get_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:
          - return the current active model if it exists or...
          - return the specification.
    '''
    ret = None
    obj_id = obj_id_from_url(request)
    gnome_sema = request.registry.settings['py_gnome_semaphore']
    gnome_sema.acquire()

    if not obj_id:
        my_model = get_active_model(request.session)
        if my_model:
            ret = my_model.serialize()
        else:
            # - return a Model specification
            ret = get_specifications(request, implemented_types)
    else:
        obj = get_session_object(obj_id, request.session)
        if obj:
            if ObjectImplementsOneOf(obj, implemented_types):
                set_active_model(request.session, obj.id)
                ret = obj.serialize()
            else:
                raise HTTPUnsupportedMediaType()
        else:
            raise HTTPNotFound()

    gnome_sema.release()
    return ret
Ejemplo n.º 5
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    log.info('location_api.cors_origins_for("get") = {0}'
             .format(location_api.cors_origins_for('get')))

    # first, lets just query that we can get to the data
    locations_dir = request.registry.settings['locations_dir']
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, _dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(ujson.load(open(join(path, f), 'r')))
             for f in filenames
             if f == 'compiled.json']

            [location_file_dirs.append(join(path, basename(path) + '_save'))
             for f in filenames
             if f == 'compiled.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            session_lock = acquire_session_lock(request)
            log.info('  session lock acquired (sess:{}, thr_id: {})'
                     .format(id(session_lock), current_thread().ident))

            try:
                location_file = location_file_dirs[matching[0][0]]
                log.info('load location: {0}'.format(location_file))
                load_location_file(location_file, request)
            except Exception:
                raise cors_exception(request, HTTPInternalServerError,
                                     with_stacktrace=True)
            finally:
                session_lock.release()
                log.info('  session lock released (sess:{}, thr_id: {})'
                         .format(id(session_lock), current_thread().ident))

            return matching[0][1]
        else:
            raise cors_exception(request, HTTPNotFound)
    else:
        features = [Feature(geometry=Point(c['geometry']['coordinates']),
                            properties={'title': c['name'],
                                        'slug': slugify.slugify_url(c['name']),
                                        'content': c['steps']
                                        }
                            )
                    for c in location_content]
        return FeatureCollection(features)
Ejemplo n.º 6
0
def get_geojson(request, implemented_types):
    '''Returns the GeoJson for a Gnome Map object.'''
    obj = get_session_object(obj_id_from_url(request), request)

    if obj:
        if ObjectImplementsOneOf(obj, implemented_types):
            return obj.to_geojson()
        else:
            raise cors_exception(request, HTTPNotImplemented)
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 7
0
def get_geojson(request, implemented_types):
    '''Returns the GeoJson for a Gnome Map object.'''
    obj = get_session_object(obj_id_from_url(request), request)

    if obj:
        if ObjectImplementsOneOf(obj, implemented_types):
            return obj.to_geojson()
        else:
            raise cors_exception(request, HTTPNotImplemented)
    else:
        raise cors_exception(request, HTTPNotFound)
Ejemplo n.º 8
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    log.info('location_api.cors_origins_for("get") = {0}'
             .format(location_api.cors_origins_for('get')))

    # first, lets just query that we can get to the data
    locations_dir = request.registry.settings['locations_dir']
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, _dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(ujson.load(open(join(path, f), 'r')))
             for f in filenames
             if f == 'compiled.json']

            [location_file_dirs.append(path + "/" + basename(path) + '_save')
             for f in filenames
             if f == 'compiled.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            gnome_sema = request.registry.settings['py_gnome_semaphore']
            gnome_sema.acquire()
            try:
                location_file = location_file_dirs[matching[0][0]]
                log.info('load location: {0}'.format(location_file))
                load_location_file(location_file, request)
            except:
                raise cors_exception(request, HTTPInternalServerError,
                                     with_stacktrace=True)
            finally:
                gnome_sema.release()

            return matching[0][1]
        else:
            raise cors_exception(request, HTTPNotFound)
    else:
        features = [Feature(geometry=Point(c['geometry']['coordinates']),
                            properties={'title': c['name'],
                                        'slug': slugify.slugify_url(c['name']),
                                        'content': c['steps']
                                        }
                            )
                    for c in location_content]
        return FeatureCollection(features)
Ejemplo n.º 9
0
def get_location(request):
    '''
        Returns a List of Location objects in JSON if no object specified.
    '''
    # first, lets just query that we can get to the data
    locations_dir = get_locations_dir_from_config(request)
    base_len = len(locations_dir.split(sep))
    location_content = []
    location_file_dirs = []

    for (path, dirnames, filenames) in walk(locations_dir):
        if len(path.split(sep)) == base_len + 1:
            [location_content.append(json.load(open(join(path, f), 'r'),
                                               object_pairs_hook=OrderedDict))
             for f in filenames
             if f[-12:] == '_wizard.json']
            [location_file_dirs.append(join(path, f[:-12] + '_save'))
             for f in filenames
             if f[-12:] == '_wizard.json']

    slug = obj_id_from_url(request)
    if slug:
        matching = [(i, c) for i, c in enumerate(location_content)
                    if slugify.slugify_url(c['name']) == slug]
        if matching:
            gnome_sema = request.registry.settings['py_gnome_semaphore']
            gnome_sema.acquire()
            try:
                location_file = location_file_dirs[matching[0][0]]
                load_location_file(location_file, request.session)
            finally:
                gnome_sema.release()

            return matching[0][1]
        else:
            return HTTPNotFound()
        pass
    else:
        return FeatureCollection(location_content).serialize()