import logging
logger = logging.getLogger(__name__)

from django.conf import settings
from django.contrib import admin
from django.utils.functional import curry
from django.forms.models import ModelForm

from mezzanine.core.admin import SingletonAdmin
from mezzanine.core.admin import TabularDynamicInlineAdmin
from mezzanine.pages.admin import PageAdmin
from mezzanine.pages.models import RichTextPage

from website.jdpages.models import ColumnElement, ColumnElementWidget
from website.jdpages.models import DocumentListing, Document
from website.jdpages.models import HomePage
from website.jdpages.models import HorizontalPosition
from website.jdpages.models import PageHeaderImageWidget, PageHeaderSettingsWidget
from website.jdpages.models import SocialMediaButton
from website.jdpages.models import Sidebar
from website.jdpages.models import SidebarBlogCategoryWidget
from website.jdpages.models import SidebarBannerWidget
from website.jdpages.models import SidebarTwitterWidget


class AlwaysChangedModelForm(ModelForm):
    def has_changed(self):
        """ Should returns True if data differs from initial.
        By always returning true even unchanged inlines will get validated and saved."""
        return True


class PageHeaderImageSettingsInline(admin.TabularInline):
    model = PageHeaderSettingsWidget
    form = AlwaysChangedModelForm
    verbose_name = "Header image type"
    verbose_name_plural = "Header image type"


class PageHeaderImageInline(TabularDynamicInlineAdmin):
    model = PageHeaderImageWidget
    verbose_name = "Header image"
    verbose_name_plural = "Header images"


class ColumnElementWidgetInline(TabularDynamicInlineAdmin):
    """ """
    model = ColumnElementWidget

    def get_formset(self, request, obj=None, **kwargs):
        """ 
        Adds the initial value of the horizontal position to the formset.
        Note: does not work for the 'Add another' button in the admin.
        """
        initial = [{'horizontal_position': self.get_default_position()}]
        formset = super(ColumnElementWidgetInline, self).get_formset(request, obj, **kwargs)
        formset.__init__ = curry(formset.__init__, initial=initial)
        return formset


class LeftColumnElementWidgetInline(ColumnElementWidgetInline):
    """ """
    verbose_name_plural = 'Left column widgets'

    def get_queryset(self, request):
        return ColumnElementWidget.objects.filter(horizontal_position=self.get_default_position())

    @staticmethod
    def get_default_position():
        return HorizontalPosition.LEFT


class RightColumnElementWidgetInline(ColumnElementWidgetInline):
    """ """
    verbose_name_plural = 'Right column widgets'

    def get_queryset(self, request):
        return ColumnElementWidget.objects.filter(horizontal_position=self.get_default_position())

    @staticmethod
    def get_default_position():
        return HorizontalPosition.RIGHT


class HomePageAdmin(PageAdmin):
    inlines = [PageHeaderImageSettingsInline, PageHeaderImageInline,
               LeftColumnElementWidgetInline, RightColumnElementWidgetInline]


class RichtTextPageAdmin(PageAdmin):
    inlines = [PageHeaderImageSettingsInline, PageHeaderImageInline]


class DocumentAdmin(admin.ModelAdmin):
    list_display = ('description', 'document', 'document_listing')


class ColumnElementAdmin(admin.ModelAdmin):
    list_display = ('id', 'content_object', 'content_type', 'object_id', 'site',)


class ColumnElementWidgetAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'column_element', 'page', 'max_items', 'horizontal_position', 'site')


class DocumentInline(TabularDynamicInlineAdmin):
    model = Document


class DocumentListingAdmin(PageAdmin):
    inlines = (DocumentInline,)


class SidebarBlogCategoryWidgetInline(admin.TabularInline):
    model = SidebarBlogCategoryWidget
    extra = 2
    max_num = 2
    verbose_name = "Blogs"
    verbose_name_plural = "Blogs"


class SidebarTwitterWidgetInline(admin.TabularInline):
    model = SidebarTwitterWidget
    verbose_name = "Twitter feed"
    verbose_name_plural = "Twitter feed"


class SidebarBannerWidgetAdmin(admin.ModelAdmin):
    model = SidebarBannerWidget
    list_display = ('id', 'active', 'title', 'image', 'url')


class SocialMediaButtonInline(TabularDynamicInlineAdmin):
    model = SocialMediaButton
    verbose_name = "Social media buttons"
    verbose_name_plural = "Social media buttons"


class SidebarAdmin(SingletonAdmin):
    model = Sidebar
    inlines = (SidebarBlogCategoryWidgetInline,
               SidebarTwitterWidgetInline,
               SocialMediaButtonInline,)


class SidebarElementWidgetAdmin(admin.ModelAdmin):
    list_display = ('id', 'sidebar_element', 'site')


class SocialMediaButtonGroupAdmin(admin.ModelAdmin):
    inlines = (SocialMediaButtonInline,)


admin.site.unregister(RichTextPage)
admin.site.register(RichTextPage, RichtTextPageAdmin)
admin.site.register(HomePage, HomePageAdmin)
admin.site.register(Sidebar, SidebarAdmin)
admin.site.register(SidebarBannerWidget, SidebarBannerWidgetAdmin)
admin.site.register(DocumentListing, DocumentListingAdmin)

if settings.DEBUG:
    admin.site.register(ColumnElement, ColumnElementAdmin)
    admin.site.register(ColumnElementWidget, ColumnElementWidgetAdmin)
    admin.site.register(Document, DocumentAdmin)

@@ -1020,16 +1020,213 @@
 lForm):%0A
+    %22%22%22%0A    A django modelform that is always changed and will thus always be saved and validated.%0A    To be used for inlines that should also be created with their default/initial values.%0A    %22%22%22%0A
     def 
@@ -1255,16 +1255,24 @@
      %22%22%22
+%0A       
  Should 
@@ -1277,17 +1277,16 @@
 d return
-s
  True if
@@ -1325,39 +1325,9 @@
     
-By always returning true even u
+U
 ncha
@@ -1344,16 +1344,21 @@
 es will 
+also 
 get vali
@@ -1372,17 +1372,56 @@
 nd saved
-.
+ by always returning true here.%0A        
 %22%22%22%0A    
@@ -1885,29 +1885,68 @@
 in):%0A    %22%22%22
+ Base admin class for a column element
  %22%22%22%0A
+%0A
     model = 
@@ -2514,37 +2514,94 @@
 Inline):%0A    %22%22%22
+ Inline for a widget in the left column of a column page
  %22%22%22%0A
+%0A
     verbose_name
@@ -2933,21 +2933,79 @@
 %0A    %22%22%22
+ Inline for a widget in the right column of a column page
  %22%22%22%0A
+%0A
     verb
@@ -5481,16 +5481,89 @@
 Admin)%0A%0A
+# we add some models to the admin for debugging, if we are in debug mode%0A
 if setti

Improve jdpages admin docstrings