combo/combo/apps/wcs/forms.py

196 lines
6.9 KiB
Python

# combo - content management system
# Copyright (C) 2014-2015 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/>.
import json
from django import forms
from django.utils.datastructures import MultiValueDict
from django.utils.translation import ugettext_lazy as _
from combo.utils.forms import MultiSortWidget
from .models import (
WcsCardInfosCell,
WcsCardsCell,
WcsCareFormsCell,
WcsCategoryCell,
WcsCurrentDraftsCell,
WcsCurrentFormsCell,
WcsFormCell,
WcsFormsOfCategoryCell,
)
from .utils import get_wcs_options, get_wcs_services
class WcsFormCellForm(forms.ModelForm):
class Meta:
model = WcsFormCell
fields = ('formdef_reference',)
def __init__(self, *args, **kwargs):
super(WcsFormCellForm, self).__init__(*args, **kwargs)
formdef_references = get_wcs_options('/api/formdefs/')
self.fields['formdef_reference'].widget = forms.Select(choices=formdef_references)
class WcsCardsCellForm(forms.ModelForm):
with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
class Meta:
model = WcsCardsCell
fields = ('carddef_reference', 'custom_title', 'only_for_user')
def __init__(self, *args, **kwargs):
instance = kwargs['instance']
initial = kwargs.pop('initial', {})
initial['with_user'] = not instance.without_user
super().__init__(initial=initial, *args, **kwargs)
card_models = get_wcs_options('/api/cards/@list', include_custom_views=True)
self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.instance.without_user = not self.cleaned_data['with_user']
self.instance.save()
return self.instance
class WcsCardInfoCellForm(forms.ModelForm):
with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
class Meta:
model = WcsCardInfosCell
fields = ('carddef_reference', 'card_id')
def __init__(self, *args, **kwargs):
instance = kwargs['instance']
initial = kwargs.pop('initial', {})
initial['with_user'] = not instance.without_user
super().__init__(initial=initial, *args, **kwargs)
card_models = get_wcs_options('/api/cards/@list')
self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.instance.without_user = not self.cleaned_data['with_user']
self.instance.save()
return self.instance
class WcsCategoryCellForm(forms.ModelForm):
class Meta:
model = WcsCategoryCell
fields = ('category_reference',)
def __init__(self, *args, **kwargs):
super(WcsCategoryCellForm, self).__init__(*args, **kwargs)
references = get_wcs_options('/api/categories/')
self.fields['category_reference'].widget = forms.Select(choices=references)
class WcsFormsOfCategoryCellForm(forms.ModelForm):
class Meta:
model = WcsFormsOfCategoryCell
fields = ('category_reference', 'ordering', 'manual_order', 'limit')
def __init__(self, *args, **kwargs):
super(WcsFormsOfCategoryCellForm, self).__init__(*args, **kwargs)
references = get_wcs_options('/api/categories/')
formdef_references = get_wcs_options('/api/formdefs/', include_category_slug=True)
self.fields['ordering'].widget = forms.Select(
choices=self.fields['ordering'].choices, attrs={'class': 'ordering-select'}
)
self.fields['category_reference'].widget = forms.Select(
choices=references, attrs={'class': 'category-select'}
)
self.fields['manual_order'].widget = MultiSortWidget(choices=formdef_references)
class CategoriesSelectMultiple(forms.SelectMultiple):
def format_value(self, value):
value = json.loads(value)
return super(CategoriesSelectMultiple, self).format_value(value.get('data') or [])
def value_from_datadict(self, data, files, name):
if isinstance(data, MultiValueDict):
return {'data': data.getlist(name)}
return data.get(name, None)
class WcsFormsMixin(object):
def _init_wcs_site(self):
if len(get_wcs_services()) == 1:
self.fields['wcs_site'].widget = forms.HiddenInput()
else:
combo_wcs_sites = [('', _('All'))]
wcs_sites = [(x, y.get('title')) for x, y in get_wcs_services().items()]
wcs_sites.sort(key=lambda x: x[1])
combo_wcs_sites.extend(wcs_sites)
self.fields['wcs_site'].widget = forms.Select(
choices=combo_wcs_sites, attrs={'class': 'wcs-site-select'}
)
def _init_categories(self):
categories = get_wcs_options('/api/categories/')
self.fields['categories'].help_text = _('By default forms from all categories are displayed.')
self.fields['categories'].widget = CategoriesSelectMultiple(
choices=categories, attrs={'class': 'categories-select'}
)
class WcsCurrentFormsCellForm(WcsFormsMixin, forms.ModelForm):
class Meta:
model = WcsCurrentFormsCell
fields = ['wcs_site', 'categories', 'current_forms', 'done_forms', 'include_drafts']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_wcs_site()
self._init_categories()
class WcsCurrentDraftsCellForm(WcsFormsMixin, forms.ModelForm):
class Meta:
model = WcsCurrentDraftsCell
fields = ['wcs_site', 'categories']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_wcs_site()
self._init_categories()
class WcsCareFormsCellForm(WcsFormsMixin, forms.ModelForm):
class Meta:
model = WcsCareFormsCell
fields = ['wcs_site', 'categories']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_wcs_site()
self._init_categories()
class BackofficeSubmissionCellForm(WcsFormsMixin, forms.ModelForm):
class Meta:
model = WcsCurrentDraftsCell
fields = ['wcs_site', 'categories']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_wcs_site()
self._init_categories()