Skip to content

Commit 90b0235

Browse files
committed
feat(*): update django from 1.7 to 1.8
1 parent 4646771 commit 90b0235

15 files changed

Lines changed: 125 additions & 130 deletions

rootfs/api/fields.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,8 @@
33
"""
44

55
from __future__ import unicode_literals
6-
from uuid import uuid4
7-
8-
from django import forms
96
from django.db import models
107

118

12-
class UuidField(models.CharField):
13-
"""A univerally unique ID field."""
14-
15-
description = __doc__
16-
17-
def __init__(self, *args, **kwargs):
18-
kwargs.setdefault('auto_created', True)
19-
kwargs.setdefault('editable', False)
20-
kwargs.setdefault('max_length', 32)
21-
kwargs.setdefault('unique', True)
22-
super(UuidField, self).__init__(*args, **kwargs)
23-
24-
def db_type(self, connection=None):
25-
"""Return the database column type for a UuidField."""
26-
if connection and 'postgres' in connection.vendor:
27-
return 'uuid'
28-
else:
29-
return "char({})".format(self.max_length)
30-
31-
def pre_save(self, model_instance, add):
32-
"""Initialize an empty field with a new UUID before it is saved."""
33-
value = getattr(model_instance, self.get_attname(), None)
34-
if not value and add:
35-
uuid = str(uuid4())
36-
setattr(model_instance, self.get_attname(), uuid)
37-
return uuid
38-
else:
39-
return super(UuidField, self).pre_save(model_instance, add)
40-
41-
def formfield(self, **kwargs):
42-
"""Tell forms how to represent this UuidField."""
43-
kwargs.update({
44-
'form_class': forms.CharField,
45-
'max_length': self.max_length,
46-
})
47-
return super(UuidField, self).formfield(**kwargs)
48-
49-
50-
try:
51-
from south.modelsinspector import add_introspection_rules
52-
# Tell the South schema migration tool to handle our custom fields.
53-
add_introspection_rules([], [r'^api\.fields\.UuidField'])
54-
except ImportError: # pragma: no cover
9+
class UuidField(models.UUIDField):
5510
pass
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import migrations, models
5+
import api.models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('api', '0001_initial'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='app',
17+
name='id',
18+
field=models.SlugField(max_length=24, unique=True, null=True, validators=[api.models.validate_id_is_docker_compatible, api.models.validate_reserved_names]),
19+
),
20+
migrations.AlterField(
21+
model_name='app',
22+
name='uuid',
23+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
24+
),
25+
migrations.AlterField(
26+
model_name='build',
27+
name='uuid',
28+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
29+
),
30+
migrations.AlterField(
31+
model_name='config',
32+
name='uuid',
33+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
34+
),
35+
migrations.AlterField(
36+
model_name='container',
37+
name='uuid',
38+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
39+
),
40+
migrations.AlterField(
41+
model_name='key',
42+
name='uuid',
43+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
44+
),
45+
migrations.AlterField(
46+
model_name='push',
47+
name='uuid',
48+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
49+
),
50+
migrations.AlterField(
51+
model_name='release',
52+
name='uuid',
53+
field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True),
54+
),
55+
]

rootfs/api/models.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
import time
1515
import json
16+
import uuid
1617
from threading import Thread
1718

1819
from django.conf import settings
@@ -28,7 +29,7 @@
2829
import requests
2930
from rest_framework.authtoken.models import Token
3031

31-
from api import fields, utils, exceptions
32+
from api import utils, exceptions
3233
from registry import publish_release
3334
from utils import dict_diff, dict_merge, fingerprint
3435

@@ -129,7 +130,12 @@ class Meta:
129130
class UuidAuditedModel(AuditedModel):
130131
"""Add a UUID primary key to an :class:`AuditedModel`."""
131132

132-
uuid = fields.UuidField('UUID', primary_key=True)
133+
uuid = models.UUIDField('UUID',
134+
default=uuid.uuid4,
135+
primary_key=True,
136+
editable=False,
137+
auto_created=True,
138+
unique=True)
133139

134140
class Meta:
135141
"""Mark :class:`UuidAuditedModel` as abstract."""
@@ -155,7 +161,6 @@ class Meta:
155161
def select_app_name(self):
156162
"""Select a unique randomly generated app name"""
157163
name = utils.generate_app_name()
158-
159164
while App.objects.filter(id=name).exists():
160165
name = utils.generate_app_name()
161166

@@ -767,7 +772,7 @@ def save(self, **kwargs):
767772
return super(Build, self).save(**kwargs)
768773

769774
def __str__(self):
770-
return "{0}-{1}".format(self.app.id, self.uuid[:7])
775+
return "{0}-{1}".format(self.app.id, str(self.uuid)[:7])
771776

772777

773778
@python_2_unicode_compatible
@@ -790,7 +795,7 @@ class Meta:
790795
unique_together = (('app', 'uuid'),)
791796

792797
def __str__(self):
793-
return "{}-{}".format(self.app.id, self.uuid[:7])
798+
return "{}-{}".format(self.app.id, str(self.uuid)[:7])
794799

795800
def save(self, **kwargs):
796801
"""merge the old config with the new"""

rootfs/api/tests/__init__.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
21
from __future__ import unicode_literals
32
import logging
43

5-
from django.test.client import RequestFactory, Client
6-
from django.test.simple import DjangoTestSuiteRunner
4+
from django.test.runner import DiscoverRunner
75
import requests
86

97

10-
# add patch support to built-in django test client
11-
12-
def construct_patch(self, path, data='',
13-
content_type='application/octet-stream', **extra):
14-
"""Construct a PATCH request."""
15-
return self.generic('PATCH', path, data, content_type, **extra)
16-
17-
18-
def send_patch(self, path, data='', content_type='application/octet-stream',
19-
follow=False, **extra):
20-
"""Send a resource to the server using PATCH."""
21-
# FIXME: figure out why we need to reimport Client (otherwise NoneType)
22-
from django.test.client import Client # @Reimport
23-
response = super(Client, self).patch(
24-
path, data=data, content_type=content_type, **extra)
25-
if follow:
26-
response = self._handle_redirects(response, **extra)
27-
return response
28-
29-
30-
RequestFactory.patch = construct_patch
31-
Client.patch = send_patch
32-
33-
34-
class SilentDjangoTestSuiteRunner(DjangoTestSuiteRunner):
8+
class SilentDjangoTestSuiteRunner(DiscoverRunner):
359
"""Prevents api log messages from cluttering the console during tests."""
3610

3711
def run_tests(self, test_labels, extra_tests=None, **kwargs):

rootfs/api/tests/test_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_app_override_id(self):
7979
response = self.client.post('/v2/apps', json.dumps(body),
8080
content_type='application/json',
8181
HTTP_AUTHORIZATION='token {}'.format(self.token))
82-
self.assertContains(response, 'This field must be unique.', status_code=400)
82+
self.assertContains(response, 'App with this id already exists.', status_code=400)
8383
return response
8484

8585
@mock.patch('requests.get')

rootfs/api/tests/test_build.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_build(self):
4848
response = self.client.post(url, json.dumps(body), content_type='application/json',
4949
HTTP_AUTHORIZATION='token {}'.format(self.token))
5050
self.assertEqual(response.status_code, 201)
51-
build_id = response.data['uuid']
51+
build_id = str(response.data['uuid'])
5252
build1 = response.data
5353
self.assertEqual(response.data['image'], body['image'])
5454
# read the build
@@ -203,7 +203,7 @@ def test_build_str(self):
203203
self.assertEqual(response.status_code, 201)
204204
build = Build.objects.get(uuid=response.data['uuid'])
205205
self.assertEqual(str(build), "{}-{}".format(
206-
response.data['app'], response.data['uuid'][:7]))
206+
response.data['app'], str(response.data['uuid'])[:7]))
207207

208208
@mock.patch('requests.post', mock_status_ok)
209209
def test_admin_can_create_builds_on_other_apps(self):
@@ -225,7 +225,7 @@ def test_admin_can_create_builds_on_other_apps(self):
225225
self.assertEqual(response.status_code, 201)
226226
build = Build.objects.get(uuid=response.data['uuid'])
227227
self.assertEqual(str(build), "{}-{}".format(
228-
response.data['app'], response.data['uuid'][:7]))
228+
response.data['app'], str(response.data['uuid'])[:7]))
229229

230230
@mock.patch('requests.post', mock_status_ok)
231231
def test_unauthorized_user_cannot_modify_build(self):

rootfs/api/tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def test_config_str(self):
250250
"""Test the text representation of a node."""
251251
config5 = self.test_config()
252252
config = Config.objects.get(uuid=config5['uuid'])
253-
self.assertEqual(str(config), "{}-{}".format(config5['app'], config5['uuid'][:7]))
253+
self.assertEqual(str(config), "{}-{}".format(config5['app'], str(config5['uuid'])[:7]))
254254

255255
@mock.patch('requests.post', mock_status_ok)
256256
def test_valid_config_keys(self):

rootfs/api/tests/test_hooks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,9 @@ def test_build_hook_dockerfile(self):
166166
build = {'username': 'autotest', 'app': app_id}
167167
url = '/v2/hooks/builds'.format(**locals())
168168
SHA = 'ecdff91c57a0b9ab82e89634df87e293d259a3aa'
169-
DOCKERFILE = """
170-
FROM busybox
171-
CMD /bin/true
172-
"""
169+
DOCKERFILE = """FROM busybox
170+
CMD /bin/true"""
171+
173172
body = {'receive_user': 'autotest',
174173
'receive_repo': app_id,
175174
'image': '{app_id}:v2'.format(**locals()),

rootfs/api/urls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
from __future__ import unicode_literals
66

77
from django.conf import settings
8-
from django.conf.urls import include, patterns, url
8+
from django.conf.urls import include, url
99

1010
from api import routers, views
11+
from rest_framework.authtoken.views import obtain_auth_token as views_obtain_auth_token
1112

1213

1314
router = routers.ApiRouter()
1415

1516
# Add the generated REST URLs and login/logout endpoint
16-
urlpatterns = patterns(
17-
'',
17+
urlpatterns = [
1818
url(r'^', include(router.urls)),
1919
# application release components
2020
url(r"^apps/(?P<id>{})/config/?".format(settings.APP_URL_REGEX),
@@ -86,7 +86,7 @@
8686
url(r'^auth/passwd/?',
8787
views.UserManagementViewSet.as_view({'post': 'passwd'})),
8888
url(r'^auth/login/',
89-
'rest_framework.authtoken.views.obtain_auth_token'),
89+
views_obtain_auth_token),
9090
url(r'^auth/tokens/',
9191
views.TokenManagementViewSet.as_view({'post': 'regenerate'})),
9292
# admin sharing
@@ -100,4 +100,4 @@
100100
views.CertificateViewSet.as_view({'get': 'list', 'post': 'create'})),
101101
# list users
102102
url(r'^users/', views.UserView.as_view({'get': 'list'})),
103-
)
103+
]

rootfs/api/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ def list(self, request, *args, **kwargs):
190190
instance = self.filter_queryset(queryset)
191191
page = self.paginate_queryset(instance)
192192
if page is not None:
193-
serializer = self.get_pagination_serializer(page)
194-
else:
195-
serializer = self.get_serializer(instance, many=True)
193+
serializer = self.get_serializer(page, many=True)
194+
return self.get_paginated_response(serializer.data)
195+
196+
serializer = self.get_serializer(instance, many=True)
197+
196198
return Response(serializer.data)
197199

198200
def post_save(self, app):

0 commit comments

Comments
 (0)