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
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
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
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