def create_tag_from_json(json): tag = models.Tag() tag.id = json['id'] tag.created_at = get_datetime_from_json_attr(json, 'created_at') tag.updated_at = get_datetime_from_json_attr(json, 'updated_at') tag.name = json['name'] return tag
class Tag(flask_restful.Resource): @validate_client() @validate_id() def get(self, client, id=None): if id: response = {'tag': models.Tag.get(id, client.id)} return marshal(response, one_tag_fields) else: response = {'tags': models.Tag.all(client.id), 'meta': {}} return marshal(response, tags_fields) @validate_client(True) def post(self, client): json = request.get_json(silent=True) logging.getLogger(__name__).debug('Post tag: %s', json) try: validate(json, tag_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 tag exists with same name use the same instead of creating a new one. archived_tag = models.Tag.get_archived_by_name(json['name'], client.id) if archived_tag: tag = archived_tag tag.client = client tag.is_visible = True else: tag = models.Tag() mapper.fill_from_json(tag, json, mapper.tag_mapping) tag.client = client db.session.add(tag) try: db.session.commit() db.session.refresh(tag) 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)