124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2014-2018 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.utils.translation import ugettext_lazy as _
|
|
|
|
from combo.data.models import Page
|
|
from .models import SearchCell
|
|
from . import engines
|
|
|
|
|
|
class SearchCellForm(forms.ModelForm):
|
|
class Meta:
|
|
model = SearchCell
|
|
fields = ('autofocus', 'input_placeholder')
|
|
|
|
|
|
class SelectWithDisabled(forms.Select):
|
|
"""
|
|
Subclass of Django's select widget that allows disabling options.
|
|
To disable an option, pass a dict instead of a string for its label,
|
|
of the form: {'label': 'option label', 'disabled': True}
|
|
"""
|
|
|
|
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
|
|
disabled = False
|
|
if isinstance(label, dict):
|
|
label, disabled = label['label'], label['disabled']
|
|
option_dict = super().create_option(name, value, label, selected, index, subindex=subindex, attrs=attrs)
|
|
if disabled:
|
|
option_dict['attrs']['disabled'] = 'disabled'
|
|
return option_dict
|
|
|
|
|
|
class TextEngineSettingsForm(forms.ModelForm):
|
|
selected_page = forms.ModelChoiceField(
|
|
label=_('Page'),
|
|
required=False,
|
|
queryset=Page.objects.none(),
|
|
help_text=_("Select a page to limit the search on this page and sub pages contents."),
|
|
widget=SelectWithDisabled(),
|
|
)
|
|
title = forms.CharField(label=_('Custom Title'), required=False)
|
|
with_description = forms.BooleanField(label=_("Display page's description in results"), required=False)
|
|
|
|
class Meta:
|
|
model = SearchCell
|
|
fields = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs.pop('engine_slug')
|
|
super().__init__(*args, **kwargs)
|
|
used_slugs = [
|
|
e['slug'].replace('_text_page_', '') for e in self.instance.search_services
|
|
if e['slug'].startswith('_text_page_')]
|
|
pages_queryset = (
|
|
Page.objects
|
|
.filter(snapshot__isnull=True, sub_slug='')
|
|
.order_by('title'))
|
|
pages = Page.get_as_reordered_flat_hierarchy(pages_queryset)
|
|
pages_choices = [('', '---------')] + [
|
|
(x.id, {'label': '%s %s' % (u'\u00a0' * x.level * 2, x.title), 'disabled': x.slug in used_slugs})
|
|
for x in pages]
|
|
# if '_text' without page is already selected, page is required
|
|
if any(e['slug'] == '_text' for e in self.instance.search_services):
|
|
pages_choices.pop(0)
|
|
self.fields['selected_page'].required = True
|
|
self.fields['selected_page'].queryset = pages_queryset.exclude(slug__in=used_slugs)
|
|
self.fields['selected_page'].choices = pages_choices
|
|
|
|
def get_title(self):
|
|
return _('Add a "Page Contents" engine')
|
|
|
|
def get_slug(self):
|
|
if self.cleaned_data['selected_page'] is not None:
|
|
return '_text_page_%s' % self.cleaned_data['selected_page'].slug
|
|
return '_text'
|
|
|
|
|
|
class CardsEngineSettingsForm(forms.ModelForm):
|
|
selected_view = forms.ChoiceField(
|
|
label=_('Custom view'),
|
|
required=False,
|
|
)
|
|
without_user = forms.BooleanField(label=_('Ignore the logged-in user'), required=False)
|
|
title = forms.CharField(label=_('Custom Title'), required=False)
|
|
|
|
class Meta:
|
|
model = SearchCell
|
|
fields = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
all_engines = engines.get_engines()
|
|
self.engine_slug = kwargs.pop('engine_slug')
|
|
self.engine = all_engines.get(self.engine_slug, {})
|
|
super().__init__(*args, **kwargs)
|
|
self.fields['selected_view'].choices = [(None, _('- All cards -'))] + [
|
|
(v['id'], v['text']) for v in self.engine.get('custom_views', [])
|
|
]
|
|
|
|
def get_title(self):
|
|
return _('Add a "%s" engine') % self.engine.get('label')
|
|
|
|
def get_slug(self):
|
|
slug = self.engine_slug
|
|
if self.cleaned_data['without_user']:
|
|
slug = '%s__without-user__' % slug
|
|
if self.cleaned_data['selected_view']:
|
|
slug = '%s:%s' % (slug, self.cleaned_data['selected_view'])
|
|
return slug
|