cubicweb/schema.py
changeset 11416 9c2fbb872e91
parent 11344 847ab4bdd985
child 11417 5e5e224239c3
--- a/cubicweb/schema.py	Fri Jul 08 10:17:14 2016 +0200
+++ b/cubicweb/schema.py	Fri Jul 08 10:17:42 2016 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -22,6 +22,7 @@
 
 import re
 from os.path import join, basename
+from hashlib import md5
 from logging import getLogger
 from warnings import warn
 
@@ -1145,6 +1146,16 @@
 
 # additional cw specific constraints ###########################################
 
+@monkeypatch(BaseConstraint)
+def name_for(self, rdef):
+    """Return a unique, size controlled, name for this constraint applied to given `rdef`.
+
+    This name may be used as name for the constraint in the database.
+    """
+    return 'cstr' + md5((rdef.subject.type + rdef.rtype.type + self.type() +
+                         (self.serialize() or '')).encode('ascii')).hexdigest()
+
+
 class BaseRQLConstraint(RRQLExpression, BaseConstraint):
     """base class for rql constraints"""
     distinct_query = None