Skip to content

Commit

Permalink
prevent relation to self #210
Browse files Browse the repository at this point in the history
  • Loading branch information
cahytinne committed Jun 3, 2015
1 parent 01a199f commit c61d9e9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
7 changes: 7 additions & 0 deletions atramhasis/static/admin/src/app/ConceptForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,31 +215,38 @@ define(
this.typeComboBox.set("value", concept.type);
if (concept.members) {
this.membersManager.setRelations(concept.members);
this.membersManager.setConceptId(concept.id);
}
if (concept.member_of)
{
this.memberofManager.setRelations(concept.member_of);
this.memberofManager.setConceptId(concept.id);

}
if (concept.broader)
{
this.broaderManager.setRelations(concept.broader);
this.broaderManager.setConceptId(concept.id);
}
if (concept.narrower)
{
this.narrowerManager.setRelations(concept.narrower);
this.narrowerManager.setConceptId(concept.id);
}
if (concept.related)
{
this.relatedManager.setRelations(concept.related);
this.relatedManager.setConceptId(concept.id);
}
if (concept.subordinate_arrays)
{
this.subordinateArraysManager.setRelations(concept.subordinate_arrays);
this.subordinateArraysManager.setConceptId(concept.id);
}
if (concept.superordinates)
{
this.superordinatesManager.setRelations(concept.superordinates);
this.superordinatesManager.setConceptId(concept.id);
}
if (concept.labels) {
this.labelManager.setLabels(concept.labels);
Expand Down
30 changes: 23 additions & 7 deletions atramhasis/static/admin/src/app/form/RelationManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define([
"dojo/_base/declare",
"dojo/_base/array",
"dojo/topic",
"dojo/dom-construct",
"dojo/query",
"dojo/on",
Expand All @@ -17,7 +18,7 @@ define([
"./ConceptDetailList",
"dojo/text!./templates/RelationManager.html"
],
function (declare, arrayUtil, domConstruct, query, on, domStyle, Dialog, WidgetBase, TemplatedMixin, Button, Memory,
function (declare, arrayUtil, topic, domConstruct, query, on, domStyle, Dialog, WidgetBase, TemplatedMixin, Button, Memory,
Cache, JsonRest, ObjectStoreModel, Tree,ConceptDetailList, template) {
return declare(
"app/form/RelationManager",
Expand All @@ -29,6 +30,7 @@ define([
_scheme: null,
_relations: null,
EditRelationButton:null,
_selfId: null,

buildRendering: function () {
this.inherited(arguments);
Expand Down Expand Up @@ -153,15 +155,24 @@ define([
addBtn.onClick = function () {
var sel = myTree.selectedItems[0];
if (sel) {
var path = arrayUtil.map(myTree.get("path"), function (item) {
return item.label;
});
self._addRelation(sel.concept_id, sel.label, path);
if (sel.concept_id === self._selfId) {
topic.publish('dGrowl', 'Concept or collection cannot be related to itself', {
'title': 'Not valid',
'sticky': true,
'channel': 'error'
});
}
else {
var path = arrayUtil.map(myTree.get("path"), function (item) {
return item.label;
});
self._addRelation(sel.concept_id, sel.label, path);

dlg.hide();
dlg.hide();
}
}
else {
alert("Nothing is selected");
topic.publish('dGrowl', 'Nothing is selected', {'title': 'Not valid', 'sticky': true, 'channel':'error'});
}
};
cancelBtn.onClick = function () {
Expand Down Expand Up @@ -203,6 +214,11 @@ define([
setScheme: function (scheme) {
this._scheme = scheme;

},

setConceptId: function (id) {
this._selfId = id;

}
});
});
33 changes: 20 additions & 13 deletions atramhasis/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from skosprovider_sqlalchemy.models import (
Concept as DomainConcept,
Collection as DomainCollection,
Thing,
Thing,
LabelType,
Language
)
Expand Down Expand Up @@ -120,6 +120,7 @@ def concept_schema_validator(node, cstruct):
languages_manager = request.data_managers['languages_manager']
conceptscheme_id = node.bindings['conceptscheme_id']
concept_type = cstruct['type']
id = cstruct['id']
narrower = None
broader = None
related = None
Expand All @@ -140,31 +141,31 @@ def concept_schema_validator(node, cstruct):
if 'related' in cstruct:
related = copy.deepcopy(cstruct['related'])
related = [m['id'] for m in related]
r_validated = concept_exists_andnot_different_conceptscheme_rule(errors, node['related'], skos_manager,
conceptscheme_id, related)
r_validated = related_to_rule(errors, node['related'], skos_manager,
conceptscheme_id, related, id)
concept_relations_rule(errors, node['related'], related, concept_type)
if 'narrower' in cstruct:
narrower = copy.deepcopy(cstruct['narrower'])
narrower = [m['id'] for m in narrower]
n_validated = concept_exists_andnot_different_conceptscheme_rule(errors, node['narrower'], skos_manager,
conceptscheme_id, narrower)
n_validated = related_to_rule(errors, node['narrower'], skos_manager,
conceptscheme_id, narrower, id)
concept_relations_rule(errors, node['narrower'], narrower, concept_type)
if 'broader' in cstruct:
broader = copy.deepcopy(cstruct['broader'])
broader = [m['id'] for m in broader]
b_validated = concept_exists_andnot_different_conceptscheme_rule(errors, node['broader'], skos_manager,
conceptscheme_id, broader)
b_validated = related_to_rule(errors, node['broader'], skos_manager,
conceptscheme_id, broader, id)
concept_relations_rule(errors, node['broader'], broader, concept_type)
if 'members' in cstruct:
members = copy.deepcopy(cstruct['members'])
members = [m['id'] for m in members]
m_validated = concept_exists_andnot_different_conceptscheme_rule(errors, node['members'], skos_manager,
conceptscheme_id, members)
m_validated = related_to_rule(errors, node['members'], skos_manager,
conceptscheme_id, members, id)
if 'member_of' in cstruct:
member_of = copy.deepcopy(cstruct['member_of'])
member_of = [m['id'] for m in member_of]
o_validated = concept_exists_andnot_different_conceptscheme_rule(errors, node['member_of'], skos_manager,
conceptscheme_id, member_of)
o_validated = related_to_rule(errors, node['member_of'], skos_manager,
conceptscheme_id, member_of, id)
if r_validated and n_validated and b_validated:
concept_type_rule(errors, node['narrower'], skos_manager, conceptscheme_id, narrower)
narrower_hierarchy_rule(errors, node['narrower'], skos_manager, conceptscheme_id, cstruct)
Expand Down Expand Up @@ -308,12 +309,18 @@ def collection_type_rule(errors, node_location, skos_manager, conceptscheme_id,
))


def concept_exists_andnot_different_conceptscheme_rule(errors, node_location, skos_manager, conceptscheme_id, members):
def related_to_rule(errors, node_location, skos_manager, conceptscheme_id, members, collection_id):
'''
Checks that the members of a collection actually exist and are within
Checks that the members of a collection is not the collection itself and that it actually exist and are within
the same conceptscheme.
'''
for member_concept_id in members:
if member_concept_id == collection_id:
errors.append(colander.Invalid(
node_location,
'A concepts cannot be related to itself'
))
return False
try:
skos_manager.get_thing(member_concept_id, conceptscheme_id)
except NoResultFound:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ def test_narrower_concept_different_conceptscheme(self):
self.assertIn({'narrower': 'Concept not found, check concept_id. Please be aware members'
' should be within one scheme'}, error.errors)


def test_narrower_concept_to_self(self):
self.json_concept['narrower'].append({"id": 4})
error_raised = False
error = None
validated_concept = None
try:
validated_concept = self.concept_schema.deserialize(self.json_concept)
except ValidationError as e:
error_raised = True
error = e
self.assertTrue(error_raised)
self.assertIsNone(validated_concept)
self.assertTrue(isinstance(error, ValidationError))
self.assertIn({'narrower': 'A concepts cannot be related to itself'}, error.errors)

def test_broader_concept_different_conceptscheme(self):
self.json_concept['broader'].append({"id": 777})
error_raised = False
Expand Down

0 comments on commit c61d9e9

Please sign in to comment.