def show_story(self, ar, story, stripped=True, **kwargs): """ Render the given story and return it as a raw HTML string. Ignore `stripped` because it makes no sense in HTML. """ from lino.core.actors import Actor from lino.core.tables import TableRequest from lino.core.requests import ActionRequest elems = [] try: for item in forcetext(story): # print("20180907 {}".format(item)) if isinstance(item, str): elems.append(item) elif iselement(item): # 20200501 elems.append(item) elems.append(tostring(item)) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) # 20200501 elems.extend(self.table2story(ar, **kwargs)) elems += [tostring(e) for e in self.table2story(ar, **kwargs)] elif isinstance(item, TableRequest): assert item.renderer is not None # 20200501 elems.extend(self.table2story(item, **kwargs)) elems += [tostring(e) for e in self.table2story(item, **kwargs)] elif isinstance(item, ActionRequest): # example : courses.StatusReport in dashboard assert item.renderer is not None # 20200501 elems.append(self.show_story(ar, item.actor.get_story(None, ar), **kwargs)) elems += [tostring(e) for e in self.show_story(ar, item.actor.get_story(None, ar), **kwargs)] elif isinstance(item, DashboardItem): elems.extend(item.render(ar, **kwargs)) # html = self.show_story(ar, item.render(ar), **kwargs) # elems.append(html) # # 20200501 if len(html): # # elems.append(E.div( # # html, # # CLASS="dashboard-item " + item.actor.actor_id.replace(".","-") if getattr(item, "actor", False) else "" # if html: # should always be a string, never a list # if hasattr(item, "actor"): # css_class = "dashboard-item " + item.actor.actor_id.replace(".","-") # else: # css_class = '' # elems.append('<div class="{}">{}</div>'.format(css_class, html)) elif isiterable(item): elems.append(self.show_story(ar, item, **kwargs)) # for i in self.show_story(item, *args, **kwargs): # yield i else: raise Exception("Cannot handle story item %r" % item) except Warning as e: elems.append(str(e)) # print("20180907 show_story in {} : {}".format(ar.renderer, elems)) # return E.div(*elems) if len(elems) else "" if len(elems): return "<div>{}</div>".format(''.join(elems)) return ""
def choices_for_field(request, holder, field): """Return the choices for the given field and the given web request (whose requesting holder is given as `holder`). holder is either a Model, an Actor or an Action. """ # model = holder.get_chooser_model() chooser = holder.get_chooser_for_field(field.name) # logger.info('20140822 choices_for_field(%s.%s) --> %s', # holder, field.name, chooser) if chooser: qs = chooser.get_request_choices(request, holder) if not isiterable(qs): raise Exception("%s.%s_choices() returned non-iterable %r" % ( holder.model, field.name, qs)) if chooser.simple_values: def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj) d[constants.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: # values are (value, text) tuples def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] return d elif field.choices: qs = field.choices def row2dict(obj, d): if type(obj) is list or type(obj) is tuple: d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] else: d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = str(obj) return d elif isinstance(field, models.ForeignKey): m = field.rel.model t = m.get_default_table() qs = t.request(request=request).data_iterator # logger.info('20120710 choices_view(FK) %s --> %s', t, qs.query) def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % field) return (qs, row2dict)
def show_story(self, ar, story, stripped=True, **kwargs): """Render the given story as reStructuredText to stdout.""" from lino.core.actors import Actor from lino.core.tables import TableRequest from lino.core.requests import ActionRequest try: for item in forcetext(story): if iselement(item): print(to_rst(item, stripped)) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) self.show_table(ar, stripped=stripped, **kwargs) elif isinstance(item, DashboardItem): self.show_story( ar, item.render(ar), stripped, **kwargs) elif isinstance(item, TableRequest): self.show_table(item, stripped=stripped, **kwargs) # print(item.table2rst(*args, **kwargs)) elif isinstance(item, ActionRequest): # example : courses.StatusReport in dashboard assert item.renderer is not None self.show_story(ar, item.actor.get_story(None, ar), **kwargs) elif isiterable(item): self.show_story(ar, item, stripped, **kwargs) # for i in self.show_story(ar, item, *args, **kwargs): # print(i) else: raise Exception("Cannot handle %r" % item) except Warning as e: print(e)
def show_story(self, ar, story, stripped=True, **kwargs): """Render the given story as an HTML element. Ignore `stripped` because it makes no sense in HTML. """ from lino.core.actors import Actor from lino.core.tables import TableRequest elems = [] try: for item in forcetext(story): if iselement(item): elems.append(item) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) elems.append(self.table2story(ar, **kwargs)) elif isinstance(item, TableRequest): assert item.renderer is not None elems.append(self.table2story(item, **kwargs)) elif isiterable(item): elems.append(self.show_story(ar, item, **kwargs)) # for i in self.show_story(item, *args, **kwargs): # yield i else: raise Exception("Cannot handle %r" % item) except Warning as e: elems.append(str(e)) return E.div(*elems)
def choices_for_field(request, actor, field): """ Return the choices for the given field and the given web request (whose requesting actor has already been identified and is given as `actor`). """ #~ logger.info("20120202 %r",field) chooser = choosers.get_for_field(field) if chooser: qs = chooser.get_request_choices(request, actor) assert isiterable(qs), \ "%s.%s_choices() returned %r which is not iterable." % ( actor.model, field.name, qs) if chooser.simple_values: def row2dict(obj, d): d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj) d[ext_requests.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj, d): d[ext_requests.CHOICES_TEXT_FIELD] = actor.get_choices_text( obj, request, field) d[ext_requests.CHOICES_VALUE_FIELD] = obj.pk return d else: # values are (value,text) tuples def row2dict(obj, d): d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj[1]) d[ext_requests.CHOICES_VALUE_FIELD] = obj[0] return d elif field.choices: qs = field.choices def row2dict(obj, d): if type(obj) is list or type(obj) is tuple: d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj[1]) d[ext_requests.CHOICES_VALUE_FIELD] = obj[0] else: d[ext_requests.CHOICES_TEXT_FIELD] = actor.get_choices_text( obj, request, field) d[ext_requests.CHOICES_VALUE_FIELD] = unicode(obj) return d elif isinstance(field, models.ForeignKey): m = field.rel.to #~ t = getattr(m,'_lino_choices_table',m.get_default_table()) t = m.get_default_table() qs = t.request(request=request).data_iterator #~ logger.info('20120710 choices_view(FK) %s --> %s',t,qs) def row2dict(obj, d): d[ext_requests.CHOICES_TEXT_FIELD] = actor.get_choices_text( obj, request, field) d[ext_requests.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % fldname) return (qs, row2dict)
def choices_for_field(request,rpt,field): #~ logger.info("20120202 %r",field) chooser = choosers.get_for_field(field) if chooser: #~ logger.info('20120710 choices_view() : has chooser') qs = chooser.get_request_choices(request,rpt) #~ qs = list(chooser.get_request_choices(ar,rpt)) #~ logger.info("20120213 %s",qs) #~ if qs is None: #~ qs = [] assert isiterable(qs), \ "%s.%s_choices() returned %r which is not iterable." % ( rpt.model,field.name,qs) if chooser.simple_values: def row2dict(obj,d): d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj) #~ 20130117 : 'int' object has no attribute 'get_choices_text' d[ext_requests.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj,d): d[ext_requests.CHOICES_TEXT_FIELD] = obj.get_choices_text(request,rpt,field) d[ext_requests.CHOICES_VALUE_FIELD] = obj.pk return d else: def row2dict(obj,d): d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj[1]) #~ d[ext_requests.CHOICES_TEXT_FIELD] = obj[1].get_choices_text(request,rpt,field) d[ext_requests.CHOICES_VALUE_FIELD] = obj[0] return d elif field.choices: qs = field.choices def row2dict(obj,d): if type(obj) is list or type(obj) is tuple: d[ext_requests.CHOICES_TEXT_FIELD] = unicode(obj[1]) d[ext_requests.CHOICES_VALUE_FIELD] = obj[0] else: d[ext_requests.CHOICES_TEXT_FIELD] = obj.get_choices_text(request,rpt,field) d[ext_requests.CHOICES_VALUE_FIELD] = unicode(obj) return d elif isinstance(field,models.ForeignKey): m = field.rel.to t = getattr(m,'_lino_choices_table',m._lino_default_table) qs = t.request(settings.LINO.ui,request).data_iterator #~ logger.info('20120710 choices_view(FK) %s --> %s',t,qs) def row2dict(obj,d): d[ext_requests.CHOICES_TEXT_FIELD] = obj.get_choices_text(request,rpt,field) d[ext_requests.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % fldname) return (qs,row2dict)
def show_story(self, ar, story, stripped=True, **kwargs): """ Render the given story and return it as an HTML tree DIV element. Ignore `stripped` because it makes no sense in HTML. """ from lino.core.actors import Actor from lino.core.tables import TableRequest from lino.core.requests import ActionRequest elems = [] try: for item in forcetext(story): # print("20180907 {}".format(item)) if iselement(item): elems.append(item) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) elems.extend(self.table2story(ar, **kwargs)) elif isinstance(item, TableRequest): assert item.renderer is not None elems.extend(self.table2story(item, **kwargs)) elif isinstance(item, ActionRequest): # example : courses.StatusReport in dashboard assert item.renderer is not None elems.append( self.show_story(ar, item.actor.get_story(None, ar), **kwargs)) elif isinstance(item, DashboardItem): html = self.show_story(ar, item.render(ar), **kwargs) if html: elems.append( E.div(html, CLASS="dashboard-item " + item.actor.actor_id.replace(".", "-") if getattr(item, "actor", False) else "")) elif isiterable(item): elems.append(self.show_story(ar, item, **kwargs)) # for i in self.show_story(item, *args, **kwargs): # yield i else: raise Exception("Cannot handle %r" % item) except Warning as e: elems.append(str(e)) # print("20180907 show_story in {} : {}".format(ar.renderer, elems)) return E.div(*elems) if elems else ""
def story2odt(self, story, *args, **kw): from lino.core.actors import Actor from lino.core.tables import TableRequest for item in story: if iselement(item): yield toxml(html2odf(item)) elif isinstance(item, type) and issubclass(item, Actor): sar = self.ar.spawn(item, *args, **kw) yield self.insert_table(sar) elif isinstance(item, TableRequest): # logger.info("20141211 story2odt %s", item) yield self.insert_table(item) elif isiterable(item): for i in self.story2odt(item, *args, **kw): yield i else: raise Exception("Cannot handle %r" % item)
def show_story(self, ar, story, stripped=True, **kwargs): """ Render the given story as an HTML element. Ignore `stripped` because it makes no sense in HTML. A story is an iterable of things that can be rendered. """ from lino.core.actors import Actor from lino.core.tables import TableRequest from lino.core.requests import ActionRequest elems = [] try: for item in forcetext(story): # print("20180907 {}".format(item)) if iselement(item): elems.append(item) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) elems.extend(self.table2story(ar, **kwargs)) elif isinstance(item, TableRequest): assert item.renderer is not None elems.extend(self.table2story(item, **kwargs)) elif isinstance(item, ActionRequest): # example : courses.StatusReport in dashboard assert item.renderer is not None elems.append(self.show_story(ar, item.actor.get_story(None, ar), **kwargs)) elif isinstance(item, DashboardItem): elems.append(E.div( self.show_story(ar, item.render(ar), **kwargs), CLASS="dashboard-item " + item.actor.actor_id.replace(".","-") if getattr(item, "actor", False) else "" )) elif isiterable(item): elems.append(self.show_story(ar, item, **kwargs)) # for i in self.show_story(item, *args, **kwargs): # yield i else: raise Exception("Cannot handle %r" % item) except Warning as e: elems.append(str(e)) # print("20180907 show_story in {} : {}".format(ar.renderer, elems)) return E.div(*elems) if elems else ""
def show_story(self, ar, story, stripped=True, **kwargs): """Render the given story as reStructuredText to stdout.""" from lino.core.actors import Actor from lino.core.requests import ActionRequest for item in story: if E.iselement(item): print(E.to_rst(item, stripped)) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) self.show_table(ar, stripped=stripped, **kwargs) elif isinstance(item, ActionRequest): self.show_table(item, stripped=stripped, **kwargs) # print(item.table2rst(*args, **kwargs)) elif isiterable(item): self.show_story(ar, item, stripped, **kwargs) # for i in self.show_story(ar, item, *args, **kwargs): # print(i) else: raise Exception("Cannot handle %r" % item)
def show_story(self, ar, story, stripped=True, **kwargs): """Render the given story as an HTML element. Ignore `stripped` because it makes no sense in HTML. """ from lino.core.actors import Actor from lino.core.tables import TableRequest elems = [] for item in story: if E.iselement(item): elems.append(item) elif isinstance(item, type) and issubclass(item, Actor): ar = item.default_action.request(parent=ar) elems.append(self.table2story(ar, **kwargs)) elif isinstance(item, TableRequest): assert item.renderer is not None elems.append(self.table2story(item, **kwargs)) elif isiterable(item): elems.append(self.show_story(ar, item, **kwargs)) # for i in self.show_story(item, *args, **kwargs): # yield i else: raise Exception("Cannot handle %r" % item) return E.div(*elems)
def choices_for_field(ar, holder, field): """ Return the choices for the given field and the given HTTP request whose `holder` is either a Model, an Actor or an Action. """ if not holder.get_view_permission(ar.request.user.user_type): raise Exception("{user} has no permission for {holder}".format( user=ar.request.user, holder=holder)) # model = holder.get_chooser_model() chooser = holder.get_chooser_for_field(field.name) # logger.info('20140822 choices_for_field(%s.%s) --> %s', # holder, field.name, chooser) if chooser: qs = chooser.get_request_choices(ar, holder) if not isiterable(qs): raise Exception("%s.%s_choices() returned non-iterable %r" % (holder.model, field.name, qs)) if chooser.simple_values: def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj) d[constants.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: # values are (value, text) tuples def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] return d return (qs, row2dict) if field.choices: qs = field.choices def row2dict(obj, d): if type(obj) is list or type(obj) is tuple: d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] else: d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = str(obj) return d return (qs, row2dict) if isinstance(field, fields.VirtualField): field = field.return_type if isinstance(field, fields.RemoteField): field = field.field if isinstance(field, models.ForeignKey): m = field.remote_field.model t = m.get_default_table() qs = t.request(request=ar.request).data_iterator # logger.info('20120710 choices_view(FK) %s --> %s', t, qs.query) def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % field) return (qs, row2dict)
def choices_for_field(request, holder, field): """Return the choices for the given field and the given web request (whose requesting holder is given as `holder`). holder is either a Model, an Actor or an Action. """ # model = holder.get_chooser_model() chooser = holder.get_chooser_for_field(field.name) # logger.info('20140822 choices_for_field(%s.%s) --> %s', # holder, field.name, chooser) if chooser: qs = chooser.get_request_choices(request, holder) if not isiterable(qs): raise Exception("%s.%s_choices() returned non-iterable %r" % (holder.model, field.name, qs)) if chooser.simple_values: def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj) d[constants.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: # values are (value, text) tuples def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] return d elif field.choices: qs = field.choices def row2dict(obj, d): if type(obj) is list or type(obj) is tuple: d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] else: d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = str(obj) return d elif isinstance(field, models.ForeignKey): m = field.rel.model t = m.get_default_table() qs = t.request(request=request).data_iterator # logger.info('20120710 choices_view(FK) %s --> %s', t, qs.query) def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % field) return (qs, row2dict)
def choices_for_field(ar, holder, field): """ Return the choices for the given field and the given HTTP request whose `holder` is either a Model, an Actor or an Action. """ if not holder.get_view_permission(ar.request.user.user_type): raise Exception( "{user} has no permission for {holder}".format( user=ar.request.user, holder=holder)) # model = holder.get_chooser_model() chooser = holder.get_chooser_for_field(field.name) # logger.info('20140822 choices_for_field(%s.%s) --> %s', # holder, field.name, chooser) if chooser: qs = chooser.get_request_choices(ar, holder) if not isiterable(qs): raise Exception("%s.%s_choices() returned non-iterable %r" % ( holder.model, field.name, qs)) if chooser.simple_values: def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj) d[constants.CHOICES_VALUE_FIELD] = obj return d elif chooser.instance_values: # same code as for ForeignKey def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: # values are (value, text) tuples def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] return d return (qs, row2dict) if field.choices: qs = field.choices def row2dict(obj, d): if type(obj) is list or type(obj) is tuple: d[constants.CHOICES_TEXT_FIELD] = str(obj[1]) d[constants.CHOICES_VALUE_FIELD] = obj[0] else: d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = str(obj) return d return (qs, row2dict) if isinstance(field, fields.VirtualField): field = field.return_type if isinstance(field, fields.RemoteField): field = field.field if isinstance(field, models.ForeignKey): m = field.remote_field.model t = m.get_default_table() qs = t.request(request=ar.request).data_iterator # logger.info('20120710 choices_view(FK) %s --> %s', t, qs.query) def row2dict(obj, d): d[constants.CHOICES_TEXT_FIELD] = holder.get_choices_text( obj, ar.request, field) d[constants.CHOICES_VALUE_FIELD] = obj.pk return d else: raise http.Http404("No choices for %s" % field) return (qs, row2dict)