<s> from __future__ import unicode_literals <EOL> from django . contrib import admin <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . admin import UserAdmin <EOL> from django . forms import ValidationError , ModelForm <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . auth . models import User as AuthUser <EOL> from mezzanine . conf import settings <EOL> from mezzanine . core . forms import DynamicInlineAdminForm <EOL> from mezzanine . core . models import ( Orderable , SitePermission , <EOL> CONTENT_STATUS_PUBLISHED ) <EOL> from mezzanine . utils . static import static_lazy as static <EOL> if settings . USE_MODELTRANSLATION : <EOL> from collections import OrderedDict <EOL> from django . utils . translation import activate , get_language <EOL> from modeltranslation . admin import ( TranslationAdmin , <EOL> TranslationInlineModelAdmin ) <EOL> class BaseTranslationModelAdmin ( TranslationAdmin ) : <EOL> """<STR_LIT>""" <EOL> class Media : <EOL> js = ( <EOL> static ( "<STR_LIT>" ) , <EOL> static ( "<STR_LIT>" % settings . JQUERY_UI_FILENAME ) , <EOL> static ( "<STR_LIT>" ) , <EOL> ) <EOL> css = { <EOL> "<STR_LIT:all>" : ( static ( <EOL> "<STR_LIT>" ) , ) , <EOL> } <EOL> else : <EOL> class BaseTranslationModelAdmin ( admin . ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> class Media : <EOL> js = ( ) <EOL> css = { "<STR_LIT:all>" : ( ) } <EOL> User = get_user_model ( ) <EOL> class DisplayableAdminForm ( ModelForm ) : <EOL> def clean_content ( form ) : <EOL> status = form . cleaned_data . get ( "<STR_LIT:status>" ) <EOL> content = form . cleaned_data . get ( "<STR_LIT:content>" ) <EOL> if status == CONTENT_STATUS_PUBLISHED and not content : <EOL> raise ValidationError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return content <EOL> class DisplayableAdmin ( BaseTranslationModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> list_display = ( "<STR_LIT:title>" , "<STR_LIT:status>" , "<STR_LIT>" ) <EOL> list_display_links = ( "<STR_LIT:title>" , ) <EOL> list_editable = ( "<STR_LIT:status>" , ) <EOL> list_filter = ( "<STR_LIT:status>" , "<STR_LIT>" ) <EOL> date_hierarchy = None if settings . USE_MODELTRANSLATION else "<STR_LIT>" <EOL> radio_fields = { "<STR_LIT:status>" : admin . HORIZONTAL } <EOL> fieldsets = ( <EOL> ( None , { <EOL> "<STR_LIT>" : [ "<STR_LIT:title>" , "<STR_LIT:status>" , ( "<STR_LIT>" , "<STR_LIT>" ) ] , <EOL> } ) , <EOL> ( _ ( "<STR_LIT>" ) , { <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , <EOL> ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , ) <EOL> } ) , <EOL> ) <EOL> form = DisplayableAdminForm <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DisplayableAdmin , self ) . __init__ ( * args , ** kwargs ) <EOL> try : <EOL> self . search_fields = list ( set ( list ( self . search_fields ) + list ( <EOL> self . model . objects . get_search_fields ( ) . keys ( ) ) ) ) <EOL> except AttributeError : <EOL> pass <EOL> def save_model ( self , request , obj , form , change ) : <EOL> """<STR_LIT>""" <EOL> super ( DisplayableAdmin , self ) . save_model ( request , obj , form , change ) <EOL> if settings . USE_MODELTRANSLATION : <EOL> lang = get_language ( ) <EOL> for code in OrderedDict ( settings . LANGUAGES ) : <EOL> if code != lang : <EOL> try : <EOL> activate ( code ) <EOL> except : <EOL> pass <EOL> else : <EOL> obj . save ( ) <EOL> activate ( lang ) <EOL> class BaseDynamicInlineAdmin ( object ) : <EOL> """<STR_LIT>""" <EOL> form = DynamicInlineAdminForm <EOL> extra = <NUM_LIT:1> <EOL> def get_fields ( self , request , obj = None ) : <EOL> fields = super ( BaseDynamicInlineAdmin , self ) . get_fields ( request , obj ) <EOL> if issubclass ( self . model , Orderable ) : <EOL> fields = list ( fields ) <EOL> try : <EOL> fields . remove ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> fields . append ( "<STR_LIT>" ) <EOL> return fields <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> fieldsets = super ( BaseDynamicInlineAdmin , self ) . get_fieldsets ( <EOL> request , obj ) <EOL> if issubclass ( self . model , Orderable ) : <EOL> for fieldset in fieldsets : <EOL> fields = [ f for f in list ( fieldset [ <NUM_LIT:1> ] [ "<STR_LIT>" ] ) <EOL> if not hasattr ( f , "<STR_LIT>" ) ] <EOL> try : <EOL> fields . remove ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> pass <EOL> fieldset [ <NUM_LIT:1> ] [ "<STR_LIT>" ] = fields <EOL> fieldsets [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ "<STR_LIT>" ] . append ( "<STR_LIT>" ) <EOL> return fieldsets <EOL> def get_inline_base_class ( cls ) : <EOL> if settings . USE_MODELTRANSLATION : <EOL> class InlineBase ( TranslationInlineModelAdmin , cls ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> return InlineBase <EOL> return cls <EOL> class TabularDynamicInlineAdmin ( BaseDynamicInlineAdmin , <EOL> get_inline_base_class ( admin . TabularInline ) ) : <EOL> pass <EOL> class StackedDynamicInlineAdmin ( BaseDynamicInlineAdmin , <EOL> get_inline_base_class ( admin . StackedInline ) ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> grappelli_name = getattr ( settings , "<STR_LIT>" ) <EOL> if grappelli_name not in settings . INSTALLED_APPS : <EOL> error = "<STR_LIT>" <EOL> raise Exception ( error ) <EOL> super ( StackedDynamicInlineAdmin , self ) . __init__ ( * args , ** kwargs ) <EOL> class OwnableAdmin ( admin . ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> def save_form ( self , request , form , change ) : <EOL> """<STR_LIT>""" <EOL> obj = form . save ( commit = False ) <EOL> if obj . user_id is None : <EOL> obj . user = request . user <EOL> return super ( OwnableAdmin , self ) . save_form ( request , form , change ) <EOL> def get_queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> opts = self . model . _meta <EOL> model_name = ( "<STR_LIT>" % ( opts . app_label , opts . object_name ) ) . lower ( ) <EOL> models_all_editable = settings . OWNABLE_MODELS_ALL_EDITABLE <EOL> models_all_editable = [ m . lower ( ) for m in models_all_editable ] <EOL> qs = super ( OwnableAdmin , self ) . get_queryset ( request ) <EOL> if request . user . is_superuser or model_name in models_all_editable : <EOL> return qs <EOL> return qs . filter ( user__id = request . user . id ) <EOL> class SitePermissionInline ( admin . TabularInline ) : <EOL> model = SitePermission <EOL> max_num = <NUM_LIT:1> <EOL> can_delete = False <EOL> class SitePermissionUserAdmin ( UserAdmin ) : <EOL> inlines = [ SitePermissionInline ] <EOL> if User == AuthUser : <EOL> if User in admin . site . _registry : <EOL> admin . site . unregister ( User ) <EOL> admin . site . register ( User , SitePermissionUserAdmin ) </s>
