Commits

Matthew Schinckel committed 44d6feb

Added ability to view holidays for a country/year/province.
Added AU 2010/2011 holidays

Comments (0)

Files changed (6)

public_holidays/admin.py

 
 class PublicHolidayAdmin(admin.ModelAdmin):
     form = PublicHolidayAdminForm
-    
-    list_display = ('name','country','date')
+    date_hierarchy = 'date'
+    list_display = ('name','country','date', 'provinces')
 
 admin.site.register(PublicHoliday, PublicHolidayAdmin)

public_holidays/fixtures/au_public_holidays.json

+[{"pk": 6, "model": "public_holidays.publicholiday", "fields": {"date": "2010-01-01", "country": "AU", "provinces": null, "name": "New Year's Day"}}, {"pk": 9, "model": "public_holidays.publicholiday", "fields": {"date": "2010-01-26", "country": "AU", "provinces": null, "name": "Australia Day"}}, {"pk": 43, "model": "public_holidays.publicholiday", "fields": {"date": "2010-03-01", "country": "AU", "provinces": "[u'WA']", "name": "Labour Day"}}, {"pk": 39, "model": "public_holidays.publicholiday", "fields": {"date": "2010-03-08", "country": "AU", "provinces": "[u'SA']", "name": "Adelaide Cup Day"}}, {"pk": 11, "model": "public_holidays.publicholiday", "fields": {"date": "2010-03-08", "country": "AU", "provinces": "[u'VIC']", "name": "Labour Day"}}, {"pk": 52, "model": "public_holidays.publicholiday", "fields": {"date": "2010-03-08", "country": "AU", "provinces": "[u'ACT']", "name": "Canberra Day"}}, {"pk": 49, "model": "public_holidays.publicholiday", "fields": {"date": "2010-03-08", "country": "AU", "provinces": "[u'TAS']", "name": "Eight Hours Day"}}, {"pk": 13, "model": "public_holidays.publicholiday", "fields": {"date": "2010-04-02", "country": "AU", "provinces": null, "name": "Good Friday"}}, {"pk": 15, "model": "public_holidays.publicholiday", "fields": {"date": "2010-04-03", "country": "AU", "provinces": "[u'ACT', u'NSW', u'NT', u'QLD', u'SA', u'VIC']", "name": "Easter Saturday"}}, {"pk": 17, "model": "public_holidays.publicholiday", "fields": {"date": "2010-04-05", "country": "AU", "provinces": null, "name": "Easter Monday"}}, {"pk": 36, "model": "public_holidays.publicholiday", "fields": {"date": "2010-04-25", "country": "AU", "provinces": "[u'TAS']", "name": "Anzac Day"}}, {"pk": 19, "model": "public_holidays.publicholiday", "fields": {"date": "2010-04-26", "country": "AU", "provinces": "[u'ACT', u'NSW', u'NT', u'QLD', u'SA', u'VIC', u'WA']", "name": "Anzac Day"}}, {"pk": 32, "model": "public_holidays.publicholiday", "fields": {"date": "2010-05-03", "country": "AU", "provinces": "[u'NT']", "name": "May Day"}}, {"pk": 37, "model": "public_holidays.publicholiday", "fields": {"date": "2010-05-03", "country": "AU", "provinces": "[u'QLD']", "name": "Labour Day"}}, {"pk": 45, "model": "public_holidays.publicholiday", "fields": {"date": "2010-06-07", "country": "AU", "provinces": "[u'WA']", "name": "Foundation Day"}}, {"pk": 21, "model": "public_holidays.publicholiday", "fields": {"date": "2010-06-14", "country": "AU", "provinces": "[u'NT', u'QLD', u'SA', u'TAS', u'VIC', u'ACT']", "name": "Queen's Birthday"}}, {"pk": 34, "model": "public_holidays.publicholiday", "fields": {"date": "2010-08-02", "country": "AU", "provinces": "[u'NT']", "name": "Picnic Day"}}, {"pk": 54, "model": "public_holidays.publicholiday", "fields": {"date": "2010-09-27", "country": "AU", "provinces": "[u'ACT']", "name": "Family & Community Day"}}, {"pk": 47, "model": "public_holidays.publicholiday", "fields": {"date": "2010-09-27", "country": "AU", "provinces": "[u'WA']", "name": "Queen's Birthday"}}, {"pk": 30, "model": "public_holidays.publicholiday", "fields": {"date": "2010-10-04", "country": "AU", "provinces": "[u'NSW', u'SA', u'ACT']", "name": "Labour Day"}}, {"pk": 23, "model": "public_holidays.publicholiday", "fields": {"date": "2010-11-02", "country": "AU", "provinces": "[u'VIC']", "name": "Melbourne Cup Day"}}, {"pk": 57, "model": "public_holidays.publicholiday", "fields": {"date": "2010-12-25", "country": "AU", "provinces": "[u'NSW']", "name": "Christmas Day"}}, {"pk": 25, "model": "public_holidays.publicholiday", "fields": {"date": "2010-12-27", "country": "AU", "provinces": null, "name": "Christmas Day"}}, {"pk": 41, "model": "public_holidays.publicholiday", "fields": {"date": "2010-12-28", "country": "AU", "provinces": "[u'SA']", "name": "Proclamation Day"}}, {"pk": 27, "model": "public_holidays.publicholiday", "fields": {"date": "2010-12-28", "country": "AU", "provinces": "[u'ACT', u'NSW', u'NT', u'QLD', u'TAS', u'VIC', u'WA']", "name": "Boxing Day"}}, {"pk": 56, "model": "public_holidays.publicholiday", "fields": {"date": "2011-01-01", "country": "AU", "provinces": "[u'VIC', u'NSW']", "name": "New Year's Day"}}, {"pk": 8, "model": "public_holidays.publicholiday", "fields": {"date": "2011-01-03", "country": "AU", "provinces": null, "name": "New Year's Day"}}, {"pk": 10, "model": "public_holidays.publicholiday", "fields": {"date": "2011-01-26", "country": "AU", "provinces": null, "name": "Australia Day"}}, {"pk": 44, "model": "public_holidays.publicholiday", "fields": {"date": "2011-03-07", "country": "AU", "provinces": "[u'WA']", "name": "Labour Day"}}, {"pk": 53, "model": "public_holidays.publicholiday", "fields": {"date": "2011-03-14", "country": "AU", "provinces": "[u'ACT']", "name": "Canberra Day"}}, {"pk": 12, "model": "public_holidays.publicholiday", "fields": {"date": "2011-03-14", "country": "AU", "provinces": "[u'VIC']", "name": "Labour Day"}}, {"pk": 50, "model": "public_holidays.publicholiday", "fields": {"date": "2011-03-14", "country": "AU", "provinces": "[u'TAS']", "name": "Eight Hours Day"}}, {"pk": 40, "model": "public_holidays.publicholiday", "fields": {"date": "2011-03-14", "country": "AU", "provinces": "[u'SA']", "name": "Adelaide Cup Day"}}, {"pk": 14, "model": "public_holidays.publicholiday", "fields": {"date": "2011-04-22", "country": "AU", "provinces": null, "name": "Good Friday"}}, {"pk": 16, "model": "public_holidays.publicholiday", "fields": {"date": "2011-04-23", "country": "AU", "provinces": "[u'ACT', u'NSW', u'NT', u'QLD', u'SA', u'VIC']", "name": "Easter Saturday"}}, {"pk": 18, "model": "public_holidays.publicholiday", "fields": {"date": "2011-04-25", "country": "AU", "provinces": null, "name": "Easter Monday"}}, {"pk": 51, "model": "public_holidays.publicholiday", "fields": {"date": "2011-04-25", "country": "AU", "provinces": "[u'ACT', u'TAS', u'NT']", "name": "Anzac Day"}}, {"pk": 20, "model": "public_holidays.publicholiday", "fields": {"date": "2011-04-26", "country": "AU", "provinces": "[u'QLD', u'SA', u'VIC', u'WA']", "name": "Anzac Day"}}, {"pk": 33, "model": "public_holidays.publicholiday", "fields": {"date": "2011-05-02", "country": "AU", "provinces": "[u'NT']", "name": "May Day"}}, {"pk": 38, "model": "public_holidays.publicholiday", "fields": {"date": "2011-05-02", "country": "AU", "provinces": "[u'QLD']", "name": "Labour Day"}}, {"pk": 46, "model": "public_holidays.publicholiday", "fields": {"date": "2011-06-06", "country": "AU", "provinces": "[u'WA']", "name": "Foundation Day"}}, {"pk": 22, "model": "public_holidays.publicholiday", "fields": {"date": "2011-06-13", "country": "AU", "provinces": "[u'NT', u'QLD', u'SA', u'TAS', u'VIC', u'ACT']", "name": "Queen's Birthday"}}, {"pk": 35, "model": "public_holidays.publicholiday", "fields": {"date": "2011-08-01", "country": "AU", "provinces": "[u'NT']", "name": "Picnic Day"}}, {"pk": 31, "model": "public_holidays.publicholiday", "fields": {"date": "2011-10-03", "country": "AU", "provinces": "[u'NSW', u'SA', u'ACT']", "name": "Labour Day"}}, {"pk": 48, "model": "public_holidays.publicholiday", "fields": {"date": "2011-10-03", "country": "AU", "provinces": "[u'WA']", "name": "Queen's Birthday"}}, {"pk": 55, "model": "public_holidays.publicholiday", "fields": {"date": "2011-10-10", "country": "AU", "provinces": "[u'ACT']", "name": "Family & Community Day"}}, {"pk": 24, "model": "public_holidays.publicholiday", "fields": {"date": "2011-11-01", "country": "AU", "provinces": "[u'VIC']", "name": "Melbourne Cup Day"}}, {"pk": 26, "model": "public_holidays.publicholiday", "fields": {"date": "2011-12-26", "country": "AU", "provinces": null, "name": "Christmas Day"}}, {"pk": 29, "model": "public_holidays.publicholiday", "fields": {"date": "2011-12-27", "country": "AU", "provinces": "[u'ACT', u'NSW', u'NT', u'QLD', u'TAS', u'VIC', u'WA']", "name": "Boxing Day"}}, {"pk": 42, "model": "public_holidays.publicholiday", "fields": {"date": "2011-12-27", "country": "AU", "provinces": "[u'SA']", "name": "Proclamation Day"}}]

