combo/combo/manager/forms.py

108 lines
3.2 KiB
Python

# combo - content management system
# Copyright (C) 2014-2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.template.loader import get_template, TemplateDoesNotExist
from django.utils.translation import ugettext_lazy as _
from combo.data.models import Page
class PageEditTitleForm(forms.ModelForm):
class Meta:
model = Page
fields = ('title',)
class PageEditSlugForm(forms.ModelForm):
class Meta:
model = Page
fields = ('slug',)
def clean_slug(self):
value = self.cleaned_data.get('slug')
if self.instance.slug == value:
return value
if Page.objects.filter(slug=value).count() > 0:
raise ValidationError(_('Slug must be unique'), code='unique')
return value
class PageEditDescriptionForm(forms.ModelForm):
class Meta:
model = Page
fields = ('description',)
class PageEditPictureForm(forms.ModelForm):
class Meta:
model = Page
fields = ('picture',)
class PageVisibilityForm(forms.ModelForm):
class Meta:
model = Page
fields = ('public', 'groups')
class PageSelectTemplateForm(forms.ModelForm):
class Meta:
model = Page
fields = ('template_name',)
def __init__(self, *args, **kwargs):
super(PageSelectTemplateForm, self).__init__(*args, **kwargs)
templates = [(x[0], x[1]['name']) for x in settings.COMBO_PUBLIC_TEMPLATES.items()]
templates = [x for x in templates if self.template_exists(x[0])]
templates.sort(lambda x, y: cmp(x[1], y[1]))
if 'template_name' in self.fields:
self.fields['template_name'].widget = forms.Select(choices=templates)
def template_exists(self, template):
try:
get_template(settings.COMBO_PUBLIC_TEMPLATES[template].get('template'))
except TemplateDoesNotExist:
return False
return True
class PageEditRedirectionForm(forms.ModelForm):
class Meta:
model = Page
fields = ('redirect_url',)
def save(self, *args, **kwargs):
page = super(PageEditRedirectionForm, self).save(*args, **kwargs)
page.redirect_url = page.redirect_url.strip()
page.save()
return page
class PageEditExcludeFromNavigationForm(forms.ModelForm):
class Meta:
model = Page
fields = ('exclude_from_navigation',)
class SiteImportForm(forms.Form):
site_json = forms.FileField(label=_('Site Export File'))
class AssetUploadForm(forms.Form):
upload = forms.FileField(label=_('File'))