Commit 902cce2e by Jocelyn Delalande

Handle draft status for articles

both from future date and with an explicit draft status.

Fix #62
parent 6762a186
......@@ -43,14 +43,20 @@ class ArticleAdmin(MarkdownxModelAdmin):
form = ArticleAdminForm
list_display = ('titre','pub_date', 'highlight', 'categorie')
fieldsets = (
(None, {'fields': ('titre', 'pub_date', 'categorie', 'concerne', 'collectif')}),
(None, {'fields': (
'titre',
'publication_status',
'pub_date',
'categorie',
'concerne', 'collectif',
)}),
('Méta-données', {'fields':
('chapeau', 'accroche',
'highlight',
'mini_illustration')}),
('Corps de l\'article', {'fields': ('markdown_text',)}),
('Options avancées', {'fields': ('texte',), 'classes': ('collapse',)}),
)
)
formfield_overrides = {
models.ManyToManyField: {'widget': forms.CheckboxSelectMultiple},
......
......@@ -13,7 +13,9 @@ class EvenementsRSS(Feed):
categories = ['Événement', 'actu']
def items(self):
return Article.objects.filter(categorie=Article.EVENEMENT).order_by('-pub_date')[:10]
return Article.objects.published()\
.filter(categorie=Article.EVENEMENT)\
.order_by('-pub_date')[:10]
def item_title(self, item):
return item.titre
......@@ -35,7 +37,9 @@ class PublicationsRSS(Feed):
categories = ['parution papier', 'actu']
def items(self):
return Article.objects.filter(categorie=Article.PUBLICATION).order_by('-pub_date')[:10]
return Article.objects.published()\
.filter(categorie=Article.PUBLICATION)\
.order_by('-pub_date')[:10]
def item_title(self, item):
return item.titre
......
# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2018-12-29 18:05
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('articles', '0005_auto_20181229_1733'),
]
operations = [
migrations.AddField(
model_name='article',
name='publication_status',
field=models.CharField(choices=[(b'published', b'Publi\xc3\xa9'), (b'draft', b'Brouillon')], default=b'published', max_length=20, verbose_name=b'\xc3\x89tat de publication'),
),
]
......@@ -8,11 +8,19 @@ from django.conf import settings
from django.contrib import auth
from django.core.files import File
from django.db.models import Q
from django.utils import timezone
from markdownx.models import MarkdownxField
from markdownx.utils import markdownify
from slughifi import slugify
class ArticleQuerySet(models.QuerySet):
def published(self):
return self.filter(
pub_date__lte=timezone.now(),
publication_status=Article.PUB_STATE_PUBLISHED,
)
class Article(models.Model):
""" Contient un texte publié, sa description, sa source Tex, sa
représentation html & pdf, ses métadonnées et ses commentaires
......@@ -21,11 +29,26 @@ class Article(models.Model):
EVENEMENT = 'E'
CATEGORIES = ((PUBLICATION,"Publications papier"), (EVENEMENT, "Événements"))
PUB_STATE_PUBLISHED='published'
PUB_STATE_DRAFT='draft'
PUB_STATES = (
(PUB_STATE_PUBLISHED, 'Publié'),
(PUB_STATE_DRAFT, 'Brouillon'),
)
titre = models.CharField(max_length=200)
auteur = models.ForeignKey(auth.models.User, blank=True,
on_delete=models.CASCADE)
slug = models.SlugField(unique=True)
pub_date = models.DateTimeField('Date de publication',default=datetime.now)
publication_status = models.CharField(
'État de publication',
help_text='Les textes brouillon ne sont pas listés publiquement sur le site',
choices=PUB_STATES, default=PUB_STATE_PUBLISHED, max_length=20,
)
pub_date = models.DateTimeField(
'Date de publication',
help_text='Mettre une date dans le futur pour publication planifiée automatique',
default=datetime.now)
highlight = models.BooleanField('Mis en valeur dans le ruban',default=False)
chapeau = models.TextField("Chapeau", max_length=1500, blank=True)
categorie = models.CharField(choices=CATEGORIES,blank=True, max_length=1,null=True)
......@@ -58,6 +81,8 @@ auquel cas, videz le champ « texte de l'article en markdown »"""
class Meta:
ordering = ['-id']
objects = ArticleQuerySet.as_manager()
def __unicode__(self):
return self.titre
......@@ -81,10 +106,14 @@ auquel cas, videz le champ « texte de l'article en markdown »"""
def is_collective(self):
return (self.concerne.count() > 1) or self.collectif
# Équipement pour les Auteurs
# Équipement pour les Auteurs
auth.models.User.ouvrages_papier = lambda s:\
Article.objects.annotate(num_auteurs=models.Count('concerne')).filter(concerne=s,categorie='P').order_by('-pub_date')
Article.objects.published()\
.annotate(num_auteurs=models.Count('concerne'))\
.filter(concerne=s, categorie='P')\
.order_by('-pub_date')
auth.models.User.ouvrages_papier_auteur = lambda s:\
s.ouvrages_papier().filter(num_auteurs=1).filter(collectif=False)
......
......@@ -39,6 +39,9 @@ class AdminTest(ArticlesTestCase):
class PublicTest(ArticlesTestCase):
def request_article_detail(self, article):
return self.client.get(reverse('article-detail', args=[article.slug]))
def test_view_publication_without_author(self):
article = Article.objects.create(
titre='éé', texte='héhé',
......@@ -48,6 +51,44 @@ class PublicTest(ArticlesTestCase):
self.assertEqual(response.status_code, 200)
self.assertIn('inconnu', response.content)
def test_publication_status_default(self):
article = Article.objects.create(
titre='hehe4678', texte='a',
categorie=Article.EVENEMENT, auteur=self.user,
)
# Listed
response = self.client.get(reverse('event-list'))
self.assertIn('hehe4678', response.content)
# Detail
response = self.request_article_detail(article)
self.assertEqual(response.status_code, 200)
def test_publication_status_draft_status(self):
article = Article.objects.create(
titre='hehe4678', texte='a',
publication_status=Article.PUB_STATE_DRAFT,
categorie=Article.EVENEMENT, auteur=self.user,
)
# Not Listed
response = self.client.get(reverse('event-list'))
self.assertNotIn('hehe4678', response.content)
# Accessible (for whom has the URL)
response = self.request_article_detail(article)
self.assertEqual(response.status_code, 200)
def test_publication_status_postponed(self):
article = Article.objects.create(
titre='hehe4678', texte='a',
pub_date=datetime.datetime(2042, 2, 2),
categorie=Article.EVENEMENT, auteur=self.user,
)
# Not Listed
response = self.client.get(reverse('event-list'))
self.assertNotIn('hehe4678', response.content)
# Detail accessible (for whom has the URL)
response = self.request_article_detail(article)
self.assertEqual(response.status_code, 200)
def test_view_articles_lists(self):
events_url = reverse('event-list')
paperbacks_url = reverse('paperback-list')
......
......@@ -8,6 +8,7 @@ from .models import Article
class AbstractArticleListView(ListView):
def get_queryset(self):
return Article.objects\
.published()\
.filter(categorie=self.category)\
.order_by('-pub_date')
......
......@@ -111,12 +111,12 @@ class EmailNotificationsTests(TestCase):
EmailSubscription.objects.create(email='sausage@example.com',
targets='texts')
with freeze_time('2013-05-02'):
self.create_text()
self.create_text(titre='lili')
with freeze_time('2101-05-02'):
self.create_text()
self.create_text(titre='lélé')
self.create_text(est_compile=False)
self.create_text(est_publie=False)
self.create_text(est_compile=False, titre="lala")
self.create_text(est_publie=False, titre="lolo")
assert len(mail.outbox) == 0
......
......@@ -354,7 +354,7 @@ def fois(n1,n2):
@register.inclusion_tag('articles/tags/vitrine.html')
def derniers_articles(n_articles=10):
# FIXME: sqlite ne semble pas vouloir faire de order_by datetime ??
tous_articles = articles.models.Article.objects.all().order_by('-pub_date')
tous_articles = articles.models.Article.objects.published().order_by('-pub_date')
try:
derniers_articles = tous_articles[:n_articles-1]
except KeyError:
......@@ -366,7 +366,7 @@ def derniers_articles(n_articles=10):
def ruban_articles():
"""Pioche au hasard parmis les articles mis en valeur
"""
all_hl = articles.models.Article.objects.filter(highlight=True)
all_hl = articles.models.Article.objects.published().filter(highlight=True)
if len(all_hl) > 0:
return {'highlighted': all_hl.order_by('?')[0]}
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment