예제 #1
0
 def get(self, instance, **kwargs):
     startup_directory = self.getStartupDirectory(instance)
     portal_url = getToolByName(instance, 'portal_url')
     portal = portal_url.getPortalObject()
     startup_folder = portal.restrictedTraverse(str(startup_directory))
     startup_folder_url = startup_folder.absolute_url()
     refcat = getToolByName(instance, 'reference_catalog')
     items = []
     kwargs['aslist'] = True
     value = ReferenceField.getRaw(self, instance,  **kwargs)
     for uid in value:
         obj = refcat.lookupObject(uid)
         item = getTitledPath(obj, startup_folder_url)
         items.append(' / '.join(item))
     return items
예제 #2
0
 def get(self, obj, field, context=None):
     """
     """
     f = obj.Schema().getField(field)
     value = ReferenceField.getRaw(f, obj, aslist=True)
     #if 'taphono' in f.__name__ and bool(value):
     #    import pdb;pdb.set_trace()  ## Breakpoint ##
     refcat = getToolByName(obj, 'reference_catalog')
     v = []
     startup = f.getStartupDirectory(obj)
     for uid in value:
         target = refcat.lookupObject(uid)
         current = "/".join(obj.getPhysicalPath()) + "/"
         path = "/".join(target.getPhysicalPath())
         if path.startswith(current):
             v.append(path[len(current):])
         else:
             v.append(path)
     ret = ''
     if v:
         ret = '\n'.join(v)
     return ret
예제 #3
0
파일: marscategory.py 프로젝트: RBINS/mars
 def get(self, obj, field, context=None):
     """
     """
     f = obj.Schema().getField(field)
     value = ReferenceField.getRaw(f, obj, aslist=True)
     #if 'taphono' in f.__name__ and bool(value):
     #    import pdb;pdb.set_trace()  ## Breakpoint ##
     refcat = getToolByName(obj, 'reference_catalog')
     v = []
     startup = f.getStartupDirectory(obj)
     for uid in value:
         target = refcat.lookupObject(uid)
         current = "/".join(obj.getPhysicalPath())+"/"
         path = "/".join(target.getPhysicalPath())
         if path.startswith(current):
             v.append(path[len(current):])
         else:
             v.append(path)
     ret = ''
     if v:
         ret = '\n'.join(v)
     return ret
예제 #4
0
    def set(self, instance, value, search_criterias=None,
            uids_found=None, ref_uids=(), auto_reference=None, **kwargs):
        """
        Mutator.

        value: list of uids, must be included in uids_found
        search_criterias': {...},
        uids_found': [...],  # list presented to the user
        'auto_reference': True or False
               }

        The from value and ref_uids the field builds a list of excluded uids and
        merge it with existing exclusion list. From this exclusion list, it
        removed already referenced uids (it means the user has unchecked them).

        Then it merge the old reference list with the new value, and from this
        new value it removes uids listed for exclusion (it means the user have
        checked them).

        To delete references, you must do a 'set' with empty value and the
        uids you wish to unref listed in 'uids_found'
        """

        if type(value) not in (ListType, TupleType):
            raise (TypeError,
                   "Invalid type for value: expected list or tuple, got '%s'"
                   % type(value))

        if search_criterias is not None:
            self.setSearchCriterias(instance, search_criterias)

        if auto_reference is not None:
            self.setAutoReference(instance, bool(auto_reference))

        old_value = dict.fromkeys(ReferenceField.getRaw(self, instance,))

        # update excluded_uids with uids listed but not checked for inclusion
        excluded_uids = self.getExcludedUIDs(instance)
        old_excluded_count = len(excluded_uids)
        update_excluded = False

        if uids_found is not None:
            new_excluded = dict.fromkeys([uid for uid in uids_found
                                          if uid not in value],
                                         True)
            update_excluded = len(new_excluded) != 0
            # remove old refs if they have been unchecked in widget
            for uid in new_excluded.iterkeys():
                if old_value.has_key(uid):
                    del old_value[uid]
            excluded_uids.update(new_excluded)
            
        # compute new value ensuring unique items in list
        value = dict.fromkeys(value)
        value.update(old_value)
        value = value.keys()

        # remove old excluded_uids if they have been checked in widget
        for k in excluded_uids.keys():
            if k in value:
                del excluded_uids[k]

        update_excluded = (update_excluded or
                           (len(excluded_uids) != old_excluded_count))

        # write excluded uids only if it has changed
        if update_excluded:
            self.setExcludedUIDs(instance, excluded_uids)
            
        ReferenceField.set(self, instance, value, **kwargs)
예제 #5
0
    def set(self,
            instance,
            value,
            search_criterias=None,
            uids_found=None,
            ref_uids=(),
            auto_reference=None,
            **kwargs):
        """
        Mutator.

        value: list of uids, must be included in uids_found
        search_criterias': {...},
        uids_found': [...],  # list presented to the user
        'auto_reference': True or False
               }

        The from value and ref_uids the field builds a list of excluded uids and
        merge it with existing exclusion list. From this exclusion list, it
        removed already referenced uids (it means the user has unchecked them).

        Then it merge the old reference list with the new value, and from this
        new value it removes uids listed for exclusion (it means the user have
        checked them).

        To delete references, you must do a 'set' with empty value and the
        uids you wish to unref listed in 'uids_found'
        """

        if type(value) not in (ListType, TupleType):
            raise (TypeError,
                   "Invalid type for value: expected list or tuple, got '%s'" %
                   type(value))

        if search_criterias is not None:
            self.setSearchCriterias(instance, search_criterias)

        if auto_reference is not None:
            self.setAutoReference(instance, bool(auto_reference))

        old_value = dict.fromkeys(ReferenceField.getRaw(
            self,
            instance,
        ))

        # update excluded_uids with uids listed but not checked for inclusion
        excluded_uids = self.getExcludedUIDs(instance)
        old_excluded_count = len(excluded_uids)
        update_excluded = False

        if uids_found is not None:
            new_excluded = dict.fromkeys(
                [uid for uid in uids_found if uid not in value], True)
            update_excluded = len(new_excluded) != 0
            # remove old refs if they have been unchecked in widget
            for uid in new_excluded.iterkeys():
                if old_value.has_key(uid):
                    del old_value[uid]
            excluded_uids.update(new_excluded)

        # compute new value ensuring unique items in list
        value = dict.fromkeys(value)
        value.update(old_value)
        value = value.keys()

        # remove old excluded_uids if they have been checked in widget
        for k in excluded_uids.keys():
            if k in value:
                del excluded_uids[k]

        update_excluded = (update_excluded
                           or (len(excluded_uids) != old_excluded_count))

        # write excluded uids only if it has changed
        if update_excluded:
            self.setExcludedUIDs(instance, excluded_uids)

        ReferenceField.set(self, instance, value, **kwargs)