combo/combo/apps/search/manager_views.py

109 lines
4.4 KiB
Python

# combo - content management system
# Copyright (C) 2014-2020 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.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from combo.apps.search.forms import TextEngineSettingsForm, CardsEngineSettingsForm
from combo.apps.search.models import SearchCell
from combo.data.models import PageSnapshot
def page_search_cell_add_engine(request, page_pk, cell_reference, engine_slug):
cell = get_object_or_404(SearchCell, pk=cell_reference.split('-')[1], page=page_pk)
def add_slug(slug, **options):
if slug in cell.available_engines or slug.startswith('_text_page') or slug.startswith('cards:'):
if not cell._search_services or not cell._search_services.get('data'):
cell._search_services = {'data': []}
if not cell._search_services.get('options'):
cell._search_services['options'] = {}
cell._search_services['data'].append(slug)
cell._search_services['options'][slug] = options
cell.save()
PageSnapshot.take(cell.page, request=request, comment=_('changed cell "%s"') % cell)
return HttpResponseRedirect('%s#cell-%s' % (
reverse('combo-manager-page-view', kwargs={'pk': page_pk}),
cell_reference))
if engine_slug != '_text' and not engine_slug.startswith('cards:'):
# add engine without intermediary form and popup
return add_slug(engine_slug)
form_class = TextEngineSettingsForm
if engine_slug.startswith('cards:'):
form_class = CardsEngineSettingsForm
if request.method == 'POST':
form = form_class(instance=cell, data=request.POST, engine_slug=engine_slug)
if form.is_valid():
kwargs = {
'title': form.cleaned_data['title'],
}
for key in ['without_user', 'with_description']:
if form.cleaned_data.get(key):
kwargs[key] = True
return add_slug(
form.get_slug(),
**kwargs)
else:
form = form_class(instance=cell, engine_slug=engine_slug)
context = {
'form': form,
'cell': cell,
}
return render(request, 'combo/manager/add-engine-form.html', context)
def page_search_cell_delete_engine(request, page_pk, cell_reference, engine_slug):
cell = get_object_or_404(SearchCell, pk=cell_reference.split('-')[1], page=page_pk)
if engine_slug in cell._search_services.get('data'):
cell._search_services['data'].remove(engine_slug)
cell.save()
PageSnapshot.take(cell.page, request=request, comment=_('changed cell "%s"') % cell)
return HttpResponseRedirect('%s#cell-%s' % (
reverse('combo-manager-page-view', kwargs={'pk': page_pk}),
cell_reference))
def search_engines_order(request, page_pk, cell_reference):
cell = get_object_or_404(SearchCell, pk=cell_reference.split('-')[1], page=page_pk)
if not cell._search_services.get('data'):
return HttpResponse(status=204)
engines = []
for i, engine_slug in enumerate(cell._search_services['data']):
try:
new_order = int(request.GET.get('pos_' + str(engine_slug)))
except TypeError:
new_order = 0
engines.append((new_order, engine_slug))
ordered_engines = [a[1] for a in sorted(engines, key=lambda a: a[0])]
if ordered_engines != cell._search_services['data']:
cell._search_services['data'] = ordered_engines
cell.save()
PageSnapshot.take(cell.page, request=request, comment=_('changed cell "%s"') % cell)
return HttpResponse(status=204)