Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sitemap #473

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'django_extensions',
'mathfilters',
'django_filters',
'django.contrib.sitemaps'
]

MIDDLEWARE = [
Expand Down Expand Up @@ -288,3 +289,5 @@
'LOCATION': 'unique-snowflake',
}
}

SITE_ID = 1
4 changes: 4 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.conf import settings
from django.contrib.sitemaps.views import sitemap
from django.contrib.staticfiles.views import serve
from django.urls import include, path
from django.views.i18n import JavaScriptCatalog

from contributors.admin.custom import site
from contributors.sitemap import sitemaps


def trigger_error(request):
Expand All @@ -20,6 +22,8 @@ def trigger_error(request):
path('', include('contributors.urls')),
path('sentry-debug/', trigger_error),
path('robots.txt', serve, {'path': 'robots.txt'}),
path('sitemap.xml', sitemap, {'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
]

if settings.DEBUG:
Expand Down
78 changes: 78 additions & 0 deletions contributors/sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from django.contrib.sitemaps import Sitemap
from django.urls import reverse

from contributors.models import (
Contributor,
Organization,
Project,
Repository,
)


class StaticViewSitemap(Sitemap):
priority = 0.5
changefreq = 'weekly'

def items(self):
return [
'contributors:home',
'contributors:about',
'contributors:landing',
'contributors:achievements'
]

def location(self, item):
return reverse(item)


class OrganizationSitemap(Sitemap):
changefreq = "monthly"
priority = 0.7

def items(self):
return Organization.objects.all()

def location(self, item):
return reverse('contributors:organization_details', args=[item.name])


class RepositorySitemap(Sitemap):
changefreq = "weekly"
priority = 0.7

def items(self):
return Repository.objects.all()

def location(self, item):
return reverse('contributors:repository_details', args=[item.name])


class ContributorSitemap(Sitemap):
changefreq = "weekly"
priority = 0.6

def items(self):
return Contributor.objects.all()

def location(self, item):
return reverse('contributors:contributor_details', args=[item.login])


class ProjectSitemap(Sitemap):
changefreq = "weekly"
priority = 0.6

def items(self):
return Project.objects.all()

def location(self, item):
return reverse('contributors:project_details', args=[item.pk])


sitemaps = {
'static': StaticViewSitemap,
'organizations': OrganizationSitemap,
'repositories': RepositorySitemap,
'contributors': ContributorSitemap,
'projects': ProjectSitemap,
}
23 changes: 11 additions & 12 deletions static/robots.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
User-agent: *
Allow: /repositories/$
Allow: /issues/$
Allow: /organizations/$
Allow: /leaderboard/$
Allow: /contributors/$
Allow: /projects/$
Disallow: /repositories/?
Disallow: /issues/?
Disallow: /organizations/?
Disallow: /leaderboard/?
Disallow: /contributors/?
Disallow: /projects/?
Disallow: /repositories/*?
Disallow: /issues/*?
Disallow: /organizations/*?
Disallow: /leaderboard/*?
Disallow: /contributors/*?
Disallow: /projects/*?
Disallow: /*page=
Disallow: /*sort=
Disallow: /*labels=
Disallow: /admin/
Crawl-delay: 10

Sitemap: https://hexlet-friends.herokuapp.com/sitemap.xml
Loading