def post(self): '''Initiate transfer request''' data = request.json subject_model = db.resolve_model(data['subject']) subject_id = data['subject']['id'] try: subject = subject_model.objects.get(id=subject_id) except subject_model.DoesNotExist: msg = 'Unkown subject id "{0}"'.format(subject_id) ns.abort(400, errors={'subject': msg}) recipient_model = db.resolve_model(data['recipient']) recipient_id = data['recipient']['id'] try: recipient = recipient_model.objects.get(id=recipient_id) except recipient_model.DoesNotExist: msg = 'Unkown recipient id "{0}"'.format(recipient_id) ns.abort(400, errors={'recipient': msg}) comment = data.get('comment') transfer = request_transfer(subject, recipient, comment) return transfer, 201
def emit_activity(classname, actor_id, related_to_cls, related_to_id, organization_id=None): log.debug('Emit new activity: %s %s %s %s %s', classname, actor_id, related_to_cls, related_to_id, organization_id) cls = db.resolve_model(classname) actor = User.objects.get(pk=actor_id) related_to = db.resolve_model(related_to_cls).objects.get(pk=related_to_id) if organization_id: organization = Organization.objects.get(pk=organization_id) else: organization = None cls.objects.create(actor=actor, related_to=related_to, organization=organization)
def process_formdata(self, valuelist): if valuelist and len(valuelist) == 1 and valuelist[0]: specs = valuelist[0] model_field = getattr(self._form.model_class, self.name) if isinstance(specs, basestring): if isinstance(model_field, db.ReferenceField): specs = { 'class': str(model_field.document_type.__name__), 'id': specs } elif isinstance(model_field, db.GenericReferenceField): message = _('Expect both class and identifier') raise validators.ValidationError(message) # No try/except required # In case of error, ValueError is raised # and is properly handled as form validation error model = db.resolve_model(specs['class']) try: self.data = model.objects.only('id').get( id=clean_oid(specs, model)) except db.DoesNotExist: message = _('{0} does not exists').format(model.__name__) raise validators.ValidationError(message) except db.ValidationError: message = _('{0} is not a valid identifier').format( specs['id']) raise validators.ValidationError(message)
def reindex(classname, id=None): model = db.resolve_model(classname) obj = model.objects.get(pk=id) adapter_class = adapter_catalog.get(model) timeout = current_app.config['ELASTICSEARCH_INDEX_TIMEOUT'] if adapter_class.is_indexable(obj): log.info('Indexing %s (%s)', model.__name__, obj.id) try: adapter = adapter_class.from_model(obj) adapter.save(using=es.client, index=es.index_name, request_timeout=timeout) except Exception: log.exception('Unable to index %s "%s"', model.__name__, str(obj.id)) elif adapter_class.exists(obj.id, using=es.client, index=es.index_name): log.info('Unindexing %s (%s)', model.__name__, obj.id) try: adapter = adapter_class.from_model(obj) adapter.delete(using=es.client, index=es.index_name, request_timeout=timeout) except Exception: log.exception('Unable to index %s "%s"', model.__name__, str(obj.id)) else: log.info('Nothing to do for %s (%s)', model.__name__, obj.id)
def reindex(obj, id=None): if id is not None: model = db.resolve_model(obj) obj = model.objects.get(pk=id) else: # TODO: Remove this branch in udata 2.0 warnings.warn( 'Document as task parameter is deprecated and will be removed in udata 2.0', DeprecationWarning) model = obj.__class__ adapter_class = adapter_catalog.get(model) timeout = current_app.config['ELASTICSEARCH_INDEX_TIMEOUT'] if adapter_class.is_indexable(obj): log.info('Indexing %s (%s)', model.__name__, obj.id) try: adapter = adapter_class.from_model(obj) adapter.save(using=es.client, index=es.index_name, request_timeout=timeout) except Exception: log.exception('Unable to index %s "%s"', model.__name__, str(obj.id)) elif adapter_class.exists(obj.id, using=es.client, index=es.index_name): log.info('Unindexing %s (%s)', model.__name__, obj.id) try: adapter = adapter_class.from_model(obj) adapter.delete(using=es.client, index=es.index_name, request_timeout=timeout) except Exception: log.exception('Unable to index %s "%s"', model.__name__, str(obj.id)) else: log.info('Nothing to do for %s (%s)', model.__name__, obj.id)
def process_formdata(self, valuelist): if not valuelist or len(valuelist) != 1 or not valuelist[0]: return specs = valuelist[0] model_field = getattr(self._form.model_class, self.name) if isinstance(specs, basestring): specs = {'id': specs} elif not specs.get('id', None): raise validators.ValidationError('Missing "id" field') if isinstance(model_field, db.ReferenceField): expected_model = str(model_field.document_type.__name__) if 'class' not in specs: specs['class'] = expected_model elif specs['class'] != expected_model: msg = 'Expect a "{0}" class but "{1}" was found'.format( expected_model, specs['class']) raise validators.ValidationError(msg) elif isinstance(model_field, db.GenericReferenceField): if 'class' not in specs: msg = _('Expect both class and identifier') raise validators.ValidationError(msg) # No try/except required # In case of error, ValueError is raised # and is properly handled as form validation error model = db.resolve_model(specs['class']) oid = clean_oid(specs, model) try: self.data = model.objects.only('id').get(id=oid) except db.DoesNotExist: label = '{0}({1})'.format(model.__name__, oid) msg = _('{0} does not exists').format(label) raise validators.ValidationError(msg)
def unindex(classname, id): model = db.resolve_model(classname) log.info('Unindexing %s (%s)', model.__name__, id) try: action = KafkaMessageType.UNINDEX message_type = f'{classname.lower()}.{action.value}' produce(id=id, message_type=message_type, index=classname.lower()) except Exception: log.exception('Unable to unindex %s "%s"', model.__name__, id)
def _compat(cls, id, name, value=None): '''Handle compatibility and deprecation warning on metrics tasks parameters''' model = db.resolve_model(cls) metrics = get_for(model) metric_cls = metrics[name] if model is Site: metric = metric_cls() else: obj = model.objects.get(pk=id) metric = metric_cls(obj) if value is not None: metric.value = value return metric
def unindex(classname, id=None): model = db.resolve_model(classname) adapter_class = adapter_catalog.get(model) if adapter_class.exists(id, using=es.client, index=es.index_name): log.info('Unindexing %s (%s)', model.__name__, id) try: adapter = adapter_class(meta={'id': id}) adapter.delete( using=es.client, index=es.index_name, request_timeout=current_app. config['ELASTICSEARCH_INDEX_TIMEOUT'], ) except Exception: log.exception('Unable to unindex %s "%s"', model.__name__, id) else: log.info('Nothing to do for %s (%s)', model.__name__, id)
def _compat(cls, id, name, value=None): '''Handle compatibility and deprecation warning on metrics tasks parameters''' if name is None: # TODO: remove in udata 2.0 warnings.warn( 'Metric as task parameter is deprecated and will be removed in udata 2.0', DeprecationWarning) return cls # is already Metric instance model = db.resolve_model(cls) metrics = get_for(model) metric_cls = metrics[name] if model is Site: metric = metric_cls() else: obj = model.objects.get(pk=id) metric = metric_cls(obj) if value is not None: metric.value = value return metric
def reindex(classname, id): model = db.resolve_model(classname) obj = model.objects.get(pk=id) adapter_class = adapter_catalog.get(model) document = adapter_class.serialize(obj) if adapter_class.is_indexable(obj): log.info('Indexing %s (%s)', model.__name__, obj.id) action = KafkaMessageType.INDEX else: log.info('Unindexing %s (%s)', model.__name__, obj.id) action = KafkaMessageType.UNINDEX try: message_type = f'{classname.lower()}.{action.value}' produce(id=str(obj.id), message_type=message_type, document=document, index=classname.lower()) except Exception: log.exception('Unable to index/unindex %s "%s"', model.__name__, str(obj.id))
def unindex(obj, id=None): if id is not None: model = db.resolve_model(obj) else: # TODO: Remove this branch in udata 2.0 warnings.warn( 'Document as task parameter is deprecated and will be removed in udata 2.0', DeprecationWarning) model = obj.__class__ id = obj.pk if isinstance(obj.pk, basestring) else str(obj.pk) adapter_class = adapter_catalog.get(model) if adapter_class.exists(id, using=es.client, index=es.index_name): log.info('Unindexing %s (%s)', model.__name__, id) try: adapter = adapter_class(meta={'id': id}) adapter.delete( using=es.client, index=es.index_name, request_timeout=current_app. config['ELASTICSEARCH_INDEX_TIMEOUT'], ) except Exception: log.exception('Unable to unindex %s "%s"', model.__name__, id) else: log.info('Nothing to do for %s (%s)', model.__name__, id)
def process_formdata(self, valuelist): if valuelist and len(valuelist) == 1 and valuelist[0]: specs = valuelist[0] model_field = getattr(self._form.model_class, self.name) if isinstance(specs, basestring): if isinstance(model_field, db.ReferenceField): specs = {'class': str(model_field.document_type.__name__), 'id': specs} elif isinstance(model_field, db.GenericReferenceField): message = _('Expect both class and identifier') raise validators.ValidationError(message) # No try/except required # In case of error, ValueError is raised # and is properly handled as form validation error model = db.resolve_model(specs['class']) try: self.data = model.objects.only('id').get(id=clean_oid(specs, model)) except db.DoesNotExist: message = _('{0} does not exists').format(model.__name__) raise validators.ValidationError(message) except db.ValidationError: message = _('{0} is not a valid identifier').format(specs['id']) raise validators.ValidationError(message)
def process_formdata(self, valuelist): if not valuelist or len(valuelist) != 1 or not valuelist[0]: return specs = valuelist[0] model_field = getattr(self._form.model_class, self.name) if isinstance(specs, basestring): specs = {'id': specs} elif not specs.get('id', None): raise validators.ValidationError('Missing "id" field') if isinstance(model_field, db.ReferenceField): expected_model = str(model_field.document_type.__name__) if 'class' not in specs: specs['class'] = expected_model elif specs['class'] != expected_model: msg = 'Expect a "{0}" class but "{1}" was found'.format( expected_model, specs['class'] ) raise validators.ValidationError(msg) elif isinstance(model_field, db.GenericReferenceField): if 'class' not in specs: msg = _('Expect both class and identifier') raise validators.ValidationError(msg) # No try/except required # In case of error, ValueError is raised # and is properly handled as form validation error model = db.resolve_model(specs['class']) oid = clean_oid(specs, model) try: self.data = model.objects.only('id').get(id=oid) except db.DoesNotExist: label = '{0}({1})'.format(model.__name__, oid) msg = _('{0} does not exists').format(label) raise validators.ValidationError(msg)
def test_raise_if_none(self): with pytest.raises(ValueError): db.resolve_model(None)
def test_resolve_from_dict(self): assert db.resolve_model({'class': 'Dataset'}) == Dataset
def test_raise_if_not_found(self): with pytest.raises(ValueError): db.resolve_model('NotFound')
def test_resolve_exact_match(self): self.assertEqual(db.resolve_model('Dataset'), Dataset)
def test_resolve_exact_match(self): assert db.resolve_model('Dataset') == Dataset
def test_raise_if_not_found(self): with self.assertRaises(ValueError): db.resolve_model('NotFound')
def test_raise_if_missing_class_entry(self): with pytest.raises(ValueError): db.resolve_model({'field': 'value'})
def test_raise_if_not_a_document(self): with pytest.raises(ValueError): db.resolve_model('UDataMongoEngine')
def test_raise_if_none(self): with self.assertRaises(ValueError): db.resolve_model(None)
def test_resolve_from_dict(self): self.assertEqual(db.resolve_model({'class': 'Dataset'}), Dataset)
def test_raise_if_not_a_document(self): with self.assertRaises(ValueError): db.resolve_model('UDataMongoEngine')
def test_raise_if_missing_class_entry(self): with self.assertRaises(ValueError): db.resolve_model({'field': 'value'})