qrurls/admin.py
changeset 47 35a8e63648c8
parent 45 33b4a60ce759
child 49 7c0dcf6603f5
--- a/qrurls/admin.py	Sun Aug 18 23:06:29 2013 +0300
+++ b/qrurls/admin.py	Sun Aug 18 23:12:54 2013 +0300
@@ -1,21 +1,29 @@
 import datetime
 
+from django.conf import settings
 from django.contrib import admin
+from django.utils import timezone, formats
 import django.utils.html
 import django.forms.models
 
 from qrurls.models import URL, URLItem, URLImage
 
 class URLItemFormset (django.forms.models.BaseInlineFormSet) :
+    """
+        Uses the existing URLItems for the URLFeed to determine the initial publishing
+        times for new items.
+    """
+
     def __init__ (self, *args, **kwargs) :
         urlfeed = kwargs.get('instance')
 
         publishing_date = None
         publishing_time = datetime.time()
         
+        # hack to get at the URLFeed to determine our initial values..
         if urlfeed and isinstance(urlfeed, URL) :
             publishing_date = urlfeed.last_item().published.date()
-            publishing_time = urlfeed.publishing_schedule
+            publishing_time = urlfeed.publishing_time
         
         def publishing_schedule (count) :
             if not publishing_date :
@@ -39,10 +47,28 @@
         super(URLItemFormset, self).__init__(*args, **kwargs)
 
 class URLItemInline (admin.TabularInline) :
+    """
+        Inline set of URLItems for an URLFeed.
+    """
+
     model = URLItem
     formset = URLItemFormset
 
 class URLAdmin (admin.ModelAdmin) :
+    def timezone (self, obj) :
+        now = timezone.localtime(obj.now())
+        tz = now.tzinfo
+        td = now.tzinfo.utcoffset(now)
+
+        if td :
+            minutes, seconds = divmod(td.total_seconds(), 60)
+            hours, minutes = divmod(minutes, 60)
+            offset = "(UTC%+03d:%02d)" % (hours, minutes)
+        else :
+            offset = ""
+
+        return u"%s %s" % (tz, offset)
+
     def qrcode_url (self, obj) :
         warn = None
 
@@ -60,12 +86,34 @@
                 img=django.utils.html.escape(obj.qrcode_img()),
         )
     qrcode_img.allow_tags = True
+    
+    # XXX: a whole bunch of ugly datetime-formatting for display...
+    def active (self, obj) :
+        item = obj.active_item()
+        if item :
+            return "%s %s" % (formats.localize(timezone.localtime(item.published)), item)
+        else :
+            return ""
+
+    def now (self, obj) :
+        return formats.localize(timezone.localtime(obj.now()))
+
+    def upcoming (self, obj) :
+        item = obj.upcoming_item()
+        if item :
+            return "%s %s" % (formats.localize(timezone.localtime(item.published)), item)
+        else :
+            return ""
 
     readonly_fields = (
+        'timezone',
         'qrcode_url',
         'qrcode_img',
         'active_item',
         'upcoming_item',
+        'active',
+        'now',
+        'upcoming',
     )
     list_display = (
         'get_absolute_url',
@@ -74,19 +122,30 @@
     )
     fieldsets = (
         (None, {
-            'fields': ('shorturl', 'active_item', 'upcoming_item', 'publishing_schedule')
+            'fields': (
+                'shorturl', 
+                'publishing_time', 
+                'timezone',
+            ),
         }),
         ("QRCode", {
             'fields': ('qrcode_url', 'qrcode_img'),
         }),
+        ("Item publishing overview", {
+            'fields': (
+                'active',
+                'now',
+                'upcoming',
+            ),
+        }),
     )
     inlines = (URLItemInline, )
 
 class URLItemAdmin (admin.ModelAdmin) :
     list_display = (
-        'shorturl', 'get_absolute_url', 'image', 'published_state', 
+        'shorturl', 'published_state', 'get_absolute_url', 'image', 'published',
     )
-    readonly_fields = ('published_state',)
+    readonly_fields = ('published_state', 'published')
     fieldsets = (
         ("Publishing", {
             'fields': ('shorturl', 'published', ),
@@ -95,7 +154,6 @@
             'fields': ('url', 'image'),
         }),
     )
-    
 
 class URLImageAdmin (admin.ModelAdmin) :
     list_display = (