本文整理汇总了Python中django.contrib.sitemaps.GenericSitemap类的典型用法代码示例。如果您正苦于以下问题:Python GenericSitemap类的具体用法?Python GenericSitemap怎么用?Python GenericSitemap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GenericSitemap类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_sitemaps
def build_sitemaps():
sitemap_element = "<sitemap><loc>%s</loc><lastmod>%s</lastmod></sitemap>"
sitemap_index = "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
for locale in settings.MDN_LANGUAGES:
queryset = (Document.objects
.filter(is_template=False, locale=locale)
.exclude(title__startswith='User:')
.exclude(title__iregex=r'Redirect [0-9]+$')
.exclude(html__iregex=r'^(<p>)?(#)?REDIRECT')
.exclude(slug__icontains='Talk:')
)
if len(queryset) > 0:
info = {'queryset': queryset, 'date_field': 'modified'}
sitemap = GenericSitemap(info, priority=0.5)
urls = sitemap.get_urls(page=1)
xml = smart_str(loader.render_to_string('sitemap.xml',
{'urlset': urls}))
xml = xml.replace('http://', 'https://')
directory = '%s/sitemaps/%s' % (settings.MEDIA_ROOT, locale)
if not os.path.exists(directory):
os.makedirs(directory)
f = open('%s/sitemap.xml' % directory, 'w')
f.write(xml)
f.close()
sitemap_url = ("https://%s/sitemaps/%s/sitemap.xml" % (
Site.objects.get_current().domain, locale))
sitemap_index = sitemap_index + sitemap_element % (sitemap_url,
time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime()))
sitemap_index = sitemap_index + "</sitemapindex>"
index_file = open('%s/sitemap.xml' % settings.MEDIA_ROOT, 'w')
index_file.write(parseString(sitemap_index).toxml())
index_file.close()
示例2: __init__
def __init__(self, info_dict, priority=None, changefreq=None):
GenericSitemap.__init__(self,
info_dict,
priority=priority,
changefreq=changefreq)
self.url = info_dict.get('url', '/')
self.slugfield = info_dict['slugfield']
self.suffix = info_dict.get('suffix', '')
示例3: test_sitemap_item
def test_sitemap_item(self):
"""
Check to make sure that the raw item is included with each
Sitemap.get_url() url result.
"""
user_sitemap = GenericSitemap({'queryset': User.objects.all()})
def is_user(url):
return isinstance(url['item'], User)
item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
self.assertTrue(item_in_url_info)
示例4: test_sitemap_item
def test_sitemap_item(self):
"""
Check to make sure that the raw item is included with each
Sitemap.get_url() url result.
"""
test_sitemap = GenericSitemap({'queryset': TestModel.objects.order_by('pk').all()})
def is_testmodel(url):
return isinstance(url['item'], TestModel)
item_in_url_info = all(map(is_testmodel, test_sitemap.get_urls()))
self.assertTrue(item_in_url_info)
示例5: generate
def generate():
sitemap = GenericSitemap({'queryset': models.Post.objects.filter(type__in=const.POST_TOPLEVEL).exclude(type=const.POST_BLOG), })
urlset = sitemap.get_urls()
text = loader.render_to_string('sitemap.xml', {'urlset': urlset})
text = smart_str(text)
site = Site.objects.get_current()
fname = path(settings.EXPORT_DIR, 'sitemap.xml')
print '*** writing sitemap for %s to %s' % (site, fname)
fp = open(fname, 'wt')
fp.write(text)
fp.close()
print '*** done'
示例6: generate_sitemap
def generate_sitemap():
sitemap = GenericSitemap({
'queryset': Post.objects.filter(type__in=Post.TOP_LEVEL).exclude(type=Post.BLOG),
})
urlset = sitemap.get_urls()
text = loader.render_to_string('sitemap.xml', {'urlset': urlset})
text = smart_str(text)
site = Site.objects.get_current()
fname = path(settings.STATIC_ROOT, 'sitemap.xml')
logger.info('*** writing sitemap for %s to %s' % (site, fname))
fp = open(fname, 'wt')
fp.write(text)
fp.close()
logger.info('*** done')
示例7: location
def location(self, obj):
url = GenericSitemap.location(self, obj)
return "%s%s" % (url, self.suffix)