Commits

Christopher Young committed fec5cc5

Utility for bulk adding

Comments (0)

Files changed (1)

+
+import datetime
+
+from room_scheduler.timeslots.models import TimeSlot, TimeSlotHistory
+from room_scheduler.renters.models import Renter
+from django.contrib.auth.models import User
+
+
+class BulkOperation(object):
+    def __init__(self, start_date, end_date, times, isoweekdays):
+        self.start_date = start_date
+        self.end_date = end_date
+        self.times = [(datetime.time(sh, sm), datetime.time(eh, em)) for ((sh, sm), (eh, em)) in times]
+        self.isoweekdays = isoweekdays
+
+    def _check_if_exists(self, start_time, end_time, date):
+        try:
+            TimeSlot.objects.get(start_time=start_time,
+                                 end_time=end_time,
+                                 date=date)
+            return True
+        except TimeSlot.DoesNotExist:
+            return False
+
+    def _timeslots_in_range(self):
+        count = 0
+        roving_date = self.start_date
+        while roving_date <= self.end_date:
+            if roving_date.isoweekday() not in self.isoweekdays:
+                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)
+                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():
+            if renter:
+                timeslot.renter = renter
+            timeslot.can_be_changed_by_renter = can_be_changed_by_renter
+            timeslot.save()
+
+
+if __name__ == '__main__':
+    start = datetime.date(2012, 1, 1)
+    end = datetime.date(2012, 12, 30)
+    times = [((15, 00), (17, 00)), 
+             ((17, 00), (19, 00)),
+             ((19, 00), (21, 00)),
+             ((21, 00), (23, 00))]
+    isoweekdays = [4]
+    b = BulkOperation(start, end, times, isoweekdays)
+    b.bulk_add_timeslots()
+
+    times = [((15, 00), (17, 00)),
+             ((17, 00), (19, 00))]
+    isoweekdays = [5]
+    b = BulkOperation(start, end, times, isoweekdays)
+    b.bulk_add_timeslots()
+
+    times = [((9, 00), (11, 00)),
+             ((11, 00), (13, 00))]
+    isoweekdays = [6]
+    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()