<s> from __future__ import absolute_import , unicode_literals <EOL> from django . apps import apps <EOL> from django . contrib . auth . models import Permission <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . text import capfirst <EOL> from wagtail . wagtailadmin . menu import MenuItem <EOL> from wagtail . wagtailcore import hooks <EOL> from . permissions import user_can_edit_setting_type <EOL> class SettingMenuItem ( MenuItem ) : <EOL> def __init__ ( self , model , icon = '<STR_LIT>' , classnames = '<STR_LIT>' , ** kwargs ) : <EOL> icon_classes = '<STR_LIT>' + icon <EOL> if classnames : <EOL> classnames += '<STR_LIT:U+0020>' + icon_classes <EOL> else : <EOL> classnames = icon_classes <EOL> self . model = model <EOL> super ( SettingMenuItem , self ) . __init__ ( <EOL> label = capfirst ( model . _meta . verbose_name ) , <EOL> url = reverse ( '<STR_LIT>' , args = [ <EOL> model . _meta . app_label , model . _meta . model_name ] ) , <EOL> classnames = classnames , <EOL> ** kwargs ) <EOL> def is_shown ( self , request ) : <EOL> return user_can_edit_setting_type ( request . user , self . model ) <EOL> class Registry ( list ) : <EOL> def register ( self , model , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if model in self : <EOL> return model <EOL> self . append ( model ) <EOL> @ hooks . register ( '<STR_LIT>' ) <EOL> def menu_hook ( ) : <EOL> return SettingMenuItem ( model , ** kwargs ) <EOL> @ hooks . register ( '<STR_LIT>' ) <EOL> def permissions_hook ( ) : <EOL> return Permission . objects . filter ( <EOL> content_type__app_label = model . _meta . app_label , <EOL> codename = '<STR_LIT>' . format ( model . _meta . model_name ) ) <EOL> return model <EOL> def register_decorator ( self , model = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if model is None : <EOL> return lambda model : self . register ( model , ** kwargs ) <EOL> return self . register ( model , ** kwargs ) <EOL> def get_by_natural_key ( self , app_label , model_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> Model = apps . get_model ( app_label , model_name ) <EOL> except LookupError : <EOL> return None <EOL> if Model not in registry : <EOL> return None <EOL> return Model <EOL> registry = Registry ( ) <EOL> register_setting = registry . register_decorator </s>
