def set(self, instance, value, **kw): """ Set Analyses to an AR :param instance: Analysis Request :param value: Single AS UID or a list of dictionaries containing AS UIDs :param kw: Additional keyword parameters passed to the field """ if not isinstance(value, (list, tuple)): value = [value] uids = [] for item in value: uid = None if isinstance(item, dict): uid = item.get("uid") if api.is_uid(value): uid = item if uid is None: logger.warn("Could extract UID of value") continue uids.append(uid) analyses = map(api.get_object_by_uid, uids) self._set(instance, analyses, **kw)
def get(context, request, resource=None, uid=None): """GET """ # we have a UID as resource, return the record if api.is_uid(resource): return api.get_record(resource) portal_type = api.resource_to_portal_type(resource) if portal_type is None: raise APIError(404, "Not Found") return api.get_batched(portal_type=portal_type, uid=uid, endpoint="senaite.jsonapi.v1.get")
def set(self, instance, value, **kw): # noqa """Set the value of the uid reference field """ ref = [] # The value is an UID if api.is_uid(value): ref.append(value) # The value is a dictionary, get the UIDs. if u.is_dict(value): ref = ref.append(value.get("uid")) # The value is already an object if api.is_at_content(value): ref.append(value) # The value is a list if u.is_list(value): for item in value: # uid if api.is_uid(item): ref.append(item) # dict (catalog query) elif u.is_dict(item): # If there is UID of objects, just use it. uid = item.get('uid', None) if uid: ref.append(uid) # Handle non multi valued fields if not self.multi_valued: if len(ref) > 1: raise ValueError("Multiple values given for single valued " "field {}".format(repr(self.field))) else: ref = ref[0] return self._set(instance, ref, **kw)
def set(self, instance, value, **kw): # noqa """Set the value of the refernce field """ ref = [] # The value is an UID if api.is_uid(value): ref.append(api.get_object_by_uid(value)) # The value is already an object if api.is_at_content(value): ref.append(value) # The value is a dictionary # -> handle it like a catalog query if u.is_dict(value): results = api.search(portal_type=self.allowed_types, **value) ref = map(api.get_object, results) # The value is a list if u.is_list(value): for item in value: # uid if api.is_uid(item): ref.append(api.get_object_by_uid(item)) continue # object if api.is_at_content(item): ref.append(api.get_object(item)) continue # path if api.is_path(item): ref.append(api.get_object_by_path(item)) continue # dict (catalog query) if u.is_dict(item): # If there is UID of objects, just use it. uid = item.get('uid', None) if uid: obj = api.get_object_by_uid(uid) ref.append(obj) else: results = api.search(portal_type=self.allowed_types, **item) objs = map(api.get_object, results) ref.extend(objs) continue # Plain string # -> do a catalog query for title if isinstance(item, basestring): results = api.search(portal_type=self.allowed_types, title=item) objs = map(api.get_object, results) ref.extend(objs) continue # The value is a physical path if api.is_path(value): ref.append(api.get_object_by_path(value)) # Handle non multi valued fields if not self.multi_valued: if len(ref) > 1: raise ValueError("Multiple values given for single valued " "field {}".format(repr(self.field))) else: ref = ref[0] return self._set(instance, ref, **kw)