django-room-scheduler / bulk_add.py

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 _start_end_times_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:
                yield (start_time, end_time, roving_date)
                roving_date = roving_date + datetime.timedelta(1)
                count += 1
                if count % 10 == 0: print "."
                

    def bulk_add_timeslots(self, renter=None, can_be_changed_by_renter=False):
        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 add():
    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()

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_remove_timeslots()

    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_remove_timeslots()


if __name__ == '__main__':
    job()
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.