Commit eaf6be2a by Peter Fogg

Fix up quality errors in config model API.

parent 96b49759
...@@ -94,7 +94,7 @@ class ConfigurationModel(models.Model): ...@@ -94,7 +94,7 @@ class ConfigurationModel(models.Model):
Clear the cached value when saving a new configuration entry Clear the cached value when saving a new configuration entry
""" """
# Always create a new entry, instead of updating an existing model # Always create a new entry, instead of updating an existing model
self.pk = None self.pk = None # pylint: disable=invalid-name
super(ConfigurationModel, self).save(*args, **kwargs) super(ConfigurationModel, self).save(*args, **kwargs)
cache.delete(self.cache_key_name(*[getattr(self, key) for key in self.KEY_FIELDS])) cache.delete(self.cache_key_name(*[getattr(self, key) for key in self.KEY_FIELDS]))
if self.KEY_FIELDS: if self.KEY_FIELDS:
......
...@@ -95,7 +95,7 @@ class ConfigurationModelTests(TestCase): ...@@ -95,7 +95,7 @@ class ConfigurationModelTests(TestCase):
self.assertEqual(rows[1].string_field, 'first') self.assertEqual(rows[1].string_field, 'first')
self.assertEqual(rows[1].is_active, False) self.assertEqual(rows[1].is_active, False)
def test_always_insert(self, mock_cache): def test_always_insert(self, __):
config = ExampleConfig(changed_by=self.user, string_field='first') config = ExampleConfig(changed_by=self.user, string_field='first')
config.save() config.save()
config.string_field = 'second' config.string_field = 'second'
...@@ -297,7 +297,11 @@ class KeyedConfigurationModelTests(TestCase): ...@@ -297,7 +297,11 @@ class KeyedConfigurationModelTests(TestCase):
@ddt.ddt @ddt.ddt
class ConfigurationModelAPITests(TestCase): class ConfigurationModelAPITests(TestCase):
"""
Tests for the configuration model API.
"""
def setUp(self): def setUp(self):
super(ConfigurationModelAPITests, self).setUp()
self.factory = APIRequestFactory() self.factory = APIRequestFactory()
self.user = User.objects.create_user( self.user = User.objects.create_user(
username='test_user', username='test_user',
...@@ -319,7 +323,7 @@ class ConfigurationModelAPITests(TestCase): ...@@ -319,7 +323,7 @@ class ConfigurationModelAPITests(TestCase):
request = self.factory.post('/config/ExampleConfig', {"string_field": "string_value"}) request = self.factory.post('/config/ExampleConfig', {"string_field": "string_value"})
request.user = self.user request.user = self.user
response = self.current_view(request) __ = self.current_view(request)
self.assertEquals("string_value", ExampleConfig.current().string_field) self.assertEquals("string_value", ExampleConfig.current().string_field)
self.assertEquals(self.user, ExampleConfig.current().changed_by) self.assertEquals(self.user, ExampleConfig.current().changed_by)
...@@ -333,13 +337,14 @@ class ConfigurationModelAPITests(TestCase): ...@@ -333,13 +337,14 @@ class ConfigurationModelAPITests(TestCase):
response = self.current_view(request) response = self.current_view(request)
self.assertEquals(201, response.status_code) self.assertEquals(201, response.status_code)
self.assertEquals(i+1, ExampleConfig.objects.all().count()) self.assertEquals(i + 1, ExampleConfig.objects.all().count())
self.assertEquals(str(i), ExampleConfig.current().string_field) self.assertEquals(str(i), ExampleConfig.current().string_field)
def test_get_current(self): def test_get_current(self):
request = self.factory.get('/config/ExampleConfig') request = self.factory.get('/config/ExampleConfig')
request.user = self.user request.user = self.user
response = self.current_view(request) response = self.current_view(request)
# pylint: disable=no-member
self.assertEquals('', response.data['string_field']) self.assertEquals('', response.data['string_field'])
self.assertEquals(10, response.data['int_field']) self.assertEquals(10, response.data['int_field'])
self.assertEquals(None, response.data['changed_by']) self.assertEquals(None, response.data['changed_by'])
......
"""
API view to allow manipulation of configuration models.
"""
from rest_framework.generics import CreateAPIView, RetrieveAPIView from rest_framework.generics import CreateAPIView, RetrieveAPIView
from rest_framework.permissions import DjangoModelPermissions from rest_framework.permissions import DjangoModelPermissions
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
...@@ -5,6 +8,7 @@ from rest_framework.serializers import ModelSerializer ...@@ -5,6 +8,7 @@ from rest_framework.serializers import ModelSerializer
class ReadableOnlyByAuthors(DjangoModelPermissions): class ReadableOnlyByAuthors(DjangoModelPermissions):
"""Only allow access by users with `add` permissions on the model."""
perms_map = DjangoModelPermissions.perms_map.copy() perms_map = DjangoModelPermissions.perms_map.copy()
perms_map['GET'] = perms_map['OPTIONS'] = perms_map['HEAD'] = perms_map['POST'] perms_map['GET'] = perms_map['OPTIONS'] = perms_map['HEAD'] = perms_map['POST']
...@@ -32,7 +36,9 @@ class ConfigurationModelCurrentAPIView(CreateAPIView, RetrieveAPIView): ...@@ -32,7 +36,9 @@ class ConfigurationModelCurrentAPIView(CreateAPIView, RetrieveAPIView):
def get_serializer_class(self): def get_serializer_class(self):
if self.serializer_class is None: if self.serializer_class is None:
class AutoConfigModelSerializer(ModelSerializer): class AutoConfigModelSerializer(ModelSerializer):
class Meta: """Serializer class for configuration models."""
class Meta(object):
"""Meta information for AutoConfigModelSerializer."""
model = self.model model = self.model
self.serializer_class = AutoConfigModelSerializer self.serializer_class = AutoConfigModelSerializer
...@@ -41,4 +47,4 @@ class ConfigurationModelCurrentAPIView(CreateAPIView, RetrieveAPIView): ...@@ -41,4 +47,4 @@ class ConfigurationModelCurrentAPIView(CreateAPIView, RetrieveAPIView):
def perform_create(self, serializer): def perform_create(self, serializer):
# Set the requesting user as the one who is updating the configuration # Set the requesting user as the one who is updating the configuration
serializer.save(changed_by = self.request.user) serializer.save(changed_by=self.request.user)
"""
Fixture to manipulate configuration models.
"""
import requests import requests
import re import re
import json import json
...@@ -79,11 +82,11 @@ class ConfigModelFixture(object): ...@@ -79,11 +82,11 @@ class ConfigModelFixture(object):
if response.ok: if response.ok:
# auto_auth returns information about the newly created user # auto_auth returns information about the newly created user
# capture this so it can be used by by the testcases. # capture this so it can be used by by the testcases.
user_pattern = re.compile('Logged in user {0} \({1}\) with password {2} and user_id {3}'.format( user_pattern = re.compile(r'Logged in user {0} \({1}\) with password {2} and user_id {3}'.format(
'(?P<username>\S+)', '(?P<email>[^\)]+)', '(?P<password>\S+)', '(?P<user_id>\d+)')) r'(?P<username>\S+)', r'(?P<email>[^\)]+)', r'(?P<password>\S+)', r'(?P<user_id>\d+)'))
user_matches = re.match(user_pattern, response.text) user_matches = re.match(user_pattern, response.text)
if user_matches: if user_matches:
self.user = user_matches.groupdict() self.user = user_matches.groupdict() # pylint: disable=attribute-defined-outside-init
return session return session
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment