2016-10-11 17:29:02 +02:00
|
|
|
import re
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from compressor.utils.decorators import cached_property
|
|
|
|
from django.conf.urls import include, url
|
|
|
|
from rest_framework.generics import GenericAPIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.routers import SimpleRouter
|
2016-09-11 21:39:01 +02:00
|
|
|
|
2017-05-01 18:37:10 +02:00
|
|
|
from c3nav.mapdata.api import GeometryTypeViewSet, GeometryViewSet, LevelViewSet, LocationViewSet, SourceViewSet
|
2016-09-11 21:39:01 +02:00
|
|
|
|
2016-10-11 17:29:02 +02:00
|
|
|
router = SimpleRouter()
|
2016-11-27 14:03:39 +01:00
|
|
|
router.register(r'levels', LevelViewSet)
|
2016-09-27 16:18:18 +02:00
|
|
|
router.register(r'sources', SourceViewSet)
|
2016-10-13 13:55:02 +02:00
|
|
|
|
2016-12-06 20:47:17 +01:00
|
|
|
router.register(r'geometrytypes', GeometryTypeViewSet, base_name='geometrytype')
|
2016-11-27 14:03:39 +01:00
|
|
|
router.register(r'geometries', GeometryViewSet, base_name='geometry')
|
2016-10-13 13:55:02 +02:00
|
|
|
|
2016-12-15 13:18:46 +01:00
|
|
|
router.register(r'locations', LocationViewSet, base_name='location')
|
|
|
|
|
2016-10-11 17:29:02 +02:00
|
|
|
|
|
|
|
class APIRoot(GenericAPIView):
|
|
|
|
"""
|
|
|
|
Welcome to the c3nav RESTful API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _format_pattern(self, pattern):
|
|
|
|
return re.sub(r'\(\?P<([^>]*[^>_])_?>[^)]+\)', r'{\1}', pattern)[1:-1]
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def urls(self):
|
|
|
|
urls = OrderedDict()
|
|
|
|
for urlpattern in router.urls:
|
|
|
|
name = urlpattern.name
|
|
|
|
url = self._format_pattern(urlpattern.regex.pattern)
|
|
|
|
base = url.split('/', 1)[0]
|
|
|
|
if '-' in name:
|
|
|
|
urls.setdefault(base, OrderedDict())[name.split('-', 1)[1]] = url
|
|
|
|
else:
|
|
|
|
urls[name] = url
|
|
|
|
return urls
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return Response(self.urls)
|
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^$', APIRoot.as_view()),
|
|
|
|
url(r'', include(router.urls)),
|
|
|
|
]
|