Skip to content
Snippets Groups Projects
Commit eaf6be2a authored by Peter Fogg's avatar Peter Fogg
Browse files

Fix up quality errors in config model API.

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