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
+
5
6
import json
6
- import redis
7
+ import logging
8
+ from builtins import object
7
9
8
- from django . contrib . auth . models import User
10
+ import redis
9
11
from django .conf .urls import url
10
- from django .shortcuts import get_object_or_404
12
+ from django .contrib . auth . models import User
11
13
from django .core .cache import cache
12
-
14
+ from django . shortcuts import get_object_or_404
13
15
from tastypie import fields
14
16
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
16
19
from tastypie .resources import ModelResource
17
- from tastypie .http import HttpCreated , HttpApplicationError
18
20
from tastypie .utils import dict_strip_unicode_keys , trailing_slash
19
21
20
22
from readthedocs .builds .constants import LATEST
21
23
from readthedocs .builds .models import Version
22
24
from readthedocs .core .utils import trigger_build
23
- from readthedocs .projects .models import Project , ImportedFile
25
+ from readthedocs .projects .models import ImportedFile , Project
24
26
25
- from .utils import SearchMixin , PostAuthentication
27
+ from .utils import PostAuthentication , SearchMixin
26
28
27
29
log = logging .getLogger (__name__ )
28
30
@@ -41,8 +43,8 @@ class Meta(object):
41
43
authorization = DjangoAuthorization ()
42
44
excludes = ['path' , 'featured' , 'programming_language' ]
43
45
filtering = {
44
- " users" : ALL_WITH_RELATIONS ,
45
- " slug" : ALL_WITH_RELATIONS ,
46
+ ' users' : ALL_WITH_RELATIONS ,
47
+ ' slug' : ALL_WITH_RELATIONS ,
46
48
}
47
49
48
50
def get_object_list (self , request ):
@@ -63,28 +65,32 @@ def post_list(self, request, **kwargs):
63
65
If a new resource is created, return ``HttpCreated`` (201 Created).
64
66
"""
65
67
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' ),
68
71
)
69
72
70
73
# 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 )
73
77
self .is_valid (bundle )
74
78
updated_bundle = self .obj_create (bundle , request = request )
75
79
return HttpCreated (location = self .get_resource_uri (updated_bundle ))
76
80
77
81
def sync_versions (self , request , ** kwargs ):
78
82
"""
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.
80
85
81
86
Returns the identifiers for the versions that have been deleted.
82
87
"""
83
88
project = get_object_or_404 (Project , pk = kwargs ['pk' ])
84
89
try :
85
90
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' ),
88
94
)
89
95
data = json .loads (post_data )
90
96
self .method_check (request , allowed = ['post' ])
@@ -104,17 +110,20 @@ def sync_versions(self, request, **kwargs):
104
110
105
111
def prepend_urls (self ):
106
112
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' ),
118
127
]
119
128
120
129
@@ -131,9 +140,9 @@ class Meta(object):
131
140
authentication = PostAuthentication ()
132
141
authorization = DjangoAuthorization ()
133
142
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 ,
137
146
}
138
147
139
148
def get_object_list (self , request ):
@@ -149,19 +158,19 @@ def build_version(self, request, **kwargs):
149
158
150
159
def prepend_urls (self ):
151
160
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
157
166
% self ._meta .resource_name ,
158
167
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' ),
165
174
]
166
175
167
176
@@ -182,18 +191,17 @@ class Meta(object):
182
191
183
192
def prepend_urls (self ):
184
193
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$' %
190
199
(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$' %
194
203
(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' ),
197
205
]
198
206
199
207
def get_anchor (self , request , ** __ ):
@@ -204,12 +212,14 @@ def get_anchor(self, request, **__):
204
212
query = request .GET .get ('q' , '' )
205
213
try :
206
214
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 )
208
216
except (AttributeError , redis .exceptions .ConnectionError ):
209
217
redis_data = []
210
218
# -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
+ ]
213
223
object_list = {'objects' : urls }
214
224
215
225
self .log_throttled_access (request )
@@ -230,12 +240,11 @@ class Meta(object):
230
240
231
241
def prepend_urls (self ):
232
242
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' ),
241
250
]
0 commit comments