<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from __future__ import print_function <EOL> from builtins import str <EOL> from past . builtins import basestring <EOL> from builtins import object <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from cgi import escape <EOL> from atelier import rstgen <EOL> from django . conf import settings <EOL> from django . utils . encoding import force_text <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . translation import get_language <EOL> from lino . utils . html2rst import RstTable <EOL> from lino . utils import isiterable <EOL> from lino . utils . xmlgen . html import E <EOL> from lino . core import constants <EOL> from lino . core . menus import Menu , MenuItem <EOL> from . import elems <EOL> if False : <EOL> from lino . utils . jscompressor import JSCompressor <EOL> jscompress = JSCompressor ( ) . compress <EOL> else : <EOL> def jscompress ( s ) : <EOL> return s <EOL> def add_user_language ( kw , ar ) : <EOL> if len ( settings . SITE . languages ) == <NUM_LIT:1> : <EOL> return <EOL> u = ar . get_user ( ) <EOL> lang = get_language ( ) <EOL> if u and u . language and lang != u . language : <EOL> kw . setdefault ( constants . URL_PARAM_USER_LANGUAGE , lang ) <EOL> elif lang != settings . SITE . get_default_language ( ) : <EOL> kw . setdefault ( constants . URL_PARAM_USER_LANGUAGE , lang ) <EOL> class NOT_GIVEN ( object ) : <EOL> pass <EOL> class Renderer ( object ) : <EOL> """<STR_LIT>""" <EOL> can_auth = False <EOL> is_interactive = False <EOL> not_implemented_js = None <EOL> def ar2js ( self , ar , obj , ** status ) : <EOL> """<STR_LIT>""" <EOL> return self . not_implemented_js <EOL> class HtmlRenderer ( Renderer ) : <EOL> row_classes_map = { } <EOL> def __init__ ( self , plugin ) : <EOL> self . plugin = plugin <EOL> def js2url ( self , js ) : <EOL> if not js : <EOL> return None <EOL> js = escape ( js ) <EOL> return '<STR_LIT>' + js <EOL> def href ( self , url , text ) : <EOL> return E . a ( text , href = url ) <EOL> def show_table ( self , * args , ** kwargs ) : <EOL> return E . tostring ( self . table2story ( * args , ** kwargs ) ) <EOL> def table2story ( self , ar , nosummary = False , stripped = True , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if ar . actor . master is not None and not nosummary : <EOL> if ar . actor . slave_grid_format == '<STR_LIT>' : <EOL> return ar . actor . get_slave_summary ( ar . master_instance , ar ) <EOL> return ar . table2xhtml ( ** kw ) <EOL> def action_call ( self , request , bound_action , status ) : <EOL> return None <EOL> def action_call_on_instance ( self , obj , ar , ba , request_kwargs = { } , ** st ) : <EOL> """<STR_LIT>""" <EOL> return self . not_implemented_js <EOL> def request_handler ( self , ar , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . not_implemented_js <EOL> def href_to_request ( self , sar , tar , text = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> uri = self . js2url ( self . request_handler ( tar ) ) <EOL> return self . href_button_action ( <EOL> tar . bound_action , uri , text or tar . get_title ( ) , ** kw ) <EOL> def href_button_action ( <EOL> self , ba , url , text = None , title = None , icon_name = NOT_GIVEN , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if icon_name is NOT_GIVEN : <EOL> icon_name = ba . action . icon_name <EOL> if not '<STR_LIT>' in kw : <EOL> if icon_name is None : <EOL> kw . update ( style = "<STR_LIT>" ) <EOL> else : <EOL> kw . update ( style = "<STR_LIT>" ) <EOL> return self . href_button ( url , text , title , icon_name = icon_name , ** kw ) <EOL> def href_button ( <EOL> self , url , text , title = None , target = None , icon_name = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if target : <EOL> kw . update ( target = target ) <EOL> if title : <EOL> kw . update ( title = str ( title ) ) <EOL> if not isinstance ( text , ( tuple , list ) ) : <EOL> text = ( text , ) <EOL> if url is None : <EOL> return E . b ( * text ) <EOL> kw . update ( href = url ) <EOL> if icon_name is not None : <EOL> src = settings . SITE . build_static_url ( <EOL> '<STR_LIT>' , '<STR_LIT>' , icon_name + '<STR_LIT>' ) <EOL> img = E . img ( src = src , alt = icon_name ) <EOL> return E . a ( img , ** kw ) <EOL> else : <EOL> return E . a ( * text , ** kw ) <EOL> def quick_add_buttons ( self , ar ) : <EOL> """<STR_LIT>""" <EOL> buttons = [ ] <EOL> sar = ar . actor . insert_action . request_from ( ar ) <EOL> if sar . get_permission ( ) : <EOL> btn = sar . ar2button ( None , _ ( "<STR_LIT>" ) ) <EOL> buttons . append ( btn ) <EOL> buttons . append ( '<STR_LIT:U+0020>' ) <EOL> n = ar . get_total_count ( ) <EOL> if n > <NUM_LIT:0> : <EOL> obj = ar . data_iterator [ n - <NUM_LIT:1> ] <EOL> st = ar . get_status ( ) <EOL> st . update ( record_id = obj . pk ) <EOL> a = ar . actor . detail_action <EOL> buttons . append ( self . window_action_button ( <EOL> ar . request , a , st , _ ( "<STR_LIT>" ) , <EOL> icon_name = '<STR_LIT>' , <EOL> title = _ ( "<STR_LIT>" ) ) ) <EOL> buttons . append ( '<STR_LIT:U+0020>' ) <EOL> buttons . append ( self . href_to_request ( <EOL> None , ar , <EOL> _ ( "<STR_LIT>" ) , <EOL> icon_name = '<STR_LIT>' , <EOL> title = _ ( "<STR_LIT>" ) ) ) <EOL> return E . p ( * buttons ) <EOL> def pk2url ( self , ar , pk , ** kw ) : <EOL> return None <EOL> def get_home_url ( self , * args , ** kw ) : <EOL> return settings . SITE . kernel . default_ui . build_plain_url ( * args , ** kw ) <EOL> def instance_handler ( self , ar , obj ) : <EOL> "<STR_LIT>" <EOL> return None <EOL> def obj2html ( self , ar , obj , text = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if text is None : <EOL> text = ( force_text ( obj ) , ) <EOL> elif isinstance ( text , basestring ) or E . iselement ( text ) : <EOL> text = ( text , ) <EOL> url = self . instance_handler ( ar , obj ) <EOL> if url is None : <EOL> return E . em ( * text ) <EOL> return E . a ( * text , href = url , ** kwargs ) <EOL> def quick_upload_buttons ( self , rr ) : <EOL> return '<STR_LIT>' <EOL> def create_layout_element ( self , * args , ** kw ) : <EOL> return elems . create_layout_element ( * args , ** kw ) <EOL> def create_layout_panel ( self , * args , ** kw ) : <EOL> return elems . create_layout_panel ( * args , ** kw ) <EOL> def ar2button ( self , ar , obj = None , label = None , title = None , ** kw ) : <EOL> ba = ar . bound_action <EOL> label = label or ba . get_button_label ( ) <EOL> status = ar . get_status ( ) <EOL> js = self . ar2js ( ar , obj , ** status ) <EOL> uri = self . js2url ( js ) <EOL> return self . href_button_action ( <EOL> ba , uri , label , title or ba . action . help_text , ** kw ) <EOL> def row_action_button ( <EOL> self , obj , ar , ba , label = None , title = None , request_kwargs = { } , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> label = label or ba . action . label <EOL> js = self . action_call_on_instance ( obj , ar , ba , request_kwargs ) <EOL> uri = self . js2url ( js ) <EOL> return self . href_button_action ( <EOL> ba , uri , label , title or ba . action . help_text , ** kw ) <EOL> def row_action_button_ar ( <EOL> self , obj , ar , label = None , title = None , request_kwargs = { } , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> ba = ar . bound_action <EOL> label = label or ba . action . label <EOL> js = self . action_call_on_instance ( obj , ar , ba ) <EOL> uri = self . js2url ( js ) <EOL> return self . href_button_action ( <EOL> ba , uri , label , title or ba . action . help_text , ** kw ) <EOL> def window_action_button ( <EOL> self , request , ba , <EOL> after_show = { } , label = None , title = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> label = str ( label or ba . get_button_label ( ) ) <EOL> url = self . js2url ( self . action_call ( request , ba , after_show ) ) <EOL> return self . href_button_action ( ba , url , label , <EOL> title or ba . action . help_text , ** kw ) <EOL> def action_button ( self , obj , ar , ba , label = None , ** kw ) : <EOL> if not label : <EOL> label = ba . action . label <EOL> return "<STR_LIT>" % label <EOL> def show_story ( self , ar , story , stripped = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from lino . core . actors import Actor <EOL> from lino . core . tables import TableRequest <EOL> elems = [ ] <EOL> for item in story : <EOL> if E . iselement ( item ) : <EOL> elems . append ( item ) <EOL> elif isinstance ( item , type ) and issubclass ( item , Actor ) : <EOL> ar = item . default_action . request ( parent = ar ) <EOL> elems . append ( self . table2story ( ar , ** kwargs ) ) <EOL> elif isinstance ( item , TableRequest ) : <EOL> assert item . renderer is not None <EOL> elems . append ( self . table2story ( item , ** kwargs ) ) <EOL> elif isiterable ( item ) : <EOL> elems . append ( self . show_story ( ar , item , ** kwargs ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % item ) <EOL> return E . div ( * elems ) <EOL> def obj2str ( self , ar , obj , text = None , ** kwargs ) : <EOL> return E . tostring ( self . obj2html ( ar , obj , text , ** kwargs ) ) <EOL> def show_menu ( self , ar , mnu , level = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( mnu , Menu ) : <EOL> assert isinstance ( mnu , MenuItem ) <EOL> if mnu . bound_action : <EOL> sr = mnu . bound_action . actor . request ( <EOL> action = mnu . bound_action , <EOL> user = ar . user , subst_user = ar . subst_user , <EOL> requesting_panel = ar . requesting_panel , <EOL> renderer = self , ** mnu . params ) <EOL> url = sr . get_request_url ( ) <EOL> else : <EOL> url = mnu . href <EOL> assert mnu . label is not None <EOL> if url is None : <EOL> return E . p ( ) <EOL> return E . li ( E . a ( mnu . label , href = url , tabindex = "<STR_LIT>" ) ) <EOL> items = [ self . show_menu ( ar , mi , level + <NUM_LIT:1> ) for mi in mnu . items ] <EOL> if level == <NUM_LIT:1> : <EOL> return E . ul ( * items , class_ = '<STR_LIT>' ) <EOL> if mnu . label is None : <EOL> raise Exception ( "<STR_LIT>" % mnu ) <EOL> if level == <NUM_LIT:2> : <EOL> cl = '<STR_LIT>' <EOL> menu_title = E . a ( <EOL> str ( mnu . label ) , E . b ( '<STR_LIT:U+0020>' , class_ = "<STR_LIT>" ) , href = "<STR_LIT:#>" , <EOL> class_ = '<STR_LIT>' , data_toggle = "<STR_LIT>" ) <EOL> elif level == <NUM_LIT:3> : <EOL> menu_title = E . a ( str ( mnu . label ) , href = "<STR_LIT:#>" ) <EOL> cl = '<STR_LIT>' <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return E . li ( <EOL> menu_title , <EOL> E . ul ( * items , class_ = '<STR_LIT>' ) , <EOL> class_ = cl ) <EOL> class TextRenderer ( HtmlRenderer ) : <EOL> """<STR_LIT>""" <EOL> user = None <EOL> def __init__ ( self , * args , ** kw ) : <EOL> HtmlRenderer . __init__ ( self , * args , ** kw ) <EOL> self . user = None <EOL> def get_request_url ( self , ar , * args , ** kw ) : <EOL> return None <EOL> def menu2rst ( self , ar , mnu , level = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( mnu , Menu ) : <EOL> return str ( mnu . label ) <EOL> has_submenus = False <EOL> for i in mnu . items : <EOL> if isinstance ( i , Menu ) : <EOL> has_submenus = True <EOL> items = [ self . menu2rst ( ar , mi , level + <NUM_LIT:1> ) for mi in mnu . items ] <EOL> if has_submenus : <EOL> s = rstgen . ul ( items ) . strip ( ) + '<STR_LIT:\n>' <EOL> if mnu . label is not None : <EOL> s = str ( mnu . label ) + '<STR_LIT>' + s <EOL> else : <EOL> s = '<STR_LIT:U+002CU+0020>' . join ( items ) <EOL> if mnu . label is not None : <EOL> s = str ( mnu . label ) + '<STR_LIT>' + s <EOL> return s <EOL> def show_menu ( self , ar , mnu , stripped = True , level = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> s = self . menu2rst ( ar , mnu , level ) <EOL> if stripped : <EOL> for ln in s . splitlines ( ) : <EOL> if ln . strip ( ) : <EOL> print ( ln ) <EOL> else : <EOL> print ( s ) <EOL> def show_table ( self , * args , ** kwargs ) : <EOL> print ( self . table2story ( * args , ** kwargs ) ) <EOL> def table2story ( self , ar , column_names = None , header_level = None , <EOL> nosummary = False , stripped = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if ar . actor . master is not None and not nosummary : <EOL> if ar . actor . slave_grid_format == '<STR_LIT>' : <EOL> s = E . to_rst ( <EOL> ar . actor . get_slave_summary ( ar . master_instance , ar ) , <EOL> stripped = stripped ) <EOL> if stripped : <EOL> s = s . strip ( ) <EOL> return s <EOL> fields , headers , widths = ar . get_field_info ( column_names ) <EOL> sums = [ fld . zero for fld in fields ] <EOL> rows = [ ] <EOL> recno = <NUM_LIT:0> <EOL> for row in ar . sliced_data_iterator : <EOL> recno += <NUM_LIT:1> <EOL> rows . append ( [ x for x in ar . row2text ( fields , row , sums ) ] ) <EOL> if len ( rows ) == <NUM_LIT:0> : <EOL> s = str ( ar . no_data_text ) <EOL> if not stripped : <EOL> s = "<STR_LIT:\n>" + s + "<STR_LIT:\n>" <EOL> return s <EOL> if not ar . actor . hide_sums : <EOL> has_sum = False <EOL> for i in sums : <EOL> if i : <EOL> has_sum = True <EOL> break <EOL> if has_sum : <EOL> rows . append ( [ x for x in ar . sums2html ( fields , sums ) ] ) <EOL> t = RstTable ( headers , ** kwargs ) <EOL> s = t . to_rst ( rows ) <EOL> if header_level is not None : <EOL> h = rstgen . header ( header_level , ar . get_title ( ) ) <EOL> if stripped : <EOL> h = h . strip ( ) <EOL> s = h + "<STR_LIT:\n>" + s <EOL> return s <EOL> def show_story ( self , ar , story , stripped = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from lino . core . actors import Actor <EOL> from lino . core . requests import ActionRequest <EOL> for item in story : <EOL> if E . iselement ( item ) : <EOL> print ( E . to_rst ( item , stripped ) ) <EOL> elif isinstance ( item , type ) and issubclass ( item , Actor ) : <EOL> ar = item . default_action . request ( parent = ar ) <EOL> self . show_table ( ar , stripped = stripped , ** kwargs ) <EOL> elif isinstance ( item , ActionRequest ) : <EOL> self . show_table ( item , stripped = stripped , ** kwargs ) <EOL> elif isiterable ( item ) : <EOL> self . show_story ( ar , item , stripped , ** kwargs ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % item ) <EOL> def obj2str ( self , ar , obj , text = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if text is None : <EOL> text = force_text ( obj ) <EOL> return settings . SITE . obj2text_template . format ( text ) </s>
