Back to index

plone3  3.1.7
ReftextField.py
Go to the documentation of this file.
00001 from AccessControl import ClassSecurityInfo
00002 from Products.Archetypes.public import *
00003 from Products.Archetypes.BaseUnit import BaseUnit
00004 from Products.Archetypes.config import REFERENCE_CATALOG
00005 from Products.Archetypes.Field import TextField
00006 from Products.Archetypes.ReferenceEngine import Reference
00007 from Products.Archetypes.exceptions import ReferenceException
00008 from ZPublisher.HTTPRequest import FileUpload
00009 from Products.CMFCore.utils import getToolByName
00010 from Products.kupu.plone.config import UID_PATTERN
00011 import re
00012 
00013 class ReftextField(TextField):
00014     __implements__ = TextField.__implements__
00015 
00016     _properties = TextField._properties.copy()
00017     _properties.update({
00018         'widget': RichWidget,
00019         'default_content_type' : 'text/html',
00020         'default_output_type'  : 'text/x-html-captioned',
00021         'allowable_content_types' : ('text/html',),
00022         'relationship' : None, # defaults to field name
00023         'referenceClass' : Reference,
00024         })
00025 
00026     security = ClassSecurityInfo()
00027 
00028     security.declarePrivate('set')
00029     def set(self, instance, value, **kwargs):
00030         """ Assign input value to object. If mimetype is not specified,
00031         pass to processing method without one and add mimetype
00032         returned to kwargs. Assign kwargs to instance.
00033         """
00034         if value is None:
00035             # nothing to do
00036             return
00037 
00038         TextField.set(self, instance, value, **kwargs)
00039 
00040         if isinstance(value, BaseUnit):
00041             # Baseunit: can occur when overriding atct text fields.
00042             value = value()
00043         if not isinstance(value, basestring):
00044             value.seek(0);
00045             value = value.read()
00046 
00047         # build list of uids from the value here
00048         uids = [ m.group('uid') for m in UID_PATTERN.finditer(value) ]
00049         uids = dict.fromkeys(uids).keys() # Remove duplicate uids.
00050 
00051         tool = getToolByName(instance, REFERENCE_CATALOG)
00052 
00053         relationship = self.relationship
00054         if relationship is None:
00055             relationship = self.__name__
00056 
00057         targetUIDs = [ref.targetUID for ref in
00058                       tool.getReferences(instance, relationship)]
00059 
00060         add = [v for v in uids if v and v not in targetUIDs]
00061         sub = [t for t in targetUIDs if t not in uids]
00062 
00063         # tweak keyword arguments for addReference
00064         addRef_kw = kwargs.copy()
00065         addRef_kw.setdefault('referenceClass', self.referenceClass)
00066         if addRef_kw.has_key('schema'): del addRef_kw['schema']
00067 
00068         for uid in add:
00069             __traceback_info__ = (instance, uid, value, targetUIDs)
00070             try:
00071                 # throws ReferenceException if uid is invalid
00072                 tool.addReference(instance, uid, relationship, **addRef_kw)
00073             except ReferenceException:
00074                 pass
00075         for uid in sub:
00076             tool.deleteReference(instance, uid, relationship)
00077 
00078 #         print "Result was:",[ref.targetUID for ref in
00079 #                       tool.getReferences(instance, relationship)]
00080 #         print "Objects:",[ref.getTargetObject() for ref in
00081 #                       tool.getReferences(instance, relationship)]