# 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 . import django from django import forms from django.utils.datastructures import MultiValueDict from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from combo.utils.forms import MultiSortWidget from .models import (WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell, WcsCurrentFormsCell, WcsCurrentDraftsCell) 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 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): # this converts data dictionary to list, for django >=1.11 return super(CategoriesSelectMultiple, self).format_value( value.get('data') or []) def render_options(self, choices, value): # this converts data dictionary to list, for django <1.11 value = value.get('data') or [] return super(CategoriesSelectMultiple, self).render_options(choices, value) 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'] def __init__(self, *args, **kwargs): super(WcsCurrentFormsCellForm, self).__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(WcsCurrentDraftsCellForm, self).__init__(*args, **kwargs) self._init_wcs_site() self._init_categories()