Make publication depend on 'published_at' field

This commit is contained in:
Víðir Valberg Guðmundsson 2016-06-10 20:21:32 +02:00
parent 4a778497ce
commit ef00c130f9
3 changed files with 17 additions and 1 deletions

View file

@ -1,9 +1,11 @@
from django.db.models import QuerySet from django.db.models import QuerySet
from django.utils import timezone
class NewsItemQuerySet(QuerySet): class NewsItemQuerySet(QuerySet):
def public(self): def public(self):
return self.filter( return self.filter(
public=True public=True,
published_at__lt=timezone.now()
) )

View file

@ -3,6 +3,11 @@
{% block content %} {% block content %}
<div> <div>
{% if draft %}
<div class="alert alert-danger">
<strong>This news item is not yet public.</strong> It will become public at {{ news_item.published_at|date:'Y-m-d H:i' }}.
</div>
{% endif %}
<h3>{{ news_item.title }} <small>{{ news_item.published_at|date:"Y-m-d" }}</small></h3> <h3>{{ news_item.title }} <small>{{ news_item.published_at|date:"Y-m-d" }}</small></h3>
</div> </div>
{{ news_item.content|commonmark }} {{ news_item.content|commonmark }}

View file

@ -1,4 +1,5 @@
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
from django.utils import timezone
from . import models from . import models
@ -14,3 +15,11 @@ class NewsDetail(DetailView):
template_name = 'news_detail.html' template_name = 'news_detail.html'
context_object_name = 'news_item' context_object_name = 'news_item'
def get_context_data(self, **kwargs):
context = super(NewsDetail, self).get_context_data(**kwargs)
news_item = self.get_object()
context['draft'] = False
if news_item.public and news_item.published_at > timezone.now():
context['draft'] = True
return context