Commits

Tom Clancy committed 9e211ac

Added logic to get release year list and cache it.

Comments (0)

Files changed (1)

 from django.db import models
 from django.conf import settings
+from django.core.cache import cache
 from django.core.urlresolvers import reverse
 
+YEARS_CACHE_KEY = 'release_years'
+
 
 class PressRelease(models.Model):
 	release_date = models.DateField()
 
 	def get_absolute_url(self):
 		return reverse('press_release_detail', args=[self.slug])
-	
+
 	def contact_info(self):
 		return ReleaseContact.objects.all()[0]
 
+	def save(self):
+		cache.delete(YEARS_CACHE_KEY)
+		super(PressRelease, self).save()
+
 	class Meta:
 		ordering = ['-release_date']
 
 	class Meta:
 		verbose_name = 'Information request'
 		verbose_name_plural = 'Information requests'
-		ordering = ['-submission_date',]
+		ordering = ['-submission_date',]
+
+
+def release_years():
+	if settings.USE_CACHE:
+		years = cache.get(YEARS_CACHE_KEY, None)
+		if years:
+			return years
+	years = []
+	for date in PressRelease.objects.dates('release_date', 'year', order='DESC'):
+		years.append(str(date.year))
+	if settings.USE_CACHE:
+		cache.set(YEARS_CACHE_KEY, years, settings.DEFAULT_CACHE_TIMEOUT)
+	return years