public_holidays/models.py

 import django_countries
 import jsonfield.fields
 
+
 class PublicHoliday(models.Model):
     """
     A PublicHoliday is an object that can be associated with a country, and
     class Meta:
         app_label = 'public_holidays'
         ordering = ('date', 'country')
+        unique_together = (
+            ('name', 'date', 'country'),
+        )
     
     def __unicode__(self):
         return "%s (%s) [%s]" % (self.name, self.date.year, self.country)

public_holidays/templates/public_holidays/year.html

+<html>
+  <head>
+    <title>
+      Public holidays for {% if province %}{{ province }}, {% endif %}
+      {{ country }}
+    </title>
+  </head>
+  <body>
+    {% for holiday in holidays %}
+      <h2>{{ holiday.name }}</h2>
+      <p>
+        {{ holiday.date|date:"l, jS F" }}
+        {% if not province and holiday.provinces %}
+          {{ holiday.provinces }}
+        {% endif %}
+      </p>
+    {% endfor %}
+  </body>
+</html>

public_holidays/urls.py

+from django.conf.urls.defaults import *
+from surlex.dj import surl
+
+import views
+
+urlpatterns = patterns('',
+    surl(r'^<country>/<year:Y>/(<province>/)$', views.holidays, name="public_holidays"),
+)

public_holidays/views.py

+# Some useful views related to public holidays.
+
+# Suggested use:
+import datetime
+
+from django.shortcuts import render_to_response
+from django.db.models import Q
+
+from models import PublicHoliday
+
+def holidays(request, country, year, province=None):
+    start = datetime.date(int(year), 1, 1)
+    finish = datetime.date(int(year), 12, 31)
+    country = country.upper()
+    
+    holidays = PublicHoliday.objects.filter(country=country, 
+        date__gte=start, date__lte=finish)
+    
+    if province:
+        holidays = holidays.filter(Q(provinces__contains=province)|Q(provinces=None))
+    
+    return render_to_response('public_holidays/year.html', {
+        'holidays': holidays,
+        'country': country,
+        'year': year,
+        'province': province
+    })