SidebarLinkInline   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import logging
2
3
from django.conf import settings
4
from django.contrib import admin
5
from django.forms.models import ModelForm
6
from django.contrib.admin import TabularInline
7
8
from mezzanine.core.admin import TabularDynamicInlineAdmin
9
from mezzanine.core.admin import StackedDynamicInlineAdmin
10
from mezzanine.forms.models import Form
11
from mezzanine.forms.admin import FormAdmin
12
from mezzanine.pages.admin import PageAdmin
13
from mezzanine.pages.models import RichTextPage
14
from mezzanine.utils.admin import SingletonAdmin
15
16
from website.jdpages.models import BlogCategoryPage
17
from website.jdpages.models import Footer
18
from website.jdpages.models import FooterLink
19
from website.jdpages.models import FooterLinks
20
from website.jdpages.models import FooterInfo
21
from website.jdpages.models import HomePage
22
from website.jdpages.models import OrganisationPage
23
from website.jdpages.models import OrganisationPartPage
24
from website.jdpages.models import OrganisationMember
25
from website.jdpages.models import OrganisationPartMember
26
from website.jdpages.models import VisionPage
27
from website.jdpages.models import VisionsPage
28
from website.jdpages.models import ActionBanner
29
from website.jdpages.models import PageHeaderImage
30
from website.jdpages.models import SidebarAgenda
31
from website.jdpages.models import SidebarSocial
32
from website.jdpages.models import SidebarTwitter
33
from website.jdpages.models import SidebarLink
34
from website.jdpages.models import SidebarRichText
35
from website.jdpages.models import SocialMediaUrls
36
from website.jdpages.models import ThatsWhyItem
37
from website.jdpages.models import WordLidPage
38
39
logger = logging.getLogger(__name__)
40
41
42
class AlwaysChangedModelForm(ModelForm):
43
    """
44
    A django modelform that is always changed and will thus always be saved and validated.
45
    To be used for inlines that should also be created with their default/initial values.
46
    """
47
    def has_changed(self):
48
        """
49
        Should return True if data differs from initial.
50
        Unchanged inlines will also get validated and saved by always returning true here.
51
        """
52
        return True
53
54
55
class FooterLinkInline(TabularDynamicInlineAdmin):
56
    model = FooterLink
57
    verbose_name = "Footer link"
58
    verbose_name_plural = "Footer links"
59
60
61
class FooterLinksAdmin(admin.ModelAdmin):
62
    model = FooterLinks
63
    inlines = [FooterLinkInline]
64
65
66
class PageHeaderImageInline(TabularDynamicInlineAdmin):
67
    model = PageHeaderImage
68
    verbose_name = "Header image"
69
    verbose_name_plural = "Header images"
70
71
72
class ActionBannerInline(StackedDynamicInlineAdmin):
73
    model = ActionBanner
74
    extra = 1
75
    verbose_name = "Action banner"
76
    verbose_name_plural = "Action banners"
77
78
79
class SidebarAgendaInline(TabularDynamicInlineAdmin):
80
    model = SidebarAgenda
81
    form = AlwaysChangedModelForm
82
    extra = 0
83
    verbose_name = "Agenda sidebar"
84
    verbose_name_plural = "Agenda sidebars"
85
86
87
class SidebarSocialInline(TabularDynamicInlineAdmin):
88
    model = SidebarSocial
89
    form = AlwaysChangedModelForm
90
    extra = 0
91
    verbose_name = "Social sidebar"
92
    verbose_name_plural = "Social sidebars"
93
94
95
class SidebarTwitterInline(TabularDynamicInlineAdmin):
96
    model = SidebarTwitter
97
    form = AlwaysChangedModelForm
98
    extra = 0
99
    verbose_name = "Twitter sidebar"
100
    verbose_name_plural = "Twitter sidebars"
101
102
103
class SidebarLinkInline(TabularDynamicInlineAdmin):
104
    model = SidebarLink
105
    form = AlwaysChangedModelForm
106
    extra = 0
107
    verbose_name = "Link list sidebar"
108
    verbose_name_plural = "Link list sidebars"
109
110
111
class SidebarRichTextInline(StackedDynamicInlineAdmin):
112
    model = SidebarRichText
113
    extra = 1
114
    verbose_name = "Content sidebar"
115
    verbose_name_plural = "Content sidebars"
116
117
118
class ThatsWhyInline(TabularDynamicInlineAdmin):
119
    model = ThatsWhyItem
120
    form = AlwaysChangedModelForm
121
    extra = 0
122
    verbose_name = "That's why item"
123
    verbose_name_plural = "That's why items"
124
125
126
class HomePageAdmin(PageAdmin):
127
    model = HomePage
128
    filter_vertical = ['vision_pages']
129
    inlines = [PageHeaderImageInline, ActionBannerInline, SidebarAgendaInline, SidebarTwitterInline, SidebarSocialInline]
130
131
132
class VisionsPageAdmin(PageAdmin):
133
    model = VisionsPage
