Skip to content

Commit

Permalink
Merge e27a573 into 0face74
Browse files Browse the repository at this point in the history
  • Loading branch information
NoumbissiValere committed Dec 16, 2020
2 parents 0face74 + e27a573 commit 15bd665
Show file tree
Hide file tree
Showing 20 changed files with 304 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -55,6 +55,7 @@ target/

# editors
*.komodoproject
.vscode

# other
*.DS_Store*
Expand Down
35 changes: 23 additions & 12 deletions .travis.yml
@@ -1,31 +1,42 @@
language: python
sudo: false
cache: pip
dist: focal

python:
- "3.4"
- "2.7"
- "3.8"

env:
- DJANGO="django>=1.11,<1.12"
- DJANGO="django>=2.0,<2.1"
- DJANGO="django~=3.1"

matrix:
exclude:
- python: "2.7"
env: DJANGO="django>=2.0,<2.1"
addons:
apt:
packages:
- sqlite3
- fping
- gdal-bin
- libproj-dev
- libgeos-dev
- libspatialite-dev
- spatialite-bin
- libsqlite3-mod-spatialite


services:
- redis-server

branches:
only:
- master

install:
before_install:
- pip install -U "pip==20.2.4" wheel setuptools
- pip install $DJANGO
- python setup.py -q develop
- pip install -r requirements-test.txt

install:
- pip install -e .

script:
- openwisp-utils-qa-checks --skip-checkmigrations
- coverage run --source=owm_legacy runtests.py

after_success:
Expand Down
78 changes: 62 additions & 16 deletions README.rst
Expand Up @@ -3,17 +3,31 @@ django-owm-legacy

.. image:: https://travis-ci.org/openwisp/django-owm-legacy.svg
:target: https://travis-ci.org/openwisp/django-owm-legacy
:alt: CI build status

.. image:: https://coveralls.io/repos/openwisp/django-owm-legacy/badge.svg
:target: https://coveralls.io/r/openwisp/django-owm-legacy
:alt: Test Coverage

.. image:: https://requires.io/github/openwisp/django-owm-legacy/requirements.svg?branch=master
:target: https://requires.io/github/openwisp/django-owm-legacy/requirements/?branch=master
:alt: Requirements Status

.. image:: https://img.shields.io/gitter/room/nwjs/nw.js.svg
:target: https://gitter.im/openwisp/general
:alt: Chat

.. image:: https://badge.fury.io/py/django-owm-legacy.svg
:target: http://badge.fury.io/py/django-owm-legacy

.. image:: https://pepy.tech/badge/django-own-legacy
:target: https://pepy.tech/project/django-own-legacy
:alt: Downloads

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://pypi.org/project/black/
:alt: code style: black

------------

Legacy features of OpenWISP Manager reimplemented in django for `OpenWISP2
Expand Down Expand Up @@ -54,19 +68,46 @@ If you want to contribute, install your cloned fork:
Setup (integrate in an existing django project)
-----------------------------------------------

Add ``django_netjsonconfig``, ``sortedm2m`` and ``owm_legacy`` to ``INSTALLED_APPS``:
Add ``openwisp_controller`` and ``owm_legacy`` to ``INSTALLED_APPS`` as follow:

.. code-block:: python
INSTALLED_APPS = [
# other apps
'django_netjsonconfig',
# ...
'django.contrib.sites',
# allauth
'allauth',
'allauth.account',
'django_extensions',
# openwisp2 modules
'openwisp_controller.config',
'openwisp_controller.pki',
'openwisp_controller.geo',
'openwisp_controller.connection',
'openwisp_users',
# openwisp2 admin theme
# (must be loaded here)
'openwisp_utils.admin_theme',
'django.contrib.admin',
'django.forms',
# other dependencies
'sortedm2m',
'reversion',
'owm_legacy'
'leaflet',
'flat_json_widget',
'owm_legacy',
# ...
]
Other settings needed in ``settings.py``:

.. code-block:: python
EXTENDED_APPS = ('django_x509', 'django_loci')
AUTH_USER_MODEL = 'openwisp_users.User'
SITE_ID = 1
Your ``urls.py`` should look like the following:

.. code-block:: python
Expand All @@ -80,8 +121,8 @@ Your ``urls.py`` should look like the following:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('django_netjsonconfig.controller.urls', namespace='controller')),
url(r'^', include('owm_legacy.urls', namespace='owm')),
url(r'^', include('openwisp_controller.urls', namespace='controller')),
url(r'^', include('owm_legacy.urls', namespace='owm_legacy')),
]
urlpatterns += staticfiles_urlpatterns()
Expand All @@ -93,7 +134,14 @@ Install sqlite:

.. code-block:: shell
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt install -y sqlite3 libsqlite3-dev openssl libssl-dev
sudo apt install -y gdal-bin libproj-dev libgeos-dev libspatialite-dev libsqlite3-mod-spatialite
Launch Redis:

.. code-block:: shell
docker-compose up -d redis
Install your forked repo:

Expand All @@ -117,6 +165,12 @@ Create database:
./manage.py migrate
./manage.py createsuperuser
Launch celery worker (for background jobs):

.. code-block:: shell
celery -A openwisp2 worker -l info
Launch development server:

.. code-block:: shell
Expand Down Expand Up @@ -149,15 +203,7 @@ checksums and download configuration archives.
Contributing
------------

1. Announce your intentions in the `OpenWISP Mailing List <https://groups.google.com/d/forum/openwisp>`_
2. Fork this repo and install it
3. Follow `PEP8, Style Guide for Python Code`_
4. Write code
5. Write tests for your code
6. Ensure all tests pass
7. Ensure test coverage is not under 90%
8. Document your changes
9. Send pull request
Please refer to the `OpenWISP contributing guidelines <http://openwisp.io/docs/developer/contributing.html>`_.

