Annotation of kupuMPIWG/plone/ReftextField.py, revision 1.1

1.1     ! dwinter     1: from AccessControl import ClassSecurityInfo
        !             2: from Products.CMFCore.utils import getToolByName
        !             3: from Products.Archetypes.public import *
        !             4: from Products.Archetypes.config import REFERENCE_CATALOG
        !             5: from Products.Archetypes.Field import TextField
        !             6: from Products.Archetypes.ReferenceEngine import Reference
        !             7: from ZPublisher.HTTPRequest import FileUpload
        !             8: import re
        !             9: 
        !            10: # UID_PATTERN matches a UID in an anchor or image tag.
        !            11: UID_PATTERN = re.compile(r'''<(?:a\b[^>]+href|img\b[^>]+src)="resolveuid/(?P<uid>[^">/]+)''', re.I)
        !            12: 
        !            13: class ReftextField(TextField):
        !            14:     __implements__ = TextField.__implements__
        !            15: 
        !            16:     _properties = TextField._properties.copy()
        !            17:     _properties.update({
        !            18:         'widget': RichWidget,
        !            19:         'default_content_type' : 'text/html',
        !            20:         'default_output_type'  : 'text/x-html-captioned',
        !            21:         'allowable_content_types' : ('text/html',),
        !            22:         'relationship' : None, # defaults to field name
        !            23:         'referenceClass' : Reference,
        !            24:         })
        !            25: 
        !            26:     security = ClassSecurityInfo()
        !            27: 
        !            28:     security.declarePrivate('set')
        !            29:     def set(self, instance, value, **kwargs):
        !            30:         """ Assign input value to object. If mimetype is not specified,
        !            31:         pass to processing method without one and add mimetype
        !            32:         returned to kwargs. Assign kwargs to instance.
        !            33:         """
        !            34:         if value is None:
        !            35:             # nothing to do
        !            36:             return
        !            37: 
        !            38:         TextField.set(self, instance, value, **kwargs)
        !            39: 
        !            40:         if not isinstance(value, basestring):
        !            41:             value.seek(0);
        !            42:             value = value.read()
        !            43: 
        !            44:         uids = UID_PATTERN.findall(value) # XXX: build list of uids from the value here
        !            45:         uids = dict.fromkeys(uids).keys() # Remove duplicate uids.
        !            46: 
        !            47:         tool = getToolByName(instance, REFERENCE_CATALOG)
        !            48: 
        !            49:         relationship = self.relationship
        !            50:         if relationship is None:
        !            51:             relationship = self.__name__
        !            52: 
        !            53:         targetUIDs = [ref.targetUID for ref in
        !            54:                       tool.getReferences(instance, relationship)]
        !            55: 
        !            56:         add = [v for v in uids if v and v not in targetUIDs]
        !            57:         sub = [t for t in targetUIDs if t not in uids]
        !            58: 
        !            59:         # tweak keyword arguments for addReference
        !            60:         addRef_kw = kwargs.copy()
        !            61:         addRef_kw.setdefault('referenceClass', self.referenceClass)
        !            62:         if addRef_kw.has_key('schema'): del addRef_kw['schema']
        !            63: 
        !            64:         for uid in add:
        !            65:             __traceback_info__ = (instance, uid, value, targetUIDs)
        !            66:             try:
        !            67:                 # throws ReferenceError if uid is invalid
        !            68:                 tool.addReference(instance, uid, relationship, **addRef_kw)
        !            69:             except ReferenceError:
        !            70:                 pass
        !            71:         for uid in sub:
        !            72:             tool.deleteReference(instance, uid, relationship)
        !            73: 
        !            74: #         print "Result was:",[ref.targetUID for ref in
        !            75: #                       tool.getReferences(instance, relationship)]
        !            76: #         print "Objects:",[ref.getTargetObject() for ref in
        !            77: #                       tool.getReferences(instance, relationship)]

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>