Skip to content

Commit 548ef7d

Browse files
committed
add django-rest-framework to manage servers via api
1 parent 17c0c68 commit 548ef7d

File tree

8 files changed

+264
-36
lines changed

8 files changed

+264
-36
lines changed

Pipfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ psycopg2 = "*"
1010
django = "*"
1111
mercantile = "*"
1212
geojson = "*"
13+
djangorestframework = "*"
14+
pyyaml = "*"
15+
drf-yasg = "*" # django-rest-framework yet-another-swagger-generator required for documentation generation
16+
dynamic-rest = "*"
1317

1418
[requires]
1519
python_version = "3"

Pipfile.lock

Lines changed: 182 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tileservermapping/mapping/models.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22
import mercantile
33

44

5-
class Mapping(models.Model):
6-
z = models.IntegerField(null=False)
7-
x = models.IntegerField(null=False)
8-
y = models.IntegerField(null=False)
9-
land = models.IntegerField(null=False) #TODO: remodel to Forigin key to a country -> server mapping
10-
11-
class Meta:
12-
indexes = [
13-
models.Index(fields=['z', 'x', 'y']),
14-
]
15-
16-
175
class Server(models.Model):
186
z = models.IntegerField(null=False)
197
x = models.IntegerField(null=False)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dynamic_rest import serializers
2+
3+
from . import models
4+
5+
6+
class ServerSerializer(serializers.DynamicModelSerializer):
7+
class Meta:
8+
model = models.Server
9+
fields = ["name", "x", "y", "z", "active", "scheme", "host", "url", "url_postfix"]
10+

tileservermapping/mapping/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
from django.conf.urls import url
2+
from django.urls import path, include
3+
from dynamic_rest import routers
24

35
from .views import get_server, get_cur_tiles
6+
from . import views
47

58
urlpatterns = [
69
url(r'^vector/v1/512/all/(\d+)/(\d+)/(\d+)\.([a-zA-Z]+)$', get_server, name='mapping_server'),
710
url(r'^cur_tiles.js$', get_cur_tiles , name='mapping_get_cur_tiles'),
811
]
12+
13+
14+
router = routers.DefaultRouter()
15+
router.register(r"servers", views.ServerViewSet)
16+
17+
urlpatterns += [
18+
path("", include(router.urls))
19+
]

tileservermapping/mapping/views.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
from django.http import HttpResponse
2+
from dynamic_rest import viewsets
23
from django.shortcuts import render
34
from geojson import Polygon, Feature
45
import mercantile
56

6-
# Create your views here.
7-
from .models import Mapping, Server
7+
from .models import Server
8+
from . import serializers
9+
10+
11+
class ServerViewSet(viewsets.DynamicModelViewSet):
12+
"""
13+
API endpoint that allows servers to be viewed or edited
14+
"""
15+
queryset = Server.objects.all()
16+
serializer_class = serializers.ServerSerializer
817

918

1019
def get_server(request, z, x, y, format):

tileservermapping/settings_base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
'django.contrib.sessions',
2929
'django.contrib.messages',
3030
'django.contrib.staticfiles',
31+
'rest_framework',
32+
'drf_yasg',
33+
'dynamic_rest',
3134
'tileservermapping.mapping',
3235
]
3336

@@ -65,6 +68,10 @@
6568
# override in settings.py
6669
}
6770

71+
LOGIN_URL = "/admin/login/"
72+
LOGOUT_URL = "/admin/logout/"
73+
LOGOUT_REDIRECT_URL = "/"
74+
6875
# Password validation
6976
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
7077

@@ -101,3 +108,17 @@
101108
# https://docs.djangoproject.com/en/2.0/howto/static-files/
102109

103110
STATIC_URL = '/static/'
111+
112+
113+
# Django Rest Framework and extensions
114+
115+
REST_FRAMEWORK = {
116+
"DEFAULT_VERSION": "v1",
117+
"ALLOWED_VERSIONS": ["v1"],
118+
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.URLPathVersioning",
119+
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
120+
}
121+
122+
DYNAMIC_REST = {
123+
"ENABLE_BULK_UPDATE": False
124+
}

0 commit comments

Comments
 (0)