<s> import sys <EOL> from django . template . loader import render_to_string <EOL> from django . template import TemplateSyntaxError <EOL> from django . utils . datastructures import SortedDict <EOL> from horizon import exceptions <EOL> from horizon . utils import html <EOL> SEPARATOR = "<STR_LIT>" <EOL> CSS_TAB_GROUP_CLASSES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> CSS_ACTIVE_TAB_CLASSES = [ "<STR_LIT>" ] <EOL> CSS_DISABLED_TAB_CLASSES = [ "<STR_LIT>" ] <EOL> class TabGroup ( html . HTMLElement ) : <EOL> """<STR_LIT>""" <EOL> slug = None <EOL> template_name = "<STR_LIT>" <EOL> param_name = '<STR_LIT>' <EOL> sticky = False <EOL> _selected = None <EOL> _active = None <EOL> @ property <EOL> def selected ( self ) : <EOL> return self . _selected <EOL> @ property <EOL> def active ( self ) : <EOL> return self . _active <EOL> def __init__ ( self , request , ** kwargs ) : <EOL> super ( TabGroup , self ) . __init__ ( ) <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> % self . __class__ ) <EOL> self . request = request <EOL> self . kwargs = kwargs <EOL> self . _data = None <EOL> tab_instances = [ ] <EOL> for tab in self . tabs : <EOL> tab_instances . append ( ( tab . slug , tab ( self , request ) ) ) <EOL> self . _tabs = SortedDict ( tab_instances ) <EOL> if self . sticky : <EOL> self . attrs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not self . _set_active_tab ( ) : <EOL> self . tabs_not_available ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . slug ) <EOL> def load_tab_data ( self ) : <EOL> """<STR_LIT>""" <EOL> for tab in self . _tabs . values ( ) : <EOL> if tab . load and not tab . data_loaded : <EOL> try : <EOL> tab . _data = tab . get_context_data ( self . request ) <EOL> except Exception : <EOL> tab . _data = False <EOL> exceptions . handle ( self . request ) <EOL> def get_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . slug <EOL> def get_default_classes ( self ) : <EOL> """<STR_LIT>""" <EOL> default_classes = super ( TabGroup , self ) . get_default_classes ( ) <EOL> default_classes . extend ( CSS_TAB_GROUP_CLASSES ) <EOL> return default_classes <EOL> def tabs_not_available ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _set_active_tab ( self ) : <EOL> marked_active = None <EOL> tab = self . get_selected_tab ( ) <EOL> if tab : <EOL> tab . _active = True <EOL> self . _active = tab <EOL> marked_active = tab <EOL> for tab in self . _tabs . values ( ) : <EOL> if tab . _allowed and tab . _enabled and not marked_active : <EOL> tab . _active = True <EOL> self . _active = tab <EOL> marked_active = True <EOL> elif tab == marked_active : <EOL> continue <EOL> else : <EOL> tab . _active = False <EOL> return marked_active <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> return render_to_string ( self . template_name , { "<STR_LIT>" : self } ) <EOL> def get_tabs ( self ) : <EOL> """<STR_LIT>""" <EOL> return filter ( lambda tab : tab . _allowed , self . _tabs . values ( ) ) <EOL> def get_tab ( self , tab_name , allow_disabled = False ) : <EOL> """<STR_LIT>""" <EOL> tab = self . _tabs . get ( tab_name , None ) <EOL> if tab and tab . _allowed and ( tab . _enabled or allow_disabled ) : <EOL> return tab <EOL> return None <EOL> def get_loaded_tabs ( self ) : <EOL> return filter ( lambda t : self . get_tab ( t . slug ) , self . _tabs . values ( ) ) <EOL> def get_selected_tab ( self ) : <EOL> """<STR_LIT>""" <EOL> selected = self . request . GET . get ( self . param_name , None ) <EOL> if selected : <EOL> tab_group , tab_name = selected . split ( SEPARATOR ) <EOL> if tab_group == self . get_id ( ) : <EOL> self . _selected = self . get_tab ( tab_name ) <EOL> return self . _selected <EOL> class Tab ( html . HTMLElement ) : <EOL> """<STR_LIT>""" <EOL> name = None <EOL> slug = None <EOL> preload = True <EOL> _active = None <EOL> def __init__ ( self , tab_group , request = None ) : <EOL> super ( Tab , self ) . __init__ ( ) <EOL> if not self . name : <EOL> raise ValueError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> self . name = unicode ( self . name ) <EOL> if not self . slug : <EOL> raise ValueError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> self . tab_group = tab_group <EOL> self . request = request <EOL> if request : <EOL> self . _allowed = self . allowed ( request ) <EOL> self . _enabled = self . enabled ( request ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . slug ) <EOL> def is_active ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _active is None : <EOL> self . tab_group . _set_active_tab ( ) <EOL> return self . _active <EOL> @ property <EOL> def load ( self ) : <EOL> load_preloaded = self . preload or self . is_active ( ) <EOL> return load_preloaded and self . _allowed and self . _enabled <EOL> @ property <EOL> def data ( self ) : <EOL> if getattr ( self , "<STR_LIT>" , None ) is None : <EOL> self . _data = self . get_context_data ( self . request ) <EOL> return self . _data <EOL> @ property <EOL> def data_loaded ( self ) : <EOL> return getattr ( self , "<STR_LIT>" , None ) is not None <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . load : <EOL> return '<STR_LIT>' <EOL> try : <EOL> context = self . data <EOL> except exceptions . Http302 : <EOL> raise <EOL> except Exception : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> raise TemplateSyntaxError , exc_value , exc_traceback <EOL> return render_to_string ( self . get_template_name ( self . request ) , context ) <EOL> def get_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return SEPARATOR . join ( [ self . tab_group . slug , self . slug ] ) <EOL> def get_query_string ( self ) : <EOL> return "<STR_LIT:=>" . join ( ( self . tab_group . param_name , self . get_id ( ) ) ) <EOL> def get_default_classes ( self ) : <EOL> """<STR_LIT>""" <EOL> default_classes = super ( Tab , self ) . get_default_classes ( ) <EOL> if self . is_active ( ) : <EOL> default_classes . extend ( CSS_ACTIVE_TAB_CLASSES ) <EOL> if not self . _enabled : <EOL> default_classes . extend ( CSS_DISABLED_TAB_CLASSES ) <EOL> return default_classes <EOL> def get_template_name ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> return self . template_name <EOL> def get_context_data ( self , request ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ . __name__ ) <EOL> def enabled ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def allowed ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class TableTab ( Tab ) : <EOL> """<STR_LIT>""" <EOL> table_classes = None <EOL> def __init__ ( self , tab_group , request ) : <EOL> super ( TableTab , self ) . __init__ ( tab_group , request ) <EOL> if not self . table_classes : <EOL> class_name = self . __class__ . __name__ <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % class_name ) <EOL> table_instances = [ ( table . _meta . name , <EOL> table ( request , ** tab_group . kwargs ) ) <EOL> for table in self . table_classes ] <EOL> self . _tables = SortedDict ( table_instances ) <EOL> self . _table_data_loaded = False <EOL> def load_table_data ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _table_data_loaded : <EOL> for table_name , table in self . _tables . items ( ) : <EOL> func_name = "<STR_LIT>" % table_name <EOL> data_func = getattr ( self , func_name , None ) <EOL> if data_func is None : <EOL> cls_name = self . __class__ . __name__ <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( func_name , cls_name ) ) <EOL> table . data = data_func ( ) <EOL> table . _meta . has_more_data = self . has_more_data ( table ) <EOL> self . _table_data_loaded = True <EOL> def get_context_data ( self , request ) : <EOL> """<STR_LIT>""" <EOL> context = { } <EOL> self . load_table_data ( ) <EOL> for table_name , table in self . _tables . items ( ) : <EOL> if len ( self . table_classes ) == <NUM_LIT:1> : <EOL> context [ "<STR_LIT>" ] = table <EOL> context [ "<STR_LIT>" % table_name ] = table <EOL> return context <EOL> def has_more_data ( self , table ) : <EOL> return False </s>
