diff options
| -rw-r--r-- | dev_status/views.py | 1 | ||||
| -rw-r--r-- | thatcomputerscientist/settings.py | 4 | ||||
| -rw-r--r-- | thatcomputerscientist/sitemaps.py | 79 | ||||
| -rw-r--r-- | thatcomputerscientist/urls.py | 11 |
4 files changed, 94 insertions, 1 deletions
diff --git a/dev_status/views.py b/dev_status/views.py index 5b607436..6a3f00b6 100644 --- a/dev_status/views.py +++ b/dev_status/views.py @@ -1,4 +1,3 @@ -from multiprocessing import context from django.shortcuts import render from github import Github from dotenv import load_dotenv diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py index aac4a3a0..d03769fb 100644 --- a/thatcomputerscientist/settings.py +++ b/thatcomputerscientist/settings.py @@ -44,6 +44,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.sites', + 'django.contrib.sitemaps', 'thatcomputerscientist', 'blog', 'users', @@ -54,6 +56,8 @@ INSTALLED_APPS = [ 'ignis', ] +SITE_ID = 1 + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/thatcomputerscientist/sitemaps.py b/thatcomputerscientist/sitemaps.py new file mode 100644 index 00000000..8c387332 --- /dev/null +++ b/thatcomputerscientist/sitemaps.py @@ -0,0 +1,79 @@ +from django.contrib.sitemaps import Sitemap +from django.urls import reverse +from blog.models import Post, Category, Tag +from github import Github +from dotenv import load_dotenv +import os + +load_dotenv() + +class PostSitemap(Sitemap): + changefreq = "weekly" + priority = 0.9 + protocol = 'http' + + def items(self): + return Post.objects.filter(is_public=True).order_by('id') + + def lastmod(self, obj): + return obj.date + + def location(self, obj): + return reverse('blog:post', args=[obj.slug]) + +class CategorySitemap(Sitemap): + changefreq = "weekly" + priority = 0.9 + protocol = 'http' + + def items(self): + return Category.objects.all().order_by('id') + + def lastmod(self, obj): + return obj.created_at + + def location(self, obj): + return '/articles/categories/%s' % obj.slug + +class TagSitemap(Sitemap): + changefreq = "weekly" + priority = 0.9 + protocol = 'http' + + def items(self): + return Tag.objects.all().order_by('id') + + def lastmod(self, obj): + return obj.created_at + + def location(self, obj): + return '/articles/tags/%s' % obj.slug + +class StaticViewSitemap(Sitemap): + changefreq = "always" + priority = 0.9 + protocol = 'http' + + def items(self): + return ['blog:home', 'blog:register'] + + def location(self, item): + return reverse(item) + +class GithubSitemap(Sitemap): + g = Github(os.getenv('GH_TOKEN')) + changefreq = "always" + priority = 0.9 + protocol = 'http' + + # get list of all public repos + public_repos = g.get_user().get_repos(type='public') + repo_names = [] + for repo in public_repos: + repo_names.append(repo.name) + + def items(self): + return self.repo_names + + def location(self, item): + return '/source/{}'.format(item)
\ No newline at end of file diff --git a/thatcomputerscientist/urls.py b/thatcomputerscientist/urls.py index 7d268bb0..274f7893 100644 --- a/thatcomputerscientist/urls.py +++ b/thatcomputerscientist/urls.py @@ -17,6 +17,16 @@ from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static +from django.contrib.sitemaps.views import sitemap +from .sitemaps import PostSitemap, CategorySitemap, TagSitemap, StaticViewSitemap, GithubSitemap + +sitemaps = { + 'posts': PostSitemap, + 'categories': CategorySitemap, + 'tags': TagSitemap, + 'static': StaticViewSitemap, + 'github': GithubSitemap, +} urlpatterns = [ path('admin/', admin.site.urls), @@ -25,6 +35,7 @@ urlpatterns = [ path('blog-admin/', include('blog_admin.urls', namespace='blog-admin')), path('source/', include(('dev_status.urls', 'dev_status'), namespace='dev_status')), path('ignis/', include(('ignis.urls', 'ignis'), namespace='ignis')), + path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), ] urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) |
