class BlogCategoryPage(Page, RichText):
    """
    Model for a page that displays a list of posts in a single blog category.
    """

    blog_category = models.ForeignKey(BlogCategory, null=False, blank=False)
    show_excerpt = models.BooleanField(default=False, null=False, blank=False,
                                       help_text='Show only the first paragraph of a blog post.')

    class Meta:
        verbose_name = "Blog categorie pagina"
        verbose_name_plural = "Blog categorie pagina's" class Footer(SiteRelated):
    links_left = models.OneToOneField(FooterLinks, auto_created=True, related_name="links_left")
    links_middle = models.OneToOneField(FooterLinks, auto_created=True, related_name="links_right")
    info_right = models.OneToOneField(FooterInfo, auto_created=True)

    class Meta:
        verbose_name = "Footer"
        verbose_name_plural = "Footer" class FooterLink(SiteRelated, Orderable):
    title = models.CharField(max_length=100, blank=True, default="")
    url = models.CharField(max_length=500, blank=True, default="")
    footer_links = models.ForeignKey(FooterLinks, blank=True, null=True)

    def __str__(self):
        return self.title class FooterLinks(SiteRelated):
    title = models.CharField(max_length=100, blank=True, default="")

    def __str__(self):
        return self.title class FooterInfo(SiteRelated):
    title = models.CharField(max_length=100, blank=True, default="")
    content = RichTextField()

    def __str__(self):
        return self.title class HomePage(Page, RichText):
    """
    Page model for the site homepage.
    Only works properly when url points to the homepage '/' as url.
    """
    header_title = models.CharField(max_length=300, blank=True, default="")
    header_subtitle = models.CharField(max_length=500, blank=True, default="")
    news_category = models.ForeignKey(BlogCategory, null=True, blank=True)
    vision_pages = models.ManyToManyField(VisionPage, blank=True, verbose_name="Standpunt pagina's")

    @property
    def blog_posts(self):
        return get_public_blogposts(self.news_category)[:4]

    class Meta:
        verbose_name = 'Home pagina'
        verbose_name_plural = "Home pagina's" class OrganisationPage(Page, RichText):
    """
    """
    organisation_part_pages = models.ManyToManyField(OrganisationPartPage, blank=True, verbose_name="Organisatie onderdelen")

    class Meta:
        verbose_name = 'Organisatie pagina'
        verbose_name_plural = "Organisatie pagina's" class OrganisationPartPage(Page, RichText):
    """
    """
    image = FileField(max_length=300, format="Image", blank=True, default="", validators=[validate_organisation_image])

    class Meta:
        verbose_name = 'Organisatie-onderdeel pagina'
        verbose_name_plural = "Organisatie-onderdeel pagina's" class OrganisationMember(SiteRelated):
    """
    """
    name = models.CharField(max_length=200, blank=False, default="")
    content = RichTextField(blank=True, default="")
    image = FileField(max_length=300, format="Image", blank=True, default="")
    email = models.EmailField(blank=True, default="")
    facebook_url = models.URLField(blank=True, default="")
    twitter_url = models.URLField(blank=True, default="")

    class Meta:
        verbose_name = 'Organisatie lid'
        verbose_name_plural = "Organisatie leden"

    def __str__(self):
        return self.name class OrganisationPartMember(SiteRelated):
    member = models.ForeignKey(OrganisationMember)
    organisation_part = models.ForeignKey(OrganisationPartPage, null=True, blank=True)
    role = models.CharField(max_length=200, blank=True, default="")

    class Meta:
        verbose_name = 'Organisatie functie'
        verbose_name_plural = "Organisatie functies"

    def __str__(self):
        return self.role + ' - ' + self.member.name class VisionPage(Page, RichText):
    """
    """
    image = FileField(max_length=300, format="Image", blank=True, default="", validators=[validate_vision_image])

    class Meta:
        verbose_name = 'Standpunt pagina'
        verbose_name_plural = "Standpunt pagina's" class VisionsPage(Page, RichText):
    """
    """
    vision_pages = models.ManyToManyField(VisionPage, blank=True, verbose_name="Standpunt pagina's")

    class Meta:
        verbose_name = 'Standpunten pagina'
        verbose_name_plural = "Standpunten pagina's" class ActionBanner(PageItem):
    title = models.CharField(max_length=500, blank=True, default="")
    content = RichTextField()
    image = FileField(max_length=300, format="Image")
    button_title = models.CharField(max_length=500, blank=True, default="")
    button_url = models.CharField(max_length=500, blank=True, default="")

    def __str__(self):
        return self.title class PageHeaderImage(SiteRelated):
    """ Page header image. """
    name = models.CharField(max_length=1000, blank=True, null=False, default="")
    page = models.ForeignKey(Page, blank=False, null=True)
    image = FileField(max_length=200, format="Image", validators=[validate_header_image]) class SidebarAgenda(PageItem):
    SITE = 'SI'
    ALL = 'AL'
    MAIN = 'MA'
    MAIN_AND_SITE = 'SM'

    EVENT_CHOICES = (
        (SITE, 'Site'),
        (ALL, 'All'),
        (MAIN, 'Main site'),
        (MAIN_AND_SITE, 'Main and site'),
    )

    type = models.CharField(max_length=2, choices=EVENT_CHOICES)

    class Meta:
        verbose_name = "Sidebar Agenda Item"

    def get_name(self):
        if self.type == self.MAIN_AND_SITE:
            return 'Events for current and main site'
        elif self.type == self.ALL:
            return 'Events for all sites'
        elif self.type == self.SITE:
            return 'Events for current site'
        elif self.type == self.MAIN:
            return 'Events for main site'
        assert False

    def __str__(self):
        return self.get_name() class SidebarSocial(PageItem):

    @property
    def urls(self):
        smulrs = SocialMediaUrls.objects.all()
        if smulrs.exists():
            return smulrs[0]
        return None

    class Meta:
        verbose_name = "Sidebar Social Media Item" class SidebarTwitter(PageItem):

    class Meta:
        verbose_name = "Sidebar Twitter Item" class SidebarLink(PageItem, Orderable):
    title = models.CharField(max_length=100, blank=True, default="")
    url = models.CharField(max_length=500, blank=True, default="") class SidebarRichText(PageItem):
    title = models.CharField(max_length=100, blank=True, default="")
    content = RichTextField()

    class Meta:
        verbose_name = "Sidebar RichText Item"

    def __str__(self):
        return self.title class SocialMediaUrls(SiteRelated):
    facebook_url = models.URLField(max_length=300, blank=True, default="")
    twitter_url = models.URLField(max_length=300, blank=True, default="")
    youtube_url = models.URLField(max_length=300, blank=True, default="")
    linkedin_url = models.URLField(max_length=300, blank=True, default="")
    instagram_url = models.URLField(max_length=300, blank=True, default="")

    class Meta:
        verbose_name = "Social media urls"
        verbose_name_plural = "Social media urls" class ThatsWhyItem(PageItem, Orderable):
    title = models.CharField(max_length=100, blank=True, default="")

    class Meta:
        verbose_name = "That's why item"
        verbose_name_plural = "That's why items"

    def __str__(self):
        return self.title class WordLidPage(Page, RichText):
    """
    """

    class Meta:
        verbose_name = 'Word lid pagina'
        verbose_name_plural = "Standpunt pagina's"
