Commits

Luke Plant committed f885ec9

Added photochanger functionality, for returning random photos from the slideshow

Comments (0)

Files changed (2)

christchurch/urls.py

 admin.autodiscover()
 
 urlpatterns = patterns('',
+                       url(r'^photochanger/$', 'christchurch.views.photochanger'),
                        url(r'^thissunday/$', 'christchurch.views.this_sunday'),
                        url(r'^upcoming-midweek/$', 'christchurch.views.upcoming_midweek'),
                        url(r'^semantic/', include('semanticeditor.urls')),

christchurch/views.py

 from datetime import datetime, timedelta
 from dateutil import tz, rrule
+import os
+import os.path
 import pytz
+import random
 import re
 
+from django.conf import settings
+from django.http import HttpResponseRedirect
 from django.shortcuts import render
+from django.utils.cache import patch_cache_control, add_never_cache_headers
 import requests
 import vobject
 
 
     events.sort()
     return events
+
+
+def photochanger(request):
+    """
+    Returns a redirect to a random photo in the slideshow
+    """
+    slideshow_dir = 'photos/slideshow'
+    files = os.listdir(os.path.join(settings.MEDIA_ROOT, slideshow_dir))
+    files = [f for f in files if f.endswith('.jpg')]
+
+    choice = random.choice(files)
+    print choice
+    response = HttpResponseRedirect(os.path.join(settings.MEDIA_URL, slideshow_dir,
+                                                 choice))
+
+    # We want the visitor to get different photos each time,
+    # so we say "No really, don't cache this"
+    add_never_cache_headers(response)
+    patch_cache_control(response, no_cache=True)
+    patch_cache_control(response, must_revalidate=True)
+    return response
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.