def set_claim_response(self, claim): """Return a list of Claim response objects as dictionaries. """ results = [] container = IResponseContainer(claim) for id, response in enumerate(container): if response is None: continue # response has been removed results.append(dict(id=id + 1, creator=type_cast(response.creator), date=type_cast(response.date), review_state=type_cast(response.review_state), text=type_cast(response.text))) return results
def serialize(self, results): """Serialize fields of a list of content type objects. :param results: [required] list of objects to be serialized :type results: list of catalog brains :returns: list of serialized objects :rtype: list of dictionaries """ s = [] for obj in results: # initialize a dictionary with the object uri # XXX: should we use the UUID? fields = dict(uri=obj.absolute_url()) # continue with the rest of the fields if IBaseContent.providedBy(obj): obj_fields = obj.Schema().fields() obj_fields = [(f.getName(), f.get(obj)) for f in obj_fields] else: schema = getUtility(IDexterityFTI, name=obj.portal_type).lookupSchema() obj_fields = [(f, getattr(obj, f)) for f in schema] for name, data in obj_fields: if name in SERIALIZABLE_FIELD_NAMES: fields[name] = type_cast(data, name, obj) s.append(fields) return s
def serialize(self, results): """Serialize fields of a list of Dexterity-based content type objects. :param results: [required] list of objects to be serialized :type results: list of catalog brains :returns: list of serialized objects :rtype: list of dictionaries """ s = [] for i in results: obj = i.getObject() # find out object schema to list its fields schema = getUtility(IDexterityFTI, name=obj.portal_type).lookupSchema() # initialize a dictionary with the object uri # XXX: should we use the UUID? fields = dict(uri=obj.absolute_url()) # continue with the rest of the fields for name, field in getFieldsInOrder(schema): # 'name' could be a @property and not a real field if not hasattr(obj, name): continue value = getattr(obj, name) fields[name] = type_cast(value, obj=obj) s.append(fields) return s
def serialize(self, results): """Serialize fields of a list of Dexterity-based content type objects. :param results: [required] list of objects to be serialized :type results: list of catalog brains :returns: list of serialized objects :rtype: list of dictionaries """ s = [] for i in results: obj = i.getObject() # initialize a dictionary with the object uri fields = dict(uri=obj.absolute_url()) # XXX: should we use UUID? # find out object schema to list its fields schema = getUtility(IDexterityFTI, name=obj.portal_type).lookupSchema() # XXX: probably there's a better way to accomplish this # but I do not know how to access the roles and permissions # of an anonymous user # read_permission_mapping = schema.queryTaggedValue(READ_PERMISSIONS_KEY) # if read_permission_mapping is None: # read_permission_mapping = {} read_permission_mapping = [] if obj.portal_type == 'Claim': read_permission_mapping = permission_map_for_anonymous() # Date fields that are not being presented. fields['modification_date'] = obj.modification_date.ISO8601() fields['creation_date'] = obj.creation_date.ISO8601() # Set the review state for a Claim. review_state = api.content.get_state(obj=obj) fields['review_state'] = review_state # Set a list of responses. fields['responses'] = self.set_claim_response(obj) if self.have_behavior('ICategorization', obj): fields['subject'] = obj.subject if self.have_behavior('IRelatedItems', obj): items = [ r.to_object.absolute_url() for r in obj.relatedItems ] fields['related_items'] = items # TODO: Should we include attached fields information to the # JSON API? # continue with the rest of the fields for name, field in getFieldsInOrder(schema): if name in read_permission_mapping: continue # private information; do not display it # 'name' could be a @property and not a real field if not hasattr(obj, name): continue value = getattr(obj, name) fields[name] = type_cast(value) s.append(fields) return s
def render(self): self.request.response.setHeader('Content-Type', 'application/json') j = {} # walk over all fields of the configlet Schema for r in self.fields: j[r] = type_cast(getattr(self.settings, r)) return json.dumps(j, sort_keys=True, indent=4)
def serialize(self, results): """Serialize fields of a list of Dexterity-based content type objects. :param results: [required] list of objects to be serialized :type results: list of catalog brains :returns: list of serialized objects :rtype: list of dictionaries """ s = [] for i in results: obj = i.getObject() # initialize a dictionary with the object uri fields = dict(uri=obj.absolute_url()) # XXX: should we use UUID? # find out object schema to list its fields schema = getUtility(IDexterityFTI, name=obj.portal_type).lookupSchema() # XXX: probably there's a better way to accomplish this # but I do not know how to access the roles and permissions # of an anonymous user # read_permission_mapping = schema.queryTaggedValue(READ_PERMISSIONS_KEY) # if read_permission_mapping is None: # read_permission_mapping = {} read_permission_mapping = [] if obj.portal_type == 'Claim': read_permission_mapping = permission_map_for_anonymous() # Date fields that are not being presented. fields['modification_date'] = obj.modification_date.ISO8601() fields['creation_date'] = obj.creation_date.ISO8601() # Set the review state for a Claim. review_state = api.content.get_state(obj=obj) fields['review_state'] = review_state # Set a list of responses. fields['responses'] = self.set_claim_response(obj) if self.have_behavior('ICategorization', obj): fields['subject'] = obj.subject if self.have_behavior('IRelatedItems', obj): items = [r.to_object.absolute_url() for r in obj.relatedItems] fields['related_items'] = items # TODO: Should we include attached fields information to the # JSON API? # continue with the rest of the fields for name, field in getFieldsInOrder(schema): if name in read_permission_mapping: continue # private information; do not display it # 'name' could be a @property and not a real field if not hasattr(obj, name): continue value = getattr(obj, name) fields[name] = type_cast(value) s.append(fields) return s