Exemplo n.º 1
0
    def put(self, slug, identifier):
        """Create or update one task. By default, the
        geometry must be supplied as WKB, but this can
        be overridden by adding ?geoformat=geojson to
        the URL"""

        task_geometries = []

        # Get the posted data
        taskdata = json.loads(request.data)

        exists = task_exists(slug, identifier)

        app.logger.debug("taskdata: %s" % (taskdata,))

        # abort if the taskdata does not contain geometries and it's a new task
        if not 'geometries' in taskdata:
            if not exists:
                abort(400)
        else:
            # extract the geometries
            geometries = taskdata.pop('geometries')
            app.logger.debug("geometries: %s" % (geometries,))
            app.logger.debug("features: %s" % (geometries['features'],))

            # parse the geometries
            for feature in geometries['features']:
                app.logger.debug(feature)
                osmid = feature['properties'].get('osmid')
                shape = asShape(feature['geometry'])
                t = TaskGeometry(osmid, shape)
                task_geometries.append(t)

        # there's two possible scenarios:
        # 1.    An existing task gets an update, in that case
        #       we only need the identifier
        # 2.    A new task is inserted, in this case we need at
        #       least an identifier and encoded geometries.

        # now we check if the task exists
        if exists:
            # if it does, update it
            app.logger.debug('existing task')
            task = get_task_or_404(slug, identifier)
            if not task.update(taskdata, task_geometries):
                abort(400)
        else:
            # if it does not, create it
            app.logger.debug('new task')
            new_task = Task(slug, identifier)
            new_task.update(taskdata, task_geometries)
        return {}
Exemplo n.º 2
0
def parse_task_json(data, slug, identifier, commit=True):
    """Parse task json coming in through the admin api"""

    task_geometries = []

    exists = task_exists(slug, identifier)

    # abort if the taskdata does not contain geometries and it's a new task
    if not 'geometries' in data:
        if not exists:
            abort(400)
    else:
        # extract the geometries
        geometries = data.pop('geometries')
        # parse the geometries
        for feature in geometries['features']:
            osmid = feature['properties'].get('osmid')
            shape = asShape(feature['geometry'])
            t = TaskGeometry(osmid, shape)
            task_geometries.append(t)

    # there's two possible scenarios:
    # 1.    An existing task gets an update, in that case
    #       we only need the identifier
    # 2.    A new task is inserted, in this case we need at
    #       least an identifier and encoded geometries.

    # now we check if the task exists
    if exists:
        # if it does, update it
        task = get_task_or_404(slug, identifier)
        if not task.update(data, task_geometries, commit=commit):
            abort(400)
    else:
        # if it does not, create it
        new_task = Task(slug, identifier)
        new_task.update(data, task_geometries, commit=commit)
    return True