Skip to content

Commit

Permalink
Enforces unique constraint in the tile model re #4375
Browse files Browse the repository at this point in the history
  • Loading branch information
chiatt committed Mar 27, 2019
1 parent 34df626 commit c1a96d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion arches/app/datatypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def after_update_all(self):
"""
pass

def values_match(self, value1, value2):
return value1 == value2

def transform_import_values(self, value, nodeid):
"""
Transforms values from probably string/wkt representation to specified
Expand Down Expand Up @@ -315,4 +318,4 @@ def to_rdf(self, edge_info, edge):

def from_rdf(self, json_ld_node):
print json_ld_node
raise NotImplementedError
raise NotImplementedError
30 changes: 30 additions & 0 deletions arches/app/models/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pytz
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext as _
from arches.app.models import models
Expand Down Expand Up @@ -190,6 +191,34 @@ def get_provisional_edit(self, tile, user):
edit = edits[str(user.id)]
return edit

def check_for_constraint_violation(self, request):
card = models.CardModel.objects.get(nodegroup=self.nodegroup)
constraints = models.ConstraintModel.objects.filter(card=card)
if constraints.count() > 0:
for constraint in constraints:
if constraint.uniquetoallinstances is True:
tiles = models.TileModel.objects.filter(nodegroup=self.nodegroup)
else:
tiles = models.TileModel.objects.filter(
Q(resourceinstance_id=self.resourceinstance.resourceinstanceid) &
Q(nodegroup=self.nodegroup))
nodes = [node for node in constraint.nodes.all()]
for tile in tiles:
match = False
duplicate_values = []
for node in nodes:
datatype_factory = DataTypeFactory()
datatype = datatype_factory.get_instance(node.datatype)
nodeid = str(node.nodeid)
if datatype.values_match(tile.data[nodeid], self.data[nodeid]):
match = True
duplicate_values.append(datatype.get_display_value(tile, node))
else:
return False
if match is True:
message = _('This card violates a unique constraint. The following value is already saved: ')
raise TileValidationError(message + (', ').join(duplicate_values))

def check_for_missing_nodes(self, request):
missing_nodes = []
for nodeid, value in self.data.iteritems():
Expand Down Expand Up @@ -235,6 +264,7 @@ def save(self, *args, **kwargs):
newprovisionalvalue = None
oldprovisionalvalue = None
self.check_for_missing_nodes(request)
self.check_for_constraint_violation(request)

try:
if user is None and request is not None:
Expand Down

0 comments on commit c1a96d6

Please sign in to comment.