This repository has been archived by the owner on Feb 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cms_wizards.py
208 lines (168 loc) · 7.25 KB
/
cms_wizards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django import forms
from django.db import transaction
from django.utils.translation import ugettext_lazy as _, ugettext
from cms.api import add_plugin
from cms.utils import permissions
from cms.utils.conf import get_cms_setting
from cms.wizards.wizard_pool import wizard_pool
from cms.wizards.wizard_base import Wizard
from cms.wizards.forms import BaseFormMixin
from djangocms_text_ckeditor.widgets import TextEditorWidget
from djangocms_text_ckeditor.html import clean_html
from parler.forms import TranslatableModelForm
from reversion.revisions import revision_context_manager
from .cms_appconfig import FaqConfig
from .models import Category, Question
from .utils import is_valid_namespace
class ConfigCheckMixin(object):
def user_has_add_permission(self, user, **kwargs):
"""
Return True if the current user has permission to add a Category or
Question (depending on value of `perm_string` class variable.
:param user: The current user
:param kwargs: Ignored here
:return: True if user has add permission, else False
"""
# No one can create an Article, if there is no app_config yet.
configs = FaqConfig.objects.all()
if not configs:
return False
if not any([is_valid_namespace(config.namespace)
for config in configs]):
return False
# Ensure user has permission to create articles.
if user.is_superuser or user.has_perm(self.perm_string):
return True
# By default, no permission.
return False
class FaqCategoryWizard(ConfigCheckMixin, Wizard):
perm_string = "aldryn_faq.add_category"
class FaqQuestionWizard(ConfigCheckMixin, Wizard):
perm_string = "aldryn_faq.add_question"
def user_has_add_permission(self, user, **kwargs):
"""
Return True if the current user has permission to add a Question and
there is at least one category.
:param user: The current user
:param kwargs: Ignored here
:return: True if user has add permission, else False
"""
base_perm = super(FaqQuestionWizard, self).user_has_add_permission(
user, **kwargs)
return base_perm and Category.objects.exists()
class CreateFaqCategoryForm(BaseFormMixin, TranslatableModelForm):
"""
The ModelForm for the FAQ Category wizard. Note that Category has few
translated fields that we need to access, so, we use TranslatableModelForm
"""
class Meta:
model = Category
fields = ['name', 'slug', 'appconfig']
# The natural widget for app_config is meant for normal Admin views and
# contains JS to refresh the page on change. This is not wanted here.
widgets = {'appconfig': forms.Select()}
def __init__(self, **kwargs):
super(CreateFaqCategoryForm, self).__init__(**kwargs)
# TODO: FIX this nasty hack to dissallow empty app_config
if 'appconfig' in self.fields:
self.fields['appconfig'].required = True
# If there's only 1 app_config, don't bother show the
# app_config choice field, we'll choose the option for the user.
app_configs = FaqConfig.objects.all()
# check if app config is apphooked
app_configs = [app_config
for app_config in app_configs
if is_valid_namespace(app_config.namespace)]
if len(app_configs) == 1:
self.fields['appconfig'].widget = forms.HiddenInput()
self.fields['appconfig'].initial = app_configs[0].pk
def save(self, commit=True):
"""
Ensure we create a revision for reversion.
"""
category = super(CreateFaqCategoryForm, self).save(commit=False)
# Ensure we make an initial revision
with transaction.atomic():
with revision_context_manager.create_revision():
category.save()
if self.user:
revision_context_manager.set_user(self.user)
revision_context_manager.set_comment(
ugettext("Initial version."))
return category
class CreateFaqQuestionForm(BaseFormMixin, TranslatableModelForm):
"""
The ModelForm for the FAQ Question wizard. Note that Category has few
translated fields that we need to access, so, we use TranslatableModelForm
"""
answer = forms.CharField(
label="Answer", required=False, widget=TextEditorWidget,
help_text=_("Optional. If provided, will be added to the main body of "
"the Question answer.")
)
class Meta:
model = Question
fields = ['title', 'category', 'is_top', 'answer_text',
'answer']
def __init__(self, **kwargs):
super(CreateFaqQuestionForm, self).__init__(**kwargs)
# If there's only 1 category, don't bother show the empty label (choice)
if Category.objects.count() == 1:
self.fields['category'].empty_label = None
def save(self, commit=True):
question = super(CreateFaqQuestionForm, self).save(commit=False)
# If 'content' field has value, create a TextPlugin with same and add
# it to the PlaceholderField
answer = clean_html(self.cleaned_data.get('answer', ''), False)
try:
# CMS >= 3.3.x
content_plugin = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN')
except KeyError:
# CMS <= 3.2.x
content_plugin = get_cms_setting('WIZARD_CONTENT_PLUGIN')
try:
# CMS >= 3.3.x
content_field = get_cms_setting('PAGE_WIZARD_CONTENT_PLUGIN_BODY')
except KeyError:
# CMS <= 3.2.x
content_field = get_cms_setting('WIZARD_CONTENT_PLUGIN_BODY')
if answer and permissions.has_plugin_permission(
self.user, content_plugin, 'add'):
# If the question has not been saved, then there will be no
# Placeholder set-up for this question yet, so, ensure we have saved
# first.
if not question.pk:
question.save()
if question and question.answer:
plugin_kwarg = {
'placeholder': question.answer,
'plugin_type': content_plugin,
'language': self.language_code,
content_field: answer,
}
add_plugin(**plugin_kwarg)
# Ensure we make an initial revision
with transaction.atomic():
with revision_context_manager.create_revision():
question.save()
if self.user:
revision_context_manager.set_user(self.user)
revision_context_manager.set_comment(
ugettext("Initial version."))
return question
faq_category_wizard = FaqCategoryWizard(
title=_(u"New FAQ category"),
weight=400,
form=CreateFaqCategoryForm,
description=_(u"Create a new FAQ category.")
)
wizard_pool.register(faq_category_wizard)
faq_category_wizard = FaqQuestionWizard(
title=_(u"New FAQ question"),
weight=450,
form=CreateFaqQuestionForm,
description=_(u"Create a new FAQ question.")
)
wizard_pool.register(faq_category_wizard)