Skip to content

Commit

Permalink
[feature] Added support for template tags openwisp#3
Browse files Browse the repository at this point in the history
Closes openwisp#3
  • Loading branch information
nemesifier committed Apr 26, 2017
1 parent 0b11395 commit 26f3eb6
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ branches:

# command to install requirements
install:
# temporary
- pip install https://github.com/openwisp/django-netjsonconfig/tarball/master
- python setup.py -q develop
- pip install -r requirements-test.txt

Expand Down
8 changes: 8 additions & 0 deletions openwisp_controller/config/controller/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db.models import Q

from django_netjsonconfig.controller.generics import (BaseChecksumView,
BaseDownloadConfigView,
BaseRegisterView,
Expand Down Expand Up @@ -55,6 +57,12 @@ def init_object(self, **kwargs):
kwargs['organization'] = self.organization
return super(RegisterView, self).init_object(**kwargs)

def get_template_queryset(self, config):
queryset = super(RegisterView, self).get_template_queryset(config)
# filter templates of the same organization or shared templates
return queryset.filter(Q(organization=self.organization) |
Q(organization=None))


checksum = ChecksumView.as_view()
download_config = DownloadConfigView.as_view()
Expand Down
55 changes: 55 additions & 0 deletions openwisp_controller/config/migrations/0003_template_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-21 13:46
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import taggit.managers
import uuid


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('config', '0002_config_settings_uuid'),
]

operations = [
migrations.CreateModel(
name='TaggedTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('object_id', models.UUIDField(db_index=True, verbose_name='Object id')),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='config_taggedtemplate_tagged_items', to='contenttypes.ContentType', verbose_name='Content type')),
],
options={
'verbose_name': 'Tagged item',
'abstract': False,
'verbose_name_plural': 'Tags',
},
),
migrations.CreateModel(
name='TemplateTag',
fields=[
('name', models.CharField(max_length=100, unique=True, verbose_name='Name')),
('slug', models.SlugField(max_length=100, unique=True, verbose_name='Slug')),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
],
options={
'verbose_name': 'Tag',
'abstract': False,
'verbose_name_plural': 'Tags',
},
),
migrations.AddField(
model_name='taggedtemplate',
name='tag',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='config_taggedtemplate_items', to='config.TemplateTag'),
),
migrations.AddField(
model_name='template',
name='tags',
field=taggit.managers.TaggableManager(blank=True, help_text='A comma-separated list of template tags, may be used to ease auto configuration with specific settings (eg: 4G, mesh, WDS, VPN, ecc.)', through='config.TaggedTemplate', to='config.TemplateTag', verbose_name='Tags'),
),
]
27 changes: 27 additions & 0 deletions openwisp_controller/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from sortedm2m.fields import SortedManyToManyField
from taggit.managers import TaggableManager

from django_netjsonconfig.base.config import TemplatesVpnMixin as BaseMixin
from django_netjsonconfig.base.config import (AbstractConfig, get_random_key,
key_validator, sortedm2m__str__)
from django_netjsonconfig.base.tag import (AbstractTaggedTemplate,
AbstractTemplateTag)
from django_netjsonconfig.base.template import AbstractTemplate
from django_netjsonconfig.base.vpn import AbstractVpn, AbstractVpnClient
from openwisp_users.mixins import OrgMixin, ShareableOrgMixin
Expand Down Expand Up @@ -85,10 +88,34 @@ class Meta(AbstractConfig.Meta):
Config.templates.through.__str__ = sortedm2m__str__


class TemplateTag(AbstractTemplateTag):
"""
openwisp-controller TemplateTag model
"""
class Meta(AbstractTemplateTag.Meta):
abstract = False


class TaggedTemplate(AbstractTaggedTemplate):
"""
openwisp-controller TaggedTemplate model
"""
tag = models.ForeignKey('config.TemplateTag',
related_name='%(app_label)s_%(class)s_items',
on_delete=models.CASCADE)

class Meta(AbstractTaggedTemplate.Meta):
abstract = False


class Template(ShareableOrgMixin, AbstractTemplate):
"""
openwisp-controller Template model
"""
tags = TaggableManager(through='config.TaggedTemplate', blank=True,
help_text=_('A comma-separated list of template tags, may be used '
'to ease auto configuration with specific settings (eg: '
'4G, mesh, WDS, VPN, ecc.)'))
vpn = models.ForeignKey('config.Vpn',
verbose_name=_('VPN'),
blank=True,
Expand Down
31 changes: 27 additions & 4 deletions openwisp_controller/config/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from openwisp_users.tests.utils import TestOrganizationMixin

from . import CreateConfigTemplateMixin
from ..models import Config, OrganizationConfigSettings
from ..models import Config, OrganizationConfigSettings, Template

TEST_MACADDR = '00:11:22:33:44:55'
TEST_MACADDR_NAME = TEST_MACADDR.replace(':', '-')
Expand All @@ -19,14 +19,15 @@ class TestController(CreateConfigTemplateMixin, TestOrganizationMixin,
tests for django_netjsonconfig.controller
"""
config_model = Config
template_model = Template

def _create_org(self, **kwargs):
def _create_org(self, shared_secret=TEST_ORG_SHARED_SECRET, **kwargs):
org = super(TestController, self)._create_org(**kwargs)
OrganizationConfigSettings.objects.create(organization=org,
shared_secret=TEST_ORG_SHARED_SECRET)
shared_secret=shared_secret)
return org

def test_register(self):
def test_register(self, **kwargs):
org = self._create_org()
response = self.client.post(REGISTER_URL, {
'secret': TEST_ORG_SHARED_SECRET,
Expand All @@ -39,6 +40,28 @@ def test_register(self):
organization=org).count()
self.assertEqual(count, 1)

def test_register_template_tags(self):
org1 = self._create_org(name='org1')
t1 = self._create_template(name='t1', organization=org1)
t1.tags.add('mesh')
t_shared = self._create_template(name='t-shared')
t_shared.tags.add('mesh')
org2 = self._create_org(name='org2', shared_secret='org2secret')
t2 = self._create_template(name='mesh', organization=org2)
t2.tags.add('mesh')
response = self.client.post(REGISTER_URL, {
'secret': TEST_ORG_SHARED_SECRET,
'name': TEST_MACADDR_NAME,
'mac_address': TEST_MACADDR,
'backend': 'netjsonconfig.OpenWrt',
'tags': 'mesh'
})
self.assertEqual(response.status_code, 201)
c = Config.objects.filter(mac_address=TEST_MACADDR,
organization=org1).first()
self.assertEqual(c.templates.filter(name=t1.name).count(), 1)
self.assertEqual(c.templates.filter(name=t_shared.name).count(), 1)

def test_register_400(self):
self._create_org()
# missing secret
Expand Down
18 changes: 18 additions & 0 deletions openwisp_controller/config/tests/test_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase

from django_netjsonconfig.tests import CreateTemplateMixin
from openwisp_users.tests.utils import TestOrganizationMixin

from ..models import Template


class TestTag(TestOrganizationMixin, CreateTemplateMixin, TestCase):
"""
tests for Tag model
"""
template_model = Template

def test_tag(self):
t = self._create_template(organization=self._create_org())
t.tags.add('mesh')
self.assertEqual(t.tags.filter(name='mesh').count(), 1)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
django-netjsonconfig>=0.4.2,<0.6.0
django-netjsonconfig>=0.5.8,<0.7.0
openwisp-users<0.2

0 comments on commit 26f3eb6

Please sign in to comment.