From 13a762341f349e5a9aa17c049cb0803d12f4408d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Wed, 20 Jun 2018 06:26:37 +0300 Subject: added feed class, have to finish adding functions for items --- weblog/feeds.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 weblog/feeds.py (limited to 'weblog') diff --git a/weblog/feeds.py b/weblog/feeds.py new file mode 100644 index 0000000..8510813 --- /dev/null +++ b/weblog/feeds.py @@ -0,0 +1,65 @@ +from django.contrib.syndication.views. import Feed +from django.urls import reverse +from weblog.models import BlogPost, Category, CategoryTranslation +from weblog.apps import SETTINGS as blog_settings +from django.utils.translation import ugettext_lazy as _, pgettext_lazy +from django.utils import translation + +class MainFeed(Feed): + title = _('%(blog_title)s RSS feed') % {'blog_title': blog_settings['blog_title']} + link = reverse('weblog:Index') + description = _('Latest blog posts on %(blog_title)s') % {'blog_title': blog_settings['blog_title']} + + def items(self): + return BlogPost.objects.order_by('-publish_date')[:blog_settings['posts_per_page']] + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.content + +class BlogFeed(Feed): + + def get_object(self, request, category_slug=None): + if category_slug: + if category_slug != 'misc': + self.category = Category.objects.get(slug=obj) + self.category_name = self.category.name + self.current_language = translation.get_language() + if self.current_language is None: + self.current_language = settings.LANGUAGE_CODE + if IS_MULTILINGUAL and obj != 'misc': + category_translations = CategoryTranslation.objects.filter(category=category) + if category_translations.count() > 0: + for cat_trans in category_translations: + if self.current_language[0:2] == cat_trans.language[0:2]: + self.category_name = cat_trans + elif category_slug == 'misc': + self.category_name = pgettext_lazy('Posts without category', 'Uncategorized') + return category_slug + return None + + def title(self, obj): + if obj: + return _('%(blog_title)s\'s %(category_name)s RSS feed') % {'blog_title': blog_settings['blog_title'], 'category_name': self.category_name} + return _('%(blog_title)s RSS feed') % {'blog_title': blog_settings['blog_title']} + + def link(self, obj): + if obj: + return reverse('weblog:CategoryIndex', kwargs={'category_slug': obj}) + return reverse('weblog:Index') + + def description(self, obj): + if obj: + return _('Latest %(category_name)s blog posts on %(blog_title)s') % {'blog_title': blog_settings['blog_title'], 'category_name': self.category_name} + return _('Latest blog posts on %(blog_title)s') % {'blog_title': blog_settings['blog_title']} + + def items(self, obj): + pass + + def item_title(self, item, obj): + pass + + def item_description(self, item, obj): + pass -- cgit v1.2.3 From 812fbefe190a4a9d4f5250ba39411a50e9167448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Wed, 20 Jun 2018 15:04:19 +0300 Subject: rss added in feeds.py --- weblog/feeds.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'weblog') diff --git a/weblog/feeds.py b/weblog/feeds.py index 8510813..d7ccb23 100644 --- a/weblog/feeds.py +++ b/weblog/feeds.py @@ -1,6 +1,6 @@ from django.contrib.syndication.views. import Feed from django.urls import reverse -from weblog.models import BlogPost, Category, CategoryTranslation +from weblog.models import BlogPost, Translation, Category, CategoryTranslation from weblog.apps import SETTINGS as blog_settings from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils import translation @@ -56,10 +56,40 @@ class BlogFeed(Feed): return _('Latest blog posts on %(blog_title)s') % {'blog_title': blog_settings['blog_title']} def items(self, obj): - pass + if obj: + return BlogPost.objects.filter(category__slug=obj).order_by('-publish_date')[:blog_settings['posts_per_page']] + return BlogPost.objects.order_by('-publish_date')[:blog_settings['posts_per_page']] def item_title(self, item, obj): - pass + translation_exists = False + post_translations = Translation.objects.filter(post=item) + if post_translations.count() > 0 and blog_settings['multilingual'] + orig_lang = item.original_language + if len(orig_lang) < 2: + orig_lang = settings.LANGUAGE_CODE[0:2] + for post_translation in post_translations: + if self.current_language[0:2] == post_translation.language[0:2]: + return post_translation.title + return item.title + + def item_pubdate(self, item): + return item.publish_date + + def item_author_name(self, item): + if blog_settings['show_author']: + if blog_settings['use_authors_username']: + return item.author.get_username() + return item.author.get_full_name() + return None def item_description(self, item, obj): - pass + translation_exists = False + post_translations = Translation.objects.filter(post=item) + if post_translation.count() > 0 and blog_settings['multilingual']: + orig_lang = item.original_language + if len(orig_lang) < 2: + orig_lang = settings.LANGUAGE_CODE[0:2] + for post_translation in post_translations: + if self.current_language[:2] == post_translation.language[:2]: + return post_translation.content + return item.content -- cgit v1.2.3 From 843e7e06709a35a162ddb28e9eef528870e2c0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Wed, 20 Jun 2018 15:12:45 +0300 Subject: added urls for rss feeds --- weblog/apps.py | 1 + weblog/feeds.py | 14 -------------- weblog/urls.py | 10 +++++++++- 3 files changed, 10 insertions(+), 15 deletions(-) (limited to 'weblog') diff --git a/weblog/apps.py b/weblog/apps.py index 6f2db71..cc800c1 100755 --- a/weblog/apps.py +++ b/weblog/apps.py @@ -12,6 +12,7 @@ SETTINGS = { 'show_categories': False, 'show_archive': True, 'posts_per_page': 10, + 'enable_rss': True, } class WeblogConfig(AppConfig): diff --git a/weblog/feeds.py b/weblog/feeds.py index d7ccb23..7e567c2 100644 --- a/weblog/feeds.py +++ b/weblog/feeds.py @@ -5,20 +5,6 @@ from weblog.apps import SETTINGS as blog_settings from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils import translation -class MainFeed(Feed): - title = _('%(blog_title)s RSS feed') % {'blog_title': blog_settings['blog_title']} - link = reverse('weblog:Index') - description = _('Latest blog posts on %(blog_title)s') % {'blog_title': blog_settings['blog_title']} - - def items(self): - return BlogPost.objects.order_by('-publish_date')[:blog_settings['posts_per_page']] - - def item_title(self, item): - return item.title - - def item_description(self, item): - return item.content - class BlogFeed(Feed): def get_object(self, request, category_slug=None): diff --git a/weblog/urls.py b/weblog/urls.py index 643e8c7..4d6a325 100755 --- a/weblog/urls.py +++ b/weblog/urls.py @@ -1,5 +1,7 @@ from django.conf.urls import url from . import views +from weblog.feeds import BlogFeed +from weblog.apps import SETTINGS as blog_settings app_name = 'weblog' urlpatterns = [ @@ -9,4 +11,10 @@ urlpatterns = [ url(r'^(?P[0-9]{4})/(?P[0-9]{1,2})/$', views.Index, name='ArchiveIndex'), url(r'^(?P[-\w]+)/$', views.Index, name='CategoryIndex'), url(r'^(?P[-\w]+)/(?P[-\w]+)/$', views.PostView, name='PostView'), -] \ No newline at end of file +] + +if blog_settings['enable_rss']: + urlpatters += [ + url(r'^/rss/$', BlogFeed(), name='RSS'), + url(r'^/(?P[-\w]+)/rss/$', BlogFeed(), name='CategoryRSS'), + ] -- cgit v1.2.3 From 06b58a6a3afbe3e9ffac45b363cfeafcf0e4b3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Wed, 20 Jun 2018 16:14:20 +0300 Subject: rss bug fixes --- weblog/feeds.py | 22 +++++++++++----------- weblog/urls.py | 17 ++++++++++------- 2 files changed, 21 insertions(+), 18 deletions(-) (limited to 'weblog') diff --git a/weblog/feeds.py b/weblog/feeds.py index 7e567c2..158e90f 100644 --- a/weblog/feeds.py +++ b/weblog/feeds.py @@ -1,4 +1,4 @@ -from django.contrib.syndication.views. import Feed +from django.contrib.syndication.views import Feed from django.urls import reverse from weblog.models import BlogPost, Translation, Category, CategoryTranslation from weblog.apps import SETTINGS as blog_settings @@ -8,15 +8,15 @@ from django.utils import translation class BlogFeed(Feed): def get_object(self, request, category_slug=None): + self.current_language = translation.get_language() + if self.current_language is None: + self.current_language = settings.LANGUAGE_CODE if category_slug: if category_slug != 'misc': - self.category = Category.objects.get(slug=obj) + self.category = Category.objects.get(slug=category_slug) self.category_name = self.category.name - self.current_language = translation.get_language() - if self.current_language is None: - self.current_language = settings.LANGUAGE_CODE - if IS_MULTILINGUAL and obj != 'misc': - category_translations = CategoryTranslation.objects.filter(category=category) + if blog_settings['multilingual'] and category_slug != 'misc': + category_translations = CategoryTranslation.objects.filter(category=self.category) if category_translations.count() > 0: for cat_trans in category_translations: if self.current_language[0:2] == cat_trans.language[0:2]: @@ -46,10 +46,10 @@ class BlogFeed(Feed): return BlogPost.objects.filter(category__slug=obj).order_by('-publish_date')[:blog_settings['posts_per_page']] return BlogPost.objects.order_by('-publish_date')[:blog_settings['posts_per_page']] - def item_title(self, item, obj): + def item_title(self, item): translation_exists = False post_translations = Translation.objects.filter(post=item) - if post_translations.count() > 0 and blog_settings['multilingual'] + if post_translations.count() > 0 and blog_settings['multilingual']: orig_lang = item.original_language if len(orig_lang) < 2: orig_lang = settings.LANGUAGE_CODE[0:2] @@ -68,10 +68,10 @@ class BlogFeed(Feed): return item.author.get_full_name() return None - def item_description(self, item, obj): + def item_description(self, item): translation_exists = False post_translations = Translation.objects.filter(post=item) - if post_translation.count() > 0 and blog_settings['multilingual']: + if post_translations.count() > 0 and blog_settings['multilingual']: orig_lang = item.original_language if len(orig_lang) < 2: orig_lang = settings.LANGUAGE_CODE[0:2] diff --git a/weblog/urls.py b/weblog/urls.py index 4d6a325..b3e95b1 100755 --- a/weblog/urls.py +++ b/weblog/urls.py @@ -4,7 +4,16 @@ from weblog.feeds import BlogFeed from weblog.apps import SETTINGS as blog_settings app_name = 'weblog' -urlpatterns = [ + +urlpatterns = [] + +if blog_settings['enable_rss']: + urlpatterns += [ + url(r'^rss/$', BlogFeed(), name='RSS'), + url(r'^(?P[-\w]+)/rss/$', BlogFeed(), name='CategoryRSS'), + ] + +urlpatterns += [ url(r'^$', views.Index, name='Index'), url(r'^change-language/(?P[-\w]+)/$', views.ChangeLanguage, name='ChangeLanguage'), url(r'^(?P[0-9]{4})/$', views.Index, name='ArchiveIndex'), @@ -12,9 +21,3 @@ urlpatterns = [ url(r'^(?P[-\w]+)/$', views.Index, name='CategoryIndex'), url(r'^(?P[-\w]+)/(?P[-\w]+)/$', views.PostView, name='PostView'), ] - -if blog_settings['enable_rss']: - urlpatters += [ - url(r'^/rss/$', BlogFeed(), name='RSS'), - url(r'^/(?P[-\w]+)/rss/$', BlogFeed(), name='CategoryRSS'), - ] -- cgit v1.2.3