diff --git a/blog/models.py b/blog/models.py index a315695..12e8b33 100644 --- a/blog/models.py +++ b/blog/models.py @@ -3,6 +3,9 @@ from django.urls import reverse from django.utils import timezone from django.contrib.auth import get_user_model from taggit.managers import TaggableManager +from django.utils.html import strip_tags +import re + User = get_user_model() @@ -67,8 +70,8 @@ class Article(models.Model): def get_seo_description(self): if self.meta_description: return self.meta_description - clean = self.content[:160].replace("\n", " ").strip() - return f"{clean}..." + clean = re.sub(r'\s+', ' ', strip_tags(self.content)).strip() + return clean[:157] + '…' if len(clean) > 160 else clean class Comment(models.Model): """Комментарий к статье""" diff --git a/programmer/sitemaps.py b/programmer/sitemaps.py index e71f587..193d5a2 100644 --- a/programmer/sitemaps.py +++ b/programmer/sitemaps.py @@ -1,31 +1,27 @@ from django.contrib.sitemaps import Sitemap from django.urls import reverse -from .models import Home, Solution, Competence, Recall -class StaticViewSitemap(Sitemap): +from blog.models import Article +from .models import Solution, Competence, Recall + + +class HttpsSitemap(Sitemap): + """Base class that forces https on all sitemap URLs.""" + protocol = 'https' + + +class StaticViewSitemap(HttpsSitemap): priority = 1.0 changefreq = 'monthly' def items(self): - return ['home', 'about', 'solution', 'ability', 'recall'] + return ['home', 'about', 'solution', 'blog:article_list', 'recall'] def location(self, item): return reverse(item) -class HomeSitemap(Sitemap): - changefreq = 'weekly' - priority = 1.0 - def items(self): - return Home.objects.filter(is_published=True) - - def lastmod(self, obj): - return obj.time_update - - # УБИРАЕМ метод location - используем default - # Django автоматически сгенерирует правильные URL - -class SolutionSitemap(Sitemap): +class SolutionSitemap(HttpsSitemap): changefreq = 'weekly' priority = 0.9 @@ -35,7 +31,8 @@ class SolutionSitemap(Sitemap): def lastmod(self, obj): return obj.time_update -class CompetenceSitemap(Sitemap): + +class CompetenceSitemap(HttpsSitemap): changefreq = 'monthly' priority = 0.8 @@ -45,7 +42,8 @@ class CompetenceSitemap(Sitemap): def lastmod(self, obj): return obj.time_update -class RecallSitemap(Sitemap): + +class RecallSitemap(HttpsSitemap): changefreq = 'monthly' priority = 0.7 @@ -54,11 +52,24 @@ class RecallSitemap(Sitemap): def lastmod(self, obj): return obj.time_update + +class BlogSitemap(HttpsSitemap): + changefreq = 'weekly' + priority = 0.9 + + def items(self): + return Article.objects.filter(is_published=True).select_related('category') + + def lastmod(self, obj): + return obj.time_update + + # Упрощаем sitemaps - убираем HomeSitemap если он дублирует главную sitemaps = { 'static': StaticViewSitemap, 'solutions': SolutionSitemap, - 'competence': CompetenceSitemap, + 'blog': BlogSitemap, + # 'competence': CompetenceSitemap, 'recall': RecallSitemap, } \ No newline at end of file diff --git a/programmer/urls.py b/programmer/urls.py index d27b2e0..8b149c2 100644 --- a/programmer/urls.py +++ b/programmer/urls.py @@ -23,8 +23,7 @@ urlpatterns = [ path('callback/', callback_request, name='callback'), path('admin/statistics/', statistics_view, name='statistics'), # Sitemap - path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, - name='django.contrib.sitemaps.views.sitemap'), + path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'), path('robots.txt', robots_txt, name='robots'), path('login/', auth_views.LoginView.as_view(template_name='programmer/login.html'), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'),