From 92a5f67ae31c7746b420dfda70c095779b89cb46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Tue, 12 Mar 2019 10:13:27 +0100 Subject: [PATCH] Add RSS feed to news. --- src/news/models.py | 3 +++ src/news/urls.py | 1 + src/news/views.py | 34 +++++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/news/models.py b/src/news/models.py index 86b2b786..31322fea 100644 --- a/src/news/models.py +++ b/src/news/models.py @@ -1,6 +1,7 @@ from django.db import models from django.utils import encoding from django.utils.text import slugify +from django.urls import reverse from utils.models import CreatedUpdatedModel @@ -42,3 +43,5 @@ class NewsItem(CreatedUpdatedModel): super(NewsItem, self).save(**kwargs) + def get_absolute_url(self): + return reverse('news:detail', kwargs={"slug": self.slug}) diff --git a/src/news/urls.py b/src/news/urls.py index 729241b2..af6127ad 100644 --- a/src/news/urls.py +++ b/src/news/urls.py @@ -5,6 +5,7 @@ app_name = 'news' urlpatterns = [ path('', views.NewsIndex.as_view(), kwargs={'archived': False}, name='index'), path('archive/', views.NewsIndex.as_view(), kwargs={'archived': True}, name='archive'), + path('feed/', views.NewsFeed(), name='feed'), path('/', views.NewsDetail.as_view(), name='detail'), ] diff --git a/src/news/views.py b/src/news/views.py index 7d324709..79220bc4 100644 --- a/src/news/views.py +++ b/src/news/views.py @@ -1,19 +1,30 @@ from django.views.generic import ListView, DetailView from django.utils import timezone +from django.contrib.syndication.views import Feed + from .models import NewsItem +def news_items_queryset(kwargs=None): + if not kwargs: + archived = False + else: + archived = kwargs['archived'] + + return NewsItem.objects.filter( + published_at__isnull=False, + published_at__lt=timezone.now(), + archived=archived + ) + + class NewsIndex(ListView): model = NewsItem template_name = 'news_index.html' context_object_name = 'news_items' def get_queryset(self): - return NewsItem.objects.filter( - published_at__isnull=False, - published_at__lt=timezone.now(), - archived=self.kwargs['archived'] - ) + return news_items_queryset(self.kwargs) class NewsDetail(DetailView): @@ -21,3 +32,16 @@ class NewsDetail(DetailView): template_name = 'news_detail.html' context_object_name = 'news_item' + +class NewsFeed(Feed): + title = "BornHack News" + link = "/news" + + def items(self): + return news_items_queryset() + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.content