def get_brain_info(brain): """Extract the brain info """ icon = api.get_icon(brain) # avoid 404 errors with these guys if "document_icon.gif" in icon: icon = "" id = api.get_id(brain) url = api.get_url(brain) title = api.get_title(brain) description = api.get_description(brain) parent = api.get_parent(brain) parent_title = api.get_title(parent) parent_url = api.get_url(parent) return { "id": id, "title": title, "title_or_id": title or id, "description": description, "url": url, "parent_title": parent_title, "parent_url": parent_url, "icon": icon, }
def spotlight_search_route(context, request): """The spotlight search route """ catalogs = [ "portal_catalog", "bika_setup_catalog", "bika_catalog", # "bika_analysis_catalog" ] search_results = [] for catalog in catalogs: search_results.extend(search(catalog=catalog)) def get_state(brain): state = getattr(brain, "review_state", "") if not isinstance(state, basestring): return "" return state items = [] for brain in search_results: icon = api.get_icon(brain) # avoid 404 errors with these guys if "document_icon.gif" in icon: icon = "" id = api.get_id(brain) title = api.get_title(brain) items.append({ "id": id, "title": title, "title_or_id": title or id, "description": api.get_description(brain), "uid": api.get_uid(brain), "path": api.get_path(brain), "url": api.get_url(brain), "state": get_state(brain), "icon": icon, }) return { "count": len(items), "items": items, }
def get_id(brain_or_object): """Proxy to senaite.api.get_id """ return api.get_id(brain_or_object)
def update_object_with_data(self, obj, data, domain): """Update an existing object with data """ # get the storage and UID map storage = self.get_storage(domain=domain) uidmap = storage["uidmap"] # Proxy Fields must be set after its dependency object is already set. # Thus, we will store all the ProxyFields and set them in the end proxy_fields = [] for fieldname, field in api.get_fields(obj).items(): fm = IFieldManager(field) value = data.get(fieldname) # handle JSON data reference fields if isinstance(value, dict) and value.get("uid"): # dereference the referenced object value = self.dereference_object(value.get("uid"), uidmap) elif isinstance(value, (list, tuple)): for item in value: # If it is list of json data dict of objects, add local # uid to that dictionary. This local_uid can be used in # Field Managers. if isinstance(item, dict): for k, v in item.iteritems(): if 'uid' in k: local_uid = uidmap.get(v) item[k] = local_uid # handle file fields if field.type in ("file", "image", "blob"): if data.get(fieldname) is not None: fileinfo = data.get(fieldname) url = fileinfo.get("download") filename = fileinfo.get("filename") data["filename"] = filename response = requests.get(url) value = response.content # Leave the Proxy Fields for later if isinstance(fm, ProxyFieldManager): proxy_fields.append({ 'field_name': fieldname, 'fm': fm, 'value': value }) continue logger.info("Setting value={} on field={} of object={}".format( repr(value), fieldname, api.get_id(obj))) try: fm.set(obj, value) except: logger.error("Could not set field '{}' with value '{}'".format( fieldname, value)) # All reference fields are set. We can set the proxy fields now. for pf in proxy_fields: field_name = pf.get("field_name") fm = pf.get("fm") value = pf.get("value") logger.info("Setting value={} on field={} of object={}".format( repr(value), field_name, api.get_id(obj))) try: fm.set(obj, value) except: logger.error("Could not set field '{}' with value '{}'".format( field_name, value)) # Set the workflow states wf_info = data.get("workflow_info", []) for wf_dict in wf_info: wf_id = wf_dict.get("workflow") review_history = wf_dict.get("review_history") self.import_review_history(obj, wf_id, review_history) # finally reindex the object self.uids_to_reindex.append([api.get_uid(obj), repr(obj)])