Skip to content

Commit eef9f65

Browse files
committed
A lot of styling/autolinting
1 parent 6befd6a commit eef9f65

File tree

10 files changed

+627
-486
lines changed

10 files changed

+627
-486
lines changed

readthedocs/api/base.py

+76-67
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
"""API resources"""
2-
from __future__ import absolute_import
3-
from builtins import object
4-
import logging
1+
# -*- coding: utf-8 -*-
2+
"""API resources."""
3+
from __future__ import (
4+
absolute_import, division, print_function, unicode_literals)
5+
56
import json
6-
import redis
7+
import logging
8+
from builtins import object
79

8-
from django.contrib.auth.models import User
10+
import redis
911
from django.conf.urls import url
10-
from django.shortcuts import get_object_or_404
12+
from django.contrib.auth.models import User
1113
from django.core.cache import cache
12-
14+
from django.shortcuts import get_object_or_404
1315
from tastypie import fields
1416
from tastypie.authorization import DjangoAuthorization
15-
from tastypie.constants import ALL_WITH_RELATIONS, ALL
17+
from tastypie.constants import ALL, ALL_WITH_RELATIONS
18+
from tastypie.http import HttpApplicationError, HttpCreated
1619
from tastypie.resources import ModelResource
17-
from tastypie.http import HttpCreated, HttpApplicationError
1820
from tastypie.utils import dict_strip_unicode_keys, trailing_slash
1921

2022
from readthedocs.builds.constants import LATEST
2123
from readthedocs.builds.models import Version
2224
from readthedocs.core.utils import trigger_build
23-
from readthedocs.projects.models import Project, ImportedFile
25+
from readthedocs.projects.models import ImportedFile, Project
2426

25-
from .utils import SearchMixin, PostAuthentication
27+
from .utils import PostAuthentication, SearchMixin
2628

2729
log = logging.getLogger(__name__)
2830

@@ -41,8 +43,8 @@ class Meta(object):
4143
authorization = DjangoAuthorization()
4244
excludes = ['path', 'featured', 'programming_language']
4345
filtering = {
44-
"users": ALL_WITH_RELATIONS,
45-
"slug": ALL_WITH_RELATIONS,
46+
'users': ALL_WITH_RELATIONS,
47+
'slug': ALL_WITH_RELATIONS,
4648
}
4749

