Beispiel #1
0
    def post(self, slug, identifier):
        """create one task."""

        if not re.match("^[\w\d_-]+$", identifier):
            abort(400,
                  message='identifier should contain only a-z, A-Z, 0-9, _, -')

        # Parse the posted data
        try:
            app.logger.debug(request.data)
            t = json_to_task(slug,
                             json.loads(request.data),
                             identifier=identifier)
            db.session.add(t)
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(
                    409,
                    message=
                    'You posted a task ({identifier}) that already existed: {message}'
                    .format(identifier=t.identifier, message=e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 201
Beispiel #2
0
    def post(self, slug):

        """bulk create"""

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

        # debug output number of tasks being posted
        app.logger.debug('posting {number} tasks...'.format(number=len(data)))

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(400, 'more than 5000 tasks in bulk create')

        try:
            for task in data:
                if not 'identifier' in task:
                    abort(400, 'task must have identifier')
                if not re.match("^[\w\d_-]+$", task['identifier']):
                    abort(400, 'identifier should contain only a-z, A-Z, 0-9, _, -')
                if not 'geometries' in task:
                    abort(400, 'new task must have geometries')
                t = json_to_task(slug, task)
                db.session.add(t)

            # commit all dirty tasks at once.
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e.message)
                db.session.rollback()
                abort(409, 'you posted a task that already existed: {}'.format(e.message))
            else:
                app.logger.warn(e.message)
                abort(500, 'something unexpected happened')
        return {}, 200
Beispiel #3
0
    def post(self, slug):
        """bulk create tasks"""

        # Get the posted data
        data = json.loads(request.data)
        app.logger.debug(len(data))
        app.logger.debug(app.config['MAX_TASKS_BULK_UPDATE'])

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(
                400,
                message=
                'more than the max number of allowed tasks ({})in bulk create'.
                format(app.config['MAX_TASKS_BULK_UPDATE']))

        # debug output number of tasks being posted
        app.logger.debug('posting {number} tasks...'.format(number=len(data)))

        for task in data:
            app.logger.debug(task)
            if not isinstance(task['identifier'], basestring):
                app.logger.debug('identifier not a string')
                abort(400, message='task identifier must exist and be string')
            existingTask = get_task_or_none(slug, task['identifier'])
            if existingTask is None:
                if not re.match("^[\w\d_-]+$", task['identifier']):
                    app.logger.debug(
                        'identifier should contain only a-z, A-Z, 0-9, _, -')
                    abort(400,
                          message=
                          'identifier should contain only a-z, A-Z, 0-9, _, -')
                if 'geometries' not in task:
                    app.logger.debug('new task must have geometries')
                    abort(400, message='new task must have geometries')
                t = json_to_task(slug, task)
                db.session.add(t)
                # commit all dirty tasks at once.
            else:
                app.logger.debug(
                    "Skipping task ({identifier}) as it already exists.".
                    format(identifier=task['identifier']))
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(
                    409,
                    message=
                    'You tried to post a task ({identifier}) that already existed: {message}'
                    .format(identifier=task['identifier'], message=e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 201
Beispiel #4
0
    def put(self, slug):
        """bulk update"""

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

        # debug output number of tasks being put
        app.logger.debug('putting {number} tasks...'.format(number=len(data)))

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(400, message='more than 5000 tasks in bulk update')

        for task in data:
            if not isinstance(task['identifier'], basestring):
                abort(400, message='task identifier must exist and be string')
            if not re.match("^[\w\d_-]+$", task['identifier']):
                abort(
                    400,
                    message='identifier should contain only a-z, A-Z, 0-9, _, -'
                )
            existingTask = get_task_or_none(slug, task['identifier'])
            if existingTask is None:
                t = json_to_task(slug, task)
            else:
                t = json_to_task(slug, task, task=existingTask)
            db.session.add(t)

        # commit all dirty tasks at once.
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(409,
                      message='the session and the database did not agree: {}'.
                      format(e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 200
Beispiel #5
0
    def put(self, slug):

        """bulk update"""

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

        # debug output number of tasks being put
        app.logger.debug('putting {number} tasks...'.format(number=len(data)))

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(400, message='more than 5000 tasks in bulk update')

        for task in data:
            if not isinstance(task['identifier'], basestring):
                abort(400, message='task identifier must exist and be string')
            if not re.match("^[\w\d_-]+$", task['identifier']):
                abort(400, message='identifier should contain only a-z, A-Z, 0-9, _, -')
            existingTask = get_task_or_none(slug, task['identifier'])
            if existingTask is None:
                t = json_to_task(slug, task)
            else:
                t = json_to_task(slug,
                                 task,
                                 task=existingTask)
            db.session.add(t)

        # commit all dirty tasks at once.
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(409, message='the session and the database did not agree: {}'.format(e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 200
Beispiel #6
0
    def post(self, slug):
        """bulk create tasks"""

        # Get the posted data
        data = json.loads(request.data)
        app.logger.debug(len(data))
        app.logger.debug(app.config['MAX_TASKS_BULK_UPDATE'])

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(400, message='more than the max number of allowed tasks ({})in bulk create'.format(app.config['MAX_TASKS_BULK_UPDATE']))

        # debug output number of tasks being posted
        app.logger.debug('posting {number} tasks...'.format(number=len(data)))

        for task in data:
            app.logger.debug(task)
            if not isinstance(task['identifier'], basestring):
                app.logger.debug('identifier not a string')
                abort(400, message='task identifier must exist and be string')
            existingTask = get_task_or_none(slug, task['identifier'])
            if existingTask is None:
                if not re.match("^[\w\d_-]+$", task['identifier']):
                    app.logger.debug('identifier should contain only a-z, A-Z, 0-9, _, -')
                    abort(400, message='identifier should contain only a-z, A-Z, 0-9, _, -')
                if 'geometries' not in task:
                    app.logger.debug('new task must have geometries')
                    abort(400, message='new task must have geometries')
                t = json_to_task(slug, task)
                db.session.add(t)
                # commit all dirty tasks at once.
            else:
                app.logger.debug("Skipping task ({identifier}) as it already exists.".format(identifier=task['identifier']))
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(409, message='You tried to post a task ({identifier}) that already existed: {message}'.format(identifier=task['identifier'], message=e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 201
Beispiel #7
0
    def put(self, slug, identifier):
        """update one task."""

        # Parse the posted data
        t = json_to_task(
            slug,
            json.loads(request.data),
            task=get_task_or_404(slug, identifier))
        db.session.add(t)
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(409, message='The session and the database did not agree: {}'.format(e))
            else:
                app.logger.warn(e)
                abort(500, message='something unexpected happened')
        return {}, 200
Beispiel #8
0
    def put(self, slug, identifier):
        """update one task."""

        # Parse the posted data
        t = json_to_task(
            slug,
            json.loads(request.data),
            task=get_task_or_404(slug, identifier))
        db.session.add(t)
        try:
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e.message)
                db.session.rollback()
                abort(409, message='The session and the database did not agree: {}'.format(e.message))
            else:
                app.logger.warn(e.message)
                abort(500, message='something unexpected happened')
        return {}, 200
Beispiel #9
0
    def post(self, slug):
        """bulk create"""

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

        # debug output number of tasks being posted
        app.logger.debug('posting {number} tasks...'.format(number=len(data)))

        if len(data) > app.config['MAX_TASKS_BULK_UPDATE']:
            abort(400, 'more than 5000 tasks in bulk create')

        try:
            for task in data:
                if not 'identifier' in task:
                    abort(400, 'task must have identifier')
                if not re.match("^[\w\d_-]+$", task['identifier']):
                    abort(
                        400,
                        'identifier should contain only a-z, A-Z, 0-9, _, -')
                if not 'geometries' in task:
                    abort(400, 'new task must have geometries')
                t = json_to_task(slug, task)
                db.session.add(t)

            # commit all dirty tasks at once.
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e.message)
                db.session.rollback()
                abort(
                    409, 'you posted a task that already existed: {}'.format(
                        e.message))
            else:
                app.logger.warn(e.message)
                abort(500, 'something unexpected happened')
        return {}, 200
Beispiel #10
0
    def post(self, slug, identifier):
        """create one task."""

        if not re.match("^[\w\d_-]+$", identifier):
            abort(400, 'identifier should contain only a-z, A-Z, 0-9, _, -')

        # Parse the posted data
        try:
            t = json_to_task(
                slug,
                json.loads(request.data),
                identifier=identifier)
            db.session.add(t)
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e.message)
                db.session.rollback()
                abort(409, 'you posted a task that already existed: {}'.format(e.message))
            else:
                app.logger.warn(e.message)
                abort(500, 'something unexpected happened')
        return {}, 201
Beispiel #11
0
    def post(self, slug, identifier):
        """create one task."""

        if not re.match("^[\w\d_-]+$", identifier):
            abort(400, message='identifier should contain only a-z, A-Z, 0-9, _, -')

        # Parse the posted data
        try:
            app.logger.debug(request.data)
            t = json_to_task(
                slug,
                json.loads(request.data),
                identifier=identifier)
            db.session.add(t)
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e)
                db.session.rollback()
                abort(409, message='You posted a task ({identifier}) that already existed: {message}'.format(identifier=t.identifier, message=e))
            else:
                app.logger.warn(e)
                abort(500, message=message_internal_server_error)
        return {}, 201
Beispiel #12
0
    def post(self, slug, identifier):
        """create one task."""

        if not re.match("^[\w\d_-]+$", identifier):
            abort(400, 'identifier should contain only a-z, A-Z, 0-9, _, -')

        # Parse the posted data
        try:
            t = json_to_task(slug,
                             json.loads(request.data),
                             identifier=identifier)
            db.session.add(t)
            db.session.commit()
        except Exception as e:
            if type(e) == IntegrityError:
                app.logger.warn(e.message)
                db.session.rollback()
                abort(
                    409, 'you posted a task that already existed: {}'.format(
                        e.message))
            else:
                app.logger.warn(e.message)
                abort(500, 'something unexpected happened')
        return {}, 201