summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-10-27 11:55:34 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:20:59 +0100
commit183c1422d978e2b1cbb1d47e0a024dcee57ae3f9 (patch)
treecc39c3e69039af5bc7854602a4ec9c956e0b4316
parent62e05e799962d810804d0cd5f8d377ac8b434a01 (diff)
downloadIshtar-183c1422d978e2b1cbb1d47e0a024dcee57ae3f9.tar.bz2
Ishtar-183c1422d978e2b1cbb1d47e0a024dcee57ae3f9.zip
Syndication - search UI
-rw-r--r--ishtar_common/context_processors.py14
-rw-r--r--ishtar_common/templates/blocks/DataTables-external-sources.html14
-rw-r--r--ishtar_common/templates/blocks/DataTables.html40
-rw-r--r--ishtar_common/views_item.py29
-rw-r--r--ishtar_common/widgets.py2
-rw-r--r--ishtar_common/wizards.py14
6 files changed, 112 insertions, 1 deletions
diff --git a/ishtar_common/context_processors.py b/ishtar_common/context_processors.py
index 7fb1a486d..a03f6429b 100644
--- a/ishtar_common/context_processors.py
+++ b/ishtar_common/context_processors.py
@@ -22,6 +22,7 @@ from django.contrib.sites.models import Site
from ishtar_common.version import __version__
from ishtar_common.models import get_current_profile
+from ishtar_common.models_rest import ApiExternalSource, ApiKeyMatch
from bootstrap_datepicker.widgets import DatePicker
from .menus import Menu
@@ -47,6 +48,19 @@ def get_base_context(request):
menu.init()
request.session['MENU'] = menu
""" # menu is now in cache - put it back in session later?
+ if "EXTERNAL_SOURCES" not in request.session:
+ q = ApiExternalSource.objects
+ # TODO: check permissions
+ request.session["EXTERNAL_SOURCES"] = {}
+ if q.count():
+ for source in q.all():
+ request.session["EXTERNAL_SOURCES"][f"{source.id}-{source.name}"] = [
+ f"{app_label}-{model_name}"
+ for app_label, model_name in ApiKeyMatch.objects.values_list(
+ "search_model__app_label", "search_model__model"
+ ).distinct()
+ ]
+
current_action = None
if "CURRENT_ACTION" in request.session:
dct["CURRENT_ACTION"] = request.session["CURRENT_ACTION"]
diff --git a/ishtar_common/templates/blocks/DataTables-external-sources.html b/ishtar_common/templates/blocks/DataTables-external-sources.html
new file mode 100644
index 000000000..ce4d67ada
--- /dev/null
+++ b/ishtar_common/templates/blocks/DataTables-external-sources.html
@@ -0,0 +1,14 @@
+{% load i18n %}
+
+<div class="d-flex justify-content-center">
+<div class="btn-group btn-group-toggle" data-toggle="buttons">
+ <label class="btn btn-secondary active">
+ <input type="radio" name="_sources" id="default" autocomplete="off" checked> {% trans "Local" %}
+ </label>
+ {% for source_id, source_label in external_sources %}
+ <label class="btn btn-secondary">
+ <input type="radio" name="_sources" id="{{source_id}}" autocomplete="off"> {{source_label}}
+ </label>
+ {% endfor %}
+</div>
+</div>
diff --git a/ishtar_common/templates/blocks/DataTables.html b/ishtar_common/templates/blocks/DataTables.html
index f116d9573..135e2a51e 100644
--- a/ishtar_common/templates/blocks/DataTables.html
+++ b/ishtar_common/templates/blocks/DataTables.html
@@ -25,6 +25,9 @@
</div>
</div>
+{% if external_sources %}
+{% include "blocks/DataTables-external-sources.html" %}
+{% endif %}
{% if current_model.STATISTIC_MODALITIES or gallery or use_map %}
{% include "blocks/DataTables-tabs.html" %}
{% else %}
@@ -104,6 +107,15 @@ datatable_submit_search = function(not_submited){
datatable_{{sname}}.ajax.url(url);
datatable_{{sname}}.draw();
+{% if external_sources %}{% for source_id, source_label in external_sources %}
+ if (!not_submited){
+ url = "{{source}}?submited=1&" + data;
+ } else {
+ url = "{{source}}?" + data;
+ }
+ datatable_{{sname}}_{{source_id}}.ajax.url(url);
+ datatable_{{sname}}_{{source_id}}.draw();
+{% endfor %}{% endif %}
setTimeout( // wait for modal-progress to be loaded on the page
function(){
@@ -219,6 +231,34 @@ jQuery(document).ready(function(){
datatable_{{sname}}.on('deselect', dt_multi_enable_disable_submit_button);
{% endif %}
+{% if external_sources %}{% for source_id, source_label in external_sources %}
+ var base_external_source = "{{source}}";
+
+ if (default_search_vector){
+ base_source += "?search_vector=" + default_search_vector + "&submited=1";
+ }
+
+ datatable_options = {
+ "ajax": {
+ "url": base_external_source,
+ "dataSrc": function (json) {
+ if (!default_search_vector) manage_pinned_search("{{name}}", json);
+ update_submit_args();
+ setTimeout( // 50ms is waited on load so...
+ function(){
+ $('.modal-progress').modal('hide');
+ }, 500);
+ return json.rows;
+ }
+ },
+ "deferLoading": 0
+ };
+ $.extend(datatable_options, datatables_default);
+ if (datatables_i18n) datatable_options['language'] = datatables_i18n;
+ datatable_{{sname}}_{{source_id}} = jQuery("#grid_{{name}}_{{source_id}}").DataTable(datatable_options);
+
+{% endfor %}{% endif %}
+
{% if multiple %}
jQuery("#add_button_{{name}}").click(function (){
var mygrid = jQuery("#grid_{{name}}");
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py
index 151dc1ac9..9ce651f7d 100644
--- a/ishtar_common/views_item.py
+++ b/ishtar_common/views_item.py
@@ -8,6 +8,7 @@ import datetime
import json
import logging
import re
+import requests
import subprocess
from tempfile import NamedTemporaryFile
import unidecode
@@ -59,7 +60,7 @@ from ishtar_common.models import get_current_profile, GeneralType, SearchAltName
from ishtar_common.models_common import HistoryError
from .menus import Menu
-from . import models
+from . import models, models_rest
from archaeological_files.models import File
from archaeological_operations.models import (
Operation,
@@ -2342,3 +2343,29 @@ def get_item(
return HttpResponse("{}", content_type="text/plain")
return func
+
+
+def get_distant_item(model):
+ def func(
+ request,
+ data_type="json",
+ full=False,
+ force_own=False,
+ col_names=None,
+ no_link=False,
+ no_limit=False,
+ return_query=False,
+ **dct
+ ):
+ if not request.GET.get("external_source", None):
+ return HttpResponse("{}", content_type="text/plain")
+ # TODO: check permissions
+ try:
+ src = models_rest.ApiExternalSource.objects.get(
+ pk=request.GET["external_source"])
+ except models_rest.ApiExternalSource.DoesNotExist:
+ return HttpResponse("{}", content_type="text/plain")
+ url = src.url
+
+
+ return func
diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py
index 29165232a..05605a258 100644
--- a/ishtar_common/widgets.py
+++ b/ishtar_common/widgets.py
@@ -1191,6 +1191,7 @@ class DataTable(Select2Media, forms.RadioSelect):
self.user = None
self.gallery = gallery
self.map = map
+ self.external_sources = None
if self.col_prefix and not self.col_prefix.endswith("__"):
self.col_prefix += "__"
@@ -1318,6 +1319,7 @@ class DataTable(Select2Media, forms.RadioSelect):
"loading": str(_("Loading...")),
"remove": str(_("Remove")),
"sname": name.replace("-", ""),
+ "external_sources": self.external_sources,
"gallery": self.gallery,
"use_map": self.map() if callable(self.map) else self.map,
"multiple": self.multiple,
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py
index a22901953..2c2d4f6f9 100644
--- a/ishtar_common/wizards.py
+++ b/ishtar_common/wizards.py
@@ -100,6 +100,8 @@ EXTRA_FORM_MODALS = ["container", "warehouse", "person", "organization"]
class IshtarWizard(NamedUrlWizardView):
+ model = None
+
def get_form_kwargs(self, step=None):
kwargs = super(IshtarWizard, self).get_form_kwargs(step)
if (
@@ -117,6 +119,18 @@ class IshtarWizard(NamedUrlWizardView):
if hasattr(form, "extra_form_modals")
else EXTRA_FORM_MODALS
)
+ if (
+ self.model
+ and "pk" in form.fields
+ and self.request.session.get("EXTERNAL_SOURCES", {})
+ ):
+ model_name = f"{self.model._meta.app_label}-{self.model.__name__.lower()}"
+ context["external_sources"] = [
+ source.split("-")
+ for source in self.request.session["EXTERNAL_SOURCES"]
+ if model_name in self.request.session["EXTERNAL_SOURCES"][source]
+ ]
+ form.fields["pk"].widget.external_sources = context["external_sources"]
open_item_id = self.request.GET.get("open_item", None)
if open_item_id and self.model and getattr(self.model, "SHOW_URL", None):