.. _PEP8, Style Guide for Python Code: http://www.python.org/dev/peps/pep-0008/

Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,8 @@
version: "3"

services:
redis:
image: redis:5.0-alpine
ports:
- "6379:6379"
entrypoint: redis-server --appendonly yes
2 changes: 1 addition & 1 deletion owm_legacy/__init__.py
@@ -1,4 +1,4 @@
VERSION = (0, 4, 1, 'final')
VERSION = (0, 5, 1, 'final')
__version__ = VERSION # alias


Expand Down
5 changes: 3 additions & 2 deletions owm_legacy/settings.py
@@ -1,4 +1,5 @@
from django.conf import settings

ALLOWED_SUBNETS = getattr(settings, 'OWM_LEGACY_ALLOWED_SUBNETS', ['10.8.0.0/16',
'127.0.0.1/32'])
ALLOWED_SUBNETS = getattr(
settings, 'OWM_LEGACY_ALLOWED_SUBNETS', ['10.8.0.0/16', '127.0.0.1/32']
)
45 changes: 30 additions & 15 deletions owm_legacy/tests.py
@@ -1,53 +1,68 @@
from time import sleep

from django.urls import reverse
from django.test import TestCase
from django.urls import reverse
from openwisp_controller.config.models import Config, Device
from openwisp_controller.config.tests import CreateConfigMixin
from openwisp_users.tests.utils import TestOrganizationMixin

from django_netjsonconfig.models import Config, Device
from django_netjsonconfig.tests import CreateConfigMixin
from owm_legacy.settings import ALLOWED_SUBNETS


class TestOwmLegacy(CreateConfigMixin, TestCase):
class TestOwmLegacy(CreateConfigMixin, TestOrganizationMixin, TestCase):
"""
tests for owm_legacy
"""

config_model = Config
device_model = Device

def test_get_config_md5(self):
c = self._create_config()
response = self.client.get(reverse('owm:get_config_md5', args=[c.mac_address]))
self.assertEqual(response['Content-Disposition'], 'attachment; filename={0}'.format(self.TEST_MAC_ADDRESS))
response = self.client.get(
reverse('owm_legacy:get_config_md5', args=[c.mac_address])
)
self.assertEqual(
response['Content-Disposition'],
'attachment; filename={0}'.format(self.TEST_MAC_ADDRESS),
)
self.assertEqual(len(response.content), 32)
checksum1 = response.content
sleep(1)
response = self.client.get(reverse('owm:get_config_md5', args=[c.mac_address]))
response = self.client.get(
reverse('owm_legacy:get_config_md5', args=[c.mac_address])
)
checksum2 = response.content
self.assertEqual(checksum1, checksum2)
c.refresh_from_db()
self.assertIsNotNone(c.last_ip)
self.assertIsNotNone(c.device.last_ip)

