Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
Wizzards for category and question, allow cms 3.2, bump translation t…
Browse files Browse the repository at this point in the history
…ools to 0.2.1
  • Loading branch information
Kirill Kniazev committed Nov 3, 2015
1 parent 667a364 commit 5a037e3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 2 deletions.
164 changes: 164 additions & 0 deletions aldryn_faq/cms_wizards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.utils.translation import ugettext_lazy as _
from django import forms

from cms.api import add_plugin
from cms.utils import permissions
from cms.wizards.wizard_pool import wizard_pool
from cms.wizards.wizard_base import Wizard
from cms.wizards.forms import BaseFormMixin

from parler.forms import TranslatableModelForm

from .cms_appconfig import FaqConfig
from .models import Category, Question
from .utils import namespace_is_apphooked


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 configs.count() < 1:
return False
if not any([namespace_is_apphooked(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)
if base_perm and Category.objects.count() > 0:
return True
# By default, no permission.
return False


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']

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 namespace_is_apphooked(app_config.namespace)]
if len(app_configs) == 1:
self.fields['appconfig'].widget = forms.HiddenInput()
self.fields['appconfig'].initial = app_configs[0].pk


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", help_text=_("Optional. If provided, will be added to "
"the main body of the Question answer."),
required=False, widget=forms.Textarea())

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)
category = Category.objects.all()
if category.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 = self.cleaned_data.get('answer', '')
if answer and permissions.has_plugin_permission(
self.user, 'TextPlugin', '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:
add_plugin(
placeholder=question.answer,
plugin_type='TextPlugin',
language=self.language_code,
body=answer,
)

if commit:
question.save()

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)
13 changes: 13 additions & 0 deletions aldryn_faq/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from django.core.urlresolvers import reverse, NoReverseMatch

default_cms_plugin_table_mapping = (
# (old_name, new_name),
Expand Down Expand Up @@ -46,3 +47,15 @@ def rename_tables_old_to_new(db, table_mapping=None):

def rename_tables_new_to_old(db, table_mapping=None):
return rename_tables(db, table_mapping, reverse=True)


def namespace_is_apphooked(namespace):
"""
Check if provided namespace has an app-hooked page.
Returns True or False.
"""
try:
reverse('{0}:faq-category-list'.format(namespace))
except (NoReverseMatch, AttributeError):
return False
return True
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
'aldryn-boilerplates>=0.6.0',
'aldryn-reversion>=1.0.0',
'aldryn-search',
'aldryn-translation-tools>=0.1.2',
'aldryn-translation-tools>=0.2.1',
'django>=1.6,<1.9',
'django-admin-sortable2>=0.5.2',
'django-cms>=3.0.12,<3.2',
'django-cms>=3.0.12,<3.3',
'django-parler>=1.4',
'django-reversion>=1.8.2,<1.9',
'django-sortedm2m',
Expand Down

0 comments on commit 5a037e3

Please sign in to comment.