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