Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# custom files
db.sqlite
settings.py

.idea

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
django
mercantile
geojson
15 changes: 0 additions & 15 deletions tileservermapping/manage.py

This file was deleted.

6 changes: 6 additions & 0 deletions tileservermapping/slowtiles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

# Register your models here.
from .models import SlowTiles

admin.site.register(SlowTiles)
32 changes: 32 additions & 0 deletions tileservermapping/slowtiles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.2.1 on 2020-02-09 20:16

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('mapping', '0006_auto_20181226_1555'),
]

operations = [
migrations.CreateModel(
name='SlowTiles',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('z', models.IntegerField()),
('x', models.IntegerField()),
('y', models.IntegerField()),
('count', models.IntegerField(default=1)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('server', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mapping.Server')),
],
options={
'unique_together': {('z', 'x', 'y')},
},
),
]
Empty file.
39 changes: 39 additions & 0 deletions tileservermapping/slowtiles/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.db import models, transaction
from tileservermapping.mapping.models import Server


class SlowTiles(models.Model):
z = models.IntegerField(null=False)
x = models.IntegerField(null=False)
y = models.IntegerField(null=False)

server = models.ForeignKey(Server, on_delete=models.CASCADE)

count = models.IntegerField(default=1)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

class Meta:
unique_together = ['z', 'x', 'y']

def __str__(self):
return "<SlowTile: {}/{}/{} c: {}>".format(self.z, self.x, self.y, self.count)

@staticmethod
def insert(z, x, y):
"""
inserts a tile z/x/y into the slow tile list, or increase the counter on an existing tile
:return: the current counter on this tile, or -1 if no tile server is responsible
"""
server = Server.get_server(z, x, y)
if server == Server.objects.get(z=0, x=0, y=0):
# we can not do anything on the external instance
return -1
with transaction.atomic():
try:
tile = SlowTiles.objects.get(z=z, x=x, y=y)
tile.count += 1
except models.ObjectDoesNotExist:
tile = SlowTiles(z=z, x=x, y=y, server=server)
tile.save()
return tile.count
8 changes: 8 additions & 0 deletions tileservermapping/slowtiles/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from .views import insert_tile, get_server_list, upload_tile

urlpatterns = [
path('add/<int:z>/<int:x>/<int:y>', insert_tile, name="insert_tile"),
path('server_list/<int:z>/<int:x>/<int:y>', get_server_list, name="server_list"),
path('upload_tile/<int:z>/<int:x>/<int:y>.<slug:format>', upload_tile, name='upload_tile'),
]
46 changes: 46 additions & 0 deletions tileservermapping/slowtiles/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from pathlib import Path

from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

from tileservermapping.mapping.models import Server
from tileservermapping.slowtiles.models import SlowTiles


def insert_tile(request, z, x, y):
tile_count = SlowTiles.insert(z, x, y)
if tile_count < 0:
return HttpResponse("No server responsible", content_type='text', status=404)
return HttpResponse("Added: {}/{}/{} Count: {}\n".format(z, x, y, tile_count), content_type='text')


def get_server_list(request, z, x, y):
server = Server.get_server(z, x, y)
tiles = SlowTiles.objects.filter(server=server, count__gte=5)
answer = ""
for tile in tiles:
answer += "{}/{}/{}\n".format(tile.z, tile.x, tile.y)
return HttpResponse(answer, content_type='text')


def handle_uploaded_file(path, f):
with open(path, 'wb') as destination:
for chunk in f.chunks():
destination.write(chunk)

@require_POST
@csrf_exempt
def upload_tile(request, z, x, y, format):
# TODO auth oth server
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is auth oth server?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be authenticate the server, because at the moment all servers having access to the api can upload files

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be fixed by using #8 and adding the checks here to

# TODO check if tile in server
if not SlowTiles.objects.filter(x=x, y=y, z=z, count__gte=5).count():
return HttpResponse('Tile should not be static\n', content_type="text", status=403)
path = Path(settings.TILE_WEBSERVER_DIR) / str(z) / str(x)
if not path.exists():
path.mkdir(parents=True)
file = path / "{}.{}".format(y, format)
existed = file.exists()
handle_uploaded_file(file, request.FILES['tile'])
return HttpResponse('Updated\n' if existed else 'OK\n', content_type='text')
1 change: 1 addition & 0 deletions tileservermapping/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^mappings/', include('tileservermapping.mapping.urls')),
path('slow_tiles/', include('tileservermapping.slowtiles.urls')),
]