<s> """<STR_LIT>""" <EOL> __all__ = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> from gluon import * <EOL> from gluon . storage import Storage <EOL> from s3utils import s3_unicode <EOL> class S3NavigationItem ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> label = None , <EOL> c = None , <EOL> f = None , <EOL> args = None , <EOL> vars = None , <EOL> extension = None , <EOL> a = None , <EOL> r = None , <EOL> m = None , <EOL> p = None , <EOL> t = None , <EOL> url = None , <EOL> tags = None , <EOL> parent = None , <EOL> translate = True , <EOL> layout = None , <EOL> check = None , <EOL> restrict = None , <EOL> link = True , <EOL> mandatory = False , <EOL> ltr = False , <EOL> ** attributes ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( label , basestring ) and translate : <EOL> self . label = current . T ( label ) <EOL> else : <EOL> self . label = label <EOL> if tags : <EOL> if type ( tags ) is not list : <EOL> tags = [ tags ] <EOL> self . tags = tags <EOL> else : <EOL> self . tags = [ ] <EOL> if r is not None : <EOL> self . r = r <EOL> if a is None : <EOL> a = r . application <EOL> if c is None : <EOL> c = r . controller <EOL> if f is None : <EOL> f = r . function <EOL> if args is None : <EOL> args = r . args <EOL> if vars is None : <EOL> vars = r . vars <EOL> else : <EOL> self . r = current . request <EOL> self . application = a <EOL> if isinstance ( c , ( list , tuple ) ) and len ( c ) : <EOL> self . controller = c [ <NUM_LIT:0> ] <EOL> self . match_controller = c <EOL> else : <EOL> self . controller = c <EOL> self . match_controller = [ c ] <EOL> if isinstance ( f , ( list , tuple ) ) and len ( f ) : <EOL> self . function = f [ <NUM_LIT:0> ] <EOL> self . match_function = f <EOL> else : <EOL> self . function = f <EOL> self . match_function = [ f ] <EOL> if args is None : <EOL> args = [ ] <EOL> elif isinstance ( args , str ) : <EOL> args = args . split ( "<STR_LIT:/>" ) <EOL> self . args = args <EOL> if vars : <EOL> self . vars = vars <EOL> else : <EOL> self . vars = Storage ( ) <EOL> self . extension = extension <EOL> self . tablename = t <EOL> self . method = m <EOL> if m is not None : <EOL> if not len ( args ) : <EOL> self . args = [ m ] <EOL> elif args [ - <NUM_LIT:1> ] != m : <EOL> self . args . append ( m ) <EOL> if p is not None : <EOL> self . p = p <EOL> else : <EOL> self . p = m <EOL> self . override_url = url <EOL> attr = Storage ( ) <EOL> opts = Storage ( ) <EOL> for k , v in attributes . iteritems ( ) : <EOL> if k [ <NUM_LIT:0> ] == "<STR_LIT:_>" : <EOL> attr [ k ] = v <EOL> else : <EOL> opts [ k ] = v <EOL> self . attr = attr <EOL> self . opts = opts <EOL> self . parent = parent <EOL> self . components = [ ] <EOL> self . enabled = True <EOL> self . selected = None <EOL> self . visible = None <EOL> self . link = link <EOL> self . mandatory = mandatory <EOL> self . ltr = ltr <EOL> self . restrict = restrict <EOL> if restrict is not None : <EOL> if not isinstance ( restrict , ( list , tuple ) ) : <EOL> self . restrict = [ restrict ] <EOL> self . check = check <EOL> renderer = None <EOL> if layout is not None : <EOL> renderer = layout <EOL> elif hasattr ( self . __class__ , "<STR_LIT>" ) : <EOL> renderer = self . get_layout ( self . OVERRIDE ) <EOL> if renderer is None and hasattr ( self . __class__ , "<STR_LIT>" ) : <EOL> renderer = self . layout <EOL> self . renderer = renderer <EOL> @ staticmethod <EOL> def get_layout ( name ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( current , "<STR_LIT>" ) : <EOL> layouts = current . layouts <EOL> else : <EOL> layouts = { } <EOL> if layouts is False : <EOL> return None <EOL> if name in layouts : <EOL> return layouts [ name ] <EOL> application = current . request . application <EOL> settings = current . deployment_settings <EOL> theme = settings . get_theme ( ) <EOL> theme_location = current . response . s3 . theme_location <EOL> if theme_location : <EOL> theme_location = "<STR_LIT>" % theme_location [ : - <NUM_LIT:1> ] <EOL> package = "<STR_LIT>" % ( application , theme_location , theme ) <EOL> try : <EOL> override = getattr ( __import__ ( package , fromlist = [ name ] ) , name ) <EOL> except ImportError : <EOL> current . layouts = False <EOL> return None <EOL> except AttributeError : <EOL> override = None <EOL> if override and hasattr ( override , "<STR_LIT>" ) and type ( override . layout ) == type ( lambda : None ) : <EOL> layout = override . layout <EOL> else : <EOL> layout = None <EOL> layouts [ name ] = layout <EOL> current . layouts = layouts <EOL> return layout <EOL> def clone ( self ) : <EOL> """<STR_LIT>""" <EOL> item = self . __class__ ( ) <EOL> item . label = self . label <EOL> item . tags = self . tags <EOL> item . r = self . r <EOL> item . application = self . application <EOL> item . controller = self . controller <EOL> item . function = self . function <EOL> item . match_controller = [ c for c in self . match_controller ] <EOL> item . match_function = [ f for f in self . match_function ] <EOL> item . args = [ a for a in self . args ] <EOL> item . vars = Storage ( ** self . vars ) <EOL> item . extension = self . extension <EOL> item . tablename = self . tablename <EOL> item . method = self . method <EOL> item . p = self . p <EOL> item . override_url = self . override_url <EOL> item . attr = Storage ( ** self . attr ) <EOL> item . opts = Storage ( ** self . opts ) <EOL> item . parent = self . parent <EOL> item . components = [ i . clone ( ) for i in self . components ] <EOL> item . enabled = self . enabled <EOL> item . selected = self . selected <EOL> item . visible = self . visible <EOL> item . link = self . link <EOL> item . mandatory = self . mandatory <EOL> if self . restrict is not None : <EOL> item . restrict = [ r for r in self . restrict ] <EOL> else : <EOL> item . restrict = None <EOL> item . check = self . check <EOL> item . renderer = self . renderer <EOL> return item <EOL> def check_active ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> c = self . get ( "<STR_LIT>" ) <EOL> if c : <EOL> return current . deployment_settings . has_module ( c ) <EOL> return True <EOL> if request is None : <EOL> request = current . request <EOL> parent = self . parent <EOL> if parent is not None : <EOL> return parent . check_active ( request ) <EOL> elif self . mandatory : <EOL> return True <EOL> elif self . match ( request ) : <EOL> return True <EOL> return False <EOL> def check_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def check_permission ( self ) : <EOL> """<STR_LIT>""" <EOL> has_role = current . auth . s3_has_role <EOL> authorized = False <EOL> restrict = self . restrict <EOL> if restrict : <EOL> for role in restrict : <EOL> if has_role ( role ) : <EOL> authorized = True <EOL> break <EOL> else : <EOL> authorized = True <EOL> if self . accessible_url ( ) == False : <EOL> authorized = False <EOL> return authorized <EOL> def check_selected ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> if self . selected is not None : <EOL> return self . selected <EOL> if request is None : <EOL> request = current . request <EOL> if self . parent is None : <EOL> branch = self . branch ( request ) <EOL> if branch is not None : <EOL> branch . select ( ) <EOL> if not self . selected : <EOL> self . selected = False <EOL> else : <EOL> root = self . get_root ( ) <EOL> if root . selected is None : <EOL> root . check_selected ( request ) <EOL> return True if self . selected else False <EOL> def check_hook ( self ) : <EOL> """<STR_LIT>""" <EOL> cond = True <EOL> check = self . check <EOL> if check is not None : <EOL> if not isinstance ( check , ( list , tuple ) ) : <EOL> check = [ check ] <EOL> for condition in check : <EOL> if callable ( condition ) and not condition ( self ) : <EOL> cond = False <EOL> elif not condition : <EOL> cond = False <EOL> return cond <EOL> def __contains__ ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> components = self . components <EOL> for i in components : <EOL> if tag in i . tags or tag in i : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def findall ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> if tag in self . tags : <EOL> items . append ( self ) <EOL> components = self . components <EOL> for c in components : <EOL> _items = c . findall ( tag ) <EOL> items . extend ( _items ) <EOL> return items <EOL> def enable ( self , tag = None ) : <EOL> """<STR_LIT>""" <EOL> if tag is not None : <EOL> items = self . findall ( tag ) <EOL> for item in items : <EOL> item . enable ( ) <EOL> else : <EOL> self . enabled = True <EOL> return <EOL> def disable ( self , tag = None ) : <EOL> """<STR_LIT>""" <EOL> if tag is not None : <EOL> items = self . findall ( tag ) <EOL> for item in items : <EOL> item . disable ( ) <EOL> else : <EOL> self . enabled = False <EOL> return <EOL> def select ( self , tag = None ) : <EOL> """<STR_LIT>""" <EOL> selected = None <EOL> if tag is None : <EOL> parent = self . parent <EOL> if parent : <EOL> parent . select ( ) <EOL> else : <EOL> self . deselect_all ( ) <EOL> selected = True <EOL> else : <EOL> for item in self . components : <EOL> if not selected : <EOL> selected = item . select ( tag = tag ) <EOL> else : <EOL> item . deselect_all ( ) <EOL> if not selected and tag in self . tags : <EOL> selected = True <EOL> self . selected = selected <EOL> return selected <EOL> def deselect_all ( self ) : <EOL> """<STR_LIT>""" <EOL> self . selected = None <EOL> for item in self . components : <EOL> item . deselect_all ( ) <EOL> return <EOL> def set_layout ( self , layout , recursive = False , tag = None ) : <EOL> """<STR_LIT>""" <EOL> if layout is not None : <EOL> if tag is None or tag in self . tags : <EOL> self . renderer = layout <EOL> if recursive : <EOL> for c in self . components : <EOL> if tag is None or tag in c . tags : <EOL> c . set_layout ( layout , recursive = recursive , tag = tag ) <EOL> return <EOL> def get ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> if name in self . __dict__ : <EOL> value = self . __dict__ [ name ] <EOL> else : <EOL> value = None <EOL> if value is not None : <EOL> return value <EOL> if name [ : <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> raise AttributeError <EOL> parent = self . parent <EOL> if parent is not None : <EOL> return parent . get ( name ) <EOL> return default <EOL> def match ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> level = <NUM_LIT:0> <EOL> args = self . args <EOL> link_vars = self . vars <EOL> if self . application is not None and self . application != request . application : <EOL> return <NUM_LIT:0> <EOL> if self . opts . selectable is False : <EOL> return <NUM_LIT:0> <EOL> check = self . check_hook ( ) <EOL> if check : <EOL> enabled = self . check_enabled ( ) <EOL> if not enabled : <EOL> check = False <EOL> if not check : <EOL> return <NUM_LIT:0> <EOL> if request is None : <EOL> request = current . request <EOL> c = self . get ( "<STR_LIT>" ) <EOL> mc = self . get ( "<STR_LIT>" ) <EOL> if not c and self . parent is None : <EOL> return <NUM_LIT:1> <EOL> rvars = request . get_vars <EOL> controller = request . controller <EOL> function = request . function <EOL> if "<STR_LIT>" in rvars : <EOL> try : <EOL> tn , record_id = rvars [ "<STR_LIT>" ] . split ( "<STR_LIT:.>" ) <EOL> controller , function = tn . split ( "<STR_LIT:_>" , <NUM_LIT:1> ) <EOL> except : <EOL> pass <EOL> if controller == c or controller in mc : <EOL> level = <NUM_LIT:1> <EOL> if level == <NUM_LIT:1> : <EOL> f = self . get ( "<STR_LIT>" ) <EOL> mf = self . get ( "<STR_LIT>" ) <EOL> if function == f or function in mf : <EOL> level = <NUM_LIT:2> <EOL> elif f == "<STR_LIT:index>" : <EOL> return <NUM_LIT:1> <EOL> elif f is not None : <EOL> return <NUM_LIT:0> <EOL> if level == <NUM_LIT:2> : <EOL> extra = <NUM_LIT:1> <EOL> for k , v in link_vars . iteritems ( ) : <EOL> if k not in rvars or k in rvars and rvars [ k ] != s3_unicode ( v ) : <EOL> extra = <NUM_LIT:0> <EOL> break <EOL> else : <EOL> extra = <NUM_LIT:2> <EOL> rargs = request . args <EOL> if rargs : <EOL> if args : <EOL> largs = [ a for a in request . args if not a . isdigit ( ) ] <EOL> if len ( args ) == len ( largs ) and all ( [ args [ i ] == largs [ i ] for i in xrange ( len ( args ) ) ] ) : <EOL> level = <NUM_LIT:5> <EOL> else : <EOL> if len ( rargs ) >= len ( args ) > <NUM_LIT:0> and rargs [ len ( args ) - <NUM_LIT:1> ] . isdigit ( ) and not str ( args [ - <NUM_LIT:1> ] ) . isdigit ( ) : <EOL> level = <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> level = <NUM_LIT:3> <EOL> elif args : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> level = <NUM_LIT:5> <EOL> level += extra <EOL> return level <EOL> def branch ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> if request is None : <EOL> request = current . request <EOL> leaf , level = self . __branch ( request ) <EOL> if level : <EOL> return leaf <EOL> else : <EOL> return None <EOL> def __branch ( self , request ) : <EOL> """<STR_LIT>""" <EOL> items = self . get_all ( enabled = True ) <EOL> l = self . match ( request ) <EOL> if not items : <EOL> return self , l <EOL> else : <EOL> match , maxlevel = None , l - <NUM_LIT:1> <EOL> for i in items : <EOL> item , level = i . __branch ( request ) <EOL> if item is not None and level > maxlevel : <EOL> match = item <EOL> maxlevel = level <EOL> if match is not None : <EOL> return match , maxlevel <EOL> else : <EOL> return self , l <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> components = [ str ( c ) for c in self . components ] <EOL> if self . enabled : <EOL> label = str ( self . label ) <EOL> else : <EOL> label = "<STR_LIT>" % self . label <EOL> label = "<STR_LIT>" % ( self . __class__ . __name__ , label ) <EOL> if components : <EOL> return "<STR_LIT>" % ( label , "<STR_LIT:U+002C>" . join ( components ) ) <EOL> else : <EOL> return "<STR_LIT>" % label <EOL> def url ( self , extension = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . link : <EOL> return None <EOL> if self . override_url : <EOL> return self . override_url <EOL> args = self . args <EOL> if self . vars : <EOL> link_vars = Storage ( self . vars ) <EOL> link_vars . update ( kwargs ) <EOL> else : <EOL> link_vars = Storage ( kwargs ) <EOL> if extension is None : <EOL> extension = self . extension <EOL> a = self . get ( "<STR_LIT>" ) <EOL> if a is None : <EOL> a = current . request . application <EOL> c = self . get ( "<STR_LIT>" ) <EOL> if c is None : <EOL> c = "<STR_LIT:default>" <EOL> f = self . get ( "<STR_LIT>" ) <EOL> if f is None : <EOL> f = "<STR_LIT:index>" <EOL> f , args = self . __format ( f , args , extension ) <EOL> return URL ( a = a , c = c , f = f , args = args , vars = link_vars ) <EOL> def accessible_url ( self , extension = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> aURL = current . auth . permission . accessible_url <EOL> if not self . link : <EOL> return None <EOL> args = self . args <EOL> if self . vars : <EOL> link_vars = Storage ( self . vars ) <EOL> link_vars . update ( kwargs ) <EOL> else : <EOL> link_vars = Storage ( kwargs ) <EOL> if extension is None : <EOL> extension = self . extension <EOL> a = self . get ( "<STR_LIT>" ) <EOL> if a is None : <EOL> a = current . request . application <EOL> c = self . get ( "<STR_LIT>" ) <EOL> if c is None : <EOL> c = "<STR_LIT:default>" <EOL> f = self . get ( "<STR_LIT>" ) <EOL> if f is None : <EOL> f = "<STR_LIT:index>" <EOL> f , args = self . __format ( f , args , extension ) <EOL> return aURL ( c = c , f = f , p = self . p , a = a , t = self . tablename , <EOL> args = args , vars = link_vars ) <EOL> @ staticmethod <EOL> def __format ( f , args , ext ) : <EOL> """<STR_LIT>""" <EOL> if not ext or ext == "<STR_LIT:html>" : <EOL> return f , args <EOL> items = [ f ] <EOL> if args : <EOL> items += args <EOL> items = [ i . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] for i in items ] <EOL> items . append ( "<STR_LIT>" % ( items . pop ( ) , ext ) ) <EOL> return ( items [ <NUM_LIT:0> ] , items [ <NUM_LIT:1> : ] ) <EOL> def render ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> renderer = self . renderer <EOL> output = None <EOL> if request is None : <EOL> request = current . request <EOL> if self . check_active ( request ) : <EOL> self . authorized = self . check_permission ( ) <EOL> self . selected = self . check_selected ( ) <EOL> cond = self . check_hook ( ) <EOL> if cond : <EOL> enabled = self . check_enabled ( ) <EOL> if not enabled : <EOL> self . enabled = False <EOL> if renderer is not None : <EOL> output = renderer ( self ) <EOL> return output <EOL> def render_components ( self ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> for c in self . components : <EOL> i = c . render ( ) <EOL> if i is not None : <EOL> if type ( i ) is list : <EOL> items . extend ( i ) <EOL> else : <EOL> items . append ( i ) <EOL> return items <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> output = self . render ( ) <EOL> if output is None : <EOL> return "<STR_LIT>" <EOL> elif hasattr ( output , "<STR_LIT>" ) : <EOL> return output . xml ( ) <EOL> else : <EOL> return str ( output ) <EOL> def set_parent ( self , p = None , i = None ) : <EOL> """<STR_LIT>""" <EOL> if p is None : <EOL> p = self . parent <EOL> if p is None : <EOL> return <EOL> parent = self . parent <EOL> if parent is not None and parent != p : <EOL> while self in parent . components : <EOL> parent . components . remove ( self ) <EOL> if i is not None : <EOL> p . component . insert ( i , self ) <EOL> else : <EOL> p . components . append ( self ) <EOL> self . parent = p <EOL> return <EOL> def append ( self , item = None ) : <EOL> """<STR_LIT>""" <EOL> if item is not None : <EOL> if type ( item ) is list : <EOL> for i in item : <EOL> self . append ( i ) <EOL> else : <EOL> item . set_parent ( self ) <EOL> return self <EOL> def insert ( self , i , item = None ) : <EOL> """<STR_LIT>""" <EOL> if item is not None : <EOL> item . set_parent ( self , i = i ) <EOL> return self <EOL> def extend ( self , items ) : <EOL> """<STR_LIT>""" <EOL> if items : <EOL> for item in items : <EOL> self . append ( item ) <EOL> return self <EOL> def __call__ ( self , * components ) : <EOL> """<STR_LIT>""" <EOL> self . extend ( components ) <EOL> return self <EOL> def __add__ ( self , items ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( items , ( list , tuple ) ) : <EOL> self . extend ( items ) <EOL> else : <EOL> self . append ( items ) <EOL> return self <EOL> def __getitem__ ( self , i ) : <EOL> """<STR_LIT>""" <EOL> return self . components . __getitem__ ( i ) <EOL> def __setitem__ ( self , i , item ) : <EOL> """<STR_LIT>""" <EOL> self . components . __setitem__ ( i , item ) <EOL> def pop ( self , i = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . components . pop ( i ) <EOL> def get_root ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent <EOL> if parent : <EOL> return parent . get_root ( ) <EOL> else : <EOL> return self <EOL> def path ( self , sub = None ) : <EOL> """<STR_LIT>""" <EOL> path = [ self ] <EOL> if sub : <EOL> path . extend ( sub ) <EOL> if self . parent : <EOL> return self . parent . path ( sub = path ) <EOL> else : <EOL> return path <EOL> def get_all ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> for item in self . components : <EOL> if not flags or all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> items . append ( item ) <EOL> return items <EOL> def get_first ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> for item in self . components : <EOL> if not flags or all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return item <EOL> return None <EOL> def get_last ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> components = list ( self . components ) <EOL> components . reverse ( ) <EOL> for item in components : <EOL> if not flags or all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return item <EOL> return None <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . components ) <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self is not None <EOL> def index ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return self . components . index ( item ) <EOL> def pos ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . parent : <EOL> return self . parent . index ( self ) <EOL> else : <EOL> return None <EOL> def is_first ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> if not flags : <EOL> return len ( self . preceding ( ) ) == <NUM_LIT:0> <EOL> if not all ( [ getattr ( self , f ) == flags [ f ] for f in flags ] ) : <EOL> return False <EOL> preceding = self . preceding ( ) <EOL> if preceding : <EOL> for item in preceding : <EOL> if all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return False <EOL> return True <EOL> def is_last ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> if not flags : <EOL> return len ( self . following ( ) ) == <NUM_LIT:0> <EOL> if not all ( [ getattr ( self , f ) == flags [ f ] for f in flags ] ) : <EOL> return False <EOL> following = self . following ( ) <EOL> if following : <EOL> for item in following : <EOL> if all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return False <EOL> return True <EOL> def preceding ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent <EOL> if parent : <EOL> pos = self . pos ( ) <EOL> if pos is not None : <EOL> return parent . components [ : pos ] <EOL> return [ ] <EOL> def following ( self ) : <EOL> """<STR_LIT>""" <EOL> parent = self . parent <EOL> if parent : <EOL> items = parent . components <EOL> pos = self . pos ( ) <EOL> if pos is not None : <EOL> pos = pos + <NUM_LIT:1> <EOL> if pos < len ( items ) : <EOL> return items [ pos : ] <EOL> return [ ] <EOL> def get_prev ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> preceding = self . preceding ( ) <EOL> preceding . reverse ( ) <EOL> for item in preceding : <EOL> if not flags or all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return item <EOL> return None <EOL> def get_next ( self , ** flags ) : <EOL> """<STR_LIT>""" <EOL> following = self . following ( ) <EOL> for item in following : <EOL> if not flags or all ( [ getattr ( item , f ) == flags [ f ] for f in flags ] ) : <EOL> return item <EOL> return None <EOL> def s3_rheader_resource ( r ) : <EOL> """<STR_LIT>""" <EOL> _vars = r . get_vars <EOL> if "<STR_LIT>" in _vars : <EOL> try : <EOL> tablename , record_id = _vars . viewing . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> db = current . db <EOL> record = db [ tablename ] [ record_id ] <EOL> except : <EOL> tablename = r . tablename <EOL> record = r . record <EOL> else : <EOL> tablename = r . tablename <EOL> record = r . record <EOL> return ( tablename , record ) <EOL> def s3_rheader_tabs ( r , tabs = None ) : <EOL> """<STR_LIT>""" <EOL> rheader_tabs = S3ComponentTabs ( tabs ) <EOL> return rheader_tabs . render ( r ) <EOL> class S3ComponentTabs ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tabs = None ) : <EOL> """<STR_LIT>""" <EOL> if not tabs : <EOL> self . tabs = [ ] <EOL> else : <EOL> self . tabs = [ S3ComponentTab ( t ) for t in tabs if t ] <EOL> def render ( self , r ) : <EOL> """<STR_LIT>""" <EOL> rheader_tabs = [ ] <EOL> tabs = tuple ( t for t in self . tabs if t . active ( r ) ) <EOL> mtab = False <EOL> if r . component is None : <EOL> for t in tabs : <EOL> if t . component == r . method : <EOL> mtab = True <EOL> break <EOL> record_id = r . id <EOL> if not record_id and r . record : <EOL> record_id = r . record [ r . table . _id ] <EOL> for i , tab in enumerate ( tabs ) : <EOL> vars_match = tab . vars_match ( r ) <EOL> if vars_match : <EOL> _vars = Storage ( r . get_vars ) <EOL> else : <EOL> _vars = Storage ( tab . vars ) <EOL> if "<STR_LIT>" in r . get_vars : <EOL> _vars . viewing = r . get_vars . viewing <EOL> if tab . function is None : <EOL> if "<STR_LIT>" in _vars : <EOL> tablename , record_id = _vars . viewing . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> function = tablename . split ( "<STR_LIT:_>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> else : <EOL> function = r . function <EOL> else : <EOL> function = tab . function <EOL> component = tab . component <EOL> here = False <EOL> if function == r . name or function == r . function : <EOL> here = r . method == component or not mtab <EOL> if component : <EOL> if r . component and r . component . alias == component and vars_match : <EOL> here = True <EOL> elif not r . component and r . method == component : <EOL> here = True <EOL> else : <EOL> here = False <EOL> else : <EOL> if r . component or not vars_match : <EOL> here = False <EOL> if here : <EOL> _class = "<STR_LIT>" <EOL> elif i == len ( tabs ) - <NUM_LIT:1> : <EOL> _class = "<STR_LIT>" <EOL> else : <EOL> _class = "<STR_LIT>" <EOL> if component : <EOL> if record_id : <EOL> args = [ record_id , component ] <EOL> else : <EOL> args = [ component ] <EOL> if "<STR_LIT>" in _vars : <EOL> del _vars [ "<STR_LIT>" ] <EOL> _href = URL ( function , args = args , vars = _vars ) <EOL> _id = "<STR_LIT>" % component <EOL> else : <EOL> args = [ ] <EOL> if function != r . name and not tab . native : <EOL> if "<STR_LIT>" not in _vars and r . id : <EOL> _vars . update ( viewing = "<STR_LIT>" % ( r . tablename , r . id ) ) <EOL> elif not tab . component and not tab . function : <EOL> if "<STR_LIT>" in _vars : <EOL> del _vars [ "<STR_LIT>" ] <EOL> args = [ record_id ] <EOL> else : <EOL> if "<STR_LIT>" not in _vars and record_id : <EOL> args = [ record_id ] <EOL> _href = URL ( function , args = args , vars = _vars ) <EOL> _id = "<STR_LIT>" % function <EOL> rheader_tabs . append ( SPAN ( A ( tab . title , <EOL> _href = _href , <EOL> _id = _id , <EOL> ) , <EOL> _class = _class , <EOL> ) ) <EOL> if rheader_tabs : <EOL> rheader_tabs = DIV ( rheader_tabs , _class = "<STR_LIT>" ) <EOL> else : <EOL> rheader_tabs = "<STR_LIT>" <EOL> return rheader_tabs <EOL> class S3ComponentTab ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tab ) : <EOL> """<STR_LIT>""" <EOL> title , component = tab [ : <NUM_LIT:2> ] <EOL> if component and component . find ( "<STR_LIT:/>" ) > <NUM_LIT:0> : <EOL> function , component = component . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> else : <EOL> function = None <EOL> self . title = title <EOL> self . native = False <EOL> if function : <EOL> self . function = function <EOL> else : <EOL> self . function = None <EOL> if component : <EOL> self . component = component <EOL> else : <EOL> self . component = None <EOL> if len ( tab ) > <NUM_LIT:2> : <EOL> vars = self . vars = Storage ( tab [ <NUM_LIT:2> ] ) <EOL> if "<STR_LIT>" in vars : <EOL> self . native = True if vars [ "<STR_LIT>" ] else False <EOL> del vars [ "<STR_LIT>" ] <EOL> else : <EOL> self . vars = None <EOL> def active ( self , r ) : <EOL> """<STR_LIT>""" <EOL> s3db = current . s3db <EOL> get_components = s3db . get_components <EOL> get_method = s3db . get_method <EOL> get_vars = r . get_vars <EOL> tablename = None <EOL> if "<STR_LIT>" in get_vars : <EOL> try : <EOL> tablename = get_vars [ "<STR_LIT>" ] . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> except : <EOL> pass <EOL> resource = r . resource <EOL> component = self . component <EOL> if component : <EOL> clist = get_components ( resource . table , names = [ component ] ) <EOL> is_component = False <EOL> if component in clist : <EOL> is_component = True <EOL> elif tablename : <EOL> clist = get_components ( tablename , names = [ component ] ) <EOL> if component in clist : <EOL> is_component = True <EOL> if is_component : <EOL> return self . authorised ( clist [ component ] ) <EOL> handler = get_method ( resource . prefix , <EOL> resource . name , <EOL> method = component ) <EOL> if handler is None and tablename : <EOL> prefix , name = tablename . split ( "<STR_LIT:_>" , <NUM_LIT:1> ) <EOL> handler = get_method ( prefix , name , <EOL> method = component ) <EOL> if handler is None : <EOL> handler = r . get_handler ( component ) <EOL> if handler is None : <EOL> return component in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> return True <EOL> def authorised ( self , hook ) : <EOL> """<STR_LIT>""" <EOL> READ = "<STR_LIT>" <EOL> has_permission = current . auth . s3_has_permission <EOL> if hook . linktable and not has_permission ( READ , hook . linktable ) : <EOL> return False <EOL> if has_permission ( READ , hook . tablename ) : <EOL> return True <EOL> return False <EOL> def vars_match ( self , r ) : <EOL> """<STR_LIT>""" <EOL> get_vars = r . get_vars <EOL> if self . vars is None : <EOL> return True <EOL> for k , v in self . vars . iteritems ( ) : <EOL> if v is None : <EOL> continue <EOL> if k not in get_vars or k in get_vars and get_vars [ k ] != v : <EOL> return False <EOL> return True <EOL> class S3ScriptItem ( S3NavigationItem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> script = None , <EOL> ** attributes ) : <EOL> """<STR_LIT>""" <EOL> self . script = script <EOL> return super ( S3ScriptItem , self ) . __init__ ( attributes ) <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . script : <EOL> current . response . s3 . jquery_ready . append ( self . script ) <EOL> return "<STR_LIT>" <EOL> @ staticmethod <EOL> def inline ( item ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> class S3ResourceHeader : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fields = None , tabs = None ) : <EOL> """<STR_LIT>""" <EOL> self . fields = fields <EOL> self . tabs = tabs <EOL> def __call__ ( self , r , tabs = None , table = None , record = None , as_div = True ) : <EOL> """<STR_LIT>""" <EOL> if table is None : <EOL> table = r . table <EOL> if record is None : <EOL> record = r . record <EOL> if tabs is None : <EOL> tabs = self . tabs <EOL> if self . fields is None and "<STR_LIT:name>" in table . fields : <EOL> fields = [ [ "<STR_LIT:name>" ] ] <EOL> else : <EOL> fields = self . fields <EOL> if record : <EOL> if tabs is not None : <EOL> rheader_tabs = s3_rheader_tabs ( r , tabs ) <EOL> else : <EOL> rheader_tabs = "<STR_LIT>" <EOL> trs = [ ] <EOL> for row in fields : <EOL> tr = TR ( ) <EOL> for col in row : <EOL> field = None <EOL> label = "<STR_LIT>" <EOL> value = "<STR_LIT>" <EOL> if isinstance ( col , ( tuple , list ) ) and len ( col ) == <NUM_LIT:2> : <EOL> label , f = col <EOL> else : <EOL> f = col <EOL> if callable ( f ) : <EOL> try : <EOL> value = f ( record ) <EOL> except : <EOL> pass <EOL> else : <EOL> if isinstance ( f , str ) : <EOL> fn = f <EOL> if "<STR_LIT:.>" in fn : <EOL> fn = f . split ( "<STR_LIT:.>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> if fn not in record or fn not in table : <EOL> continue <EOL> field = table [ fn ] <EOL> value = record [ fn ] <EOL> if callable ( value ) : <EOL> value = value ( ) <EOL> elif isinstance ( f , Field ) and f . name in record : <EOL> field = f <EOL> value = record [ f . name ] <EOL> if field is not None : <EOL> if not label : <EOL> label = field . label <EOL> if hasattr ( field , "<STR_LIT>" ) and field . represent is not None : <EOL> value = field . represent ( value ) <EOL> if label is not None : <EOL> tr . append ( TH ( "<STR_LIT>" % label ) ) <EOL> v = value <EOL> if not isinstance ( v , basestring ) and not isinstance ( value , DIV ) : <EOL> try : <EOL> v = unicode ( v ) <EOL> except : <EOL> pass <EOL> tr . append ( TD ( v ) ) <EOL> trs . append ( tr ) <EOL> if as_div : <EOL> rheader = DIV ( TABLE ( trs ) , rheader_tabs ) <EOL> else : <EOL> rheader = ( TABLE ( trs ) , rheader_tabs ) <EOL> return rheader <EOL> return None </s>