def test_get_config(self):
d = self._create_device(name='test')
c = self._create_config(device=d)
response = self.client.get(reverse('owm:get_config', args=[c.mac_address]))
self.assertEqual(response['Content-Disposition'], 'attachment; filename=test.tar.gz')
response = self.client.get(
reverse('owm_legacy:get_config', args=[c.mac_address])
)
self.assertEqual(
response['Content-Disposition'], 'attachment; filename=test.tar.gz'
)

def test_last_ip(self):
c = self._create_config()
response = self.client.get(reverse('owm:get_config', args=[c.mac_address]))
self.client.get(reverse('owm_legacy:get_config', args=[c.mac_address]))
c.refresh_from_db()
self.assertIsNotNone(c.last_ip)
self.assertIsNotNone(c.device.last_ip)

def test_status(self):
c = self._create_config()
response = self.client.get(reverse('owm:get_config', args=[c.mac_address]))
self.client.get(reverse('owm_legacy:get_config', args=[c.mac_address]))
c.refresh_from_db()
self.assertEqual(c.status, 'running')
self.assertEqual(c.status, 'modified')

def test_forbidden_ip(self):
ALLOWED_SUBNETS.remove('127.0.0.1/32')
response = self.client.get(reverse('owm:get_config', args=['00:11:22:33:44:55']))
response = self.client.get(
reverse('owm_legacy:get_config', args=['00:11:22:33:44:55'])
)
self.assertEqual(response.status_code, 403)
ALLOWED_SUBNETS.append('127.0.0.1/32')
10 changes: 5 additions & 5 deletions owm_legacy/urls.py
Expand Up @@ -5,10 +5,10 @@
app_name = "owm_legacy"

urlpatterns = [
url(r'^get_config/(?P<mac_address>[^/^.]+).md5$',
url(
r'^get_config/(?P<mac_address>[^/^.]+).md5$',
views.get_config_md5,
name='get_config_md5'),
url(r'^get_config/(?P<mac_address>[^/^.]+)$',
views.get_config,
name='get_config'),
name='get_config_md5',
),
url(r'^get_config/(?P<mac_address>[^/^.]+)$', views.get_config, name='get_config'),
]
25 changes: 9 additions & 16 deletions owm_legacy/views.py
@@ -1,28 +1,21 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.shortcuts import get_object_or_404

from django_netjsonconfig.utils import send_config, send_file, update_last_ip
from openwisp_controller.config.models import Config
from openwisp_controller.config.utils import (
send_device_config,
send_file,
update_last_ip,
)

from .utils import forbid_unallowed

if 'django_netjsonconfig' in settings.INSTALLED_APPS:
from django_netjsonconfig.models import Config
elif 'openwisp_controller.config' in settings.INSTALLED_APPS: # pragma: nocover
from openwisp_controller.config.models import Config
else: # pragma: nocover
raise ImproperlyConfigured('django-owm-legacy depends on django-netjsonconfig or '
'openwisp_controller.config, but neither is present '
'in settings.INSTALLED_APPS')


def get_config_md5(request, mac_address):
"""
returns md5 of configuration
"""
forbid_unallowed(request)
config = get_object_or_404(Config, device__mac_address__iexact=mac_address)
update_last_ip(config, request)
update_last_ip(config.device, request)
return send_file(mac_address, config.checksum)


Expand All @@ -32,5 +25,5 @@ def get_config(request, mac_address):
"""
forbid_unallowed(request)
config = get_object_or_404(Config, device__mac_address__iexact=mac_address)
config.set_status_running(save=False)
return send_config(config, request)
config.set_status_modified(save=False)
return send_device_config(config, request)
3 changes: 2 additions & 1 deletion requirements-test.txt
@@ -1,2 +1,3 @@
coveralls
openwisp-utils[qa]>=0.4.1
openwisp-utils[qa]~=0.7.0
django-redis
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1 +1 @@
django-netjsonconfig>=0.8.0,<0.9.0
openwisp-controller~=0.8
3 changes: 2 additions & 1 deletion runtests.py
Expand Up @@ -5,10 +5,11 @@
import sys

sys.path.insert(0, "tests")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "openwisp2.settings")

if __name__ == "__main__":
from django.core.management import execute_from_command_line

args = sys.argv
args.insert(1, "test")
args.insert(2, "owm_legacy")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -60,5 +60,5 @@ def get_install_requires():
'Topic :: System :: Networking',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
]
],
)

0 comments on commit 15bd665

Please sign in to comment.