Skip to content

Commit 92b6e6a

Browse files
Andy Doanstephenfin
authored andcommitted
REST: Add base configuration hooks for a REST API
This adds the ability to expose a REST API based on the Django REST framework project. Since this project isn't packaged in most current distributions, we ensure that its both installed and enabled before trying to use it. Signed-off-by: Andy Doan <andy.doan@linaro.org> Inspired-by: Damien Lespiau <damien.lespiau@intel.com> Reviewed-by: Stephen Finucane <stephen.finucane@intel.com>
1 parent 6f283ef commit 92b6e6a

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

patchwork/settings/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
'patchwork',
2929
]
3030

31+
try:
32+
# django rest framework isn't a standard package in most distros, so
33+
# don't make it compulsory
34+
import rest_framework # NOQA
35+
INSTALLED_APPS += ['rest_framework']
36+
except ImportError:
37+
pass
38+
3139
# HTTP
3240

3341
MIDDLEWARE_CLASSES = [
@@ -148,6 +156,9 @@
148156
# Set to True to enable the Patchwork XML-RPC interface
149157
ENABLE_XMLRPC = False
150158

159+
# Set to True to enable the Patchwork REST API
160+
ENABLE_REST_API = False
161+
151162
# Set to True to enable redirections or URLs from previous versions
152163
# of patchwork
153164
COMPAT_REDIR = True

patchwork/settings/dev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@
8686
#
8787

8888
ENABLE_XMLRPC = True
89+
90+
ENABLE_REST_API = True

patchwork/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@
143143
name='pwclientrc'),
144144
]
145145

146+
if settings.ENABLE_REST_API:
147+
if 'rest_framework' not in settings.INSTALLED_APPS:
148+
raise RuntimeError(
149+
'djangorestframework must be installed to enable the REST API.')
150+
import patchwork.views.rest_api
151+
urlpatterns += [
152+
url(r'^api/1.0/', include(patchwork.views.rest_api.router.urls)),
153+
]
154+
146155
# redirect from old urls
147156
if settings.COMPAT_REDIR:
148157
urlpatterns += [

patchwork/views/rest_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patchwork - automated patch tracking system
2+
# Copyright (C) 2016 Linaro Corporation
3+
#
4+
# This file is part of the Patchwork package.
5+
#
6+
# Patchwork is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Patchwork is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with Patchwork; if not, write to the Free Software
18+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
20+
from rest_framework import routers
21+
22+
router = routers.DefaultRouter()

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ mysqlclient==1.3.7 # replace this with psycopg2 for a PostgreSQL backend
22
django-debug-toolbar==1.4
33
python-dateutil>2.0,<3.0
44
selenium>2.0,<3.0
5+
djangorestframework>=3.3,<3.4

0 commit comments

Comments
 (0)