Repository: dotkom/onlineweb4

Issue:
Articles should not be published before it's publishing date
This goes for the widget as well for the archive. In other words, this is the ability for articles to have drafts before publication. 



Discussion:

Resolving patch:
diff --git a/apps/api/v0/article.py b/apps/api/v0/article.py
--- a/apps/api/v0/article.py
+++ b/apps/api/v0/article.py
@@ -1,18 +1,20 @@
 #-*- coding: utf-8 -*-
 from copy import copy
-from datetime import datetime
-from django.conf import settings
 
+from django.conf import settings
 from django.template.defaultfilters import slugify
+from django.utils import timezone
 
+from filebrowser.base import FileObject
+from filebrowser.settings import VERSIONS
 from tastypie import fields
 from tastypie.resources import ModelResource
 
-from apps.article.models import Article, ArticleTag, Tag
 from apps.api.v0.authentication import UserResource
+from apps.article.models import Article, ArticleTag, Tag
+
+
 
-from filebrowser.base import FileObject
-from filebrowser.settings import VERSIONS
 
 class ArticleResource(ModelResource):
     author = fields.ToOneField(UserResource, 'created_by')
@@ -69,26 +71,25 @@ def get_object_list(self, request):
         if (request_year is not None):
             if (request_month is not None):
                 # Filtering on both year and month
-                queryset = Article.objects.filter(published_date__year=request_year, published_date__month=request_month).order_by('-published_date')
+                queryset = Article.objects.filter(published_date__year=request_year, published_date__month=request_month, published_date__lte=timezone.now()).order_by('-published_date')
             else:
                 # Filtering on only year
-                queryset = Article.objects.filter(published_date__year=request_year).order_by('-published_date')
+                queryset = Article.objects.filter(published_date__year=request_year, published_date__lte=timezone.now()).order_by('-published_date')
         else:
             # Not filtering on year, check if filtering on slug (tag) or return default query
             if (request_tag is not None):
                 # Filtering on slug
                 slug_query = Tag.objects.filter(slug = request_tag)
                 slug_connect = ArticleTag.objects.filter(tag = slug_query).values('article_id')
-                queryset = Article.objects.filter(id__in = slug_connect).order_by('-published_date')
-                #queryset = slug_query
+                queryset = Article.objects.filter(id__in = slug_connect, published_date__lte=timezone.now()).order_by('-published_date')
             else:
                 # No filtering at all, return default query
-                queryset = Article.objects.all().order_by('-published_date')
+                queryset = Article.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
         return queryset
     
     class Meta:     
         API_LIMIT_PER_PAGE = 9
-        queryset = Article.objects.all()
+        queryset = Article.objects.filter(published_date__lte=timezone.now())
         resource_name = 'article/all'
         ordering = ['-published_date']
         include_absolute_url = True
@@ -101,7 +102,7 @@ class ArticleLatestResource(ModelResource):
     author = fields.ToOneField(UserResource, 'created_by')
     
     class Meta:
-        queryset = Article.objects.all()
+        queryset = Article.objects.filter(published_date__lte=timezone.now())
         
         resource_name = 'article/latest'
         filtering = {
diff --git a/apps/article/urls.py b/apps/article/urls.py
--- a/apps/article/urls.py
+++ b/apps/article/urls.py
@@ -3,7 +3,6 @@
 from django.conf.urls import patterns, include, url
 
 urlpatterns = patterns('apps.article.views',
-    #url(r'^$', 'index', name='article_index'),
     url(r'^archive$', 'archive', name='article_archive'),
     url(r'^(?P<article_id>\d+)/(?P<article_slug>[a-zA-Z0-9_-]+)$', 'details', name='article_details'), 
     url(r'^tag/(?P<name>[^\.]+)/(?P<slug>[^\.]+)', 'archive_tag', name='view_article_tag'),
diff --git a/apps/article/views.py b/apps/article/views.py
--- a/apps/article/views.py
+++ b/apps/article/views.py
@@ -1,27 +1,14 @@
+import random
+
 from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
 from django.db.models import Count
 from django.http import HttpResponse
+from django.utils import timezone
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import Template, Context, loader, RequestContext
-from models import Article, Tag, ArticleTag
-import random
 
-def index(request):
-    # Featured
-    featured = Article.objects.filter(featured=True).order_by('-id')[:1]
-    
-    # Siste 4 nyheter
-    latestNews = Article.objects.filter(featured=False).order_by('-id')[:4]
-    
-    i = 0
-    for latest in latestNews:
-        if (i % 2 == 0):
-            latest.i = 0
-        else:
-            latest.i = 1
-        i += 1
-
-    return render_to_response('article/index.html', {'featured' : featured[0], 'latest': latestNews}, context_instance=RequestContext(request))
+from apps.article.models import Article, Tag, ArticleTag
+
 
 def archive(request, name=None, slug=None, year=None, month=None):
     """
@@ -37,7 +24,7 @@ def archive(request, name=None, slug=None, year=None, month=None):
         Article month (published_date), most likely in norwegian written format.
     """
 
-    articles = Article.objects.all().order_by('-published_date')
+    articles = Article.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
 
     month_strings = {
         '1': u'Januar',
@@ -83,55 +70,12 @@ def archive(request, name=None, slug=None, year=None, month=None):
             del sorted_months[i]
         dates[year] = sorted_months
 
-    # If we're filtering by tag
-    if name:
-        filtered = []
-        for article in articles:
-            for tag in article.tags:
-                if name == tag.name:
-                    filtered.append(article)
-        articles = filtered
-
-    # If we're filtering by year
-    if 'year' in request.path:
-        filtered = []
-        # If we're filtering by year and month
-        if 'month' in request.path:
-            month = rev_month_strings[month]
-            for article in articles:
-                if article.published_date.year == int(year) and article.published_date.month == int(month):
-                    filtered.append(article)
-        # If we're filtering by year, but not month
-        else:
-            for article in articles:
-                if article.published_date.year == int(year):
-                    filtered.append(article)
-        articles = filtered
-
     # Get the 30 most used tags, then randomize them
     tags = Tag.objects.filter(article_tags__isnull=False).distinct().annotate(num_tags=Count('article_tags__tag')).order_by('-num_tags')
     tags = list(tags[:30])
     random.shuffle(tags)
-    # Get max frequency of tags. This is used for relative sizing in the tag cloud.
-    try:
-        max_tag_frequency = max([x.frequency for x in tags])
-    except ValueError:
-        max_tag_frequency = 1
-
-    # Paginator
-    # Shows 10 articles per page. Note that these are also filtered beforehand by tag or date.
-    paginator = Paginator(articles, 10)
-    page = request.GET.get('page')
-    try:
-        articles = paginator.page(page)
-    except PageNotAnInteger:
-        # Deliver first page.
-        articles = paginator.page(1)
-    except EmptyPage:
-        # Deliver last page.
-        articles = paginator.page(paginator.num_pages)
-
-    return render_to_response('article/archive.html', {'articles': articles, 'tags': tags, 'max_tag_frequency': max_tag_frequency, 'dates': dates } ,context_instance=RequestContext(request))
+
+    return render_to_response('article/archive.html', {'tags': tags, 'dates': dates } ,context_instance=RequestContext(request))
 
 def archive_tag(request, name, slug):
     return archive(request, name=name, slug=slug)


Test patch: