Skip to content

Commit

Permalink
Améliore le PublishedContent pour éviter les incohérences
Browse files Browse the repository at this point in the history
  • Loading branch information
artragis authored and pierre-24 committed Oct 25, 2015
1 parent 8dd5a21 commit 613a984
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 14 deletions.
12 changes: 10 additions & 2 deletions templates/tutorialv2/includes/content_item_type_article.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@

{% captureas content_title %}
{% if public_article and online %}
{{ public_article.load_public_version.title }}
{{ public_article.title }}
{% else %}
{{ article.title }}
{% endif %}
{% endcaptureas %}

{% captureas content_subtitle %}
{% if public_article and online %}
{{ public_article.description}}
{% else %}
{{ article.description }}
{% endif %}
{% endcaptureas %}

{# Authors (by X, Y and Z) ; can't have multiple whitespaces because of the title ! #}
{% captureas authors_text %}
{% for author in article.authors.all %}{% if forloop.first %}{% trans "par" %}{% elif forloop.last %} {% trans "et" %}{% else %},{% endif %} {% if author == user %}{% trans "vous" %}{% else %}{{ author.username }}{% endif %}{% endfor %}
Expand All @@ -40,7 +48,7 @@
</a>

<div class="content-info">
<a href="{{ link }}" title="{{ content_title }}{% if article.description and show_description %} − {{ article.description }}{% endif %}">
<a href="{{ link }}" title="{{ content_title }}{% if content_subtitle and show_description %} − {{ content_subtitle }}{% endif %}">
<h3 class="content-title" itemprop="itemListElement">
{{ content_title }}
</h3>
Expand Down
12 changes: 10 additions & 2 deletions templates/tutorialv2/includes/content_item_type_tutoriel.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@

{% captureas content_title %}
{% if public_tutorial and online %}
{{ public_tutorial.load_public_version.title }}
{{ public_tutorial.title }}
{% else %}
{{ tutorial.title }}
{% endif %}
{% endcaptureas %}

{% captureas content_subtitle %}
{% if public_tutorial and online %}
{{ public_tutorial.description }}
{% else %}
{{ tutorial.description }}
{% endif %}
{% endcaptureas %}

{# Authors (by X, Y and Z) ; can't have multiple whitespaces because of the title ! #}
{% captureas authors_text %}
{% for author in tutorial.authors.all %}{% if forloop.first %}{% trans "par" %}{% elif forloop.last %} {% trans "et" %}{% else %},{% endif %} {% if author == user %}{% trans "vous" %}{% else %}{{ author.username }}{% endif %}{% endfor %}
Expand All @@ -38,7 +46,7 @@
{% endcaptureas %}

<article class="content-item expand-description tutorial-item{{ item_class }}">
<a href="{{ link }}" title="{{ content_title }}{% if tutorial.description and show_description %} − {{ tutorial.description }}{% endif %}">
<a href="{{ link }}" title="{{ content_title }}{% if content_subtitle and show_description %} − {{ content_subtitle }}{% endif %}">
<div class="content-illu">
{% if tutorial.image %}
<img src="{{ tutorial.image.physical.content_thumb.url }}" alt="">
Expand Down
16 changes: 11 additions & 5 deletions templates/tutorialv2/includes/tags_authors.part.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load date %}
{% load i18n %}
{% load category_url %}

{% load captureas %}

{% if content.subcategory.all|length > 0 %}
<ul class="taglist" itemprop="keywords">
Expand All @@ -11,19 +11,25 @@
</ul>
{% endif %}


{% captureas update_date %}
{% if online %}
{{ content.public_object.get_last_action_date }}
{% else %}
{{ content.update_date }}
{% endif %}
{% endcaptureas %}

{% if content.update_date %}
<span class="pubdate">
{% if online and content.update_date == content.pubdate %}
{% if online and update_date == content.pubdate %}
{% trans "Publié" %} :
<time datetime="{{ content.pubdate|date:"c" }}" pubdate="pubdate" itemprop="dateModified">
<time datetime="{{ content.public_object.pubdate|date:"c" }}" pubdate="pubdate" itemprop="dateModified">
{{ content.pubdate|format_date }}
</time>
{% else %}
{% trans "Dernière mise à jour" %} :
<time datetime="{{ content.update_date|date:"c" }}" pubdate="pubdate" itemprop="dateModified">
{{ content.update_date|format_date }}
{{ update_date|format_date }}
</time>
{% endif %}
</span>
Expand Down
20 changes: 20 additions & 0 deletions zds/tutorialv2/migrations/0008_publishedcontent_update_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('tutorialv2', '0007_auto_20150803_1059'),
]

operations = [
migrations.AddField(
model_name='publishedcontent',
name='update_date',
field=models.DateTimeField(default=None, null=True, verbose_name=b'Date de mise \xc3\xa0 jour', db_index=True, blank=True),
preserve_default=True,
),
]
5 changes: 4 additions & 1 deletion zds/tutorialv2/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def get_public_object(self):
"""Get the published version, if any
"""

return PublishedContent.objects.filter(content_pk=self.object.pk, must_redirect=False).last()
object = PublishedContent.objects.filter(content_pk=self.object.pk, must_redirect=False).last()
if object:
object.load_public_version()
return object


