Skip to content

Commit

Permalink
📆 monthly archives
Browse files Browse the repository at this point in the history
  • Loading branch information
zackmdavis committed Nov 1, 2014
1 parent 4e778d2 commit c5d298e
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 9 deletions.
33 changes: 31 additions & 2 deletions core/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import calendar
from collections import Counter

from django.conf import settings
from django.contrib.auth.forms import AuthenticationForm

from core.models import Tag
from core.models import Post, Tag
from core.views.view_utils import tag_cloud_context

def tag_cloud_context_processor(request):
Expand All @@ -10,11 +13,37 @@ def tag_cloud_context_processor(request):
def sidebar_login_form_context_processor(request):
return {'sidebar_login_form': AuthenticationForm() }

def monthly_archives_context_processor(request):
month_counts = Counter([(p.year, p.month)
for p in Post.objects.all()])
month_counts = sorted(
month_counts.items(),
key=lambda k: (int(k[0][0]), int(k[0][1]))
)
months_info = [
(
archive[0],
"{} {} ({})".format(
calendar.month_name[int(archive[0][1])], archive[0][0],
archive[1]
)
) for archive in month_counts
]
return {'months': months_info}

# [
# (k, "{} {} ({})".format(
# calendar.month_name[k[1]], k[0], v
# )
# for k, v in months_info}
# }

def contextual_static_serving_context_processor(request):
if settings.SERVE_STATIC_LIBS_LOCALLY:
jquery_url = "/static/libs/jquery-2.1.1.min.js"
underscore_url = "/static/libs/underscore-min.js"
else:
jquery_url = "//code.jquery.com/jquery-2.1.1.min.js"
underscore_url = "//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"
underscore_url = ("//cdnjs.cloudflare.com/ajax/libs/underscore.js/"
"1.7.0/underscore-min.js")
return locals()
25 changes: 25 additions & 0 deletions core/views/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
from datetime import datetime
import calendar

import bleach

from django.conf import settings
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.http import HttpResponseForbidden
Expand All @@ -11,6 +13,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.debug import sensitive_post_parameters
from django.views.decorators.http import require_POST
from django.views.generic.list import ListView
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages
Expand Down Expand Up @@ -71,6 +74,28 @@ def show_post(request, year, month, slug):
'top_level_comments': top_level_comments})
)

class MonthlyArchive(ListView):
context_object_name = 'posts'
template_name = 'monthly_archive.html'
paginate_by = settings.POSTS_PER_PAGE

def get_queryset(self):
year = int(self.kwargs['year'])
month = int(self.kwargs['month'])
start_of_month = datetime(year=year, month=month, day=1)
end_year = year if month < 12 else (year + 1)
next_month = (month % 12) + 1
end_of_month = datetime(year=end_year, month=next_month, day=1)
return Post.objects.filter(
published_at__gte=start_of_month, published_at__lt=end_of_month
)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['year'] = int(self.kwargs['year'])
context['month'] = calendar.month_name[int(self.kwargs['month'])]
return context

@login_required
def new_post(request):
url = HttpRequest.build_absolute_uri(request, reverse("home"))
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'core.context_processors.tag_cloud_context_processor',
'core.context_processors.contextual_static_serving_context_processor',
'core.context_processors.sidebar_login_form_context_processor',
'core.context_processors.monthly_archives_context_processor',
)

STATIC_ROOT = 'staticfiles'
Expand Down
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</div>
{% endif %}
{% include "includes/tag_cloud.html" %}
{% include "includes/sidebar_archives_list.html" %}
</div>
</div>

Expand Down
3 changes: 0 additions & 3 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
{% for post in posts %}
<div class="row">
{% include "includes/post.html" %}
<p><a href="{% url 'show_post' post.year post.month post.slug %}#comments">
<strong>{{ post.comment_set.count }} comments</strong>
</a></p>
</div>
{% endfor %}

Expand Down
5 changes: 5 additions & 0 deletions templates/includes/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ <h3><a href="{% url 'show_post' post.year post.month post.slug %}">{{ post.title
...
</div>
</div>
<p>
<a href="{% url 'show_post' post.year post.month post.slug %}#comments">
<strong>{{ post.comment_set.count }} comments</strong>
</a>
</p>
12 changes: 12 additions & 0 deletions templates/includes/sidebar_archives_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div id="sidebar-archives-list">
<h3>Archives</h3>
<ul>
{% for date_info, list_item in months %}
<li>
<a href="/{{ date_info.0 }}/{{ date_info.1 }}/">
{{ list_item }}
</a>
</li>
{% endfor %}
</ul>
</div>
18 changes: 18 additions & 0 deletions templates/monthly_archive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block valuation %}
{# TODO #}
{% endblock %}

{% block subtitle %}posts during {{ month }} {{ year }}{% endblock %}

{% block content %}
<h4>posts during {{ month }} {{ year }}</h4>

{% for post in posts %}
<div class="row">
{% include "includes/post.html" %}
</div>
{% endfor %}

{% endblock %}
5 changes: 1 addition & 4 deletions templates/tagged.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

{% block content %}
<h4>Posts tagged '{{ tag.label }}'</h4>

{% for post in posts %}
{# XXX: duplication; another fragment, or comment link inside includes/post? #}
<div class="row">
{% include "includes/post.html" %}
<p><a href="{{ post.pk }}/#comments">
<strong>{{ post.comment_set.count }} comments</strong>
</a></p>
</div>
{% endfor %}

Expand Down
3 changes: 3 additions & 0 deletions urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
url(r'^edit_profile/(.+)/$', views.edit_profile, name='edit_profile'),
url(r'^add_comment/(\d+)/$', views.add_comment, name='add_comment'),
url(r'^check_slug/$', views.check_slug, name='check_slug'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?:page/(?P<page>\d+)/)?$',
views.MonthlyArchive.as_view(),
name='monthly_archive'),
url(r'^(\d{4})/(\d{2})/([a-z\d\-]+)/$', views.show_post, name='show_post')
)

0 comments on commit c5d298e

Please sign in to comment.