1. magik cypress
  2. django-minicms

Commits

magik_cypress  committed dff13da Draft

fix article and comment after expire

  • Participants
  • Parent commits 9a612af
  • Branches default

Comments (0)

Files changed (5)

File README

View file
 
 ChangeLog:
 
+- fix article and comment after expire
+
+--
+
 - fix display or not rubrique on public page
 - fix display or not article on public page
 - fix author_ip for visitor (gethostname and gethostbyname bug for Django 1.3)

File articles/models.py

View file
 from django.conf import settings
 from django.contrib.auth.models import User
 from django.template.defaultfilters import slugify
-from datetime import datetime
+from datetime import datetime, timedelta
 from rubriques.models import Rubrique
 import os
 
     expiry_date = models.DateTimeField('Expire date', default=datetime.now)
     comments_enabled = models.BooleanField('Comments')
     choices = (
-        ('never', _('Never')),
+        ('never', _('After this application is dead')),
         ('week', _('1 Week')),
         ('month', _('1 Month')),
         ('half_year', _('6 Monthes')))
     comments_expiry = models.CharField(choices=choices, max_length=9)
+    comments_expiry_date = models.DateTimeField('Comment expiry data')
 
+    # custom label templates
     required_css_class = 'required'
 
     def __unicode__(self):
 
     def save(self, *args, **kwargs):
         self.slug = slugify(self.title)
+        if self.comments_expiry:
+            self.comments_expiry_date = {
+                # With no simple way of adding an exact month,
+                # approximate day representations are used
+                'never': lambda now: now + timedelta(111182),
+                'week': lambda now: now + timedelta(7),
+                'month': lambda now: now + timedelta(30),
+                'half_year': lambda now: now + timedelta(182),
+            }[self.comments_expiry](self.publish_date)
         super(Article, self).save(*args, **kwargs)
 
     def get_absolute_url(self):

File articles/views.py

View file
 from django.shortcuts import render_to_response, get_object_or_404
 from django.views.decorators.csrf import csrf_protect
 from django.template import Context, RequestContext
+from django.utils.timezone import utc
 from datetime import datetime
 from minicms.utils import queryset_iterator
 from articles.models import Article
         else:
             form_comment = form
 
-    # DoubleCheck for comment expiry
-    comments_expired = False
-    if entry.comments_enabled:
-            comments_expired = True
+    # Check for comment expiry
+    published = False
+    if entry.published and entry.expiry_date:
+        expiry_date = entry.expiry_date.replace(microsecond=0)
+        datetime_now = datetime.utcnow().replace(
+            microsecond=0).replace(tzinfo=utc)
+        if expiry_date > datetime_now:
+            published = True
+
+    # Check for comment expiry
+    comments_enabled = False
+    if entry.comments_enabled and entry.comments_expiry_date:
+        comments_expiry_date = entry.comments_expiry_date.replace(
+            microsecond=0)
+        datetime_now = datetime.utcnow().replace(
+            microsecond=0).replace(tzinfo=utc)
+        if comments_expiry_date > datetime_now:
+            comments_enabled = True
 
     context = {
         'entry': entry,
+        'published': published,
         'list_comments': list_comments,
+        'comments_enabled': comments_enabled,
         'form': form_comment,
-        'comments_expired': comments_expired
     }
 
     return render_to_response(

File minicms/templates/minicms/articles/detail.html

View file
 {% block content %}
 
     <section>
+    {% if entry.published and published %}
+
     <h1>{{ entry.title }}</h1>
     <span class="date"><strong>{% trans "Publish date:" %}</strong> {{ entry.publish_date|date:"DATE_FORMAT" }}</span>
     <span class="author"><strong>{% trans "by:" %}</strong> 
     {% endfor %}
     </ul>
     {% else %}
-    {% trans "There are no comments for this post." %}
+        <p>{% trans "No comment for this post." %}</p>
     {% endif %}
 
-
-    {% if not entry.comments_expired %}
+    {% if comments_enabled %}
     <h3>{% trans "Add comment" %}</h3>
     <p>{% trans "You may use" %} <a href="http://daringfireball.net/projects/markdown/basics">Markdown</a> {% trans "syntax but raw HTML will be escaped and headings normalised." %}</p>
     <form action="." method="post" id="formblock">
             </tr>
         </table>
     </form>
+    {% else %}
+        <p>{% trans "Comments on this post expired." %}</p> 
+    {% endif %}
 
     {% else %}
         <p>{% trans "Comments on this post have now been closed." %}</p>
     {% endif %}
-    {% else %}
-        <p>{% trans "Comments for this post are closed." %}</p>
-    {% endif %}
+
     </div>
 
+    {% else %}
+        <p>{% trans "This post have now been closed." %}</p> 
+
+    {% endif %}
     </section>
 
 {% endblock %}

File rubriques/views.py

View file
 def detail(request, rubrique_id):
     """ detail rubrique
     """
+    from datetime import datetime
     entry = get_object_or_404(Rubrique, pk=rubrique_id)
     list_articles = queryset_iterator(Article.objects.filter(
-        rubrique=rubrique_id).order_by('-publish_date'))
+        rubrique=rubrique_id, expiry_date__gt=datetime.now()).order_by(
+            '-publish_date'))
 
     context = {
         'entry': entry,
-        'list_articles': list_articles
+        'list_articles': list_articles,
     }
 
     return render_to_response(