<s> from __future__ import unicode_literals <EOL> from django . http import HttpResponse , Http404 <EOL> from django . template import loader <EOL> from django . contrib . sites . models import get_current_site <EOL> from django . core import urlresolvers <EOL> from django . core . paginator import EmptyPage , PageNotAnInteger <EOL> from django . contrib . gis . db . models . fields import GeometryField <EOL> from django . db import connections , DEFAULT_DB_ALIAS <EOL> from django . db . models import get_model <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext as _ <EOL> from django . contrib . gis . shortcuts import render_to_kml , render_to_kmz <EOL> def index ( request , sitemaps ) : <EOL> """<STR_LIT>""" <EOL> current_site = get_current_site ( request ) <EOL> sites = [ ] <EOL> protocol = '<STR_LIT>' if request . is_secure ( ) else '<STR_LIT:http>' <EOL> for section , site in sitemaps . items ( ) : <EOL> if callable ( site ) : <EOL> pages = site ( ) . paginator . num_pages <EOL> else : <EOL> pages = site . paginator . num_pages <EOL> sitemap_url = urlresolvers . reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : section } ) <EOL> sites . append ( '<STR_LIT>' % ( protocol , current_site . domain , sitemap_url ) ) <EOL> if pages > <NUM_LIT:1> : <EOL> for page in range ( <NUM_LIT:2> , pages + <NUM_LIT:1> ) : <EOL> sites . append ( '<STR_LIT>' % ( protocol , current_site . domain , sitemap_url , page ) ) <EOL> xml = loader . render_to_string ( '<STR_LIT>' , { '<STR_LIT>' : sites } ) <EOL> return HttpResponse ( xml , content_type = '<STR_LIT>' ) <EOL> def sitemap ( request , sitemaps , section = None ) : <EOL> """<STR_LIT>""" <EOL> maps , urls = [ ] , [ ] <EOL> if section is not None : <EOL> if section not in sitemaps : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % section ) <EOL> maps . append ( sitemaps [ section ] ) <EOL> else : <EOL> maps = list ( six . itervalues ( sitemaps ) ) <EOL> page = request . GET . get ( "<STR_LIT:p>" , <NUM_LIT:1> ) <EOL> current_site = get_current_site ( request ) <EOL> for site in maps : <EOL> try : <EOL> if callable ( site ) : <EOL> urls . extend ( site ( ) . get_urls ( page = page , site = current_site ) ) <EOL> else : <EOL> urls . extend ( site . get_urls ( page = page , site = current_site ) ) <EOL> except EmptyPage : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % page ) <EOL> except PageNotAnInteger : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % page ) <EOL> xml = loader . render_to_string ( '<STR_LIT>' , { '<STR_LIT>' : urls } ) <EOL> return HttpResponse ( xml , content_type = '<STR_LIT>' ) <EOL> def kml ( request , label , model , field_name = None , compress = False , using = DEFAULT_DB_ALIAS ) : <EOL> """<STR_LIT>""" <EOL> placemarks = [ ] <EOL> klass = get_model ( label , model ) <EOL> if not klass : <EOL> raise Http404 ( '<STR_LIT>' % ( label , model ) ) <EOL> if field_name : <EOL> try : <EOL> info = klass . _meta . get_field_by_name ( field_name ) <EOL> if not isinstance ( info [ <NUM_LIT:0> ] , GeometryField ) : <EOL> raise Exception <EOL> except : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> connection = connections [ using ] <EOL> if connection . ops . postgis : <EOL> placemarks = klass . _default_manager . using ( using ) . kml ( field_name = field_name ) <EOL> else : <EOL> placemarks = [ ] <EOL> if connection . ops . oracle : <EOL> qs = klass . _default_manager . using ( using ) . transform ( <NUM_LIT> , field_name = field_name ) <EOL> else : <EOL> qs = klass . _default_manager . using ( using ) . all ( ) <EOL> for mod in qs : <EOL> mod . kml = getattr ( mod , field_name ) . kml <EOL> placemarks . append ( mod ) <EOL> if compress : <EOL> render = render_to_kmz <EOL> else : <EOL> render = render_to_kml <EOL> return render ( '<STR_LIT>' , { '<STR_LIT>' : placemarks } ) <EOL> def kmz ( request , label , model , field_name = None , using = DEFAULT_DB_ALIAS ) : <EOL> """<STR_LIT>""" <EOL> return kml ( request , label , model , field_name , compress = True , using = using ) </s>