4850
def get_object_list(self, request):
@@ -63,28 +65,32 @@ def post_list(self, request, **kwargs):
6365
If a new resource is created, return ``HttpCreated`` (201 Created).
6466
"""
6567
deserialized = self.deserialize(
66-
request, request.body,
67-
format=request.META.get('CONTENT_TYPE', 'application/json')
68+
request,
69+
request.body,
70+
format=request.META.get('CONTENT_TYPE', 'application/json'),
6871
)
6972

7073
# Force this in an ugly way, at least should do "reverse"
71-
deserialized["users"] = ["/api/v1/user/%s/" % request.user.id]
72-
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request)
74+
deserialized['users'] = ['/api/v1/user/%s/' % request.user.id]
75+
bundle = self.build_bundle(
76+
data=dict_strip_unicode_keys(deserialized), request=request)
7377
self.is_valid(bundle)
7478
updated_bundle = self.obj_create(bundle, request=request)
7579
return HttpCreated(location=self.get_resource_uri(updated_bundle))
7680

7781
def sync_versions(self, request, **kwargs):
7882
"""
79-
Sync the version data in the repo (on the build server) with what we have in the database.
83+
Sync the version data in the repo (on the build server) with what we
84+
have in the database.
8085
8186
Returns the identifiers for the versions that have been deleted.
8287
"""
8388
project = get_object_or_404(Project, pk=kwargs['pk'])
8489
try:
8590
post_data = self.deserialize(
86-
request, request.body,
87-
format=request.META.get('CONTENT_TYPE', 'application/json')
91+
request,
92+
request.body,
93+
format=request.META.get('CONTENT_TYPE', 'application/json'),
8894
)
8995
data = json.loads(post_data)
9096
self.method_check(request, allowed=['post'])
@@ -104,17 +110,20 @@ def sync_versions(self, request, **kwargs):
104110

105111
def prepend_urls(self):
106112
return [
107-
url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name,
108-
self.wrap_view('get_schema'), name="api_get_schema"),
109-
url(r"^(?P<resource_name>%s)/search%s$" % (
110-
self._meta.resource_name, trailing_slash()),
111-
self.wrap_view('get_search'), name="api_get_search"),
112-
url(r"^(?P<resource_name>%s)/(?P<pk>\d+)/sync_versions%s$" % (
113-
self._meta.resource_name, trailing_slash()),
114-
self.wrap_view('sync_versions'), name="api_sync_versions"),
115-
url((r"^(?P<resource_name>%s)/(?P<slug>[a-z-_]+)/$")
116-
% self._meta.resource_name, self.wrap_view('dispatch_detail'),
117-
name="api_dispatch_detail"),
113+
url(
114+
r'^(?P<resource_name>%s)/schema/$' % self._meta.resource_name,
115+
self.wrap_view('get_schema'), name='api_get_schema'),
116+
url(
117+
r'^(?P<resource_name>%s)/search%s$' %
118+
(self._meta.resource_name, trailing_slash()),
119+
self.wrap_view('get_search'), name='api_get_search'),
120+
url(
121+
r'^(?P<resource_name>%s)/(?P<pk>\d+)/sync_versions%s$' %
122+
(self._meta.resource_name, trailing_slash()),
123+
self.wrap_view('sync_versions'), name='api_sync_versions'),
124+
url((r'^(?P<resource_name>%s)/(?P<slug>[a-z-_]+)/$') %
125+
self._meta.resource_name, self.wrap_view('dispatch_detail'),
126+
name='api_dispatch_detail'),
118127
]
119128

120129

@@ -131,9 +140,9 @@ class Meta(object):
131140
authentication = PostAuthentication()
132141
authorization = DjangoAuthorization()
133142
filtering = {
134-
"project": ALL_WITH_RELATIONS,
135-
"slug": ALL_WITH_RELATIONS,
136-
"active": ALL,
143+
'project': ALL_WITH_RELATIONS,
144+
'slug': ALL_WITH_RELATIONS,
145+
'active': ALL,
137146
}
138147

139148
def get_object_list(self, request):
@@ -149,19 +158,19 @@ def build_version(self, request, **kwargs):
149158

150159
def prepend_urls(self):
151160
return [
152-
url(r"^(?P<resource_name>%s)/schema/$"
153-
% self._meta.resource_name,
154-
self.wrap_view('get_schema'),
155-
name="api_get_schema"),
156-
url(r"^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+[a-z0-9-_]+)/$" # noqa
161+
url(
162+
r'^(?P<resource_name>%s)/schema/$' % self._meta.resource_name,
163+
self.wrap_view('get_schema'), name='api_get_schema'),
164+
url(
165+
r'^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+[a-z0-9-_]+)/$' # noqa
157166
% self._meta.resource_name,
158167
self.wrap_view('dispatch_list'),
159-
name="api_version_list"),
160-
url((r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+[a-z0-9-_]+)/(?P"
161-
r"<version_slug>[a-z0-9-_.]+)/build/$")
162-
% self._meta.resource_name,
163-
self.wrap_view('build_version'),
164-
name="api_version_build_slug"),
168+
name='api_version_list'),
169+
url((
170+
r'^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+[a-z0-9-_]+)/(?P'
171+
r'<version_slug>[a-z0-9-_.]+)/build/$') %
172+
self._meta.resource_name, self.wrap_view('build_version'),
173+
name='api_version_build_slug'),
165174
]
166175

167176

@@ -182,18 +191,17 @@ class Meta(object):
182191

183192
def prepend_urls(self):
184193
return [
185-
url(r"^(?P<resource_name>%s)/schema/$" %
186-
self._meta.resource_name,
187-
self.wrap_view('get_schema'),
188-
name="api_get_schema"),
189-
url(r"^(?P<resource_name>%s)/search%s$" %
194+
url(
195+
r'^(?P<resource_name>%s)/schema/$' % self._meta.resource_name,
196+
self.wrap_view('get_schema'), name='api_get_schema'),
197+
url(
198+
r'^(?P<resource_name>%s)/search%s$' %
190199
(self._meta.resource_name, trailing_slash()),
191-
self.wrap_view('get_search'),
192-
name="api_get_search"),
193-
url(r"^(?P<resource_name>%s)/anchor%s$" %
200+
self.wrap_view('get_search'), name='api_get_search'),
201+
url(
202+
r'^(?P<resource_name>%s)/anchor%s$' %
194203
(self._meta.resource_name, trailing_slash()),
195-
self.wrap_view('get_anchor'),
196-
name="api_get_anchor"),
204+
self.wrap_view('get_anchor'), name='api_get_anchor'),
197205
]
198206

199207
def get_anchor(self, request, **__):
@@ -204,12 +212,14 @@ def get_anchor(self, request, **__):
204212
query = request.GET.get('q', '')
205213
try:
206214
redis_client = cache.get_client(None)
207-
redis_data = redis_client.keys("*redirects:v4*%s*" % query)
215+
redis_data = redis_client.keys('*redirects:v4*%s*' % query)
208216
except (AttributeError, redis.exceptions.ConnectionError):
209217
redis_data = []
210218
# -2 because http:
211-
urls = [''.join(data.split(':')[6:]) for data in redis_data
212-
if 'http://' in data]
219+
urls = [
220+
''.join(data.split(':')[6:]) for data in redis_data
221+
if 'http://' in data
222+
]
213223
object_list = {'objects': urls}
214224

215225
self.log_throttled_access(request)
@@ -230,12 +240,11 @@ class Meta(object):
230240

231241
def prepend_urls(self):
232242
return [
233-
url(r"^(?P<resource_name>%s)/schema/$" %
234-
self._meta.resource_name,
235-
self.wrap_view('get_schema'),
236-
name="api_get_schema"),
237-
url(r"^(?P<resource_name>%s)/(?P<username>[a-z-_]+)/$" %
238-
self._meta.resource_name,
239-
self.wrap_view('dispatch_detail'),
240-
name="api_dispatch_detail"),
243+
url(
244+
r'^(?P<resource_name>%s)/schema/$' % self._meta.resource_name,
245+
self.wrap_view('get_schema'), name='api_get_schema'),
246+
url(
247+
r'^(?P<resource_name>%s)/(?P<username>[a-z-_]+)/$' %
248+
self._meta.resource_name, self.wrap_view('dispatch_detail'),
249+
name='api_dispatch_detail'),
241250
]

0 commit comments

Comments
 (0)