class Disruptions(flask_restful.Resource): def __init__(self): self.navitia = None self.parsers = {} self.parsers["get"] = reqparse.RequestParser() parser_get = self.parsers["get"] parser_get.add_argument("start_page", type=int, default=1) parser_get.add_argument("items_per_page", type=int, default=20) parser_get.add_argument("publication_status[]", type=option_value(publication_status_values), action="append", default=publication_status_values) parser_get.add_argument("tag[]", type=utils.get_uuid, action="append") parser_get.add_argument("current_time", type=utils.get_datetime) parser_get.add_argument("uri", type=str) parser_get.add_argument("status[]", type=option_value(disruption_status_values), action="append", default=disruption_status_values) @validate_navitia() @validate_contributor() @manage_navitia_error() @validate_id() def get(self, contributor, navitia, id=None): self.navitia = navitia if id: return marshal( {'disruption': models.Disruption.get(id, contributor.id)}, one_disruption_fields) else: args = self.parsers['get'].parse_args() page_index = args['start_page'] if page_index == 0: abort(400, message="page_index argument value is not valid") items_per_page = args['items_per_page'] if items_per_page == 0: abort(400, message="items_per_page argument value is not valid") publication_status = args['publication_status[]'] tags = args['tag[]'] uri = args['uri'] statuses = args['status[]'] g.current_time = args['current_time'] result = models.Disruption.all_with_filter( page_index=page_index, items_per_page=items_per_page, contributor_id=contributor.id, publication_status=publication_status, tags=tags, uri=uri, statuses=statuses) response = { 'disruptions': result.items, 'meta': make_pager(result, 'disruption') } return marshal(response, disruptions_fields) @validate_navitia() @validate_client(True) @manage_navitia_error() def post(self, client, navitia): self.navitia = navitia json = request.get_json(silent=True) logging.getLogger(__name__).debug('POST disruption: %s', json) try: validate(json, disruptions_input_format) except ValidationError, e: logging.debug(str(e)) # TODO: generate good error messages return marshal({'error': { 'message': utils.parse_error(e) }}, error_fields), 400 disruption = models.Disruption() mapper.fill_from_json(disruption, json, mapper.disruption_mapping) # Use contributor_code present in the json to get contributor_id if 'contributor' in json: disruption.contributor = models.Contributor.get_or_create( json['contributor']) disruption.client = client # Add localization present in Json try: db_helper.manage_pt_object_without_line_section( self.navitia, disruption.localizations, 'localization', json) except exceptions.ObjectUnknown, e: return marshal({'error': { 'message': '{}'.format(e.message) }}, error_fields), 404
class Disruptions(flask_restful.Resource): def __init__(self): self.navitia = None self.parsers = {} self.parsers["get"] = reqparse.RequestParser() parser_get = self.parsers["get"] parser_get.add_argument("start_page", type=int, default=1) parser_get.add_argument("items_per_page", type=int, default=20) parser_get.add_argument("publication_status[]", type=option_value(publication_status_values), action="append", default=publication_status_values) parser_get.add_argument("ends_after_date", type=utils.get_datetime), parser_get.add_argument("ends_before_date", type=utils.get_datetime), parser_get.add_argument("tag[]", type=utils.get_uuid, action="append") parser_get.add_argument("current_time", type=utils.get_datetime) parser_get.add_argument("uri", type=str) parser_get.add_argument("line_section", type=types.boolean, default=False) parser_get.add_argument("status[]", type=option_value(disruption_status_values), action="append", default=disruption_status_values) parser_get.add_argument("depth", type=int, default=1) @validate_navitia() @validate_contributor() @manage_navitia_error() @validate_id() def get(self, contributor, navitia, id=None): self.navitia = navitia args = self.parsers['get'].parse_args() depth = args['depth'] g.display_impacts = depth > 1 if id: return self._get_disruption_by_id(id, contributor.id) else: return self._get_disruptions(contributor.id, args) def _get_disruption_by_id(self, id, contributor_id): return marshal( {'disruption': models.Disruption.get(id, contributor_id)}, one_disruption_fields) def _get_disruptions(self, contributor_id, args): self._validate_arguments_for_disruption_list(args) g.current_time = args['current_time'] page_index = args['start_page'] items_per_page = args['items_per_page'] publication_status = args['publication_status[]'] ends_after_date = args['ends_after_date'] ends_before_date = args['ends_before_date'] tags = args['tag[]'] uri = args['uri'] line_section = args['line_section'] statuses = args['status[]'] result = models.Disruption.all_with_filter( page_index=page_index, items_per_page=items_per_page, contributor_id=contributor_id, publication_status=publication_status, ends_after_date=ends_after_date, ends_before_date=ends_before_date, tags=tags, uri=uri, line_section=line_section, statuses=statuses) response = { 'disruptions': result.items, 'meta': make_pager(result, 'disruption') } ''' The purpose is to remove any database-loaded state from all current objects so that the next access of any attribute, or any query execution, will retrieve new state, freshening those objects which are still referenced outside of the session with the most recent available state. ''' for o in result.items: models.db.session.expunge(o) return marshal(response, disruptions_fields) def _validate_arguments_for_disruption_list(self, args): if args['start_page'] == 0: abort(400, message="page_index argument value is not valid") if args['items_per_page'] == 0: abort(400, message="items_per_page argument value is not valid") def get_post_error_response_and_log(self, exception, status_code): return self.get_error_response_and_log('POST', exception, status_code) def get_put_error_response_and_log(self, exception, status_code): return self.get_error_response_and_log('PUT', exception, status_code) def get_error_response_and_log(self, method, exception, status_code): response_content = marshal( {'error': { 'message': '{}'.format(exception.message) }}, error_fields) logging.getLogger(__name__).debug( "\nError REQUEST %s disruption: [X-Customer-Id:%s;X-Coverage:%s;X-Contributors:%s;Authorization:%s] with payload \n%s" + "\ngot RESPONSE with status %d:\n%s", method, request.headers.get('X-Customer-Id'), request.headers.get('X-Coverage'), request.headers.get('X-Contributors'), request.headers.get('Authorization'), json.dumps(request.get_json(silent=True)), status_code, json.dumps(response_content)) return response_content @validate_navitia() @validate_client(True) @manage_navitia_error() def post(self, client, navitia): self.navitia = navitia json = request.get_json(silent=True) try: validate(json, disruptions_input_format) except ValidationError, e: response = self.get_post_error_response_and_log(e, 400) return response, 400 disruption = models.Disruption() mapper.fill_from_json(disruption, json, mapper.disruption_mapping) # Use contributor_code present in the json to get contributor_id if 'contributor' in json: disruption.contributor = models.Contributor.get_or_create( json['contributor']) disruption.client = client # Add localization present in Json try: db_helper.manage_pt_object_without_line_section( self.navitia, disruption.localizations, 'localization', json) except exceptions.ObjectUnknown, e: response = self.get_post_error_response_and_log(e, 404) return response, 404
'message': 'The current disruption is already\ published and cannot get back to the \'draft\' status.' } }, error_fields), 409 mapper.fill_from_json(disruption, json, mapper.disruption_mapping) # Use contributor_code present in the json to get contributor_id if 'contributor' in json: disruption.contributor = models.Contributor.get_or_create( json['contributor']) # Add localization present in Json try: db_helper.manage_pt_object_without_line_section( self.navitia, disruption.localizations, 'localization', json) except exceptions.ObjectUnknown, e: return marshal({'error': { 'message': '{}'.format(e.message) }}, error_fields), 404 # Add/delete tags present/ not present in Json db_helper.manage_tags(disruption, json) # Add all impacts present in Json try: db_helper.manage_impacts(disruption, json, self.navitia) except exceptions.ObjectUnknown, e: return marshal({'error': { 'message': '{}'.format(e.message) }}, error_fields), 404
if disruption.is_published() and 'status' in json\ and json['status'] == 'draft': return marshal( {'error': {'message': 'The current disruption is already\ published and cannot get back to the \'draft\' status.'}}, error_fields), 409 mapper.fill_from_json(disruption, json, mapper.disruption_mapping) # Use contributor_code present in the json to get contributor_id if 'contributor' in json: disruption.contributor = models.Contributor.get_or_create(json['contributor']) # Add localization present in Json try: db_helper.manage_pt_object_without_line_section(self.navitia, disruption.localizations, 'localization', json) except exceptions.ObjectUnknown, e: return marshal({'error': {'message': '{}'.format(e.message)}}, error_fields), 404 # Add/delete tags present/ not present in Json db_helper.manage_tags(disruption, json) # Add all impacts present in Json try: db_helper.manage_impacts(disruption, json, self.navitia) except exceptions.ObjectUnknown, e: return marshal({'error': {'message': '{}'.format(e.message)}}, error_fields), 404 except exceptions.InvalidJson, e: return marshal({'error': {'message': '{}'.format(e.message)}}, error_fields), 400