def set(self, instance, value, **kwargs): """The passed in object should be a records object, or a sequence of dictionaries About link data: * interpretations: * if data not starts with standard protocol names (http://, ftp://) than *uid* field data will be used as reference * record removed if: * no data; * data contains UID of not existent object About title: * if there is UID of existent object and record has same title to the original object - title will not be saved. """ catalog = getToolByName(instance, "uid_catalog") if value is None: value = () if not isinstance(value, (ListType, TupleType)): value = value, result = [] for row in value: data = {} for key in [key for key in set(row) if not(key.startswith('_') or key.endswith('_'))]: data[key] = str(encode(row[key], self)).strip() uid = str(row.get("uid", "")).strip() link = str(row.get("link", "")).strip() title = str(encode(row.get("title", ""), self)) if not title == "": data["title"] = title if link == "": continue elif self.isRemoteURL(link): data["link"] = urlparse.urlunparse(urlparse.urlparse(link)) else: if uid == '': continue brains = catalog(UID=uid) if len(brains) == 0: continue # Found objects with pointed UID brain = brains[0] data["uid"] = uid # Fix title for uid if data.get("title", "") == getattr(brain, "Title", ""): data['title'] = "" result.append(data) DataGridField.set(self, instance, result, **kwargs) uids = [r["uid"] for r in result if r.get("uid")] ReferenceField.set(self, instance, uids, **kwargs)
def set(self, instance, value, **kwargs): """ The passed in object should be a records object, or a sequence of dictionaries About link data: * interpretations: * if data not starts with standard protocol names (http://, ftp://) than *uid* field data will be used as reference * record removed if: * no data; * data contains UID of not existent object About title: * if there is UID of existent object and record has same title to the original object - title will not be saved. """ catalog = getToolByName(instance, "uid_catalog") if value is None: value = () if not isinstance(value, (ListType, TupleType)): value = value, result = [] for row in value: data = {} for key in [ key for key in set(row) if not (key.startswith('_') or key.endswith('_')) ]: data[key] = str(row[key]).strip() uid = str(row.get("uid", "")).strip() link = str(row.get("link", "")).strip() title = str(row.get("title", "")) if not title == "": data["title"] = title if link == "": continue elif self.isRemoteURL(link): data["link"] = urlparse.urlunparse(urlparse.urlparse(link)) else: if uid == '': continue brains = catalog(UID=uid) if len(brains) == 0: continue # Found objects with pointed UID brain = brains[0] data["uid"] = uid # Fix title for uid if data.get("title", "") == getattr(brain, "Title", ""): data['title'] = "" result.append(data) DataGridField.set(self, instance, result, **kwargs) uids = [r["uid"] for r in result if r.get("uid")] ReferenceField.set(self, instance, uids, **kwargs)
def __init__(self, name=None, **kwargs): """ """ ReferenceField.__init__(self, name, **kwargs) self.multiValued = True # force multivalued self.search_criterias = {'SearchableText': '', 'portal_types': (), }
def __init__(self, name=None, **kwargs): """ """ ReferenceField.__init__(self, name, **kwargs) self.multiValued = True # force multivalued self.search_criterias = { 'SearchableText': '', 'portal_types': (), }
def get(self, instance, aslist=False, **kwargs): """ Return referenced objects + newer if auto_reference is set """ result = ReferenceField.get(self, instance, aslist, **kwargs) if not self.getAutoReference(instance): return result uid_ct = getToolByName(instance, 'uid_catalog') x_uids = self.getExcludedUIDs(instance) # also exclude 'real' references to avoid doublons x_uids.update(dict.fromkeys([o.UID() for o in result])) # search with empty would return all catalog if len(x_uids): x_uids = uid_ct.searchResults(UID={'query': x_uids.keys(),}) x_uids = {}.fromkeys([b.getPath() for b in x_uids], True) # brains path from uid catalog are relative to portal root urlTool = getToolByName(instance, 'portal_url') portal_path = '/'.join(urlTool.getPortalObject().getPhysicalPath()) portal_path_len = len(portal_path) + 1 # trailling '/' counted auto_refs = self.searchContents(instance) auto_refs = [b.getObject() for b in auto_refs if not x_uids.has_key(b.getPath()[portal_path_len:])] result.extend(auto_refs) return result
def get(self, instance, aslist=False, **kwargs): """ Return referenced objects + newer if auto_reference is set """ result = ReferenceField.get(self, instance, aslist, **kwargs) if not self.getAutoReference(instance): return result uid_ct = getToolByName(instance, 'uid_catalog') x_uids = self.getExcludedUIDs(instance) # also exclude 'real' references to avoid doublons x_uids.update(dict.fromkeys([o.UID() for o in result])) # search with empty would return all catalog if len(x_uids): x_uids = uid_ct.searchResults(UID={ 'query': x_uids.keys(), }) x_uids = {}.fromkeys([b.getPath() for b in x_uids], True) # brains path from uid catalog are relative to portal root urlTool = getToolByName(instance, 'portal_url') portal_path = '/'.join(urlTool.getPortalObject().getPhysicalPath()) portal_path_len = len(portal_path) + 1 # trailling '/' counted auto_refs = self.searchContents(instance) auto_refs = [ b.getObject() for b in auto_refs if not x_uids.has_key(b.getPath()[portal_path_len:]) ] result.extend(auto_refs) return result
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
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
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
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)
def __init__(self, *args, **kwargs): if not kwargs['relationship']: kwargs['relationship'] = 'hasMarsCat%s' % args[0] ReferenceField.__init__(self, *args, **kwargs)
"Signature", widget=ImageWidget( label=_("Signature"), description=_("Upload a scanned signature to be used on printed " "analysis results reports. Ideal size is 250 pixels " "wide by 150 high"), ), ), ReferenceField( "Departments", required=0, vocabulary_display_path_bound=sys.maxint, allowed_types=("Department", ), relationship="LabContactDepartment", vocabulary="_departmentsVoc", referenceClass=HoldingReference, multiValued=1, widget=ReferenceWidget( checkbox_bound=0, label=_("Departments"), description=_("The laboratory departments"), ), ), UIDReferenceField( "DefaultDepartment", required=0, vocabulary_display_path_bound=sys.maxint, vocabulary="_defaultDepsVoc", accessor="getDefaultDepartmentUID", widget=SelectionWidget( visible=True,
widget=TextAreaWidget( description=_( "To be displayed below each Analysis Category section on results " "reports."), label=_("Comments")), ) Department = ReferenceField( "Department", required=1, allowed_types=("Department",), relationship="AnalysisCategoryDepartment", referenceClass=HoldingReference, widget=ReferenceWidget( label=_("Department"), description=_("The laboratory department"), showOn=True, catalog_name=SETUP_CATALOG, base_query={ "is_active": True, "sort_on": "sortable_title", "sort_order": "ascending" }, ) ) SortKey = FloatField( "SortKey", validators=("SortKeyValidator",), widget=DecimalWidget( label=_("Sort Key"), description=_(
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)