Refactor rest_api app and the method end points are registered. All apps API URL endpoints are now registered under the 'rest_api' namespace.
This commit is contained in:
@@ -1,19 +1,21 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django import apps
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from common import menu_tools
|
from common import MayanAppConfig, menu_tools
|
||||||
|
|
||||||
from .classes import APIEndPoint
|
from .classes import APIEndPoint
|
||||||
from .links import link_api, link_api_documentation
|
from .links import link_api, link_api_documentation
|
||||||
|
|
||||||
|
|
||||||
class RESTAPIApp(apps.AppConfig):
|
class RESTAPIApp(MayanAppConfig):
|
||||||
|
app_url = 'api'
|
||||||
name = 'rest_api'
|
name = 'rest_api'
|
||||||
verbose_name = _('REST API')
|
verbose_name = _('REST API')
|
||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
APIEndPoint('rest_api')
|
super(RESTAPIApp, self).ready()
|
||||||
|
|
||||||
|
APIEndPoint(app=self, version_string='1')
|
||||||
|
|
||||||
menu_tools.bind_links(links=(link_api, link_api_documentation))
|
menu_tools.bind_links(links=(link_api, link_api_documentation))
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ from django.conf.urls import include, patterns, url
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
|
from rest_framework.reverse import reverse
|
||||||
|
|
||||||
|
|
||||||
class APIEndPoint(object):
|
class APIEndPoint(object):
|
||||||
_registry = {}
|
_registry = {}
|
||||||
@@ -17,14 +19,16 @@ class APIEndPoint(object):
|
|||||||
return cls._registry[name]
|
return cls._registry[name]
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return unicode(self.name)
|
return unicode(self.app.name)
|
||||||
|
|
||||||
def __init__(self, name, app_name=None):
|
def __init__(self, app, version_string, name=None):
|
||||||
self.name = name
|
self.app = app
|
||||||
self.endpoints = []
|
self.endpoints = []
|
||||||
|
self.name = name
|
||||||
|
self.version_string = version_string
|
||||||
try:
|
try:
|
||||||
api_urls = import_string(
|
api_urls = import_string(
|
||||||
'{0}.urls.api_urls'.format(app_name or name)
|
'{0}.urls.api_urls'.format(app.name)
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
@@ -35,13 +39,26 @@ class APIEndPoint(object):
|
|||||||
else:
|
else:
|
||||||
self.register_urls(api_urls)
|
self.register_urls(api_urls)
|
||||||
|
|
||||||
self.__class__._registry[name] = self
|
self.__class__._registry[app.name] = self
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self):
|
||||||
|
return reverse('rest_api:api-version-1-app', args=[self.app.name])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def app_name(self):
|
||||||
|
return self.app.name
|
||||||
|
|
||||||
def register_urls(self, urlpatterns):
|
def register_urls(self, urlpatterns):
|
||||||
from .urls import version_0_urlpatterns
|
from .urls import version_1_urlpatterns
|
||||||
endpoint_urls = patterns(
|
endpoint_urls = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^%s/' % self.name, include(urlpatterns)),
|
url(r'^%s/' % self.app.name, include(urlpatterns)),
|
||||||
)
|
)
|
||||||
|
version_1_urlpatterns += endpoint_urls
|
||||||
|
|
||||||
version_0_urlpatterns += endpoint_urls
|
|
||||||
|
class APIVersion(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.version_string = '1'
|
||||||
|
self.url = reverse('rest_api:api-version-1')
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
|
|
||||||
from navigation import Link
|
from navigation import Link
|
||||||
|
|
||||||
link_api = Link(icon='fa fa-plug', text=_('REST API'), view='api-root')
|
link_api = Link(icon='fa fa-plug', text=_('REST API'), view='rest_api:api-root')
|
||||||
link_api_documentation = Link(
|
link_api_documentation = Link(
|
||||||
icon='fa fa-book', text=_('API Documentation'),
|
icon='fa fa-book', text=_('API Documentation'),
|
||||||
view='django.swagger.base.view'
|
view='django.swagger.base.view'
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
class APISerializer(serializers.Serializer):
|
class APIVersionSerializer(serializers.Serializer):
|
||||||
name = serializers.CharField()
|
|
||||||
url = serializers.URLField()
|
url = serializers.URLField()
|
||||||
|
version_string = serializers.CharField()
|
||||||
|
|
||||||
|
|
||||||
class APIAppSerializer(serializers.Serializer):
|
class APIAppSerializer(serializers.Serializer):
|
||||||
name = serializers.CharField()
|
app_name = serializers.CharField()
|
||||||
url = serializers.URLField()
|
url = serializers.URLField()
|
||||||
|
version_string = serializers.CharField()
|
||||||
|
|
||||||
class APIVersionSerializer(serializers.Serializer):
|
|
||||||
apps = APIAppSerializer()
|
|
||||||
|
|||||||
@@ -2,21 +2,21 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.conf.urls import include, patterns, url
|
from django.conf.urls import include, patterns, url
|
||||||
|
|
||||||
from .views import APIBase, Version_0, APIAppView, BrowseableObtainAuthToken
|
from .views import APIBase, APIVersionView, APIAppView, BrowseableObtainAuthToken
|
||||||
|
|
||||||
version_0_urlpatterns = patterns(
|
version_1_urlpatterns = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^$', Version_0.as_view(), name='api-version-0'),
|
url(r'^$', APIVersionView.as_view(), name='api-version-1'),
|
||||||
url(
|
url(
|
||||||
r'^(?P<app_name>\w+)/$', APIAppView.as_view(),
|
r'^(?P<app_name>\w+)/$', APIAppView.as_view(),
|
||||||
name='api-version-0-app'
|
name='api-version-1-app'
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = patterns(
|
urlpatterns = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^$', APIBase.as_view(), name='api-root'),
|
url(r'^$', APIBase.as_view(), name='api-root'),
|
||||||
url(r'^v0/', include(version_0_urlpatterns)),
|
url(r'^v1/', include(version_1_urlpatterns)),
|
||||||
)
|
)
|
||||||
|
|
||||||
api_urls = patterns(
|
api_urls = patterns(
|
||||||
|
|||||||
@@ -7,72 +7,46 @@ from rest_framework.authtoken.views import ObtainAuthToken
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
|
||||||
from .classes import APIEndPoint
|
from .classes import APIVersion, APIEndPoint
|
||||||
from .serializers import APIAppSerializer, APISerializer, APIVersionSerializer
|
from .serializers import APIAppSerializer, APIVersionSerializer
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
registered_version_0_endpoints = [
|
registered_version_1_endpoints = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class APIBase(generics.GenericAPIView):
|
class APIBase(generics.RetrieveAPIView):
|
||||||
"""
|
"""
|
||||||
Main entry point of the API.
|
Main entry point of the API.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = APISerializer
|
|
||||||
|
|
||||||
def get(self, request, format=None):
|
|
||||||
return Response([
|
|
||||||
{
|
|
||||||
'name': 'Version 0', 'url': reverse(
|
|
||||||
'api-version-0', request=request, format=format
|
|
||||||
)
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class Version_0(generics.GenericAPIView):
|
|
||||||
"""
|
|
||||||
API version 0 entry points.
|
|
||||||
"""
|
|
||||||
|
|
||||||
serializer_class = APIVersionSerializer
|
serializer_class = APIVersionSerializer
|
||||||
|
|
||||||
def get(self, request, format=None):
|
def get_object(self):
|
||||||
return Response({
|
return APIVersion()
|
||||||
'apps': [
|
|
||||||
{
|
|
||||||
'name': unicode(endpoint),
|
|
||||||
'url': reverse('api-version-0-app',
|
|
||||||
args=[unicode(endpoint)], request=request,
|
|
||||||
format=format
|
|
||||||
)
|
|
||||||
} for endpoint in APIEndPoint.get_all()
|
|
||||||
],
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
class APIAppView(generics.GenericAPIView):
|
class APIVersionView(generics.ListAPIView):
|
||||||
|
"""
|
||||||
|
API version entry points.
|
||||||
|
"""
|
||||||
|
|
||||||
|
serializer_class = APIAppSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return APIEndPoint.get_all()
|
||||||
|
|
||||||
|
|
||||||
|
class APIAppView(generics.RetrieveAPIView):
|
||||||
"""
|
"""
|
||||||
Entry points of the selected app.
|
Entry points of the selected app.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = APIAppSerializer
|
serializer_class = APIAppSerializer
|
||||||
|
|
||||||
def get(self, request, app_name, format=None):
|
def get_object(self):
|
||||||
api_app = APIEndPoint.get(app_name)
|
return APIEndPoint.get(self.kwargs['app_name'])
|
||||||
|
|
||||||
return Response(
|
|
||||||
{
|
|
||||||
'name': api_app.name,
|
|
||||||
'url': reverse(
|
|
||||||
'api-version-0-app', args=[unicode(api_app.name)],
|
|
||||||
request=request, format=format
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class BrowseableObtainAuthToken(ObtainAuthToken):
|
class BrowseableObtainAuthToken(ObtainAuthToken):
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
|
|||||||
CORS_ORIGIN_ALLOW_ALL = True
|
CORS_ORIGIN_ALLOW_ALL = True
|
||||||
# ------ Django REST Swagger -----
|
# ------ Django REST Swagger -----
|
||||||
SWAGGER_SETTINGS = {
|
SWAGGER_SETTINGS = {
|
||||||
'api_version': '0', # Specify your API's version
|
'api_version': '1', # Specify your API's version
|
||||||
}
|
}
|
||||||
# ------ Timezone --------
|
# ------ Timezone --------
|
||||||
TIMEZONE_COOKIE_NAME = 'django_timezone'
|
TIMEZONE_COOKIE_NAME = 'django_timezone'
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ admin.autodiscover()
|
|||||||
urlpatterns = patterns(
|
urlpatterns = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
url(r'^admin/', include(admin.site.urls)),
|
||||||
url(r'^api/', include('rest_api.urls')),
|
|
||||||
url(r'^docs/', include('rest_framework_swagger.urls')),
|
url(r'^docs/', include('rest_framework_swagger.urls')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user