move publishing schedule to URL model, make timetz aware
authorTero Marttila <terom@fixme.fi>
Thu, 22 Aug 2013 02:44:51 +0300
changeset 56 96e1c616a955
parent 55 eb36c4d046c1
child 57 6142621de48e
move publishing schedule to URL model, make timetz aware
qrurls/admin.py
qrurls/models.py
--- a/qrurls/admin.py	Thu Aug 22 02:12:13 2013 +0300
+++ b/qrurls/admin.py	Thu Aug 22 02:44:51 2013 +0300
@@ -22,14 +22,16 @@
     """
 
     def __init__ (self, *args, **kwargs) :
-        urlfeed = kwargs.get('instance')
         extra = kwargs.get('extra', 5)
 
-        publishing_date, publishing_time = URLAdmin.publish_scheduling(urlfeed)
-    
+        # hack to get at the URLFeed to determine our initial values..
+        urlfeed = kwargs.get('instance')
+        if not isinstance(urlfeed, URL) :
+            urlfeed = None
+        
         kwargs.update(initial=[
-                dict(published=publish) for publish in URLAdmin.publishing_schedule(
-                    publishing_date, publishing_time, count=extra)
+            # Either generic from today, or based on the actual urlfeed
+            dict(published=publish) for publish in URLAdmin.publishing_schedule(urlfeed, count=extra)
         ])
         super(URLItemFormset, self).__init__(*args, **kwargs)
 
@@ -43,27 +45,19 @@
 
 class URLAdmin (admin.ModelAdmin) :
     @classmethod
-    def publish_scheduling (cls, urlfeed) :
-        """Calculate initial URLItem.published values for feed, or defaults."""
-        # hack to get at the URLFeed to determine our initial values..
-        if urlfeed and isinstance(urlfeed, URL) :
-            # starting from the following day
-            return (
-                    urlfeed.last_item().published.date() + datetime.timedelta(days=1),
-                    urlfeed.publishing_time,
-            )
+    def publishing_schedule (cls, urlfeed, count):
+        """Yield URLItem.published values for feed, or defaults."""
+        if urlfeed :
+            date, time, offset = urlfeed.publishing_schedule()
         else:
-            # no data...
-            return datetime.date.today(), datetime.time()
-       
-    @classmethod
-    def publishing_schedule (cls, publishing_date, publishing_time, count) :
-        """Yield URLItem.published values."""
-        for days in xrange(0, count) :
-            yield datetime.datetime.combine(
-                    publishing_date + datetime.timedelta(days=days),
-                    publishing_time
-            )
+            # no data... use defaults
+            tznow = timezone.now() # with tzinfo
+            
+            date = tznow.date()
+            time = tznow.timetz()
+            offset = datetime.timedelta(days=1)
+        
+        return URL.apply_publishing_schedule(date, time, offset, count)
 
     def timezone (self, obj) :
         now = timezone.localtime(obj.now())
@@ -163,7 +157,9 @@
     def import_images_handler (self, url_feed, data, images=()) :
         """Custom for backend for mass-importing images into a feed."""
         
-        publishing = self.publishing_schedule(data['publishing_date'], data['publishing_time'], len(images))
+        # series of datetimes
+        publishing = url_feed.apply_publishing_schedule(
+            data['publishing_date'], data['publishing_time'], url_feed.publishing_offset, len(images))
 
         for image, publish in zip(images, publishing) :
             url_image = URLImage(image=image)
@@ -185,7 +181,7 @@
             if form.is_valid() :
                 return self.import_images_handler(url_feed, form.cleaned_data, images)
         else :
-            publishing_date, publishing_time = self.publish_scheduling(url_feed)
+            publishing_date, publishing_time, publishing_offset = url_feed.publishing_schedule()
 
             form = self.ImportForm(initial=dict(
                 shorturl        = url_feed.id,
--- a/qrurls/models.py	Thu Aug 22 02:12:13 2013 +0300
+++ b/qrurls/models.py	Thu Aug 22 02:44:51 2013 +0300
@@ -106,6 +106,35 @@
         except IndexError :
             return None
 
+    @property
+    def publishing_timetz (self) :
+        """publishing_time, with tzinfo on the correct timezone."""
+        return self.publishing_time.replace(tzinfo=timezone.get_current_timezone())
+    
+    # XXX: hardcoded for now
+    publishing_offset = datetime.timedelta(days=1)
+
+    def publishing_schedule (self) :
+        """Calculate initial URLItem.published values for feed."""
+
+        # following the last item in the queue
+        item = self.last_item()
+
+        if item and item.published > self.now():
+            # starting from the following day
+            date = item.published.date() + self.publishing_offset
+        else :
+            # defaults to today
+            date = self.now().date()
+        
+        return date, self.publishing_timetz, self.publishing_offset
+    
+    @classmethod
+    def apply_publishing_schedule (self, date, time, offset, count) :
+        """Yield publishing times off given date/time to offset * count."""
+        for index in xrange(0, count) :
+            yield datetime.datetime.combine(date + offset * index, time)
+
     def __unicode__ (self) :
         return self.shorturl