Commits

Dan Watson committed 5a904a4

Added date-based archive views and navigation

  • Participants
  • Parent commits aebee03

Comments (0)

Files changed (7)

File leaves/models.py

 	return types_pks
 
 class Preferences (models.Model):
+	DATE_ARCHIVE_CHOICES = (
+		('month', 'Monthly'),
+		('year', 'Yearly'),
+	)
+
 	site = models.OneToOneField(Site, related_name='preferences', unique=True)
 	theme = models.CharField(max_length=100, choices=settings.AVAILABLE_THEMES, default='default')
 	stream_count = models.PositiveIntegerField(default=10)
 	stream_types = models.ManyToManyField(ContentType, blank=True, related_name='preferences', limit_choices_to={'pk__in': leaf_types})
 	default_comment_status = models.CharField(max_length=10, choices=COMMENT_STATUS_CHOICES, default=settings.LEAVES_DEFAULT_COMMENT_STATUS)
 	feed_count = models.PositiveIntegerField(default=5)
+	date_archives = models.CharField(max_length=5, choices=DATE_ARCHIVE_CHOICES, default='month')
 
 	class Meta:
 		verbose_name_plural = _('preferences')

File leaves/templates/leaf_dates.html

+<ul class="date-archives">
+{% for d in dates %}
+    <li{% ifequal d current %} class="selected"{% endifequal %}>
+        {% ifequal group "month" %}
+        <a href="{% url view-archive year=d.year month=d.month %}">{{ d|date:"F Y" }}</a>
+        {% else %}
+        <a href="{% url view-archive year=d.year %}">{{ d|date:"Y" }}</a>
+        {% endifequal %}
+    </li>
+{% endfor %}
+</ul>

File leaves/templatetags/leaf.py

 @register.simple_tag
 def leaf_content(leaf):
 	return format_text(leaf.content_trans, leaf.format)
-	
+
 @register.simple_tag
 def leaf_summarize(leaf, paragraphs=1):
 	if leaf.summary_trans:
 	for plugin in settings.LEAVES_PLUGINS:
 		nav_html += navigation(plugin.split('.')[-1])
 	return nav_html
+
+@register.inclusion_tag('leaf_dates.html')
+def leaf_dates(group='month', current=None):
+	dates = [d.date() for d in Leaf.objects.dates('pub_date', group, order='DESC')]
+	return {
+		'dates': dates,
+		'group': group,
+		'current': current,
+	}

File leaves/themes/stream/templates/archive.html

+{% extends "index.html" %}
+{% load leaf %}
+{% block title %}{{ site.name }} - Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}{% endblock %}
+{% block content-header %}<h1 class="tag">Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}</h1>{% endblock %}
+{% block datenav %}
+    <div class="box">
+        <div class="title">Archive</div>
+        {% leaf_dates site.preferences.date_archives current_date %}
+    </div>
+{% endblock %}

File leaves/themes/stream/templates/base.html

 			{% navigation "photos" %}
 		</div>
 	{% endblock %}
+    {% block datenav %}
+       <div class="box">
+           <div class="title">Archive</div>
+           {% leaf_dates site.preferences.date_archives %}
+       </div>
+    {% endblock %}
 		<div class="box">
 			<div class="title">Search</div>
 			<div class="padded">

File leaves/urls.py

 	url(r'^tag/(?P<slug>[\w-]+)/$', 'view_tag', name='view-tag'),
 	url(r'^author/(?P<username>[\w-]+)/$', 'view_author', name='view-author'),
 	url(r'^leaf/(?P<leaf_id>\d+)/$', 'view_leaf', name='view-leaf'),
+	url(r'^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'view_archive', name='view-archive'),
+	url(r'^archive/(?P<year>\d{4})/$', 'view_archive', name='view-archive'),
 )
 
 urlpatterns += patterns('',

File leaves/views.py

 from django.contrib import messages
 from django.contrib.auth.models import User
 from django.conf import settings
+from django.http import Http404
 from leaves.models import Leaf, Tag
 from leaves.forms import CommentForm
 import datetime
 		'leaves': leaves.order_by('-pub_date')[:prefs.stream_count],
 	})
 
+def view_archive(request, year, month=None):
+	try:
+		year = int(year)
+		if month:
+			month = int(month)
+	except:
+		raise Http404
+	prefs = request.site.preferences
+	leaves = Leaf.on_site.active().filter(subclass__in=prefs.stream_types.all(), stream=True, pub_date__year=year)
+	current_date = datetime.date(year, 1, 1)
+	archive_type = 'year'
+	if month:
+		leaves = leaves.filter(pub_date__month=month)
+		current_date = datetime.date(year, month, 1)
+		archive_type = 'month'
+	return TemplateResponse(request, 'archive.html', {
+		'leaves': leaves.order_by('-pub_date'),
+		'archive_type': archive_type,
+		'current_date': current_date,
+	})
+
 def leaf_search(request):
 	prefs = request.site.preferences
 	leaves = Leaf.on_site.active().filter(subclass__in=prefs.stream_types.all(), stream=True)