move source image storage away from package directory

This commit is contained in:
Laura Klünder 2017-05-01 16:50:36 +02:00
parent 582f7df7ca
commit 8b3265e3e9
3 changed files with 34 additions and 6 deletions

View file

@ -1,11 +1,8 @@
import hashlib import hashlib
import json import json
import mimetypes import mimetypes
import os
from collections import OrderedDict from collections import OrderedDict
from django.conf import settings
from django.core.files import File
from django.http import Http404, HttpResponse, HttpResponseNotModified from django.http import Http404, HttpResponse, HttpResponseNotModified
from rest_framework.decorators import detail_route, list_route from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response from rest_framework.response import Response
@ -156,9 +153,7 @@ class SourceViewSet(CachedReadOnlyViewSetMixin, ReadOnlyModelViewSet):
def _image(self, request, name=None): def _image(self, request, name=None):
source = self.get_object() source = self.get_object()
response = HttpResponse(content_type=mimetypes.guess_type(source.name)[0]) response = HttpResponse(content_type=mimetypes.guess_type(source.name)[0])
image_path = os.path.join(settings.MAP_ROOT, source.package.directory, 'sources', source.name) response.write(source.image)
for chunk in File(open(image_path, 'rb')).chunks():
response.write(chunk)
return response return response

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-05-01 14:41
from __future__ import unicode_literals
import os
from django.conf import settings
from django.db import migrations, models
def save_images_to_db(apps, schema_editor):
Source = apps.get_model('mapdata', 'Source')
for source in Source.objects.all():
image_path = os.path.join(settings.MAP_ROOT, source.package.directory, 'sources', source.name)
source.image = open(image_path, 'rb').read()
source.save()
class Migration(migrations.Migration):
dependencies = [
('mapdata', '0037_auto_20170428_0902'),
]
operations = [
migrations.AddField(
model_name='source',
name='image',
field=models.BinaryField(default=b'', verbose_name='image data'),
preserve_default=False,
),
migrations.RunPython(save_images_to_db),
]

View file

@ -12,6 +12,7 @@ class Source(MapItem):
left = models.DecimalField(_('left coordinate'), max_digits=6, decimal_places=2) left = models.DecimalField(_('left coordinate'), max_digits=6, decimal_places=2)
top = models.DecimalField(_('top coordinate'), max_digits=6, decimal_places=2) top = models.DecimalField(_('top coordinate'), max_digits=6, decimal_places=2)
right = models.DecimalField(_('right coordinate'), max_digits=6, decimal_places=2) right = models.DecimalField(_('right coordinate'), max_digits=6, decimal_places=2)
image = models.BinaryField(_('image data')) # todo migrate to better storage
class Meta: class Meta:
verbose_name = _('Source') verbose_name = _('Source')