<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ( <EOL> ugettext as _ , get_language_from_request , override ) <EOL> from cms . toolbar_base import CMSToolbar <EOL> from cms . toolbar_pool import toolbar_pool <EOL> from aldryn_apphooks_config . utils import get_app_instance <EOL> from aldryn_translation_tools . utils import ( <EOL> get_object_from_request , <EOL> get_admin_url , <EOL> ) <EOL> from . models import Article <EOL> from . cms_appconfig import NewsBlogConfig <EOL> @ toolbar_pool . register <EOL> class NewsBlogToolbar ( CMSToolbar ) : <EOL> watch_models = [ Article , ] <EOL> supported_apps = ( '<STR_LIT>' , ) <EOL> def get_on_delete_redirect_url ( self , article , language ) : <EOL> with override ( language ) : <EOL> url = reverse ( <EOL> '<STR_LIT>' . format ( article . app_config . namespace ) ) <EOL> return url <EOL> def __get_newsblog_config ( self ) : <EOL> try : <EOL> __ , config = get_app_instance ( self . request ) <EOL> if not isinstance ( config , NewsBlogConfig ) : <EOL> return None <EOL> except ImproperlyConfigured : <EOL> return None <EOL> return config <EOL> def populate ( self ) : <EOL> config = self . __get_newsblog_config ( ) <EOL> if not config : <EOL> return <EOL> user = getattr ( self . request , '<STR_LIT:user>' , None ) <EOL> try : <EOL> view_name = self . request . resolver_match . view_name <EOL> except AttributeError : <EOL> view_name = None <EOL> if user and view_name : <EOL> language = get_language_from_request ( self . request , check_path = True ) <EOL> if view_name == '<STR_LIT>' . format ( config . namespace ) : <EOL> article = get_object_from_request ( Article , self . request ) <EOL> else : <EOL> article = None <EOL> menu = self . toolbar . get_or_create_menu ( '<STR_LIT>' , <EOL> config . get_app_title ( ) ) <EOL> change_config_perm = user . has_perm ( <EOL> '<STR_LIT>' ) <EOL> add_config_perm = user . has_perm ( <EOL> '<STR_LIT>' ) <EOL> config_perms = [ change_config_perm , add_config_perm ] <EOL> change_article_perm = user . has_perm ( <EOL> '<STR_LIT>' ) <EOL> delete_article_perm = user . has_perm ( <EOL> '<STR_LIT>' ) <EOL> add_article_perm = user . has_perm ( '<STR_LIT>' ) <EOL> article_perms = [ change_article_perm , add_article_perm , <EOL> delete_article_perm , ] <EOL> if change_config_perm : <EOL> url_args = { } <EOL> if language : <EOL> url_args = { '<STR_LIT>' : language , } <EOL> url = get_admin_url ( '<STR_LIT>' , <EOL> [ config . pk , ] , ** url_args ) <EOL> menu . add_modal_item ( _ ( '<STR_LIT>' ) , url = url ) <EOL> if any ( config_perms ) and any ( article_perms ) : <EOL> menu . add_break ( ) <EOL> if change_article_perm : <EOL> url_args = { } <EOL> if config : <EOL> url_args = { '<STR_LIT>' : config . pk } <EOL> url = get_admin_url ( '<STR_LIT>' , <EOL> ** url_args ) <EOL> menu . add_sideframe_item ( _ ( '<STR_LIT>' ) , url = url ) <EOL> if add_article_perm : <EOL> url_args = { '<STR_LIT>' : config . pk , '<STR_LIT>' : user . pk , } <EOL> if language : <EOL> url_args . update ( { '<STR_LIT>' : language , } ) <EOL> url = get_admin_url ( '<STR_LIT>' , ** url_args ) <EOL> menu . add_modal_item ( _ ( '<STR_LIT>' ) , url = url ) <EOL> if change_article_perm and article : <EOL> url_args = { } <EOL> if language : <EOL> url_args = { '<STR_LIT>' : language , } <EOL> url = get_admin_url ( '<STR_LIT>' , <EOL> [ article . pk , ] , ** url_args ) <EOL> menu . add_modal_item ( _ ( '<STR_LIT>' ) , url = url , <EOL> active = True ) <EOL> if delete_article_perm and article : <EOL> redirect_url = self . get_on_delete_redirect_url ( <EOL> article , language = language ) <EOL> url = get_admin_url ( '<STR_LIT>' , <EOL> [ article . pk , ] ) <EOL> menu . add_modal_item ( _ ( '<STR_LIT>' ) , url = url , <EOL> on_close = redirect_url ) </s>
