From 6b6ff36189e97efb3b463643018806e9ff8e14a8 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 11 Jun 2024 22:21:02 +0200 Subject: [PATCH] fixes #4475: Split thema into separate fields. --- src/catalogue/views.py | 27 +++++++++++++++++-- .../static/js/wiki/view_properties.js | 21 +++++++++++++++ src/wlxml/views.py | 12 ++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 2ed4216d..097c5493 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -1,8 +1,9 @@ # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +import json from django.apps import apps -from django.db.models import Prefetch +from django.db.models import Prefetch, Q from django.http import Http404, JsonResponse from django.urls import reverse from django.utils.formats import localize_input @@ -219,7 +220,29 @@ class ThemaChooser(Chooser): res = res[0]['sub'] return res - for thema in self.queryset.all(): + def apply_filter(filt): + if 'not' in filt: + return ~apply_filter(filt['not']) + if 'startswith' in filt: + q = None + for prefix in filt['startswith']: + q2 = Q(code__startswith=prefix) + if q: + q |= q2 + else: + q = q2 + return q + assert False + + qs = self.queryset + try: + filt = json.loads(request.GET.get('filter')) + except: + pass + else: + qs = qs.filter(apply_filter(filt)) + + for thema in qs: populate(thema) missing = list(getmissing(tree)) diff --git a/src/redakcja/static/js/wiki/view_properties.js b/src/redakcja/static/js/wiki/view_properties.js index 51f26d50..7101c5cf 100644 --- a/src/redakcja/static/js/wiki/view_properties.js +++ b/src/redakcja/static/js/wiki/view_properties.js @@ -139,6 +139,9 @@ rdfdesc.append('\n '); rdf.append('\n '); } + if (field.filter && field.filter.startswith) { + span.text(field.filter.startswith[0]); + } span.appendTo(rdfdesc); rdfdesc.append('\n '); @@ -230,6 +233,7 @@ $.ajax({ url: field.value_type.chooser.source, + data: field.filter ? {'filter': JSON.stringify(field.filter)} : '', success: function(data) { add_options(body, data, input.val()); } @@ -384,6 +388,18 @@ // lang } + applyFilter(filter, text) { + if (filter.not) { + return !this.applyFilter(filter.not, text) + } else if (filter.startswith) { + for (prefix of filter.startswith) { + if (text.startsWith(prefix)) + return true; + } + return false; + } + } + displayMetaProperty($fg) { let self = this; let ns = $fg.data('ns'); @@ -398,6 +414,11 @@ selector += "[x-ns='"+ns+"']"; } $(selector, self.$edited).each(function() { + if (field.filter) { + let text = $(this).text(); + if (!self.applyFilter(field.filter, text)) + return; + } self.addMetaInput( $('.c', $fg), field, diff --git a/src/wlxml/views.py b/src/wlxml/views.py index 3375b0d9..8c03e058 100644 --- a/src/wlxml/views.py +++ b/src/wlxml/views.py @@ -166,7 +166,17 @@ class MetaTagsView(View): ) ) ) - fields.append(d) + if d['name'] == 'thema': + d_loc = d.copy() + d_time = d.copy() + d['filter'] = {'not': {'startswith': ['1', '3']}} + d_loc['filter'] = {'startswith': ['1']} + d_loc['name'] += ' (miejsca)' + d_time['filter'] = {'startswith': ['3']} + d_time['name'] += ' (czas)' + fields.extend([d, d_loc, d_time]) + else: + fields.append(d) return HttpResponse( 'let META_FIELDS = ' + json.dumps(fields), -- 2.20.1