Skip to content

Commit 47b703a

Browse files
committed
Make restapi URL additions conditional (#4609)
* Make restapi URL additions conditional We already have logic that checks for readthedocsext import availability when adding to INSTALLED_APPS, so checking here for import availability isn't correct. For instance, on the readthedocsinc side, we remove `readthedocsext.donate` from INSTALLED_APPS inherited from settings.base, but urls.py would import the module, add the URLs (for packages that aren't in INSTALLED_APPS anymore), and things broke. * Fix issues with pylint during strict prospector pass, autolint * Fix issue with py27 and trailing comma
1 parent a34338d commit 47b703a

File tree

2 files changed

+92
-42
lines changed

2 files changed

+92
-42
lines changed

common

readthedocs/restapi/urls.py

Lines changed: 91 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
"""Define routes between URL paths and views/endpoints."""
1+
# -*- coding: utf-8 -*-
22

3-
from __future__ import absolute_import
3+
"""Define routes between URL paths and views/endpoints."""
44

5-
from django.conf.urls import url, include
5+
from __future__ import (
6+
absolute_import,
7+
division,
8+
print_function,
9+
unicode_literals,
10+
)
611

12+
from django.conf import settings
13+
from django.conf.urls import include, url
714
from rest_framework import routers
815

916
from readthedocs.constants import pattern_opts
1017
from readthedocs.restapi import views
1118
from readthedocs.restapi.views import (
12-
core_views, footer_views, search_views, task_views, integrations
19+
core_views,
20+
footer_views,
21+
integrations,
22+
search_views,
23+
task_views,
1324
)
1425

15-
from .views.model_views import (BuildViewSet, BuildCommandViewSet,
16-
ProjectViewSet, NotificationViewSet,
17-
VersionViewSet, DomainViewSet,
18-
RemoteOrganizationViewSet,
19-
RemoteRepositoryViewSet,
20-
SocialAccountViewSet)
26+
from .views.model_views import (
27+
BuildCommandViewSet,
28+
BuildViewSet,
29+
DomainViewSet,
30+
NotificationViewSet,
31+
ProjectViewSet,
32+
RemoteOrganizationViewSet,
33+
RemoteRepositoryViewSet,
34+
SocialAccountViewSet,
35+
VersionViewSet,
36+
)
2137

2238
router = routers.DefaultRouter()
2339
router.register(r'build', BuildViewSet, base_name='build')
@@ -27,11 +43,20 @@
2743
router.register(r'notification', NotificationViewSet, base_name='emailhook')
2844
router.register(r'domain', DomainViewSet, base_name='domain')
2945
router.register(
30-
r'remote/org', RemoteOrganizationViewSet, base_name='remoteorganization')
46+
r'remote/org',
47+
RemoteOrganizationViewSet,
48+
base_name='remoteorganization',
49+
)
3150
router.register(
32-
r'remote/repo', RemoteRepositoryViewSet, base_name='remoterepository')
51+
r'remote/repo',
52+
RemoteRepositoryViewSet,
53+
base_name='remoterepository',
54+
)
3355
router.register(
34-
r'remote/account', SocialAccountViewSet, base_name='remoteaccount')
56+
r'remote/account',
57+
SocialAccountViewSet,
58+
base_name='remoteaccount',
59+
)
3560

3661
urlpatterns = [
3762
url(r'^', include(router.urls)),
@@ -45,65 +70,90 @@
4570
]
4671

4772
search_urls = [
48-
url(r'index_search/',
73+
url(
74+
r'index_search/',
4975
search_views.index_search,
50-
name='index_search'),
76+
name='index_search',
77+
),
5178
url(r'search/$', views.search_views.search, name='api_search'),
52-
url(r'search/project/$',
79+
url(
80+
r'search/project/$',
5381
search_views.project_search,
54-
name='api_project_search'),
55-
url(r'search/section/$',
82+
name='api_project_search',
83+
),
84+
url(
85+
r'search/section/$',
5686
search_views.section_search,
57-
name='api_section_search'),
87+
name='api_section_search',
88+
),
5889
]
5990

