diff options
author | ttt | 2017-05-13 00:29:47 +0530 |
---|---|---|
committer | ttt | 2017-05-13 00:29:47 +0530 |
commit | abf599be33b383a6a5baf9493093b2126a622ac8 (patch) | |
tree | 4c5ab6e0d935d5e65fabcf0258e4a00dd20a5afa /lib/python2.7/site-packages/django/contrib/gis/sitemaps | |
download | SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.gz SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.tar.bz2 SBHS-2018-Rpi-abf599be33b383a6a5baf9493093b2126a622ac8.zip |
added all server files
Diffstat (limited to 'lib/python2.7/site-packages/django/contrib/gis/sitemaps')
4 files changed, 234 insertions, 0 deletions
diff --git a/lib/python2.7/site-packages/django/contrib/gis/sitemaps/__init__.py b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/__init__.py new file mode 100644 index 0000000..9b6287f --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/__init__.py @@ -0,0 +1,4 @@ +# Geo-enabled Sitemap classes. +from django.contrib.gis.sitemaps.georss import GeoRSSSitemap +from django.contrib.gis.sitemaps.kml import KMLSitemap, KMZSitemap + diff --git a/lib/python2.7/site-packages/django/contrib/gis/sitemaps/georss.py b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/georss.py new file mode 100644 index 0000000..ccb7dc2 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/georss.py @@ -0,0 +1,53 @@ +from django.core import urlresolvers +from django.contrib.sitemaps import Sitemap + +class GeoRSSSitemap(Sitemap): + """ + A minimal hook to produce sitemaps for GeoRSS feeds. + """ + def __init__(self, feed_dict, slug_dict=None): + """ + This sitemap object initializes on a feed dictionary (as would be passed + to `django.contrib.gis.views.feed`) and a slug dictionary. + If the slug dictionary is not defined, then it's assumed the keys provide + the URL parameter to the feed. However, if you have a complex feed (e.g., + you override `get_object`, then you'll need to provide a slug dictionary. + The slug dictionary should have the same keys as the feed dictionary, but + each value in the slug dictionary should be a sequence of slugs that may + be used for valid feeds. For example, let's say we have a feed that + returns objects for a specific ZIP code in our feed dictionary: + + feed_dict = {'zipcode' : ZipFeed} + + Then we would use a slug dictionary with a list of the zip code slugs + corresponding to feeds you want listed in the sitemap: + + slug_dict = {'zipcode' : ['77002', '77054']} + """ + # Setting up. + self.feed_dict = feed_dict + self.locations = [] + if slug_dict is None: slug_dict = {} + # Getting the feed locations. + for section in feed_dict.keys(): + if slug_dict.get(section, False): + for slug in slug_dict[section]: + self.locations.append('%s/%s' % (section, slug)) + else: + self.locations.append(section) + + def get_urls(self, page=1, site=None): + """ + This method is overrridden so the appropriate `geo_format` attribute + is placed on each URL element. + """ + urls = Sitemap.get_urls(self, page=page, site=site) + for url in urls: url['geo_format'] = 'georss' + return urls + + def items(self): + return self.locations + + def location(self, obj): + return urlresolvers.reverse('django.contrib.gis.views.feed', args=(obj,)) + diff --git a/lib/python2.7/site-packages/django/contrib/gis/sitemaps/kml.py b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/kml.py new file mode 100644 index 0000000..837fe62 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/kml.py @@ -0,0 +1,63 @@ +from django.core import urlresolvers +from django.contrib.sitemaps import Sitemap +from django.contrib.gis.db.models.fields import GeometryField +from django.db import models + +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) + + 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: + sources = models.get_models() + 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, + source._meta.model_name, + field.name)) + elif isinstance(source, (list, tuple)): + if len(source) != 3: + 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 + + def get_urls(self, page=1, site=None): + """ + This method is overrridden so the appropriate `geo_format` attribute + is placed on each URL element. + """ + urls = Sitemap.get_urls(self, page=page, site=site) + for url in urls: url['geo_format'] = self.geo_format + return urls + + def items(self): + return self.locations + + def location(self, obj): + return urlresolvers.reverse('django.contrib.gis.sitemaps.views.%s' % self.geo_format, + kwargs={'label' : obj[0], + 'model' : obj[1], + 'field_name': obj[2], + } + ) +class KMZSitemap(KMLSitemap): + geo_format = 'kmz' diff --git a/lib/python2.7/site-packages/django/contrib/gis/sitemaps/views.py b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/views.py new file mode 100644 index 0000000..9682c12 --- /dev/null +++ b/lib/python2.7/site-packages/django/contrib/gis/sitemaps/views.py @@ -0,0 +1,114 @@ +from __future__ import unicode_literals + +from django.http import HttpResponse, Http404 +from django.template import loader +from django.contrib.sites.models import get_current_site +from django.core import urlresolvers +from django.core.paginator import EmptyPage, PageNotAnInteger +from django.contrib.gis.db.models.fields import GeometryField +from django.db import connections, DEFAULT_DB_ALIAS +from django.db.models import get_model +from django.utils import six +from django.utils.translation import ugettext as _ + +from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz + +def index(request, sitemaps): + """ + This view generates a sitemap index that uses the proper view + for resolving geographic section sitemap URLs. + """ + current_site = get_current_site(request) + sites = [] + protocol = 'https' if request.is_secure() else 'http' + for section, site in sitemaps.items(): + if callable(site): + pages = site().paginator.num_pages + else: + pages = site.paginator.num_pages + sitemap_url = urlresolvers.reverse('django.contrib.gis.sitemaps.views.sitemap', kwargs={'section': section}) + sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) + + if pages > 1: + for page in range(2, pages+1): + sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) + xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) + return HttpResponse(xml, content_type='application/xml') + +def sitemap(request, sitemaps, section=None): + """ + This view generates a sitemap with additional geographic + elements defined by Google. + """ + maps, urls = [], [] + if section is not None: + if section not in sitemaps: + raise Http404(_("No sitemap available for section: %r") % section) + maps.append(sitemaps[section]) + else: + maps = list(six.itervalues(sitemaps)) + + page = request.GET.get("p", 1) + current_site = get_current_site(request) + for site in maps: + try: + if callable(site): + urls.extend(site().get_urls(page=page, site=current_site)) + else: + urls.extend(site.get_urls(page=page, site=current_site)) + except EmptyPage: + raise Http404(_("Page %s empty") % page) + except PageNotAnInteger: + raise Http404(_("No page '%s'") % page) + xml = loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls}) + return HttpResponse(xml, content_type='application/xml') + +def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS): + """ + This view generates KML for the given app label, model, and field name. + + The model's default manager must be GeoManager, and the field name + must be that of a geographic field. + """ + placemarks = [] + klass = get_model(label, model) + if not klass: + raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model)) + + if field_name: + try: + info = klass._meta.get_field_by_name(field_name) + if not isinstance(info[0], GeometryField): + raise Exception + except: + raise Http404('Invalid geometry field.') + + connection = connections[using] + + if connection.ops.postgis: + # PostGIS will take care of transformation. + placemarks = klass._default_manager.using(using).kml(field_name=field_name) + else: + # There's no KML method on Oracle or MySQL, so we use the `kml` + # attribute of the lazy geometry instead. + placemarks = [] + if connection.ops.oracle: + qs = klass._default_manager.using(using).transform(4326, field_name=field_name) + else: + qs = klass._default_manager.using(using).all() + for mod in qs: + mod.kml = getattr(mod, field_name).kml + placemarks.append(mod) + + # Getting the render function and rendering to the correct. + if compress: + render = render_to_kmz + else: + render = render_to_kml + return render('gis/kml/placemarks.kml', {'places' : placemarks}) + +def kmz(request, label, model, field_name=None, using=DEFAULT_DB_ALIAS): + """ + This view returns KMZ for the given app label, model, and field name. + """ + return kml(request, label, model, field_name, compress=True, using=using) |