Add RSS feed to news.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-12 10:13:27 +01:00
parent ed311fe2aa
commit 92a5f67ae3
3 changed files with 33 additions and 5 deletions

View file

@ -1,6 +1,7 @@
from django.db import models from django.db import models
from django.utils import encoding from django.utils import encoding
from django.utils.text import slugify from django.utils.text import slugify
from django.urls import reverse
from utils.models import CreatedUpdatedModel from utils.models import CreatedUpdatedModel
@ -42,3 +43,5 @@ class NewsItem(CreatedUpdatedModel):
super(NewsItem, self).save(**kwargs) super(NewsItem, self).save(**kwargs)
def get_absolute_url(self):
return reverse('news:detail', kwargs={"slug": self.slug})

View file

@ -5,6 +5,7 @@ app_name = 'news'
urlpatterns = [ urlpatterns = [
path('', views.NewsIndex.as_view(), kwargs={'archived': False}, name='index'), path('', views.NewsIndex.as_view(), kwargs={'archived': False}, name='index'),
path('archive/', views.NewsIndex.as_view(), kwargs={'archived': True}, name='archive'), path('archive/', views.NewsIndex.as_view(), kwargs={'archived': True}, name='archive'),
path('feed/', views.NewsFeed(), name='feed'),
path('<slug:slug>/', views.NewsDetail.as_view(), name='detail'), path('<slug:slug>/', views.NewsDetail.as_view(), name='detail'),
] ]

View file

@ -1,19 +1,30 @@
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
from django.utils import timezone from django.utils import timezone
from django.contrib.syndication.views import Feed
from .models import NewsItem 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): class NewsIndex(ListView):
model = NewsItem model = NewsItem
template_name = 'news_index.html' template_name = 'news_index.html'
context_object_name = 'news_items' context_object_name = 'news_items'
def get_queryset(self): def get_queryset(self):
return NewsItem.objects.filter( return news_items_queryset(self.kwargs)
published_at__isnull=False,
published_at__lt=timezone.now(),
archived=self.kwargs['archived']
)
class NewsDetail(DetailView): class NewsDetail(DetailView):
@ -21,3 +32,16 @@ class NewsDetail(DetailView):
template_name = 'news_detail.html' template_name = 'news_detail.html'
context_object_name = 'news_item' 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