class SingleContentPostMixin(SingleContentViewMixin):
Expand Down
23 changes: 21 additions & 2 deletions zds/tutorialv2/models/models_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,30 @@ class Meta:
content_pk = models.IntegerField('Pk du contenu publié', db_index=True)

publication_date = models.DateTimeField('Date de publication', db_index=True, blank=True, null=True)
update_date = models.DateTimeField('Date de mise à jour', db_index=True, blank=True, null=True, default=None)
sha_public = models.CharField('Sha1 de la version publiée', blank=True, null=True, max_length=80, db_index=True)

must_redirect = models.BooleanField(
'Redirection vers une version plus récente', blank=True, db_index=True, default=False)

objects = PublishedContentManager()
versioned_model = None

def __unicode__(self):
return _('Version publique de "{}"').format(self.content.title)

def title(self):
if self.versioned_model:
return self.versioned_model.title
else:
return self.load_public_version().title

def description(self):
if self.versioned_model:
return self.versioned_model.description
else:
return self.load_public_version().description

def get_prod_path(self, relative=False):
if not relative:
return os.path.join(settings.ZDS_APP['content']['repo_public_path'], self.content_public_slug)
Expand All @@ -568,14 +582,16 @@ def load_public_version_or_404(self):
:rtype: zds.tutorialv2.models.models_database.PublicContent
:raise Http404: if the version is not available
"""
return self.content.load_version_or_404(sha=self.sha_public, public=self)
self.versioned_model = self.content.load_version_or_404(sha=self.sha_public, public=self)
return self.versioned_model

def load_public_version(self):
"""
:rtype: zds.tutorialv2.models.models_database.PublicContent
:return: the public content
"""
return self.content.load_version(sha=self.sha_public, public=self)
self.versioned_model = self.content.load_version(sha=self.sha_public, public=self)
return self.versioned_model

def is_article(self):
"""
Expand Down Expand Up @@ -719,6 +735,9 @@ def get_absolute_url_zip(self):

return self.get_absolute_url_to_extra_content('zip')

def get_last_action_date(self):
return self.update_date or self.publication_date


class ContentReaction(Comment):
"""
Expand Down
46 changes: 44 additions & 2 deletions zds/tutorialv2/tests/tests_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding: utf-8

from django.core.urlresolvers import reverse
from datetime import datetime, timedelta
import os
import shutil

Expand All @@ -9,8 +10,11 @@
from zds.settings import BASE_DIR

from zds.member.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.factories import PublishableContentFactory, ContainerFactory, ExtractFactory, LicenceFactory
from zds.tutorialv2.factories import PublishableContentFactory, ContainerFactory, ExtractFactory, LicenceFactory, \
PublishedContentFactory, SubCategoryFactory
from zds.gallery.factories import UserGalleryFactory
from zds.tutorialv2.models.models_database import PublishableContent
from zds.tutorialv2.utils import publish_content

overrided_zds_app = settings.ZDS_APP
overrided_zds_app['content']['repo_private_path'] = os.path.join(BASE_DIR, 'contents-private-test')
Expand Down Expand Up @@ -424,6 +428,44 @@ def test_ensure_slug_stay(self):
for index, child in enumerate(current.children[0].children):
self.assertEqual(child.slug, chapter.children[index].slug) # slug remains for extract as well !

def test_publication_and_attributes_consistency(self):
pubdate = datetime.now() - timedelta(days=1)
article = PublishedContentFactory(type="ARTILCE", author_list=[self.user_author])
public_version = article.public_version
public_version.publication_date = pubdate
public_version.save()
# everything must come from database to have good datetime comparison
article = PublishableContent.objects.get(pk=article.pk)
article.public_version.load_public_version()
old_date = article.public_version.publication_date
old_title = article.public_version.title()
old_description = article.public_version.description()
article.licence = LicenceFactory()
article.save()
self.assertEqual(
self.client.login(
username=self.user_author.username,
password='hostel77'),
True)
self.client.post(reverse("content:edit", args=[article.pk, article.slug]), {
"title": old_title + "bla",
"description": old_description + "bla",
"type": "ARTICLE",
"licence": article.licence.pk,
"subcategory": SubCategoryFactory().pk,
"last_hash": article.sha_draft
})
article = PublishableContent.objects.prefetch_related("public_version").get(pk=article.pk)
article.public_version.load_public_version()
self.assertEqual(old_title, article.public_version.title())
self.assertEqual(old_description, article.public_version.description())
self.assertEqual(old_date, article.public_version.publication_date)
publish_content(article, article.load_version(), False)
article = PublishableContent.objects.get(pk=article.pk)
article.public_version.load_public_version()
self.assertEqual(old_date, article.public_version.publication_date)
self.assertNotEqual(old_date, article.public_version.update_date)

def tearDown(self):
if os.path.isdir(settings.ZDS_APP['content']['repo_private_path']):
shutil.rmtree(settings.ZDS_APP['content']['repo_private_path'])
Expand Down
2 changes: 2 additions & 0 deletions zds/tutorialv2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ def publish_content(db_object, versioned, is_major_update=True):
# save public version
if is_major_update or not is_update:
public_version.publication_date = datetime.now()
elif is_update:
public_version.update_date = datetime.now()

public_version.sha_public = versioned.current_version
public_version.save()
Expand Down

0 comments on commit 613a984

Please sign in to comment.