From 0be38ac4661df5f0cb0f425b7434466f4d0676f2 Mon Sep 17 00:00:00 2001 From: SDE Date: Sat, 29 Jun 2024 16:34:36 +0300 Subject: [PATCH] 1.4.0 sitemap --- GeneralApp/funcs_options.py | 18 ++--- SitemapApp/__init__.py | 0 SitemapApp/models.py | 3 + SitemapApp/tests.py | 16 ++++ SitemapApp/urls.py | 11 +++ SitemapApp/views.py | 149 ++++++++++++++++++++++++++++++++++++ TWB/settings.py | 2 + TWB/urls.py | 4 +- 8 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 SitemapApp/__init__.py create mode 100644 SitemapApp/models.py create mode 100644 SitemapApp/tests.py create mode 100644 SitemapApp/urls.py create mode 100644 SitemapApp/views.py diff --git a/GeneralApp/funcs_options.py b/GeneralApp/funcs_options.py index 80bb858..efc2171 100644 --- a/GeneralApp/funcs_options.py +++ b/GeneralApp/funcs_options.py @@ -13,15 +13,15 @@ def get_options_by_opt_types(opt_types, only_vals=False): res = {} opts = opts.values('opt_type', 'value', 'prefix') for item in opts: - if item['opt_type'] == 'domain': - - try: - from django.contrib.sites.models import Site - current_site = Site.objects.get_current() - res.update({item['opt_type']: current_site.domain}) - continue - except Exception as e: - print(str(e)) + # if item['opt_type'] == 'domain': + # + # try: + # from django.contrib.sites.models import Site + # current_site = Site.objects.get_current() + # res.update({item['opt_type']: current_site.domain}) + # # continue + # except Exception as e: + # print(str(e)) if item['prefix']: res.update({item['opt_type']: f"{item['prefix']}{item['value']}"}) diff --git a/SitemapApp/__init__.py b/SitemapApp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/SitemapApp/models.py b/SitemapApp/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/SitemapApp/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/SitemapApp/tests.py b/SitemapApp/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/SitemapApp/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/SitemapApp/urls.py b/SitemapApp/urls.py new file mode 100644 index 0000000..c4f2fc6 --- /dev/null +++ b/SitemapApp/urls.py @@ -0,0 +1,11 @@ +from django.urls import include, path +from django.contrib.sitemaps import views as sitemaps_views +from django.views.decorators.cache import cache_page + +from django.contrib.sitemaps.views import sitemap +from SitemapApp.views import sitemaps + +urlpatterns = [ + path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, + name='django.contrib.sitemaps.views.sitemap') +] diff --git a/SitemapApp/views.py b/SitemapApp/views.py new file mode 100644 index 0000000..364f0e8 --- /dev/null +++ b/SitemapApp/views.py @@ -0,0 +1,149 @@ +# coding=utf-8 +from django.contrib.sitemaps import Sitemap +from django.urls import reverse + +from BaseModels.mailSender import techSendMail +import json +from datetime import datetime, time, timezone +# from PageSetsApp.models import * +# from ArticlesApp.models import * +# from BaseModels.base_api_requests import base_api_request +# from tEsiteProj.settings import API_URL +from django.db.models import Q + + +limit_records = 1000 + + + +sitemaps = { + +} + +protocol = 'https' + +class sm_StaticPage(Sitemap): + changefreq = 'monthly' + priority = 1 + i18n = True + protocol = protocol + + def items(self): + from GeneralApp.models import StaticPage + return StaticPage.objects.filter(enable=True) + + def location(self, item): + if item.url == 'main': + return reverse('main') + else: + return reverse('static_page', args=[item.url]) + + def lastmod(self, obj): + return obj.modifiedDT + +sitemaps.update({'static_pages': sm_StaticPage}) + + + +class sm_ArticlesPage(Sitemap): + changefreq = 'daily' + priority = 2 + i18n = True + protocol = protocol + + def items(self): + return [''] + + def location(self, item): + return reverse('articles') + + def lastmod(self, obj): + from ArticlesApp.models import ArticleModel + article = ArticleModel.objects.filter(enable=True).order_by('-modifiedDT').first() + if article: + return article.modifiedDT + else: + return datetime.now() + +sitemaps.update({'articles_page': sm_ArticlesPage}) + + + +class sm_Article(Sitemap): + changefreq = 'yearly' + priority = 2 + i18n = True + protocol = protocol + + def items(self): + from ArticlesApp.views import ArticleModel + objs = ArticleModel.objects.filter(enable=True) + + return objs + + def location(self, item): + return reverse('article_one', args=[item.url]) + + + def lastmod(self, obj): + return obj.modifiedDT + +sitemaps.update({'article': sm_Article}) + + +class sm_UserPage(Sitemap): + changefreq = 'yearly' + priority = 2 + i18n = True + protocol = protocol + + def items(self): + from ArticlesApp.views import UserPageModel + objs = UserPageModel.objects.filter(enable=True) + + return objs + + def location(self, item): + return reverse('user_page', args=[item.url]) + + + def lastmod(self, obj): + return obj.modifiedDT + +sitemaps.update({'user_page': sm_UserPage}) + + +class sm_Registration(Sitemap): + changefreq = 'never' + priority = 1 + i18n = True + protocol = protocol + + def items(self): + return [''] + + def location(self, item): + return reverse('registration_page') + + def lastmod(self, obj): + return datetime(2024, 6, 1) + +sitemaps.update({'registration': sm_Registration}) + + +class sm_Login(Sitemap): + changefreq = 'never' + priority = 1 + i18n = True + protocol = protocol + + def items(self): + return [''] + + def location(self, item): + return reverse('login_profile') + + def lastmod(self, obj): + return datetime(2024, 6, 1) + +sitemaps.update({'login': sm_Login}) \ No newline at end of file diff --git a/TWB/settings.py b/TWB/settings.py index 06af68e..83a4419 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -95,6 +95,8 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'django.contrib.humanize', + 'django.contrib.sitemaps', + 'django.contrib.sites', 'colorfield', diff --git a/TWB/urls.py b/TWB/urls.py index 4b96b79..de99189 100644 --- a/TWB/urls.py +++ b/TWB/urls.py @@ -40,7 +40,9 @@ urlpatterns = [ path('test_404', Page404, name='page_404'), - path('', include('PushMessages.urls')) + path('', include('PushMessages.urls')), + + path('', include('SitemapApp.urls')), ] from django.conf.urls.i18n import i18n_patterns