From 762120134a740fd9cbc50c0f52dfae4f8ca8360f Mon Sep 17 00:00:00 2001 From: Guido Longoni Date: Wed, 5 Jul 2023 16:57:38 +0200 Subject: [PATCH] opzione included_only ora attiva e funzionante --- django/contatti_app/admin.py | 28 +++++++++++++- django/contatti_app/drilldown_autocomplete.py | 37 ++++++++++++------- django/static/admin/js/autocomplete.js | 2 +- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/django/contatti_app/admin.py b/django/contatti_app/admin.py index 51163a9..146db9c 100644 --- a/django/contatti_app/admin.py +++ b/django/contatti_app/admin.py @@ -144,6 +144,30 @@ class RecapitoInline(StackedPolymorphicInline, DrillDownAutocompleteModelAdmin): class IndirizzoInline(StackedPolymorphicInline.Child, DrillDownAutocompleteModelAdmin): model = models.Indirizzo autocomplete_fields = ('dug','comune','cap','nazione',) + drilldown_autocomplete_fields = { + 'cap': { + 'linked': { + 'comune': 'comuni', + }, + 'reset_on_included': {}, + 'reset_on_excluded': {}, + 'reset_on_reset': {}, + 'autoupdate_on_reset': False, + 'autoselect_on_singleton': True, + 'included_only': True, + }, + 'comune': { + 'linked': { + 'cap': 'cap', + }, + 'reset_on_included': {}, + 'reset_on_excluded': {}, + 'reset_on_reset': {}, + 'autoupdate_on_reset': False, + 'autoselect_on_singleton': True, + 'included_only': False, + } + } class SedeInline(StackedPolymorphicInline.Child): model = models.Sede @@ -277,7 +301,7 @@ class IndirizzoAdmin(HiddenModel, DrillDownAutocompleteModelAdmin, RicercaOrdina 'reset_on_reset': {}, 'autoupdate_on_reset': False, 'autoselect_on_singleton': True, - 'included_only': False, + 'included_only': True, }, 'comune': { 'linked': { @@ -287,7 +311,7 @@ class IndirizzoAdmin(HiddenModel, DrillDownAutocompleteModelAdmin, RicercaOrdina 'reset_on_excluded': {}, 'reset_on_reset': {}, 'autoupdate_on_reset': False, - 'autoselect_on_singleton': False, + 'autoselect_on_singleton': True, 'included_only': False, } } diff --git a/django/contatti_app/drilldown_autocomplete.py b/django/contatti_app/drilldown_autocomplete.py index 1050789..599a6cc 100644 --- a/django/contatti_app/drilldown_autocomplete.py +++ b/django/contatti_app/drilldown_autocomplete.py @@ -31,22 +31,23 @@ class DrillDownAutocompleteJsonView(AutocompleteJsonView): source_field, to_field_name ) = super().process_request(request) - + if 'drilldown' in request.GET: try: drilldown_enabled = int(request.GET.get('drilldown')) except ValueError: raise PermissionDenied from e try: - drilldown_enabled = {1:True, 0:False}[drilldown_enabled] + drilldown_enabled = {1: True, 0: False}[drilldown_enabled] except KeyError: raise PermissionDenied from e self.drilldown_enabled = drilldown_enabled if drilldown_enabled: - if 'linkedfields' in request.GET: + if 'linkedfields' in request.GET: try: - linkedfields = json.loads(request.GET.get("linkedfields")) + linkedfields = json.loads( + request.GET.get("linkedfields")) except json.decoder.JSONDecodeError as e: raise PermissionDenied from e app_label = request.GET["app_label"] @@ -76,12 +77,17 @@ class DrillDownAutocompleteJsonView(AutocompleteJsonView): qs = super().get_queryset() if getattr(self, 'drilldown_enabled', False): if hasattr(self, 'linkedfields'): - if(self.drilldown_field['autoselect_on_singleton']): - self.autoselect = False + dd_field = self.drilldown_field drilldown_filter_conditions = Q(**self.drilldown_filter_data) - qs = qs.annotate(ddok=Max(Case(When(drilldown_filter_conditions, then=Value( - 1)), default=Value(0)))).order_by('-ddok', *qs.query.order_by) - if not getattr(self,'autoselect',True) and qs.filter(ddok=1).count() == 1: + if dd_field.get('autoselect_on_singleton', False): + self.autoselect = False + if dd_field.get('included_only', False): + qs = qs.filter(drilldown_filter_conditions).annotate( + ddok=Value(1)) + else: + qs = qs.annotate(ddok=Max(Case(When(drilldown_filter_conditions, then=Value( + 1)), default=Value(0)))).order_by('-ddok', *qs.query.order_by) + if not getattr(self, 'autoselect', True) and qs.filter(ddok=1).count() == 1: self.autoselect = True else: qs = qs.annotate(ddok=Value(1)) @@ -92,10 +98,10 @@ class DrillDownAutocompleteJsonView(AutocompleteJsonView): Convert the provided model object to a dictionary that is added to the results list. """ - out=super().serialize_result(obj, to_field_name) + out = super().serialize_result(obj, to_field_name) if hasattr(obj, 'ddok'): out['ddok'] = obj.ddok - if getattr(self,'autoselect',False) and obj.ddok == 1: + if getattr(self, 'autoselect', False) and obj.ddok == 1: out['autoselect'] = True return out @@ -110,6 +116,7 @@ class DrillDownAutocompleteSelect(AutocompleteSelect, DrillDownAutocompleteMixin class DrillDownAutocompleteModelAdmin(admin.options.BaseModelAdmin): drilldown_autocomplete_fields = dict() + class Media: css = { 'all': ('admin/css/drilldown_autocomplete.css',) @@ -127,10 +134,12 @@ class DrillDownAutocompleteModelAdmin(admin.options.BaseModelAdmin): if "widget" not in kwargs: daf = self.get_drilldown_autocomplete_fields(request) if db_field.name in daf: + attrs = { + "data-linkedfields": json.dumps(list(daf[db_field.name]['linked'].keys())), } + if daf[db_field.name].get('included_only',False): + attrs['data-included_only'] = 1 kwargs["widget"] = DrillDownAutocompleteSelect( - db_field, self.admin_site, attrs={ - "data-linkedfields": json.dumps(list(daf[db_field.name]['linked'].keys())), - }, using=db + db_field, self.admin_site, attrs=attrs, using=db ) return super().formfield_for_foreignkey(db_field, request, **kwargs) diff --git a/django/static/admin/js/autocomplete.js b/django/static/admin/js/autocomplete.js index 6e4ed4c..1e37f2a 100644 --- a/django/static/admin/js/autocomplete.js +++ b/django/static/admin/js/autocomplete.js @@ -51,7 +51,7 @@ } container.classList.add(styleClass); if (item.hasOwnProperty('autoselect')) { - if ($(element).select2('data').length == 0) { + if ((element.dataset.included_only ?? false) || $(element).select2('data').length == 0) { $(element).select2("trigger", "select", { data: item })