from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from mezzanine.conf import settings
from models import BlockCategory, Block, RichBlock, ImageBlock


class BlockCategoryAdmin(admin.ModelAdmin):
    ordering = ('title', )
    list_display = ('title', )
    search_fields = ('title', )
    fields = ("title", )


    def in_menu(self):
        """
        Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``.
        """
        for (name, items) in settings.ADMIN_MENU_ORDER:
            if "mezzanine_blocks.BlockCategory" in items:
                return True
        return False


class BlockAdmin(admin.ModelAdmin):
    ordering = ('title', 'category')
    list_display = ('title', 'category', 'login_required', 'show_title')
    list_editable = ('login_required', 'show_title', 'category')
    search_fields = ('title', 'content')

    fieldsets = (
        (None, {
            "fields": ["title", "content", "category"],
        }),
        (_("Advanced data"), {
            "fields": ['login_required', 'show_title', "slug" ],
            "classes": ("collapse-closed",)
        }),
    )

    def in_menu(self):
        """
        Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``.
        """
        for (name, items) in settings.ADMIN_MENU_ORDER:
            if "mezzanine_blocks.Block" in items:
                return True
        return False


class RichBlockAdmin(admin.ModelAdmin):
    ordering = ('title', 'category')
    list_display = ('title', 'category', 'login_required', 'show_title')
    list_editable = ('login_required', 'show_title', 'category')
    search_fields = ('title', 'content')

    fieldsets = (
        (None, {
            "fields": ["title", "content", "category"],
        }),
        (_("Advanced data"), {
            "fields": ['login_required', 'show_title', "slug" ],
            "classes": ("collapse-closed",)
        }),
    )

    def in_menu(self):
        """
        Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``.
        """
        for (name, items) in settings.ADMIN_MENU_ORDER:
            if "mezzanine_blocks.RichBlock" in items:
                return True
        return False


class ImageBlockAdmin(admin.ModelAdmin):
    ordering = ('title', 'category')
    list_display = ('admin_thumb', 'title', 'category', 'height', 'width', 'quality', 'login_required', 'show_title')
    list_display_links = ('admin_thumb', 'title')
    list_editable = ('login_required', 'show_title', 'category', 'height', 'width', 'quality')
    search_fields = ('title', 'description', 'url')

    fieldsets = (
        (None, {
            "fields": ["title", "description", "category", "image", 'url'],
        }),
        (_("Advanced data"), {
            "fields": [('height', 'width', 'quality'), 'login_required', 'show_title', "slug" ],
            "classes": ("collapse-closed",)
        }),
    )

    def in_menu(self):
        """
        Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``.
        """
        for (name, items) in settings.ADMIN_MENU_ORDER:
            if "mezzanine_blocks.ImageBlock" in items:
                return True
        return False

admin.site.register(BlockCategory, BlockCategoryAdmin)
admin.site.register(Block, BlockAdmin)
admin.site.register(RichBlock, RichBlockAdmin)
admin.site.register(ImageBlock, ImageBlockAdmin)

@@ -1,20 +1,51 @@
+from functools import partial%0A%0A
 from django.utils.tr
@@ -379,300 +379,8 @@
 )%0A%0A%0A
-    def in_menu(self):%0A        %22%22%22%0A        Hide from the admin menu unless explicitly set in %60%60ADMIN_MENU_ORDER%60%60.%0A        %22%22%22%0A        for (name, items) in settings.ADMIN_MENU_ORDER:%0A            if %22mezzanine_blocks.BlockCategory%22 in items:%0A                return True%0A        return False%0A%0A%0A
 clas
@@ -894,291 +894,8 @@
  )%0A%0A
-    def in_menu(self):%0A        %22%22%22%0A        Hide from the admin menu unless explicitly set in %60%60ADMIN_MENU_ORDER%60%60.%0A        %22%22%22%0A        for (name, items) in settings.ADMIN_MENU_ORDER:%0A            if %22mezzanine_blocks.Block%22 in items:%0A                return True%0A        return False%0A%0A
 %0Acla
@@ -1414,295 +1414,8 @@
  )%0A%0A
-    def in_menu(self):%0A        %22%22%22%0A        Hide from the admin menu unless explicitly set in %60%60ADMIN_MENU_ORDER%60%60.%0A        %22%22%22%0A        for (name, items) in settings.ADMIN_MENU_ORDER:%0A            if %22mezzanine_blocks.RichBlock%22 in items:%0A                return True%0A        return False%0A%0A
 %0Acla
@@ -2119,20 +2119,17 @@
 %0A    )%0A%0A
-    
+%0A
 def in_m
@@ -2136,31 +2136,27 @@
 enu(
-self):%0A   
+app_name):%0A
     
- 
 %22%22%22%0A
-    
     
@@ -2235,41 +2235,35 @@
     
-    %22%22%22%0A        for (name
+%22%22%22%0A    for category
 , items
-)
  in 
@@ -2301,52 +2301,110 @@
     
-    if %22mezzanine_blocks.ImageBlock%22
+for item in items:%0A            if isinstance(item, (tuple, list)):%0A                if app_name
  in item
 s:%0A 
@@ -2391,35 +2391,38 @@
 app_name in item
-s
 :%0A
+    
                 
@@ -2445,20 +2445,354 @@
     
-return False
+    elif item == app_name:%0A                return True%0A%0A%0ABlockCategoryAdmin.in_menu = partial(in_menu, %22mezzanine_blocks.BlockCategory%22)%0ABlockAdmin.in_menu = partial(in_menu, %22mezzanine_blocks.Block%22)%0ARichBlockAdmin.in_menu = partial(in_menu, %22mezzanine_blocks.RichBlock%22)%0AImageBlockAdmin.in_menu = partial(in_menu, %22mezzanine_blocks.ImageBlock%22)
 %0A%0Aad

Fix behavior of ADMIN_MENU_ORDER when using custom app names