def render_compact_attribute(submodel, item, context): attr = IAttribute(submodel) if attr.value_info.value_type is ValueTypes.binary: if item.reference is not None: return item.reference.resolve(context) elif attr.is_readable: d = attr.fetch_value() d.addCallback(render_value, context) return d
def render_attribute(model, context, result=None): if not IAttribute.providedBy(model): return result and result.wait() result = result or AsyncDict() subcontext = context.descend(model) attr = IAttribute(model) result.add("info", render_value_info(attr.value_info)) result.add_if_true("readable", attr.is_readable) result.add_if_true("writable", attr.is_writable) result.add_if_true("deletable", attr.is_deletable) if attr.is_readable: if attr.value_info.value_type is not ValueTypes.binary: d = attr.fetch_value() d.addCallback(render_value, subcontext) result.add("value", d) return result.wait()
def render_compact_model(model, context): if IAttribute.providedBy(model): attr = IAttribute(model) if attr.is_readable: d = attr.fetch_value() d.addCallback(render_value, context) return d return defer.succeed(None) if render_as_list(model): return render_model_as_list(model, context) result = AsyncDict() if model.reference: result.add_result("href", model.reference, "resolve", context) d = model.fetch_items() d.addCallback(render_compact_items, context, result) return d
def _action_errback(self, fail, action, model): if IAttribute.providedBy(model) and fail.check(ValueError): return error.handle_failure(self, fail, "Failed running action name: %s on model: %r", action.name, model) self.fail("Calling action %s on model %s failed, look at logs." % (action.name, model.identity, ))
def render_model(self, model, request, response, context): request.debug("Rendering model, identity: %s name: %r", model.identity, model.name) if IAttribute.providedBy(model): return self.render_attribute(model, request, response, context) arguments = _validate_arguments(request.arguments) d = response.write_object(self.model, context=context, **arguments) d.addErrback(self.filter_errors) return d
def _writing_errback(self, fail, mime_type, model): if fail.check(NotSupported): self.info("Model %r doesn't support fetching items", model) return if IAttribute.providedBy(model) and fail.check(ValueError): return error.handle_failure( self, fail, "Failed writing the model for the mime type: %r, \n" "model: %r\nsource: %r", mime_type, model, model.source) self.fail("Failed writing model, look at logs.")
def write(self, doc, model, *args, **kwargs): self.log("Rendering html doc for a model: %r", model.identity) context = kwargs['context'] title = model.label or model.name markup = ModelLayout(title, context) if IAttribute.providedBy(model): attr = self._format_attribute(model, context) markup.content.content.append(attr) else: yield self._render_items(model, markup, context) yield self._render_actions(model, markup, context) yield markup.render(doc)
def _build_tree(self, tree, model, limit, context): if not IModel.providedBy(model): return items = yield model.fetch_items() # [dict of attributes added by this level, list of child rows] tree.append([dict(), list()]) for item in items: submodel = yield safe_fetch(item) if not submodel: continue if not IAttribute.providedBy(submodel): if limit > 0: if IModel.providedBy(submodel): subcontext = context.descend(submodel) yield self._build_tree(tree[-1][1], submodel, limit - 1, subcontext) else: column_name = item.label or item.name tree[-1][0][(column_name, limit)] = (item, context)
def _render_items(self, model, markup, context): try: items = yield model.fetch_items() if not items: return except NotSupported: return array_deepness = render_array(model) if array_deepness: markup.div(_class='array')( self._render_array(model, array_deepness, context)).close() else: ordered = self._order_items(model, items) ul = markup.ul(_class="items") for item in ordered: submodel = yield safe_fetch(item) li = markup.li() if item.reference: url = item.reference.resolve(context) markup.span(_class='name')( html.tags.a(href=url)(item.label or item.name)).close() else: markup.span(_class='name')(item.label or item.name).close() if IAttribute.providedBy(submodel): li.append(self._format_attribute_item(item, context)) else: markup.span(_class="value").close() if item.desc: markup.span(_class='desc')(item.desc).close() array_deepness = render_array(item) if submodel and array_deepness: if IModel.providedBy(submodel): array = self._render_array( submodel, array_deepness, context) markup.div(_class='array')(array).close() li.close() ul.close()
def _build_tree(self, tree, model, limit, context): if not IModel.providedBy(model): return items = yield model.fetch_items() #FIXME: column ordering do not work ordered = self._order_items(model, items) # [dict of attributes added by this level, list of child rows] tree.append([dict(), list()]) for item in ordered: #FIXME: should not need to fetch model for this m = yield item.fetch() if not IAttribute.providedBy(m): if limit > 0: submodel = yield item.fetch() if IModel.providedBy(submodel): subcontext = context.descend(submodel) yield self._build_tree(tree[-1][1], submodel, limit - 1, subcontext) else: column_name = item.label or item.name tree[-1][0][(column_name, limit)] = (item, context)
def _render_items(self, model, markup, context): items = yield model.fetch_items() if not items: return array_deepness = render_array(model) if array_deepness: markup.div(_class='array')( self._render_array(model, array_deepness, context)).close() else: ordered = self._order_items(model, items) ul = markup.ul(_class="items") for item in ordered: li = markup.li() url = item.reference.resolve(context) markup.span(_class='name')( html.tags.a(href=url)(item.label or item.name)).close() #FIXME: shouldn't need to fetch the model for that m = yield item.fetch() if IAttribute.providedBy(m): li.append(self._format_attribute_item(item, context)) else: markup.span(_class="value").close() if item.desc: markup.span(_class='desc')(item.desc).close() array_deepness = render_array(item) if array_deepness: submodel = yield item.fetch() if IModel.providedBy(submodel): array = self._render_array(submodel, array_deepness, context) markup.div(_class='array')(array).close() li.close() ul.close() markup.hr()