2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2008-08-24 03:22:23 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.sitemaps import Sitemap
|
|
|
|
from django.core import urlresolvers
|
2008-08-24 03:22:23 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
class KMLSitemap(Sitemap):
|
|
|
|
"""
|
|
|
|
A minimal hook to produce KML sitemaps.
|
|
|
|
"""
|
|
|
|
geo_format = 'kml'
|
|
|
|
|
|
|
|
def __init__(self, locations=None):
|
|
|
|
# If no locations specified, then we try to build for
|
|
|
|
# every model in installed applications.
|
|
|
|
self.locations = self._build_kml_sources(locations)
|
2013-10-11 04:42:30 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def _build_kml_sources(self, sources):
|
|
|
|
"""
|
|
|
|
Goes through the given sources and returns a 3-tuple of
|
|
|
|
the application label, module name, and field name of every
|
|
|
|
GeometryField encountered in the sources.
|
|
|
|
|
|
|
|
If no sources are provided, then all models.
|
|
|
|
"""
|
|
|
|
kml_sources = []
|
|
|
|
if sources is None:
|
2013-12-24 19:25:17 +08:00
|
|
|
sources = apps.get_models()
|
2008-08-24 03:22:23 +08:00
|
|
|
for source in sources:
|
|
|
|
if isinstance(source, models.base.ModelBase):
|
|
|
|
for field in source._meta.fields:
|
|
|
|
if isinstance(field, GeometryField):
|
|
|
|
kml_sources.append((source._meta.app_label,
|
2013-02-05 17:16:07 +08:00
|
|
|
source._meta.model_name,
|
2008-08-24 03:22:23 +08:00
|
|
|
field.name))
|
|
|
|
elif isinstance(source, (list, tuple)):
|
2013-10-11 04:42:30 +08:00
|
|
|
if len(source) != 3:
|
2008-08-24 03:22:23 +08:00
|
|
|
raise ValueError('Must specify a 3-tuple of (app_label, module_name, field_name).')
|
|
|
|
kml_sources.append(source)
|
|
|
|
else:
|
|
|
|
raise TypeError('KML Sources must be a model or a 3-tuple.')
|
|
|
|
return kml_sources
|
|
|
|
|
2014-03-23 01:31:52 +08:00
|
|
|
def get_urls(self, page=1, site=None, protocol=None):
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
This method is overrridden so the appropriate `geo_format` attribute
|
|
|
|
is placed on each URL element.
|
|
|
|
"""
|
2014-03-23 01:31:52 +08:00
|
|
|
urls = Sitemap.get_urls(self, page=page, site=site, protocol=protocol)
|
2013-10-17 16:17:41 +08:00
|
|
|
for url in urls:
|
|
|
|
url['geo_format'] = self.geo_format
|
2008-08-24 03:22:23 +08:00
|
|
|
return urls
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return self.locations
|
|
|
|
|
|
|
|
def location(self, obj):
|
|
|
|
return urlresolvers.reverse('django.contrib.gis.sitemaps.views.%s' % self.geo_format,
|
2013-10-27 09:27:42 +08:00
|
|
|
kwargs={'label': obj[0],
|
|
|
|
'model': obj[1],
|
2008-08-24 03:22:23 +08:00
|
|
|
'field_name': obj[2],
|
|
|
|
}
|
|
|
|
)
|
2013-11-03 01:18:46 +08:00
|
|
|
|
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
class KMZSitemap(KMLSitemap):
|
|
|
|
geo_format = 'kmz'
|