<s> import json <EOL> from abc import ABCMeta <EOL> from collections import defaultdict <EOL> from django . template . loader import render_to_string <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import Promise <EOL> from cms . constants import RIGHT , LEFT , REFRESH_PAGE , URL_CHANGE <EOL> class ItemSearchResult ( object ) : <EOL> def __init__ ( self , item , index ) : <EOL> self . item = item <EOL> self . index = index <EOL> def __add__ ( self , other ) : <EOL> return ItemSearchResult ( self . item , self . index + other ) <EOL> def __sub__ ( self , other ) : <EOL> return ItemSearchResult ( self . item , self . index - other ) <EOL> def __int__ ( self ) : <EOL> return self . index <EOL> def may_be_lazy ( thing ) : <EOL> if isinstance ( thing , Promise ) : <EOL> return thing . _proxy____args [ <NUM_LIT:0> ] <EOL> else : <EOL> return thing <EOL> class ToolbarAPIMixin ( six . with_metaclass ( ABCMeta ) ) : <EOL> REFRESH_PAGE = REFRESH_PAGE <EOL> URL_CHANGE = URL_CHANGE <EOL> LEFT = LEFT <EOL> RIGHT = RIGHT <EOL> def __init__ ( self ) : <EOL> self . items = [ ] <EOL> self . menus = { } <EOL> self . _memo = defaultdict ( list ) <EOL> def _memoize ( self , item ) : <EOL> self . _memo [ item . __class__ ] . append ( item ) <EOL> def _unmemoize ( self , item ) : <EOL> self . _memo [ item . __class__ ] . remove ( item ) <EOL> def _item_position ( self , item ) : <EOL> return self . items . index ( item ) <EOL> def _add_item ( self , item , position ) : <EOL> if position is not None : <EOL> self . items . insert ( position , item ) <EOL> else : <EOL> self . items . append ( item ) <EOL> def _remove_item ( self , item ) : <EOL> if item in self . items : <EOL> self . items . remove ( item ) <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" % item ) <EOL> def get_item_count ( self ) : <EOL> return len ( self . items ) <EOL> def add_item ( self , item , position = None ) : <EOL> if not isinstance ( item , BaseItem ) : <EOL> raise ValueError ( "<STR_LIT>" % item ) <EOL> if isinstance ( position , ItemSearchResult ) : <EOL> position = position . index <EOL> elif isinstance ( position , BaseItem ) : <EOL> position = self . _item_position ( position ) <EOL> elif not ( position is None or isinstance ( position , ( int , ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" % position ) <EOL> self . _add_item ( item , position ) <EOL> self . _memoize ( item ) <EOL> return item <EOL> def find_items ( self , item_type , ** attributes ) : <EOL> results = [ ] <EOL> attr_items = attributes . items ( ) <EOL> notfound = object ( ) <EOL> for candidate in self . _memo [ item_type ] : <EOL> if all ( may_be_lazy ( getattr ( candidate , key , notfound ) ) == value for key , value in attr_items ) : <EOL> results . append ( ItemSearchResult ( candidate , self . _item_position ( candidate ) ) ) <EOL> return results <EOL> def find_first ( self , item_type , ** attributes ) : <EOL> try : <EOL> return self . find_items ( item_type , ** attributes ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def get_alphabetical_insert_position ( self , new_menu_name , item_type , <EOL> default = <NUM_LIT:0> ) : <EOL> results = self . find_items ( item_type ) <EOL> if not len ( results ) : <EOL> return default <EOL> last_position = <NUM_LIT:0> <EOL> for result in sorted ( results , key = lambda x : x . item . name ) : <EOL> if result . item . name > new_menu_name : <EOL> return result . index <EOL> if result . index > last_position : <EOL> last_position = result . index <EOL> else : <EOL> return last_position + <NUM_LIT:1> <EOL> def remove_item ( self , item ) : <EOL> self . _remove_item ( item ) <EOL> self . _unmemoize ( item ) <EOL> def add_sideframe_item ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None , on_close = None , side = LEFT , position = None ) : <EOL> item = SideframeItem ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> on_close = on_close , <EOL> side = side , <EOL> ) <EOL> self . add_item ( item , position = position ) <EOL> return item <EOL> def add_modal_item ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None , on_close = REFRESH_PAGE , side = LEFT , position = None ) : <EOL> item = ModalItem ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> on_close = on_close , <EOL> side = side , <EOL> ) <EOL> self . add_item ( item , position = position ) <EOL> return item <EOL> def add_link_item ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None , side = LEFT , position = None ) : <EOL> item = LinkItem ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> side = side <EOL> ) <EOL> self . add_item ( item , position = position ) <EOL> return item <EOL> def add_ajax_item ( self , name , action , active = False , disabled = False , <EOL> extra_classes = None , data = None , question = None , <EOL> side = LEFT , position = None , on_success = None ) : <EOL> item = AjaxItem ( name , action , self . csrf_token , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> data = data , <EOL> question = question , <EOL> side = side , <EOL> on_success = on_success , <EOL> ) <EOL> self . add_item ( item , position = position ) <EOL> return item <EOL> class BaseItem ( six . with_metaclass ( ABCMeta ) ) : <EOL> template = None <EOL> def __init__ ( self , side = LEFT ) : <EOL> self . side = side <EOL> @ property <EOL> def right ( self ) : <EOL> return self . side is RIGHT <EOL> def render ( self ) : <EOL> return render_to_string ( self . template , self . get_context ( ) ) <EOL> def get_context ( self ) : <EOL> return { } <EOL> class TemplateItem ( BaseItem ) : <EOL> def __init__ ( self , template , extra_context = None , side = LEFT ) : <EOL> super ( TemplateItem , self ) . __init__ ( side ) <EOL> self . template = template <EOL> self . extra_context = extra_context <EOL> def get_context ( self ) : <EOL> if self . extra_context : <EOL> return self . extra_context <EOL> return { } <EOL> class SubMenu ( ToolbarAPIMixin , BaseItem ) : <EOL> template = "<STR_LIT>" <EOL> sub_level = True <EOL> active = False <EOL> def __init__ ( self , name , csrf_token , side = LEFT ) : <EOL> ToolbarAPIMixin . __init__ ( self ) <EOL> BaseItem . __init__ ( self , side ) <EOL> self . name = name <EOL> self . csrf_token = csrf_token <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def add_break ( self , identifier = None , position = None ) : <EOL> item = Break ( identifier ) <EOL> self . add_item ( item , position = position ) <EOL> return item <EOL> def get_items ( self ) : <EOL> return self . items <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . get_items ( ) , <EOL> '<STR_LIT:title>' : self . name , <EOL> '<STR_LIT>' : self . sub_level <EOL> } <EOL> class Menu ( SubMenu ) : <EOL> sub_level = False <EOL> def get_or_create_menu ( self , key , verbose_name , side = LEFT , position = None ) : <EOL> if key in self . menus : <EOL> return self . menus [ key ] <EOL> menu = SubMenu ( verbose_name , self . csrf_token , side = side ) <EOL> self . menus [ key ] = menu <EOL> self . add_item ( menu , position = position ) <EOL> return menu <EOL> class LinkItem ( BaseItem ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , name , url , active = False , disabled = False , extra_classes = None , side = LEFT ) : <EOL> super ( LinkItem , self ) . __init__ ( side ) <EOL> self . name = name <EOL> self . url = url <EOL> self . active = active <EOL> self . disabled = disabled <EOL> self . extra_classes = extra_classes or [ ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT:url>' : self . url , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . disabled , <EOL> '<STR_LIT>' : self . extra_classes , <EOL> } <EOL> class FrameItem ( BaseItem ) : <EOL> def __init__ ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None , on_close = None , side = LEFT ) : <EOL> super ( FrameItem , self ) . __init__ ( side ) <EOL> self . name = "<STR_LIT>" % force_text ( name ) <EOL> self . url = url <EOL> self . active = active <EOL> self . disabled = disabled <EOL> self . extra_classes = extra_classes or [ ] <EOL> self . on_close = on_close <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT:url>' : self . url , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . disabled , <EOL> '<STR_LIT>' : self . extra_classes , <EOL> '<STR_LIT>' : self . on_close , <EOL> } <EOL> class SideframeItem ( FrameItem ) : <EOL> template = "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> class ModalItem ( FrameItem ) : <EOL> template = "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> class AjaxItem ( BaseItem ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , name , action , csrf_token , data = None , active = False , <EOL> disabled = False , extra_classes = None , <EOL> question = None , side = LEFT , on_success = None ) : <EOL> super ( AjaxItem , self ) . __init__ ( side ) <EOL> self . name = name <EOL> self . action = action <EOL> self . active = active <EOL> self . disabled = disabled <EOL> self . csrf_token = csrf_token <EOL> self . data = data or { } <EOL> self . extra_classes = extra_classes or [ ] <EOL> self . question = question <EOL> self . on_success = on_success <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def get_context ( self ) : <EOL> data = { } <EOL> data . update ( self . data ) <EOL> data [ '<STR_LIT>' ] = self . csrf_token <EOL> data = json . dumps ( data ) <EOL> return { <EOL> '<STR_LIT:action>' : self . action , <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . disabled , <EOL> '<STR_LIT>' : self . extra_classes , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : self . question , <EOL> '<STR_LIT>' : self . on_success <EOL> } <EOL> class Break ( BaseItem ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , identifier = None ) : <EOL> self . identifier = identifier <EOL> class BaseButton ( six . with_metaclass ( ABCMeta ) ) : <EOL> template = None <EOL> def render ( self ) : <EOL> return render_to_string ( self . template , self . get_context ( ) ) <EOL> def get_context ( self ) : <EOL> return { } <EOL> class Button ( BaseButton ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None ) : <EOL> self . name = name <EOL> self . url = url <EOL> self . active = active <EOL> self . disabled = disabled <EOL> self . extra_classes = extra_classes or [ ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:url>' : self . url , <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . disabled , <EOL> '<STR_LIT>' : self . extra_classes , <EOL> } <EOL> class ModalButton ( Button ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , name , url , active = False , disabled = False , extra_classes = None , on_close = None ) : <EOL> self . name = name <EOL> self . url = url <EOL> self . active = active <EOL> self . disabled = disabled <EOL> self . extra_classes = extra_classes or [ ] <EOL> self . on_close = on_close <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:url>' : self . url , <EOL> '<STR_LIT>' : self . active , <EOL> '<STR_LIT>' : self . disabled , <EOL> '<STR_LIT>' : self . extra_classes , <EOL> '<STR_LIT>' : self . on_close , <EOL> } <EOL> class SideframeButton ( ModalButton ) : <EOL> template = "<STR_LIT>" <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % force_text ( self . name ) <EOL> class ButtonList ( BaseItem ) : <EOL> template = "<STR_LIT>" <EOL> def __init__ ( self , identifier = None , extra_classes = None , side = LEFT ) : <EOL> super ( ButtonList , self ) . __init__ ( side ) <EOL> self . extra_classes = extra_classes or [ ] <EOL> self . buttons = [ ] <EOL> self . identifier = identifier <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . identifier <EOL> def add_item ( self , item ) : <EOL> if not isinstance ( item , Button ) : <EOL> raise ValueError ( "<STR_LIT>" % item ) <EOL> self . buttons . append ( item ) <EOL> def add_button ( self , name , url , active = False , disabled = False , <EOL> extra_classes = None ) : <EOL> item = Button ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes <EOL> ) <EOL> self . buttons . append ( item ) <EOL> return item <EOL> def add_modal_button ( self , name , url , active = False , disabled = False , extra_classes = None , on_close = REFRESH_PAGE ) : <EOL> item = ModalButton ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> on_close = on_close , <EOL> ) <EOL> self . buttons . append ( item ) <EOL> return item <EOL> def add_sideframe_button ( self , name , url , active = False , disabled = False , extra_classes = None , on_close = None ) : <EOL> item = SideframeButton ( name , url , <EOL> active = active , <EOL> disabled = disabled , <EOL> extra_classes = extra_classes , <EOL> on_close = on_close , <EOL> ) <EOL> self . buttons . append ( item ) <EOL> return item <EOL> def get_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . buttons , <EOL> '<STR_LIT>' : self . extra_classes <EOL> } </s>
