refactor models, change map package storage and map package loading
This commit is contained in:
parent
0656090f27
commit
376d85f9b4
12 changed files with 226 additions and 150 deletions
|
@ -1,3 +1,5 @@
|
|||
from collections import OrderedDict
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -13,3 +15,35 @@ class Package(models.Model):
|
|||
left = models.DecimalField(_('left coordinate'), null=True, max_digits=6, decimal_places=2)
|
||||
top = models.DecimalField(_('top coordinate'), null=True, max_digits=6, decimal_places=2)
|
||||
right = models.DecimalField(_('right coordinate'), null=True, max_digits=6, decimal_places=2)
|
||||
|
||||
directory = models.CharField(_('folder name'), max_length=100)
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, directory):
|
||||
kwargs = {
|
||||
'directory': directory
|
||||
}
|
||||
|
||||
if 'name' not in data:
|
||||
raise ValueError('pkg.json: missing package name.')
|
||||
kwargs['name'] = data['name']
|
||||
|
||||
if 'bounds' in data:
|
||||
bounds = data['bounds']
|
||||
if len(bounds) != 2 or len(bounds[0]) != 2 or len(bounds[1]) != 2:
|
||||
raise ValueError('pkg.json: Invalid bounds format.')
|
||||
if not all(isinstance(i, (float, int)) for i in sum(bounds, [])):
|
||||
raise ValueError('pkg.json: All bounds coordinates have to be int or float.')
|
||||
if bounds[0][0] >= bounds[1][0] or bounds[0][1] >= bounds[1][1]:
|
||||
raise ValueError('pkg.json: bounds: lower coordinate has to be first.')
|
||||
(kwargs['bottom'], kwargs['left']), (kwargs['top'], kwargs['right']) = bounds
|
||||
|
||||
return kwargs
|
||||
|
||||
def tofile(self):
|
||||
data = OrderedDict()
|
||||
data['name'] = self.name
|
||||
if self.bottom is not None:
|
||||
data['bounds'] = ((float(self.bottom), float(self.left)), (float(self.top), float(self.right)))
|
||||
|
||||
return data
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue