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, is_redirect=False).exclude( title__startswith='User:'******'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('wiki/sitemap.xml', {'urlset': urls})) xml = xml.replace('http://developer.mozilla.org', 'https://developer.mozilla.org') 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+00:00', time.gmtime())) 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()
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:'******'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()
def test_get_protocol_default_warning(self): sitemap = GenericSitemap({'queryset': None}) msg = ("The default sitemap protocol will be changed from 'http' to " "'https' in Django 5.0. Set Sitemap.protocol to silence this " "warning.") with self.assertWarnsMessage(RemovedInDjango50Warning, msg): sitemap.get_protocol()
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', '')
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)
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.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)
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)
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'
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')
def base_unicms_sitemap(request): website = _get_site_from_host(request) protocol = request.scheme webpaths_map = { 'queryset': WebPath.objects.filter(site=website, is_active=True, alias__isnull=True, alias_url=""), 'date_field': 'modified', } # news_map = { # 'queryset': PublicationContext.objects.filter(webpath__site=website, # webpath__is_active=True, # date_start__lte=timezone.localtime(), # date_end__gte=timezone.localtime(), # publication__is_active=True), # 'date_field': 'modified', # } sitemap_dict = { 'webpaths': GenericSitemap(webpaths_map, priority=SITEMAP_WEBPATHS_PRIORITY, protocol=protocol), # 'news': GenericSitemap(news_map, # priority=SITEMAP_NEWS_PRIORITY, # protocol=protocol) } return sitemap_dict
def get_sitemap(): book_dict = { 'queryset': Book.objects.filter(open_yn="Y"), 'date_field': 'create_time', } page_dict = { 'queryset': Page.objects.filter(book__open_yn="Y", open_yn="Y"), 'date_field': 'modify_time', } sitemaps = { 'docs': GenericSitemap(book_dict, priority=0.5), 'page': GenericSitemap(page_dict, priority=0.5), } return sitemaps
def my_sitemap(request, **kwargs): from django.contrib.sitemaps.views import sitemap blog_dict = { 'queryset': BlogPost.objects.published(), 'date_field': 'created', } page_dict = { 'queryset': RichTextPage.objects.published(), } sitemaps = { 'pages': GenericSitemap(page_dict, priority=0.8, changefreq="always"), 'blog': GenericSitemap(blog_dict, priority=0.6, changefreq="always"), } return sitemap(request, sitemaps)
def sitemap(request): sitemaps = { 'pages': GenericSitemap({ 'queryset': Page.objects.filter(status=Page.PUBLISHED), 'date_field': 'last_modification_date', }), 'news': GenericSitemap({ 'queryset': News.objects.filter(show=True), 'date_field': 'date', }), 'news_root': GenericSitemap({ 'queryset': NewsRoot.objects.all(), 'date_field': 'last_modified', }), } return sitemap_view(request, sitemaps, template_name='service/sitemap.xml')
def get_sitemap_dict(site, tags, portfolio_key, projects_key=None, prices_key=None): site_meta = None try: site_meta = SiteMeta.objects.get(site=site) except SiteMeta.DoesNotExist: pass geo_tags = site_meta.tags if site_meta else None result = { 'blog': GenericSitemap(get_blog_dict(tags, geo_tags), priority=0.6), 'static': StaticViewSitemap, 'cases': CaseGenericSitemap( get_portfolio_dict(portfolio_key, geo_tags), priority=0.6, ), } if projects_key: result['projects'] = ProjectGenericSitemap( get_projects_dict(projects_key), priority=0.6, ) if prices_key: result['prices'] = PriceGenericSitemap( get_projects_dict(prices_key), priority=0.6, ) return result
def sitemap(cls, priority=None): from django.contrib.sitemaps import GenericSitemap object_list = { 'queryset': cls.objects.available( ), #TODO make sure this only shows _released objects 'date_field': 'modification_date', } return GenericSitemap(object_list, priority=priority)
def generate_sitemap(): valid_models = filter(lambda m: hasattr(m, 'get_absolute_url'), models.get_models()) all_overrides = SEOPageOverride.objects.all() all_omissions = map(lambda x: x.path, all_overrides.filter(omit=True)) sitemaps = {} for model in valid_models: model_str = "%s.%s" % (model.__module__, model.__name__) defaults = first_of(SEOModelDefault.objects.filter(model=model_str)) # TODO: make this a setting priority = .5 changefreq = "weekly" if defaults: if defaults.omit: continue if defaults.priority: priority = defaults.priority if defaults.changefreq: changefreq = defaults.changefreq allowed_items = model.objects.all() # remove items that match omission regexes for omission in all_omissions: rx = re.compile('^%s$' % omission) allowed_items = map( lambda i: i.pk, filter(lambda x: not rx.match(x.get_absolute_url()), allowed_items)) queryset = model.objects.filter(pk__in=allowed_items) info_dict = {'queryset': queryset} sitemaps[model_str] = GenericSitemap(info_dict, priority=priority, changefreq=changefreq) resolver = urlresolvers.get_resolver(None) # clear previous sitemap.xml entries current_map = filter(lambda y: y.regex.pattern == '^sitemap\\.xml$', resolver.url_patterns) map(lambda x: resolver.url_patterns.remove(x), current_map) # append our new sitemap resolver.url_patterns.append( url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}, name="seo-cascade-sitemap"))
def get_sitemap(cls): """Возвращает объект карты сайта (sitemap). :return: """ if cls.sitemap is None: settings = { 'queryset': cls.model.get_actual(), 'date_field': cls.sitemap_date_field, } cls.sitemap = GenericSitemap(settings, priority=0.5, changefreq=cls.sitemap_changefreq) return cls.sitemap
def school_sitemaps(chunk=2000): """ next we'll attemtp to generate a number of sitemaps in chunks using Paginator and GenericSitemap """ school_sitemap = {} schools = School.objects.all() paginated_schools = Paginator(schools, chunk) for this_page in paginated_schools.page_range: school_dict = { 'queryset': paginated_schools.page(this_page).object_list, 'date_field': 'updated_on', } school_sitemap['schools_%s' % this_page] = GenericSitemap( school_dict, priority=0.6, changefreq='monthly') return school_sitemap
def test_generic_sitemap_attributes(self): datetime_value = datetime.now() queryset = TestModel.objects.all() generic_sitemap = GenericSitemap( info_dict={ 'queryset': queryset, 'date_field': datetime_value, }, priority=0.6, changefreq='monthly', ) attr_values = ( ('date_field', datetime_value), ('priority', 0.6), ('changefreq', 'monthly'), ) for attr_name, expected_value in attr_values: with self.subTest(attr_name=attr_name): self.assertEqual(getattr(generic_sitemap, attr_name), expected_value) self.assertCountEqual(generic_sitemap.queryset, queryset)
def test_generic_sitemap_attributes(self): datetime_value = datetime.now() queryset = TestModel.objects.all() generic_sitemap = GenericSitemap( info_dict={ "queryset": queryset, "date_field": datetime_value, }, priority=0.6, changefreq="monthly", protocol="https", ) attr_values = ( ("date_field", datetime_value), ("priority", 0.6), ("changefreq", "monthly"), ("protocol", "https"), ) for attr_name, expected_value in attr_values: with self.subTest(attr_name=attr_name): self.assertEqual(getattr(generic_sitemap, attr_name), expected_value) self.assertCountEqual(generic_sitemap.queryset, queryset)
from django.contrib.sitemaps import GenericSitemap from django.contrib.auth.models import User from okqa.qa.models import Question from okqa.user.candidates import candidate_group question_dict = { 'queryset': Question.objects.all(), 'date_field': 'updated_at', } candidate_dict = { 'queryset': User.objects.filter(groups__in=[candidate_group]), 'date_field': 'last_login', } sitemaps = { 'questions': GenericSitemap(question_dict, priority=1, changefreq='hourly'), 'candidates': GenericSitemap(candidate_dict, priority=0.6, changefreq='weekly'), }
path("__debug__/", include("debug_toolbar.urls")), path("administration/", admin.site.urls), path("", include("pages.urls")), path("search/", include("scrapers.urls")), path("tailored-vacancies/", include("resume_analyzer.urls")), path( "robots.txt", TemplateView.as_view(template_name="robots.txt", content_type="text/plain")), path( "sitemap.xml", sitemap, { "sitemaps": { "static": StaticViewSitemap, "dynamic": GenericSitemap({"queryset": Job.objects.all()}) } }, name="django.contrib.sitemaps.views.sitemap", ), path("api/", include("api.urls")), path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), path("api/token/verify/", TokenVerifyView.as_view(), name="token_verify"), # Swagger UI for DRF. path("api/schema/", SpectacularAPIView.as_view(api_version="v3"), name="schema"),
name='logout'), path('accounts/reset-password', auth_views.PasswordResetView.as_view(), name="password-reset"), path('accounts/reset-password-done', auth_views.PasswordResetDoneView.as_view(), name="password_reset_done"), path('accounts/reset-password-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), path('accounts/reset-password-complete', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), path('sitemap.xml', sitemap, { 'sitemaps': { 'home': IndexViewSitemap, 'pages': StaticViewSitemap, 'courses': GenericSitemap( {"queryset": CourseCode.objects.all()}) } }, name='django.contrib.sitemaps.views.sitemap'), path('oauth/', include('social_django.urls', namespace='social')), path('', include('scheduler.urls')), path('api/', include('api.urls')), path('rest-api/', include('api.rest_api.urls', namespace='course_api')), path('schedules-rest-api/', include('scheduler.rest_api.urls', namespace='rest_api_scheduler')), ]
path('imagegallery/', include('gallery.urls')), path('', include('newsroom.urls')), path('', include('payment.urls')), path('', include('letters.urls')), path('', include('agony.urls')), path('', include('socialmedia.urls')), path('', include('target.urls')), path('', include('sudoku.urls')), path('', include('analyzer.urls')), path('', include('security.urls')), path('', include('allauth_2fa.urls')), path('accounts/', include('allauth.urls')), url(r'^sitemap\.xml$', sitemap, {'sitemaps': {'articles': GenericSitemap(article_dict, priority=0.5), 'authors': GenericSitemap(author_dict, priority=0.5, changefreq='weekly'), 'photos': GenericSitemap(photo_dict, priority=0.5), 'flatpages': FlatPageSitemap}}, name='django.contrib.sitemaps.views.sitemap'), path('cache/', include('clearcache.urls', namespace="cache")), url(r'^robots\.txt', TemplateView.as_view(template_name='robots.txt', content_type='text/plain'), name='robots.txt'), url(r'^404testing', TemplateView.as_view(template_name='404.html'), name='test404'),
from django.conf import settings from board.models import Post info_dict = { 'queryset': Post.objects.active().filter(), 'date_field': 'created_at', } urlpatterns = [ path('admin', admin.site.urls), # SEO 용 path('sitemap.xml', sitemap, {'sitemaps': { 'board': GenericSitemap(info_dict, priority=0.6) }}, name='django.contrib.sitemaps.views.sitemap'), path( 'robots.txt', lambda x: HttpResponse( "User-Agent: *\nDisallow: /admin/\nDisallow: /account/\nAllow: /", content_type="text/plain")), path( 'ads.txt', lambda x: HttpResponse( "google.com, pub-1987584597836949, DIRECT, f08c47fec0942fa0", content_type="text/plain")), path('notification', include('notification.urls')), path('', include('board.urls')), # 일반 로그인 path('accounts/', include('accounts.urls')),
def __init__(self, info_dict, priority=None, changefreq=None): GenericSitemap.__init__(self, info_dict, priority=priority, changefreq=changefreq) self.suffix = info_dict.get('suffix', '')
urlpatterns = [ path('', include('django.contrib.auth.urls')), path('admin/', admin.site.urls), path('comptes/', include('accounts.urls')), path('', accueil, name='accueil'), path('nous_contacter/', ContactView.as_view(), name='contact'), path('', include('orgues.urls')), path('api-auth/', include('rest_framework.urls')), path('api/v1/', include(('project.api_urls', 'project'), namespace='api')), path('sitemap.xml', sitemap, { 'sitemaps': { 'orgues': GenericSitemap( { 'queryset': Orgue.objects.all(), 'date_field': 'modified_date' }, priority=0.6) } }, name='django.contrib.sitemaps.views.sitemap'), path('lexique/', TemplateView.as_view(template_name='lexique.html'), name='lexique'), path('error/403/', TemplateView.as_view(template_name='403.html'), name='403'), path('error/404/', TemplateView.as_view(template_name='404.html'), name='404'), path('error/500/',
from publish import models, categorie import settings admin.autodiscover() # Regex génériques # urlbegin_re = '([-\w]+/){0,%d}' % categorie.PROFONDEUR_MAX # Sitemap # info_dict = { 'queryset': models.Article.objects.filter(online=True), 'date_field': 'date_parution', } sitemaps = { 'blog': GenericSitemap(info_dict, priority=0.6), } urlpatterns = patterns('', # Index url(r'^$', direct_to_template, {'template': 'index.html', 'extra_context': {'edito' : models.Edito.objects.get_latest_publication()}}, name="index"), # Crawlers url(r'^sitemap.xml$', sitemap, {'sitemaps': sitemaps}, name="sitemap"), url(r'^robots.txt$', lambda _: HttpResponse('User-agent: *\nDisallow:\n', mimetype='text/plain')), #url(r'^favicon.ico$', lambda _: HttpResponseRedirect('%scss/images/logo.png' % settings.MEDIA_URL)), # Admin (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^admin/(.*)', admin.site.root),
info_dict = { 'queryset': Post.objects.filter(published_date__lte=timezone.now(), private=False).order_by('-published_date'), } urlpatterns = [ # My Apps path('', include('blog.urls')), path('pages/', include('pages.urls')), path('polls/', include('polls.urls')), path('todo/', include('todo.urls')), path('s/', include('shortener.urls')), # Django Admin path('admin/doc/', include('django.contrib.admindocs.urls')), path('admin/', admin.site.urls), # User management path('users/', include('users.urls')), path('accounts/', include('allauth.urls')), # Markdownx path(r'markdownx/', include('markdownx.urls')), path('sitemap.xml', sitemap, {'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6)}}, name='django.contrib.sitemaps.views.sitemap'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
priority = 1.0 changefreq = 'daily' def items(self): return ['index-view', 'news-view'] def location(self, item): return reverse(item) sitemaps = { 'article-is-top': GenericSitemap( { 'queryset': Article.objects.filter(status=0, is_top=True).all(), 'date_field': 'pub_time' }, priority=1.0, changefreq='daily'), 'article-is-not-top': GenericSitemap( { 'queryset': Article.objects.filter(status=0).all(), 'date_field': 'pub_time' }, priority=0.8, changefreq='daily'), 'news': GenericSitemap({ 'queryset': News.objects.all(), 'data_field': 'pub_time'
blog_dict = { 'queryset': Post.objects.filter(status=Post.PUBLISHED), 'date_field': 'created', } page_dict = { 'queryset': Page.objects.filter(visible=True), 'date_field': 'created' } urlpatterns = [ path('sitemap.xml', sitemap, { 'sitemaps': { 'blog': GenericSitemap( blog_dict, priority=1, protocol='https'), 'page': GenericSitemap( page_dict, priority=0.5, protocol='https') } }, name='django.contrib.sitemaps.views.sitemap'), path('redactor/', include('redactor.urls')), path('cpadmin/', admin.site.urls), path('subscribe/', SubscriptionView.as_view(), name='subscription'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += i18n_patterns( path('notes/', include('apps.notes.urls', namespace='notes')), path('', include('apps.blog.urls', namespace='blog')), )
return article.pubdate else: return article.update def location(self, article): return article.get_absolute_url_online() sitemaps = { 'tutos': TutoSitemap, 'articles': ArticleSitemap, 'categories': GenericSitemap({'queryset': Category.objects.all()}, changefreq='yearly', priority=0.7), 'forums': GenericSitemap( { 'queryset': Forum.objects.filter(group__isnull=True).exclude( pk=settings.ZDS_APP['forum']['beta_forum_id']) }, changefreq='yearly', priority=0.7), 'topics': GenericSitemap( { 'queryset': Topic.objects.filter(
""" Sitemap for person pages app. """ from django.contrib.sitemaps import GenericSitemap from .models import PersonTag, TagGroup TagGroup_Sitemap = GenericSitemap({"queryset": TagGroup.objects.active()}) PersonTag_Sitemap = GenericSitemap({"queryset": PersonTag.objects.active()})
from django.conf.urls import url, include from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.conf import settings from django.urls import path from django.contrib import admin from article.views import SiteIndexView from django.contrib.sitemaps.views import sitemap from django.contrib.sitemaps import GenericSitemap from article.models import Article urlpatterns = [ path('', SiteIndexView.as_view(), name='home'), path('admin/', admin.site.urls), path('ckeditor/', include('ckeditor_uploader.urls')), path('articles/', include('article.urls.view_urls', 'article')), path('api/', include('article.urls.api_urls')), path('user/', include('user.urls', 'user')), path('sitemap.xml', sitemap, { 'sitemaps': { 'article': GenericSitemap(dict(queryset=Article.objects.all(), date_field='pub_date'), priority=0.6) } }, name='django.contrib.sitemaps.views.sitemap'), ] + static('/uploads', document_root=settings.CKEDITOR_UPLOAD_PATH) urlpatterns += staticfiles_urlpatterns()
def location(self, obj): url = GenericSitemap.location(self, obj) return "%s%s" % (url, self.suffix)