Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Finished the archvie view.
Browse files Browse the repository at this point in the history
  • Loading branch information
myles committed Oct 7, 2011
1 parent aebb41c commit 1b9a821
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 4 deletions.
Expand Up @@ -8,6 +8,25 @@ <h2>Blog Archive</h2>

{% block content %}
<table id="archive">
<thead>
<th>Year</th>
<th colspan="12">Months</th>
</thead>
<tbody>
{% for year, months in archive.items %}
<tr>
<th>{{ year.year }}</th>
{% for month in months %}
<td>
<a href="{% url blog_archive_month month.year month|date:"b" %}">
{{ month|date:"F" }}
</a>
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>

<thead>
{% for year in years %}
<th><a href="{% url blog_archive_year year.year %}">{{ year.year }}</a></th>
Expand Down
Expand Up @@ -8,6 +8,6 @@ <h2>Blog Archive for {{ this_day|date:"jS F Y" }}</h2>

{% block content %}
<div id="blog_posts">
{% for post in posts %}{% include "blog/_post.html" %}{% endfor %}
{% for post in post_list %}{% include "blog/_post.html" %}{% endfor %}
</div>
{% endblock %}
Expand Up @@ -8,6 +8,6 @@ <h2>Blog Archive for {{ this_month|date:"F Y" }}</h2>

{% block content %}
<div id="blog_posts">
{% for post in posts %}{% include "blog/_post.html" %}{% endfor %}
{% for post in post_list %}{% include "blog/_post.html" %}{% endfor %}
</div>
{% endblock %}
Expand Up @@ -8,7 +8,7 @@ <h2>Updated Blog Posts</h2>

{% block content %}
<ul id="blog_posts">
{% for post in posts %}
{% for post in post_list %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a> was originally
<strong>published on {{ post.published|date }} at {{ post.published|time }}</strong>
Expand Down
Expand Up @@ -8,6 +8,6 @@ <h2>Blog Archive for {{ this_year|date:"Y" }}</h2>

{% block content %}
<div id="blog_posts">
{% for post in posts %}{% include "blog/_post.html" %}{% endfor %}
{% for post in post_list %}{% include "blog/_post.html" %}{% endfor %}
</div>
{% endblock %}
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions blog/urls.py
Expand Up @@ -11,10 +11,30 @@
#
# Archive
#
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$',
view = views.BlogPostDayArchiveView.as_view(),
name = 'blog_archive_day',
),
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$',
view = views.BlogPostMonthArchiveView.as_view(),
name = 'blog_archive_month',
),
url(r'^(?P<year>\d{4})/$',
view = views.BlogPostYearArchiveView.as_view(),
name = 'blog_archive_year',
),
url(r'^updated/feed/$',
view = feeds.BlogUpdatedPostFeed(),
name = 'blog_updated_post_feed',
),
url(r'^updated/$',
view = views.BlogPostUpdatedArchiveView.as_view(),
name = 'blog_updated',
),
url(r'^archive/$',
view = views.BlogPostArchiveView.as_view(),
name = 'blog_archive',
),
url(r'^feed/$',
view = feeds.BlogPostFeed(),
name = 'blog_post_feed',
Expand Down
6 changes: 6 additions & 0 deletions blog/views/__init__.py
Expand Up @@ -5,12 +5,18 @@
from blog.models import Post
from blog.settings import BLOG_PAGINATE_BY
from blog.views.category import BlogCategoryDetailView, BlogCategoryListView
from blog.views.archive import BlogPostYearArchiveView, BlogPostMonthArchiveView, BlogPostDayArchiveView, BlogPostUpdatedArchiveView, BlogPostArchiveView

__all__ = [
'BlogPostListView',
'BlogPostDeatilView',
'BlogCategoryListView',
'BlogCategoryDetailView',
'BlogPostYearArchiveView',
'BlogPostMonthArchiveView',
'BlogPostDayArchiveView',
'BlogPostUpdatedArchiveView',
'BlogPostArchiveView',
]

class BlogPostListView(ListView):
Expand Down
92 changes: 92 additions & 0 deletions blog/views/archive.py
@@ -0,0 +1,92 @@
import time
import datetime

from django.views.generic import TemplateView, ListView

from blog.models import Post
from blog.settings import BLOG_PAGINATE_BY

class BlogPostArchiveView(TemplateView):

template_name = "blog/archive/archive.html"

def get_context_data(self, **kwargs):
posts = Post.objects.published()
years = posts.dates('published', 'year')
context = super(BlogPostArchiveView, self).get_context_data(**kwargs)
context['archive'] = {}
for year in years:
context['archive'][year] = Post.objects.archvie_year(year).dates('published', 'month')
return context

class BlogPostYearArchiveView(ListView):

context_object_name = "post_list"
template_name = "blog/archive/year.html"
paginate_by = BLOG_PAGINATE_BY

def get_context_data(self, **kwargs):
context = super(BlogPostYearArchiveView, self).get_context_data(**kwargs)
context['this_year'] = self.this_year
context['next_year'] = self.this_year + datetime.timedelta(days=+366)
context['prev_year'] = self.this_year + datetime.timedelta(days=-365)
return context

def get_queryset(self):
self.this_year = datetime.date(int(self.kwargs['year']), 1, 1)
return Post.objects.archvie_year(self.this_year).select_related()

class BlogPostMonthArchiveView(ListView):

context_object_name = "post_list"
template_name = "blog/archive/month.html"
paginate_by = BLOG_PAGINATE_BY

def get_context_data(self, **kwargs):
first_day = self.this_month.replace(day=1)
if first_day.month == 12:
last_day = first_day.replace(year=first_day.year + 1, month=1)
else:
last_day = first_day.replace(month=first_day.month + 1)

context = super(BlogPostMonthArchiveView, self).get_context_data(**kwargs)
context['this_month'] = self.this_month
context['next_month'] = last_day + datetime.timedelta(days=1)
context['prev_month'] = first_day - datetime.timedelta(days=-1)
return context

def get_queryset(self):
try:
self.this_month = datetime.date(*time.strptime(self.kwargs['year']+self.kwargs['month'], '%Y%b')[:3])
except ValueError:
raise Http404

return Post.objects.archive_month(self.this_month).select_related()

class BlogPostDayArchiveView(ListView):
context_object_name = "post_list"
template_name = "blog/archive/day.html"
paginate_by = BLOG_PAGINATE_BY

def get_context_data(self, **kwargs):
context = super(BlogPostDayArchiveView, self).get_context_data(**kwargs)
context['next_day'] = self.this_day + datetime.timedelta(days=+1)
context['prev_day'] = self.this_day - datetime.timedelta(days=-1)
context['this_day'] = self.this_day
return context

def get_queryset(self):
try:
self.this_day = datetime.date(*time.strptime(self.kwargs['year']+self.kwargs['month']+self.kwargs['day'], '%Y%b%d')[:3])
except ValueError:
raise Http404

return Post.objects.archive_day(self.this_day).select_related()

class BlogPostUpdatedArchiveView(ListView):
context_object_name = "post_list"
template_name = "blog/archive/updated.html"
paginate_by = BLOG_PAGINATE_BY

def get_queryset(self):
return Post.objects.updated()
Empty file removed blog/views/archvie.py
Empty file.

0 comments on commit 1b9a821

Please sign in to comment.