6091
task_urls = [
61-
url(r'jobs/status/(?P<task_id>[^/]+)/',
92+
url(
93+
r'jobs/status/(?P<task_id>[^/]+)/',
6294
task_views.job_status,
63-
name='api_job_status'),
64-
url(r'jobs/sync-remote-repositories/',
95+
name='api_job_status',
96+
),
97+
url(
98+
r'jobs/sync-remote-repositories/',
6599
task_views.sync_remote_repositories,
66-
name='api_sync_remote_repositories'),
100+
name='api_sync_remote_repositories',
101+
),
67102
]
68103

69104
integration_urls = [
70-
url(r'webhook/github/(?P<project_slug>{project_slug})/$'.format(**pattern_opts),
105+
url(
106+
r'webhook/github/(?P<project_slug>{project_slug})/$'
107+
.format(**pattern_opts),
71108
integrations.GitHubWebhookView.as_view(),
72-
name='api_webhook_github'),
73-
url(r'webhook/gitlab/(?P<project_slug>{project_slug})/$'.format(**pattern_opts),
109+
name='api_webhook_github',
110+
),
111+
url(
112+
r'webhook/gitlab/(?P<project_slug>{project_slug})/$'
113+
.format(**pattern_opts),
74114
integrations.GitLabWebhookView.as_view(),
75-
name='api_webhook_gitlab'),
76-
url(r'webhook/bitbucket/(?P<project_slug>{project_slug})/$'.format(**pattern_opts),
115+
name='api_webhook_gitlab',
116+
),
117+
url(
118+
r'webhook/bitbucket/(?P<project_slug>{project_slug})/$'
119+
.format(**pattern_opts),
77120
integrations.BitbucketWebhookView.as_view(),
78-
name='api_webhook_bitbucket'),
79-
url(r'webhook/generic/(?P<project_slug>{project_slug})/$'.format(**pattern_opts),
121+
name='api_webhook_bitbucket',
122+
),
123+
url(
124+
r'webhook/generic/(?P<project_slug>{project_slug})/$'
125+
.format(**pattern_opts),
80126
integrations.APIWebhookView.as_view(),
81-
name='api_webhook_generic'),
82-
url((r'webhook/(?P<project_slug>{project_slug})/'
83-
r'(?P<integration_pk>{integer_pk})/$'.format(**pattern_opts)),
127+
name='api_webhook_generic',
128+
),
129+
url(
130+
(
131+
r'webhook/(?P<project_slug>{project_slug})/'
132+
r'(?P<integration_pk>{integer_pk})/$'.format(**pattern_opts)
133+
),
84134
integrations.WebhookView.as_view(),
85-
name='api_webhook'),
135+
name='api_webhook',
136+
),
86137
]
87138

88139
urlpatterns += function_urls
89140
urlpatterns += search_urls
90141
urlpatterns += task_urls
91142
urlpatterns += integration_urls
92143

93-
try:
144+
if 'readthedocsext.search' in settings.INSTALLED_APPS:
145+
# pylint: disable=import-error
94146
from readthedocsext.search.docsearch import DocSearch
95147
api_search_urls = [
96148
url(r'^docsearch/$', DocSearch.as_view(), name='doc_search'),
97149
]
98150
urlpatterns += api_search_urls
99-
except ImportError:
100-
pass
101151

102-
try:
103-
from readthedocsext.donate.restapi.urls import urlpatterns as sustainability_urls
152+
if 'readthedocsext.donate' in settings.INSTALLED_APPS:
153+
# pylint: disable=import-error
154+
from readthedocsext.donate.restapi.urls import urlpatterns \
155+
as sustainability_urls
104156

105157
urlpatterns += [
106158
url(r'^sustainability/', include(sustainability_urls)),
107159
]
108-
except ImportError:
109-
pass

0 commit comments

Comments
 (0)