import logging
from django.conf import settings
from django.contrib import admin
from django.forms.models import ModelForm
from django.contrib.admin import TabularInline
from mezzanine.core.admin import TabularDynamicInlineAdmin
from mezzanine.core.admin import StackedDynamicInlineAdmin
from mezzanine.forms.models import Form
from mezzanine.forms.admin import FormAdmin
from mezzanine.pages.admin import PageAdmin
from mezzanine.pages.models import RichTextPage
from mezzanine.utils.admin import SingletonAdmin
from website.jdpages.models import BlogCategoryPage
from website.jdpages.models import Footer
from website.jdpages.models import FooterLink
from website.jdpages.models import FooterLinks
from website.jdpages.models import FooterInfo
from website.jdpages.models import HomePage
from website.jdpages.models import OrganisationPage
from website.jdpages.models import OrganisationPartPage
from website.jdpages.models import OrganisationMember
from website.jdpages.models import OrganisationPartMember
from website.jdpages.models import VisionPage
from website.jdpages.models import VisionsPage
from website.jdpages.models import ActionBanner
from website.jdpages.models import PageHeaderImage
from website.jdpages.models import SidebarAgenda
from website.jdpages.models import SidebarSocial
from website.jdpages.models import SidebarTwitter
from website.jdpages.models import SidebarLink
from website.jdpages.models import SidebarRichText
from website.jdpages.models import SocialMediaUrls
from website.jdpages.models import ThatsWhyItem
from website.jdpages.models import WordLidPage




logger = logging.getLogger(__name__)


class AlwaysChangedModelForm(ModelForm):
    """
    A django modelform that is always changed and will thus always be saved and validated.
    To be used for inlines that should also be created with their default/initial values.
    """
    def has_changed(self):
        """
        Should return True if data differs from initial.
        Unchanged inlines will also get validated and saved by always returning true here.
        """
        return True


class FooterLinkInline(TabularDynamicInlineAdmin):
    model = FooterLink
    verbose_name = "Footer link"
    verbose_name_plural = "Footer links"


class FooterLinksAdmin(admin.ModelAdmin):

    model = FooterLinks