Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
[enhancement] Enable validation of context variables in templates #175
Browse files Browse the repository at this point in the history
Fixed the issue by removing uneccessary work which @okraits began by
doing after he extracted the gsoc2019 branch.

Fixes #175
  • Loading branch information
NoumbissiValere authored and atb00ker committed May 16, 2020
1 parent 9d6644f commit 5a0b141
Show file tree
Hide file tree
Showing 13 changed files with 758 additions and 622 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -62,3 +62,4 @@ target/
local_settings.py
*.db
*.tar.gz
Pipfile
7 changes: 3 additions & 4 deletions .travis.yml
Expand Up @@ -13,7 +13,6 @@ env:
branches:
only:
- master
- gsoc2019

before_install:
- pip install -U pip wheel setuptools
Expand All @@ -27,9 +26,9 @@ install:

script:
- |
openwisp-utils-qa-checks \
--migration-path ./django_netjsonconfig/migrations/ \
--migration-module django_netjsonconfig
openwisp-utils-qa-checks \
--migration-path ./django_netjsonconfig/migrations/ \
--migration-module django_netjsonconfig
- coverage run --source=django_netjsonconfig runtests.py

after_success:
Expand Down
1 change: 1 addition & 0 deletions django_netjsonconfig/base/admin.py
Expand Up @@ -347,6 +347,7 @@ class AbstractTemplateAdmin(BaseConfigAdmin):
'vpn',
'auto_cert',
'tags',
'default_values',
'default',
'config',
'created',
Expand Down
10 changes: 8 additions & 2 deletions django_netjsonconfig/base/base.py
Expand Up @@ -138,15 +138,21 @@ def get_backend_instance(self, template_instances=None):
"""
backend = self.backend_class
kwargs = {'config': self.get_config()}
context = {}
# determine if we can pass templates
# expecting a many2many relationship
if hasattr(self, 'templates'):
if template_instances is None:
template_instances = self.templates.all()
kwargs['templates'] = [t.config for t in template_instances]
templates_list = list()
for t in template_instances:
templates_list.append(t.config)
context.update(t.get_context())
kwargs['templates'] = templates_list
# pass context to backend if get_context method is defined
if hasattr(self, 'get_context'):
kwargs['context'] = self.get_context()
context.update(self.get_context())
kwargs['context'] = context
backend_instance = backend(**kwargs)
# remove accidentally duplicated files when combining config and templates
# this may happen if a device uses multiple VPN client templates
Expand Down
20 changes: 14 additions & 6 deletions django_netjsonconfig/base/template.py
@@ -1,10 +1,12 @@
from collections import OrderedDict
from copy import copy

from django.contrib.admin.models import ADDITION, LogEntry
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField
from taggit.managers import TaggableManager

from ..settings import DEFAULT_AUTO_CERT
Expand Down Expand Up @@ -57,6 +59,15 @@ class AbstractTemplate(BaseConfig):
'be automatically managed behind the scenes '
'for each configuration using this template, '
'valid only for the VPN type'))
default_values = JSONField(_('Default Values'),
default=dict,
blank=True,
help_text=_('A dictionary containing the default '
'values for the variables used by this '
'template; these default variables will '
'be used during schema validation.'),
load_kwargs={'object_pairs_hook': OrderedDict},
dump_kwargs={'indent': 4})
__template__ = True

class Meta:
Expand Down Expand Up @@ -110,12 +121,9 @@ def clean(self, *args, **kwargs):
self.config = self.vpn.auto_client(auto_cert=self.auto_cert)

def get_context(self):
c = {
'id': str(self.id),
'name': self.name,
}
c.update(super().get_context())
return c
context = self.default_values or {}
context.update(super().get_context())
return context

def clone(self, user):
clone = copy(self)
Expand Down
20 changes: 20 additions & 0 deletions django_netjsonconfig/migrations/0044_template_default_values.py
@@ -0,0 +1,20 @@
# Generated by Django 3.0.6 on 2020-05-15 15:18

import collections
from django.db import migrations
import jsonfield.fields


class Migration(migrations.Migration):

dependencies = [
('django_netjsonconfig', '0043_add_indexes_on_ip_fields'),
]

operations = [
migrations.AddField(
model_name='template',
name='default_values',
field=jsonfield.fields.JSONField(blank=True, default=dict, dump_kwargs={'ensure_ascii': False, 'indent': 4}, help_text='A dictionary containing the default values for the variables used by this template; these default variables will be used during schema validation.', load_kwargs={'object_pairs_hook': collections.OrderedDict}, verbose_name='Default Values'),
),
]

0 comments on commit 5a0b141

Please sign in to comment.