134
    filter_vertical = ['vision_pages']
135
    inlines = [PageHeaderImageInline, SidebarTwitterInline]
136
    verbose_name = "Standpuntenpagina"
137
    verbose_name_plural = "Standpuntenpagina's"
138
139
    def get_form(self, request, obj=None, **kwargs):
140
        # remove the '+' (add) button for vision pages, should not be created here
141
        form = super().get_form(request, obj, **kwargs)
142
        form.base_fields["vision_pages"].widget.can_add_related = False
143
        return form
144
145
146
class VisionPageAdmin(PageAdmin):
147
    model = VisionPage
148
    inlines = [PageHeaderImageInline, SidebarTwitterInline]
149
    verbose_name = "Standpuntpagina"
150
    verbose_name_plural = "Standpuntpagina's"
151
152
153
class OrganisationPageAdmin(PageAdmin):
154
    model = OrganisationPage
155
    inlines = [PageHeaderImageInline, SidebarAgendaInline, SidebarTwitterInline, SidebarSocialInline]
156
    filter_vertical = ['organisation_part_pages']
157
    verbose_name = "Organisatiepagina"
158
    verbose_name_plural = "Organisatiepagina's"
159
160
    def get_form(self, request, obj=None, **kwargs):
161
        # remove the '+' (add) button for part pages, should not be created here
162
        form = super().get_form(request, obj, **kwargs)
163
        form.base_fields["organisation_part_pages"].widget.can_add_related = False
164
        return form
165
166
167
class OrganisationPartPageAdmin(PageAdmin):
168
    model = OrganisationPartPage
169
    inlines = [PageHeaderImageInline, SidebarAgendaInline, SidebarTwitterInline, SidebarSocialInline]
170
    verbose_name = "Organisatieonderdeelpagina"
171
    verbose_name_plural = "Organisatieonderdeelpagina's"
172
173
174
class OrganisationMemberAdmin(admin.ModelAdmin):
175
    model = OrganisationMember
176
    verbose_name = "Organisatielid"
177
    verbose_name_plural = "Organisatieleden"
178
    list_display = (
179
        'name',
180
        'image',
181
        'facebook_url',
182
        'twitter_url',
183
    )
184
    search_fields = ['name']
185
186
187
class OrganisationPartMemberAdmin(admin.ModelAdmin):
188
    model = OrganisationPartMember
189
    verbose_name = "Organisatiefunctie"
190
    verbose_name_plural = "Organisatiefuncties"
191
    list_display = (
192
        'member',
193
        'organisation_part',
194
        'role',
195
    )
196
    search_fields = ['member__name']
197
198
199
class RichtTextPageAdmin(PageAdmin):
200
    inlines = [
201
        PageHeaderImageInline, SidebarAgendaInline, SidebarSocialInline,
202
        SidebarTwitterInline, SidebarLinkInline, SidebarRichTextInline
203
    ]
204
205
206
class BlogPageAdmin(PageAdmin):
207
    inlines = [PageHeaderImageInline]
208
209
210
class WordLidAdmin(PageAdmin):
211
    model = WordLidPage
212
    inlines = [
213
      PageHeaderImageInline, ActionBannerInline, SidebarTwitterInline, SidebarLinkInline, ThatsWhyInline
214
    ]
215
216
class CustomFormAdmin(FormAdmin):
217
    model = Form
218
    inlines = [PageHeaderImageInline, SidebarRichTextInline]
219
    inlines.insert(0, FormAdmin.inlines[0])
220
221
222
class FooterAdmin(SingletonAdmin):
223
    model = Footer
224
    verbose_name = "Footer"
225
    verbose_name_plural = "Footers"
226
227
228
class SocialMediaUrlsAdmin(SingletonAdmin):
229
    model = SocialMediaUrls
230
    verbose_name = "Social media url list"
231
    verbose_name_plural = "Social media url lists"
232
233
234
admin.site.unregister(RichTextPage)
235
admin.site.register(RichTextPage, RichtTextPageAdmin)
236
237
admin.site.unregister(Form)
238
admin.site.register(Form, CustomFormAdmin)
239
240
admin.site.register(HomePage, HomePageAdmin)
241
admin.site.register(OrganisationPartPage, OrganisationPartPageAdmin)
242
admin.site.register(OrganisationPage, OrganisationPageAdmin)
243
admin.site.register(VisionPage, VisionPageAdmin)
244
admin.site.register(VisionsPage, VisionsPageAdmin)
245
admin.site.register(BlogCategoryPage, BlogPageAdmin)
246
admin.site.register(WordLidPage, WordLidAdmin)
247
248
admin.site.register(OrganisationMember, OrganisationMemberAdmin)
249
admin.site.register(OrganisationPartMember, OrganisationPartMemberAdmin)
250
251
admin.site.register(Footer, FooterAdmin)
252
admin.site.register(FooterInfo)
253
admin.site.register(FooterLinks, FooterLinksAdmin)
254
admin.site.register(SocialMediaUrls, SocialMediaUrlsAdmin)
255