From fd9afe42875d1525af836fa7fba37bcc37f02274 Mon Sep 17 00:00:00 2001 From: Oliver Kraitschy Date: Thu, 9 Nov 2023 16:20:00 +0100 Subject: [PATCH] [feature] Add field list_handling to AbstractTemplate model --- openwisp_controller/config/admin.py | 1 + openwisp_controller/config/base/base.py | 3 ++ openwisp_controller/config/base/template.py | 17 ++++++++++ .../migrations/0054_template_list_handling.py | 31 +++++++++++++++++++ .../migrations/0006_template_list_handling.py | 30 ++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 openwisp_controller/config/migrations/0054_template_list_handling.py create mode 100644 tests/openwisp2/sample_config/migrations/0006_template_list_handling.py diff --git a/openwisp_controller/config/admin.py b/openwisp_controller/config/admin.py index bf4b980ce..e3cb3dfe5 100644 --- a/openwisp_controller/config/admin.py +++ b/openwisp_controller/config/admin.py @@ -831,6 +831,7 @@ class TemplateAdmin(MultitenantAdminMixin, BaseConfigAdmin, SystemDefinedVariabl 'name', 'organization', 'type', + 'list_handling', 'backend', 'vpn', 'auto_cert', diff --git a/openwisp_controller/config/base/base.py b/openwisp_controller/config/base/base.py index db0ae3bad..338410b15 100644 --- a/openwisp_controller/config/base/base.py +++ b/openwisp_controller/config/base/base.py @@ -155,10 +155,13 @@ def get_backend_instance(self, template_instances=None, context=None, **kwargs): if template_instances is None: template_instances = self.templates.all() templates_list = list() + templates_list_handling = list() for t in template_instances: templates_list.append(t.config) + templates_list_handling.append(t.list_handling) context.update(t.get_context()) kwargs['templates'] = templates_list + kwargs['templates_list_handling'] = templates_list_handling # pass context to backend if get_context method is defined if hasattr(self, 'get_context'): context.update(self.get_context()) diff --git a/openwisp_controller/config/base/template.py b/openwisp_controller/config/base/template.py index 0c3f805bd..60243bfa0 100644 --- a/openwisp_controller/config/base/template.py +++ b/openwisp_controller/config/base/template.py @@ -15,6 +15,11 @@ from .base import BaseConfig TYPE_CHOICES = (('generic', _('Generic')), ('vpn', _('VPN-client'))) +LIST_HANDLING_CHOICES = ( + ('insert_at_beginning', _('Insert items at the beginning')), + ('append_at_end', _('Append items at the end')), + ('override', _('Override all items')), +) def default_auto_cert(): @@ -55,6 +60,18 @@ class AbstractTemplate(ShareableOrgMixinUniqueName, BaseConfig): db_index=True, help_text=_('template type, determines which features are available'), ) + list_handling = models.CharField( + _('list handling'), + max_length=20, + choices=LIST_HANDLING_CHOICES, + default='append_at_end', + db_index=True, + help_text=_( + 'list handling, determines how list items in this template' + ' are handled if the same list exists in a previously ' + 'applied template' + ), + ) default = models.BooleanField( _('enabled by default'), default=False, diff --git a/openwisp_controller/config/migrations/0054_template_list_handling.py b/openwisp_controller/config/migrations/0054_template_list_handling.py new file mode 100644 index 000000000..afabb240f --- /dev/null +++ b/openwisp_controller/config/migrations/0054_template_list_handling.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.7 on 2023-11-09 18:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("config", "0053_vpnclient_secret"), + ] + + operations = [ + migrations.AddField( + model_name="template", + name="list_handling", + field=models.CharField( + choices=[ + ("insert_at_beginning", "Insert items at the beginning"), + ("append_at_end", "Append items at the end"), + ("override", "Override all items"), + ], + db_index=True, + default="append_at_end", + help_text="list handling, determines how list items in this " + "template are handled if the same list exists in a previously" + " applied template", + max_length=20, + verbose_name="list handling", + ), + ), + ] diff --git a/tests/openwisp2/sample_config/migrations/0006_template_list_handling.py b/tests/openwisp2/sample_config/migrations/0006_template_list_handling.py new file mode 100644 index 000000000..57693bfce --- /dev/null +++ b/tests/openwisp2/sample_config/migrations/0006_template_list_handling.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.7 on 2023-11-13 18:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("sample_config", "0005_add_organizationalloweddevice"), + ] + + operations = [ + migrations.AddField( + model_name="template", + name="list_handling", + field=models.CharField( + choices=[ + ("insert_at_beginning", "Insert items at the beginning"), + ("append_at_end", "Append items at the end"), + ("override", "Override all items"), + ], + db_index=True, + default="append_at_end", + help_text="list handling, determines how list items in this template " + "are handled if the same list exists in a previously applied template", + max_length=20, + verbose_name="list handling", + ), + ), + ]