Commits

Christopher Young committed 5e09258

Refactor ad hoc script, which now supports removal

  • Participants
  • Parent commits d018d84

Comments (0)

Files changed (1)

         except TimeSlot.DoesNotExist:
             return False
 
-    def _timeslots_in_range(self):
+    def _start_end_times_in_range(self):
         count = 0
         roving_date = self.start_date
         while roving_date <= self.end_date:
                 roving_date = roving_date + datetime.timedelta(1)
                 continue
             for (start_time, end_time) in self.times:
-                if self._check_if_exists(start_time,
-                                         end_time,
-                                         roving_date):
-                    continue
-                yield TimeSlot(start_time=start_time,
-                               end_time=end_time,
-                               date=roving_date)
+                yield (start_time, end_time, roving_date)
+                roving_date = roving_date + datetime.timedelta(1)
                 count += 1
                 if count % 10 == 0: print "."
-            roving_date = roving_date + datetime.timedelta(1)
-        print "\nDone!"
+                
 
     def bulk_add_timeslots(self, renter=None, can_be_changed_by_renter=False):
-        for timeslot in self._timeslots_in_range():
+        for (start_time, end_time, roving_date) in self._start_end_times_in_range():
+            if self._check_if_exists(start_time, end_time, roving_date):
+                continue
+            timeslot = TimeSlot(start_time=start_time, end_time=end_time, date=roving_date)
             if renter:
                 timeslot.renter = renter
             timeslot.can_be_changed_by_renter = can_be_changed_by_renter
             timeslot.save()
+        print "\nDone!"
 
+    def bulk_remove_timeslots(self):
+        for (start_time, end_time, date) in self._start_end_times_in_range():
+            print "{s}, {e}, {d}".format(s=start_time, e=end_time, d=date)
+            if self._check_if_exists(start_time, end_time, date):
+                t = TimeSlot.objects.get(start_time=start_time, 
+                                         end_time=end_time, 
+                                         date=date)
+                print "Deleting {t}".format(t=t)
+                t.delete()
+        print "\nDone!"
 
-def job():
+
+def add():
     start = datetime.date(2012, 1, 1)
     end = datetime.date(2012, 12, 30)
     times = [((15, 00), (17, 00)), 
     b = BulkOperation(start, end, times, isoweekdays)
     b.bulk_add_timeslots()
 
-    times = [((15, 00), (17, 00)),
-             ((17, 00), (19, 00))]
-    isoweekdays = [5]
+def remove():
+    start = datetime.date(2012, 1, 1)
+    end = datetime.date(2012, 2, 29)
+    times = [((21, 00), (23, 00))]
+    isoweekdays = [4]
     b = BulkOperation(start, end, times, isoweekdays)
-    b.bulk_add_timeslots()
+    b.bulk_remove_timeslots()
 
-    times = [((9, 00), (11, 00)),
-             ((11, 00), (13, 00))]
-    isoweekdays = [6]
+    start = datetime.date(2012, 3, 1)
+    end = datetime.date(2012, 12, 31)
+    times = [((20, 00), (22, 00))]
+    isoweekdays = [4]
     b = BulkOperation(start, end, times, isoweekdays)
-    b.bulk_add_timeslots()
-
-    times = [((10, 00), (12, 00)),
-             ((12, 00), (14, 00))]
-    isoweekdays = [7]
-    b = BulkOperation(start, end, times, isoweekdays)
-    b.bulk_add_timeslots()
+    b.bulk_remove_timeslots()
 
 
 if __name__ == '__main__':