def create_cause_category_from_json(json_data): if not json_data['category']: return None category = models.Category() cause_json_data = json_data['category'] category.id = cause_json_data['id'] category.created_at = get_datetime_from_json_attr(cause_json_data, 'created_at') category.updated_at = get_datetime_from_json_attr(cause_json_data, 'updated_at') category.name = cause_json_data['name'] return category
class Category(flask_restful.Resource): @validate_client() @validate_id() def get(self, client, id=None): if id: response = {'category': models.Category.get(id, client.id)} return marshal(response, one_category_fields) else: response = { 'categories': models.Category.all(client.id), 'meta': {} } return marshal(response, categories_fields) @validate_client(True) def post(self, client): json = request.get_json(silent=True) logging.getLogger(__name__).debug('Post category: %s', json) try: validate(json, category_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 # if an archived category exists with same name use the same instead of creating a new one. archived_category = models.Category.get_archived_by_name( json['name'], client.id) if archived_category: category = archived_category category.is_visible = True else: category = models.Category() mapper.fill_from_json(category, json, mapper.category_mapping) category.client = client db.session.add(category) try: db.session.commit() db.session.refresh(category) except IntegrityError, e: logging.debug(str(e)) return marshal({'error': { 'message': utils.parse_error(e) }}, error_fields), 400
def test_disruption_with_rabbitmq_exception(): ''' Tests when a disruption fail to publish to rabbitmq :return: ''' disruption = models.Disruption() #contributor disruption.contributor = models.Contributor() disruption.contributor_id = disruption.contributor.id #cause disruption.cause = models.Cause() disruption.cause.wording = "CauseTest" disruption.cause.category = models.Category() disruption.cause.category.name = "CategoryTest" disruption.reference = "DisruptionTest" #localization localization = models.PTobject() localization.uri = "stop_area:123" localization.type = "stop_area" disruption.localizations.append(localization) # Wording wording = models.Wording() wording.key = "key_1" wording.value = "value_1" disruption.cause.wordings.append(wording) wording = models.Wording() wording.key = "key_2" wording.value = "value_2" disruption.cause.wordings.append(wording) # Tag tag = models.Tag() tag.name = "rer" disruption.tags.append(tag) disruption.status = 'published' chaos.publisher.publish = MagicMock(return_value=False) to_rabbitmq_not_sent = send_disruption_to_navitia(disruption) eq_(to_rabbitmq_not_sent, False)
def test_disruption_with_published_status_is_sent(): ''' Tests that a disruption with status published is sent to navitia :return: ''' disruption = models.Disruption() #contributor disruption.contributor = models.Contributor() disruption.contributor_id = disruption.contributor.id #cause disruption.cause = models.Cause() disruption.cause.wording = "CauseTest" disruption.cause.category = models.Category() disruption.cause.category.name = "CategoryTest" disruption.reference = "DisruptionTest" #localization localization = models.PTobject() localization.uri = "stop_area:123" localization.type = "stop_area" disruption.localizations.append(localization) # Wording wording = models.Wording() wording.key = "key_1" wording.value = "value_1" disruption.cause.wordings.append(wording) wording = models.Wording() wording.key = "key_2" wording.value = "value_2" disruption.cause.wordings.append(wording) # Tag tag = models.Tag() tag.name = "rer" disruption.tags.append(tag) disruption.status = 'published' has_been_sent = send_disruption_to_navitia(disruption) eq_(has_been_sent, None)