Skip to content

Commit

Permalink
[django] Generalize url/key logic
Browse files Browse the repository at this point in the history
  • Loading branch information
waleko committed Jan 22, 2020
1 parent f31721d commit 2964628
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,7 @@ branches:
- master

install:
- # TODO: this is temporary, remove it when django-freeradius 0.1 is released
# TODO: this is temporary, remove it when django-freeradius 0.1 is released
- pip install https://github.com/openwisp/django-freeradius/tarball/master
- pip install $DJANGO
- python setup.py -q develop
Expand Down
7 changes: 4 additions & 3 deletions openwisp_radius/migrations/0002_initial_openwisp_radius.py
Expand Up @@ -7,12 +7,13 @@
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
import openwisp_utils.base
import openwisp_users.mixins
import openwisp_utils.utils
from django.conf import settings
from django.db import migrations, models
from openwisp_radius.migrations import add_default_organization

import openwisp_radius.models
import openwisp_users.mixins

class Migration(migrations.Migration):
"""
Expand Down Expand Up @@ -77,7 +78,7 @@ class Migration(migrations.Migration):
name='OrganizationRadiusSettings',
fields=[
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('token', models.CharField(default=openwisp_radius.models.generate_token, max_length=32, validators=[django.core.validators.RegexValidator(re.compile('^[^\\s/\\.]+$'), code='invalid', message='Key must not contain spaces, dots or slashes.')])),
('token', openwisp_utils.base.KeyField(default=openwisp_utils.utils.get_random_key, help_text=None, max_length=32, validators=[django.core.validators.RegexValidator(re.compile('^[^\\s/\\.]+$'), code='invalid', message='This value must not contain spaces, dots or slashes.')])),
('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='openwisp_users.Organization', verbose_name='organization')),
],
options={
Expand Down
25 changes: 3 additions & 22 deletions openwisp_radius/models.py
@@ -1,10 +1,6 @@
import uuid

from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.core.validators import RegexValidator, _lazy_re_compile
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.translation import ugettext_lazy as _
from django_freeradius.base.models import (AbstractNas, AbstractRadiusAccounting, AbstractRadiusBatch,
AbstractRadiusCheck, AbstractRadiusGroup, AbstractRadiusGroupCheck,
Expand All @@ -14,6 +10,7 @@

from openwisp_users.mixins import OrgMixin
from openwisp_users.models import OrganizationUser
from openwisp_utils.base import KeyField, UUIDModel


class RadiusCheck(OrgMixin, AbstractRadiusCheck):
Expand Down Expand Up @@ -120,28 +117,12 @@ class Meta(AbstractRadiusToken.Meta):
swappable = swappable_setting('openwisp_radius', 'RadiusToken')


key_validator = RegexValidator(
_lazy_re_compile('^[^\s/\.]+$'),
message=_('Key must not contain spaces, dots or slashes.'),
code='invalid',
)


def generate_token():
return get_random_string(length=32)


class OrganizationRadiusSettings(models.Model):
id = models.UUIDField(default=uuid.uuid4,
primary_key=True,
editable=False)
class OrganizationRadiusSettings(UUIDModel):
organization = models.OneToOneField('openwisp_users.Organization',
verbose_name=_('organization'),
related_name='radius_settings',
on_delete=models.CASCADE)
token = models.CharField(max_length=32,
validators=[key_validator],
default=generate_token)
token = KeyField(max_length=32)

class Meta:
verbose_name = _('Organization radius settings')
Expand Down
2 changes: 1 addition & 1 deletion openwisp_radius/tests/tests.py
Expand Up @@ -634,7 +634,7 @@ def test_bad_token(self):
rad.full_clean()
except ValidationError as e:
self.assertEqual(e.message_dict['token'][0],
'Key must not contain spaces, dots or slashes.')
'This value must not contain spaces, dots or slashes.')

def test_cache(self):
# clear cache set from previous tests
Expand Down
2 changes: 1 addition & 1 deletion requirements-test.txt
@@ -1,7 +1,7 @@
coveralls
responses
freezegun
openwisp-utils[qa]>=0.3.2
openwisp-utils[qa]>=0.4.1
djangorestframework-jwt
django-cors-headers>=2.5.2
mock>=2.0.0
Expand Down

0 comments on commit 2964628

Please sign in to comment.