remove tofile, fromfile and similar
This commit is contained in:
parent
72716154b9
commit
582f7df7ca
6 changed files with 1 additions and 361 deletions
|
@ -33,25 +33,6 @@ class MapItem(models.Model, metaclass=MapItemMeta):
|
|||
return self.titles[lang]
|
||||
return next(iter(self.titles.values())) if self.titles else self.name
|
||||
|
||||
@classmethod
|
||||
def get_path_prefix(cls):
|
||||
return cls._meta.default_related_name + '/'
|
||||
|
||||
@classmethod
|
||||
def get_path_regex(cls):
|
||||
return r'^' + cls.get_path_prefix()
|
||||
|
||||
def get_filename(self):
|
||||
return self._meta.default_related_name + '/' + self.name + '.json'
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = {}
|
||||
return kwargs
|
||||
|
||||
def tofile(self, form=None):
|
||||
return OrderedDict()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
with set_last_mapdata_update():
|
||||
super().save(*args, **kwargs)
|
||||
|
|
|
@ -4,7 +4,7 @@ from django.db import models
|
|||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from shapely.geometry import CAP_STYLE, JOIN_STYLE, Point
|
||||
from shapely.geometry.geo import mapping, shape
|
||||
from shapely.geometry.geo import mapping
|
||||
|
||||
from c3nav.mapdata.fields import GeometryField
|
||||
from c3nav.mapdata.models import Elevator
|
||||
|
@ -33,19 +33,6 @@ class GeometryMapItem(MapItem, metaclass=GeometryMapItemMeta):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'geometry' not in data:
|
||||
raise ValueError('missing geometry.')
|
||||
try:
|
||||
kwargs['geometry'] = shape(data['geometry'])
|
||||
except:
|
||||
raise ValueError(_('Invalid GeoJSON.'))
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
return OrderedDict((
|
||||
('type', self.__class__.__name__.lower()),
|
||||
|
@ -60,11 +47,6 @@ class GeometryMapItem(MapItem, metaclass=GeometryMapItemMeta):
|
|||
('geometry', format_geojson(mapping(self.geometry), round=False)),
|
||||
))
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['geometry'] = format_geojson(mapping(self.geometry))
|
||||
return result
|
||||
|
||||
def get_shadow_geojson(self):
|
||||
return None
|
||||
|
||||
|
@ -81,27 +63,11 @@ class GeometryMapItemWithLevel(GeometryMapItem):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'level' not in data:
|
||||
raise ValueError('missing level.')
|
||||
kwargs['level'] = data['level']
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
result['level'] = self.level.name
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['level'] = self.level.name
|
||||
result.move_to_end('geometry')
|
||||
return result
|
||||
|
||||
|
||||
class DirectedLineGeometryMapItemWithLevel(GeometryMapItemWithLevel):
|
||||
geomtype = 'polyline'
|
||||
|
@ -197,26 +163,11 @@ class Escalator(GeometryMapItemWithLevel):
|
|||
verbose_name_plural = _('Escalators')
|
||||
default_related_name = 'escalators'
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'direction' not in data:
|
||||
raise ValueError('missing direction.')
|
||||
kwargs['direction'] = data['direction']
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
result['direction'] = 'up' if self.direction else 'down'
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['direction'] = self.direction
|
||||
return result
|
||||
|
||||
|
||||
class Stair(DirectedLineGeometryMapItemWithLevel):
|
||||
"""
|
||||
|
@ -242,27 +193,12 @@ class Obstacle(GeometryMapItemWithLevel):
|
|||
verbose_name_plural = _('Obstacles')
|
||||
default_related_name = 'obstacles'
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'crop_to_level' in data:
|
||||
kwargs['crop_to_level'] = data['crop_to_level']
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
if self.crop_to_level is not None:
|
||||
result['crop_to_level'] = self.crop_to_level.name
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
if self.crop_to_level is not None:
|
||||
result['crop_to_level'] = self.crop_to_level.name
|
||||
return result
|
||||
|
||||
|
||||
class LineObstacle(GeometryMapItemWithLevel):
|
||||
"""
|
||||
|
@ -285,26 +221,11 @@ class LineObstacle(GeometryMapItemWithLevel):
|
|||
result['original_geometry'] = original_geometry
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'width' not in data:
|
||||
raise ValueError('missing width.')
|
||||
kwargs['width'] = data['width']
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
result['width'] = float(self.width)
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['width'] = float(self.width)
|
||||
return result
|
||||
|
||||
|
||||
class LevelConnector(GeometryMapItem):
|
||||
"""
|
||||
|
@ -318,32 +239,11 @@ class LevelConnector(GeometryMapItem):
|
|||
verbose_name_plural = _('Level Connectors')
|
||||
default_related_name = 'levelconnectors'
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'levels' not in data:
|
||||
raise ValueError('missing levels.')
|
||||
levels = data.get('levels', None)
|
||||
if not isinstance(levels, list):
|
||||
raise TypeError('levels has to be a list')
|
||||
if len(levels) < 2:
|
||||
raise ValueError('a level connector needs at least two levels')
|
||||
kwargs['levels'] = levels
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
result['levels'] = tuple(self.levels.all().order_by('name').values_list('name', flat=True))
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['levels'] = sorted(self.levels.all().order_by('name').values_list('name', flat=True))
|
||||
result.move_to_end('geometry')
|
||||
return result
|
||||
|
||||
|
||||
class Door(GeometryMapItemWithLevel):
|
||||
"""
|
||||
|
@ -391,31 +291,6 @@ class ElevatorLevel(GeometryMapItemWithLevel):
|
|||
result['button'] = self.button
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'elevator' not in data:
|
||||
raise ValueError('missing elevator.')
|
||||
kwargs['elevator'] = data['elevator']
|
||||
|
||||
if 'button' not in data:
|
||||
raise ValueError('missing button.')
|
||||
kwargs['button'] = data['button']
|
||||
|
||||
if 'override_altitude' in data:
|
||||
kwargs['override_altitude'] = data['override_altitude']
|
||||
|
||||
return kwargs
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['elevator'] = self.elevator.name
|
||||
result['button'] = self.button
|
||||
if self.override_altitude is not None:
|
||||
result['override_altitude'] = float(self.override_altitude)
|
||||
return result
|
||||
|
||||
@cached_property
|
||||
def altitude(self):
|
||||
if self.override_altitude is not None:
|
||||
|
|
|
@ -34,43 +34,12 @@ class Level(MapItem):
|
|||
def geometries(self):
|
||||
return LevelGeometries.by_level(self, only_public=False)
|
||||
|
||||
def tofilename(self):
|
||||
return 'levels/%s.json' % self.name
|
||||
|
||||
def lower(self):
|
||||
return Level.objects.filter(altitude__lt=self.altitude).order_by('altitude')
|
||||
|
||||
def higher(self):
|
||||
return Level.objects.filter(altitude__gt=self.altitude).order_by('altitude')
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'altitude' not in data:
|
||||
raise ValueError('missing altitude.')
|
||||
|
||||
if not isinstance(data['altitude'], (int, float)):
|
||||
raise ValueError('altitude has to be int or float.')
|
||||
|
||||
kwargs['altitude'] = data['altitude']
|
||||
|
||||
if 'intermediate' not in data:
|
||||
raise ValueError('missing intermediate.')
|
||||
|
||||
if not isinstance(data['intermediate'], bool):
|
||||
raise ValueError('intermediate has to be boolean.')
|
||||
|
||||
kwargs['intermediate'] = data['intermediate']
|
||||
|
||||
return kwargs
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['altitude'] = float(self.altitude)
|
||||
result['intermediate'] = self.intermediate
|
||||
return result
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
|
|
@ -38,55 +38,6 @@ class LocationModelMixin(Location):
|
|||
result['titles'] = OrderedDict(sorted(self.titles.items()))
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'titles' not in data:
|
||||
raise ValueError('missing titles.')
|
||||
titles = data['titles']
|
||||
if not isinstance(titles, dict):
|
||||
raise ValueError('Invalid titles format.')
|
||||
if any(not isinstance(lang, str) for lang in titles.keys()):
|
||||
raise ValueError('titles: All languages have to be strings.')
|
||||
if any(not isinstance(title, str) for title in titles.values()):
|
||||
raise ValueError('titles: All titles have to be strings.')
|
||||
if any(not title for title in titles.values()):
|
||||
raise ValueError('titles: Titles must not be empty strings.')
|
||||
kwargs['titles'] = titles
|
||||
|
||||
if 'can_search' not in data:
|
||||
raise ValueError('Missing can_search')
|
||||
can_search = data['can_search']
|
||||
if not isinstance(can_search, bool):
|
||||
raise ValueError('can_search has to be boolean!')
|
||||
kwargs['can_search'] = can_search
|
||||
|
||||
if 'can_describe' not in data:
|
||||
raise ValueError('Missing can_describe')
|
||||
can_describe = data['can_describe']
|
||||
if not isinstance(can_describe, bool):
|
||||
raise ValueError('can_describe has to be boolean!')
|
||||
kwargs['can_describe'] = can_describe
|
||||
|
||||
if 'color' in data:
|
||||
color = data['color']
|
||||
if not isinstance(color, str):
|
||||
raise ValueError('color has to be str!')
|
||||
if color:
|
||||
kwargs['color'] = color
|
||||
|
||||
return kwargs
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile(form=form)
|
||||
result['titles'] = OrderedDict(sorted(self.titles.items()))
|
||||
result['can_search'] = self.can_search
|
||||
result['can_describe'] = self.can_describe
|
||||
if self.color:
|
||||
result['color'] = self.color
|
||||
return result
|
||||
|
||||
@property
|
||||
def subtitle(self):
|
||||
return self._meta.verbose_name
|
||||
|
@ -143,28 +94,10 @@ class LocationGroup(LocationModelMixin, MapItem):
|
|||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'compiled_room' not in data:
|
||||
raise ValueError('Missing compiled_room')
|
||||
compiled_room = data['compiled_room']
|
||||
if not isinstance(compiled_room, bool):
|
||||
raise ValueError('compiled_room has to be boolean!')
|
||||
kwargs['compiled_room'] = compiled_room
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['compiled_room'] = self.compiled_room
|
||||
return result
|
||||
|
||||
|
||||
class AreaLocation(LocationModelMixin, GeometryMapItemWithLevel):
|
||||
LOCATION_TYPES = (
|
||||
|
@ -249,51 +182,10 @@ class AreaLocation(LocationModelMixin, GeometryMapItemWithLevel):
|
|||
def get_sort_key(cls, arealocation):
|
||||
return cls.LOCATION_TYPES_ORDER.index(arealocation.location_type)
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
groups = data.get('groups', [])
|
||||
if not isinstance(groups, list):
|
||||
raise TypeError('groups has to be a list')
|
||||
kwargs['groups'] = groups
|
||||
|
||||
if 'location_type' not in data:
|
||||
raise ValueError('Missing location type')
|
||||
location_type = data['location_type']
|
||||
if location_type not in dict(cls.LOCATION_TYPES):
|
||||
raise ValueError('Invalid location type')
|
||||
kwargs['location_type'] = location_type
|
||||
|
||||
if 'routing_inclusion' not in data:
|
||||
raise ValueError('Missing routing inclusion')
|
||||
routing_inclusion = data['routing_inclusion']
|
||||
if routing_inclusion not in dict(cls.ROUTING_INCLUSIONS):
|
||||
raise ValueError('Invalid routing inclusion')
|
||||
kwargs['routing_inclusion'] = routing_inclusion
|
||||
|
||||
kwargs['bssids'] = data.get('bssids', '')
|
||||
validate_bssid_lines(kwargs['bssids'])
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_geojson_properties(self):
|
||||
result = super().get_geojson_properties()
|
||||
return result
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile(form=form)
|
||||
if form is not None:
|
||||
result['groups'] = sorted(group.name for group in form.cleaned_data['groups'])
|
||||
else:
|
||||
result['groups'] = sorted(self.groups.all().order_by('name').values_list('name', flat=True))
|
||||
result['location_type'] = self.location_type
|
||||
result['routing_inclusion'] = self.routing_inclusion
|
||||
if self.bssids:
|
||||
result['bssids'] = self.bssids
|
||||
result.move_to_end('geometry')
|
||||
return result
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from collections import OrderedDict
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -29,13 +27,6 @@ class Package(models.Model):
|
|||
verbose_name_plural = _('Map Packages')
|
||||
default_related_name = 'packages'
|
||||
|
||||
@classmethod
|
||||
def get_path_regex(cls):
|
||||
return '^package.json$'
|
||||
|
||||
def get_filename(self):
|
||||
return 'package.json'
|
||||
|
||||
@property
|
||||
def package(self):
|
||||
return self
|
||||
|
@ -50,51 +41,6 @@ class Package(models.Model):
|
|||
def public(self):
|
||||
return self.name in settings.PUBLIC_PACKAGES
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = {}
|
||||
|
||||
if 'name' not in data:
|
||||
raise ValueError('missing package name.')
|
||||
kwargs['name'] = data['name']
|
||||
|
||||
depends = data.get('depends', [])
|
||||
if not isinstance(depends, list):
|
||||
raise TypeError('depends has to be a list')
|
||||
kwargs['depends'] = depends
|
||||
|
||||
kwargs['home_repo'] = data['home_repo'] if 'home_repo' in data else None
|
||||
|
||||
if 'bounds' in data:
|
||||
bounds = data['bounds']
|
||||
if len(bounds) != 2 or len(bounds[0]) != 2 or len(bounds[1]) != 2:
|
||||
raise ValueError('Invalid bounds format.')
|
||||
if not all(isinstance(i, (float, int)) for i in sum(bounds, [])):
|
||||
raise ValueError('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('bounds: lower coordinate has to be first.')
|
||||
else:
|
||||
bounds = (None, None), (None, None)
|
||||
(kwargs['bottom'], kwargs['left']), (kwargs['top'], kwargs['right']) = bounds
|
||||
|
||||
return kwargs
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
def tofilename(self):
|
||||
return 'package.json'
|
||||
|
||||
def tofile(self, form=None):
|
||||
data = OrderedDict()
|
||||
data['name'] = self.name
|
||||
if self.home_repo is not None:
|
||||
data['home_repo'] = self.home_repo
|
||||
if self.depends.exists():
|
||||
data['depends'] = sorted(self.depends.all().order_by('name').values_list('name', flat=True))
|
||||
if self.bottom is not None:
|
||||
data['bounds'] = ((float(self.bottom), float(self.left)), (float(self.top), float(self.right)))
|
||||
|
||||
return data
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
with set_last_mapdata_update():
|
||||
super().save(*args, **kwargs)
|
||||
|
|
|
@ -28,26 +28,3 @@ class Source(MapItem):
|
|||
@property
|
||||
def bounds(self):
|
||||
return (float(self.bottom), float(self.left)), (float(self.top), float(self.right))
|
||||
|
||||
@classmethod
|
||||
def fromfile(cls, data, file_path):
|
||||
kwargs = super().fromfile(data, file_path)
|
||||
|
||||
if 'bounds' not in data:
|
||||
raise ValueError('missing bounds.')
|
||||
|
||||
bounds = data['bounds']
|
||||
if len(bounds) != 2 or len(bounds[0]) != 2 or len(bounds[1]) != 2:
|
||||
raise ValueError('Invalid bounds format.')
|
||||
if not all(isinstance(i, (float, int)) for i in sum(bounds, [])):
|
||||
raise ValueError('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('bounds: lower coordinate has to be first.')
|
||||
(kwargs['bottom'], kwargs['left']), (kwargs['top'], kwargs['right']) = bounds
|
||||
|
||||
return kwargs
|
||||
|
||||
def tofile(self, form=None):
|
||||
result = super().tofile()
|
||||
result['bounds'] = ((float(self.bottom), float(self.left)), (float(self.top), float(self.right)))
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue