Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
bug 1159406 - Namespace the v1 API views
Browse files Browse the repository at this point in the history
Use the DRF NamespaceVersioning strategy for serving the v1 API.  This
will allow the v2 API to exist alongside the v1 API.
  • Loading branch information
jwhitlock committed Nov 11, 2015
1 parent d0df25e commit 7050c0e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion webplatformcompat/jinja2/webplatformcompat/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<li{% if request.path.startswith(url('about')) %} class="active"{% endif %}>
<a href="{{url('about')}}">About</a></li>
<li{% if request.path.startswith(url('api_root')) %} class="active"{% endif %}>
<a href="{{url('api-root')}}">API</a></li>
<a href="{{url('v1:api-root')}}">API</a></li>
<li{% if request.path.startswith(url('browse')) %} class="active"{% endif %}>
<a href="{{url('browse')}}">Browse</a></li>
<li{% if request.path.startswith(url('feature_page_list')) %} class="active"{% endif %}>
Expand Down
2 changes: 1 addition & 1 deletion webplatformcompat/jinja2/webplatformcompat/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h2>Browse the Data</h2>
<div class="col-md-3">
<h2>Explore the API</h2>
<p>Jump right into the self-documenting API.</p>
<p><a class="btn btn-default" href="{{ url('api-root') }}" role="button">API &raquo;</a></p>
<p><a class="btn btn-default" href="{{ url('v1:api-root') }}" role="button">API &raquo;</a></p>
</div>
<div class="col-md-3">
<h2>Import from MDN</h2>
Expand Down
13 changes: 13 additions & 0 deletions webplatformcompat/renderers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from collections import OrderedDict
from json import loads

from django.core.urlresolvers import get_resolver
from django.template import loader
from django.utils import encoding, translation
from django.utils.six.moves.urllib.parse import urlparse, urlunparse

from rest_framework.relations import ManyRelatedField
from rest_framework.renderers import TemplateHTMLRenderer
Expand Down Expand Up @@ -223,6 +225,17 @@ def fields_from_resource(self, resource):
else:
return super(JsonApiRenderer, self).fields_from_resource(resource)

def url_to_template(self, view_name, request, template_name):
prefix, resolver = get_resolver(None).namespace_dict['v1']
info = resolver.reverse_dict[view_name]
path_template = info[0][0][0]
id_field = info[0][0][1][0]
path = prefix + path_template % {id_field: '{%s}' % template_name}
parsed_url = urlparse(request.build_absolute_uri())
return urlunparse(
[parsed_url.scheme, parsed_url.netloc, path, '', '', '']
)


class JsonApiTemplateHTMLRenderer(TemplateHTMLRenderer):
"""Render to a template, but use JSON API format as context."""
Expand Down
5 changes: 3 additions & 2 deletions webplatformcompat/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
class TestMixin(object):
"""Useful methods for testing"""
maxDiff = None
namespace = 'v1'

def tearDown(self):
cache.clear()

def api_reverse(self, viewname, **kwargs):
"""Create a path to an API view"""
return reverse(viewname, kwargs=kwargs)
"""Create a path to a namespaced API view."""
return reverse("%s:%s" % (self.namespace, viewname), kwargs=kwargs)

def login_user(self, groups=None):
"""Create and login a user, saving to self.user.
Expand Down
2 changes: 1 addition & 1 deletion webplatformcompat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace='rest_framework')),
url(r'^api/$', RedirectView.as_view(url='/api/v1/', permanent=False),
name='api_root'),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/', include(router.urls, namespace='v1')),
url(r'^importer$', RedirectView.as_view(
url='/importer/', permanent=False)),
url(r'^importer/', include(mdn_urlpatterns)),
Expand Down
7 changes: 5 additions & 2 deletions webplatformcompat/view_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Meta:

class DjangoResourceClient(object):
"""Implement tools.client.Client using Django native functions"""
namespace = 'v1'

def url(self, resource_type, resource_id=None):
"""Use Django reverse to determine URL."""
if resource_type == 'maturities':
Expand All @@ -89,9 +91,10 @@ def url(self, resource_type, resource_id=None):
singular = resource_type[:-1]
if resource_id:
return reverse(
singular + '-detail', kwargs={'pk': resource_id})
self.namespace + ':' + singular + '-detail',
kwargs={'pk': resource_id})
else:
return reverse(singular + '-list')
return reverse(self.namespace + ':' + singular + '-list')

def open_changeset(self):
"""Skip opening changesets (opened at the request/view level)."""
Expand Down
2 changes: 2 additions & 0 deletions wpcsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def env_to_bool(environ_key, default):
],
'DEFAULT_PAGINATION_CLASS': 'webplatformcompat.pagination.Pagination',
'PAGE_SIZE': 10,
'DEFAULT_VERSIONING_CLASS': (
'rest_framework.versioning.NamespaceVersioning'),
}

# Django nose
Expand Down

0 comments on commit 7050c0e

Please sign in to comment.