196 lines
6.8 KiB
Python
196 lines
6.8 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.contrib.auth.models import Group
|
|
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
|
|
|
|
from .fields import ImageIncludingSvgField
|
|
|
|
|
|
def get_groups_as_choices():
|
|
return [(x.id, x.name) for x in Group.objects.all().order_by('name')]
|
|
|
|
|
|
class PageDuplicateForm(forms.Form):
|
|
title = forms.CharField(label=_('New title'), max_length=150, required=False)
|
|
|
|
|
|
class PageEditTitleForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Page
|
|
fields = ('title',)
|
|
|
|
|
|
class PageEditSlugForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Page
|
|
fields = ('slug', 'sub_slug')
|
|
|
|
def clean_slug(self):
|
|
value = self.cleaned_data.get('slug')
|
|
if self.instance.slug == value:
|
|
return value
|
|
if Page.objects.filter(slug=value, parent_id=self.instance.parent_id).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',)
|
|
field_classes = {
|
|
'picture': ImageIncludingSvgField
|
|
}
|
|
|
|
|
|
class PageVisibilityForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Page
|
|
fields = ('public', 'groups')
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(PageVisibilityForm, self).__init__(*args, **kwargs)
|
|
self.fields['groups'].queryset = Group.objects.order_by('name')
|
|
|
|
|
|
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(key=lambda x: x[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 PageEditIncludeInNavigationForm(forms.ModelForm):
|
|
include_in_navigation = forms.BooleanField(
|
|
label=_('Include in navigation menus'),
|
|
required=False
|
|
)
|
|
|
|
class Meta:
|
|
model = Page
|
|
fields = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
instance = kwargs['instance']
|
|
initial = kwargs.pop('initial', {})
|
|
initial['include_in_navigation'] = not instance.exclude_from_navigation
|
|
super(PageEditIncludeInNavigationForm, self).__init__(initial=initial, *args, **kwargs)
|
|
|
|
def save(self, *args, **kwargs):
|
|
super(PageEditIncludeInNavigationForm, self).save(*args, **kwargs)
|
|
self.instance.exclude_from_navigation = not self.cleaned_data['include_in_navigation']
|
|
self.instance.save()
|
|
return self.instance
|
|
|
|
|
|
class CellVisibilityForm(forms.Form):
|
|
visibility = forms.ChoiceField(
|
|
label=_('Visibility'),
|
|
choices=[('all', _('Everybody')),
|
|
('logged', _('Logged users')),
|
|
('unlogged', _('Unlogged users')),
|
|
('groups-any', _('Users with one of these groups')),
|
|
('groups-none', _('Users with none of these groups'))])
|
|
groups = forms.MultipleChoiceField(
|
|
label=_('Groups'),
|
|
required=False,
|
|
choices=get_groups_as_choices)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.instance = instance = kwargs.pop('instance')
|
|
initial = kwargs.get('initial')
|
|
initial['groups'] = [x.id for x in instance.groups.all()]
|
|
if instance.public:
|
|
if instance.restricted_to_unlogged:
|
|
initial['visibility'] = 'unlogged'
|
|
else:
|
|
initial['visibility'] = 'all'
|
|
else:
|
|
initial['visibility'] = 'logged'
|
|
if initial['groups']:
|
|
if instance.restricted_to_unlogged:
|
|
initial['visibility'] = 'groups-none'
|
|
else:
|
|
initial['visibility'] = 'groups-any'
|
|
super(CellVisibilityForm, self).__init__(*args, **kwargs)
|
|
|
|
def save(self):
|
|
if self.cleaned_data['visibility'] == 'all':
|
|
self.instance.public = True
|
|
self.instance.restricted_to_unlogged = False
|
|
self.instance.groups.clear()
|
|
elif self.cleaned_data['visibility'] == 'logged':
|
|
self.instance.public = False
|
|
self.instance.restricted_to_unlogged = False
|
|
self.instance.groups.clear()
|
|
elif self.cleaned_data['visibility'] == 'unlogged':
|
|
self.instance.public = True
|
|
self.instance.restricted_to_unlogged = True
|
|
self.instance.groups.clear()
|
|
elif self.cleaned_data['visibility'] == 'groups-any':
|
|
self.instance.public = False
|
|
self.instance.restricted_to_unlogged = False
|
|
self.instance.groups.set(self.cleaned_data['groups'])
|
|
elif self.cleaned_data['visibility'] == 'groups-none':
|
|
self.instance.public = False
|
|
self.instance.restricted_to_unlogged = True
|
|
self.instance.groups.set(self.cleaned_data['groups'])
|
|
self.instance.save()
|
|
return self.instance
|
|
|
|
|
|
class SiteImportForm(forms.Form):
|
|
site_json = forms.FileField(label=_('Site Export File'))
|