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

Commit

Permalink
[controller] update last_ip during checksum #17
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
nemesifier committed Feb 11, 2016
1 parent fd57b72 commit 5d04bd7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 7 additions & 3 deletions django_netjsonconfig/controller/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.views.decorators.csrf import csrf_exempt

from ..models import Config
from ..utils import ControllerResponse, get_config_or_404, forbid_unallowed, send_config
from ..utils import (ControllerResponse, get_config_or_404, forbid_unallowed, send_config,
update_last_ip)
from ..settings import REGISTRATION_ENABLED, SHARED_SECRET, BACKENDS


Expand All @@ -12,8 +13,11 @@ def checksum(request, pk):
returns configuration checksum
"""
config = get_config_or_404(pk)
return (forbid_unallowed(request.GET, 'key', config.key) or
ControllerResponse(config.checksum, content_type='text/plain'))
bad_request = forbid_unallowed(request.GET, 'key', config.key)
if bad_request:
return bad_request
update_last_ip(config, request)
return ControllerResponse(config.checksum, content_type='text/plain')


@require_http_methods(['GET'])
Expand Down
8 changes: 5 additions & 3 deletions django_netjsonconfig/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def _check_header(self, response):
self.assertEqual(response['X-Openwisp-Controller'], 'true')

def test_checksum(self):
d = self._create_config()
response = self.client.get(reverse('controller:checksum', args=[d.pk]), {'key': d.key})
self.assertEqual(response.content.decode(), d.checksum)
c = self._create_config()
response = self.client.get(reverse('controller:checksum', args=[c.pk]), {'key': c.key})
self.assertEqual(response.content.decode(), c.checksum)
self._check_header(response)
c.refresh_from_db()
self.assertIsNotNone(c.last_ip)

def test_checksum_bad_uuid(self):
d = self._create_config()
Expand Down
15 changes: 12 additions & 3 deletions django_netjsonconfig/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@ def send_file(filename, contents):

def send_config(config, request):
"""
sends config in http response and logs ip
sends config in http response and updates last_ip
"""
config.last_ip = request.META.get('REMOTE_ADDR')
config.save()
update_last_ip(config, request)
return send_file(filename='{0}.tar.gz'.format(config.name),
contents=config.generate().getvalue())


def update_last_ip(config, request):
"""
updates last_ip if necessary
"""
latest_ip = request.META.get('REMOTE_ADDR')
if config.last_ip != latest_ip:
config.last_ip = latest_ip
config.save()


def forbid_unallowed(params, param, allowed_values=None):
value = params.get(param)
if not value:
Expand Down

0 comments on commit 5d04bd7

Please sign in to comment.