Skip to content

Commit

Permalink
Delete auto-created fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdbng committed Mar 1, 2016
1 parent 534c4ef commit e8ba264
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
34 changes: 29 additions & 5 deletions portiaui/app/components/list-item-annotation-field.js
Expand Up @@ -24,15 +24,39 @@ export default Ember.Component.extend({
const annotation = this.get('annotation');
const schema = annotation.get('field.schema');
const currentType = annotation.get('type');
const field = this.get('dispatcher').addNamedField(
schema, name, currentType, /* redirect = */false);
annotation.set('field', field);
annotation.save();
const dispatcher = this.get('dispatcher');

Ember.RSVP.all([
dispatcher.addNamedField(schema, name, currentType, /* redirect = */false),
annotation.get('field'),
]).then(([newField, oldField]) => {
annotation.set('field', newField);
annotation.save().then(() => {
if(annotation.get('_autoCreatedField') && oldField.get('_autoCreatedBy') &&
annotation.get('_autoCreatedField.id') === oldField.get('id')) {
oldField.destroyRecord();
}
annotation.set('_autoCreatedField', null);
});
});
},

changeField() {
const annotation = this.get('annotation');
annotation.save();
const field = annotation.get('field');
annotation.set('field', field);

annotation.save().then((annotation) => {
if(field.get('_autoCreatedBy')) {
field.set('_autoCreatedBy._autoCreatedField', null);
}
if(annotation.get('_autoCreatedField')) {
if(annotation.get('_autoCreatedField.id') !== annotation.get('field.id')) {
annotation.get('_autoCreatedField').destroyRecord();
annotation.set('_autoCreatedField', null);
}
}
});
}
}
});
8 changes: 6 additions & 2 deletions portiaui/app/services/dispatcher.js
Expand Up @@ -75,14 +75,14 @@ export default Ember.Service.extend({
type: type || 'text',
schema
});
field.save().then(() => {
return field.save().then((field) => {
if (redirect) {
field.set('new', true);
const routing = this.get('routing');
routing.transitionTo('projects.project.schema.field', [field], {}, true);
}
return field;
});
return field;
},

addSpider(project, redirect = false) {
Expand Down Expand Up @@ -215,6 +215,10 @@ export default Ember.Service.extend({
annotation.save().then(() => {
if (redirect) {
annotation.set('new', true);
annotation.get('field').then(f => {
annotation.set('_autoCreatedField', f);
f.set('_autoCreatedBy', annotation);
});
}
if (element) {
this.selectAnnotationElement(annotation, element, redirect);
Expand Down
16 changes: 15 additions & 1 deletion slyd/slyd/resources/annotations.py
Expand Up @@ -81,7 +81,12 @@ def update_annotation(manager, spider_id, sample_id, annotation_id,
annotation['selector'] = ', '.join(annotation['accept_selectors'])

manager.savejson(sample, ['spiders', spider_id, sample_id])
context = ctx(manager, spider_id=spider_id, sample_id=sample_id)
schema_id = annotation.get('schema_id')
if not schema_id:
container = _find_container(annotation, sample)
if container:
schema_id = container.get('schema_id')
context = ctx(manager, spider_id=spider_id, sample_id=sample_id, schema_id=schema_id, field_id=field_id)
split_annotations = _split_annotations([annotation])
a = filter(lambda x: x['id'] == annotation_id, split_annotations)[0]
return AnnotationSchema(context=context).dump(a).data
Expand Down Expand Up @@ -193,6 +198,15 @@ def _create_annotation(sample, attributes):
return annotation


def _find_container(annotation, sample):
container_id = annotation.get('container_id')
if container_id:
for a in sample['plugins']['annotations-plugin']['extracts']:
if a["id"] == container_id:
return a
return None


def _create_field_for_annotation(manager, annotation, sample):
field, parent, container_id = None, None, annotation['container_id']
for a in sample['plugins']['annotations-plugin']['extracts']:
Expand Down

0 comments on commit e8ba264

Please sign in to comment.