def test_shift_manager_for_facility(self):
        """
            checks that get_days_with_shifts() returns only dates later than datetime.now()
        """
        now = datetime.now()
        yesterday_start = now - timedelta(1)
        yesterday_end = yesterday_start + timedelta(hours=1)
        tomorrow_start = now + timedelta(1)
        tomorrow_end = tomorrow_start + timedelta(hours=1)

        facility = FacilityFactory.create()
        task = TaskFactory.create(facility=facility)

        yesterday_shift = ShiftFactory.create(facility=facility,
                                              task=task,
                                              starting_time=yesterday_start,
                                              ending_time=yesterday_end)

        tomorrow_shift = ShiftFactory.create(facility=facility,
                                             task=task,
                                             starting_time=tomorrow_start,
                                             ending_time=tomorrow_end)

        assert Facility.objects.count(
        ) == 1, "test case assumes that shifts have been created for the same facility, as the ShiftFactory indeed does at the time of writing of this test case"
        assert Facility.objects.get() == task.facility

        shifts = Shift.open_shifts.filter(facility=facility)

        assert shifts.count(
        ) == 1, "only 1 shift should be found with Shifts.open_shifts"
        shift = shifts.get()
        assert shift == tomorrow_shift, "wrong shift was found"
        assert shift.ending_time > now, "the time has to be in the future"
    def handle(self, *args, **options):
        if options['flush']:
            print "delete all data in app tables"
            RegistrationProfile.objects.all().delete()

            Shift.objects.all().delete()
            Task.objects.all().delete()
            Workplace.objects.all().delete()
            Facility.objects.all().delete()

            UserAccount.objects.all().delete()

            # delete geographic information
            Country.objects.all().delete()
            Region.objects.all().delete()
            Area.objects.all().delete()
            Place.objects.all().delete()

            User.objects.filter().exclude(is_superuser=True).delete()

        # create regional data
        places = list()
        for i in range(0, 10):
            places.append(PlaceFactory.create())

        organizations = list()
        for i in range(0, 4):
            organizations.append(OrganizationFactory.create())

        # create shifts for number of days
        for day in range(0, options['days'][0]):
            for i in range(2, 23):
                place = places[random.randint(0, len(places) - 1)]
                organization = organizations[random.randint(
                    0,
                    len(organizations) - 1)]
                facility = FacilityFactory.create(description=LOREM,
                                                  place=place,
                                                  organization=organization)
                shift = ShiftFactory.create(starting_time=gen_date(hour=i - 1,
                                                                   day=day),
                                            ending_time=gen_date(hour=i,
                                                                 day=day),
                                            facility=facility)
                # assign random volunteer for each shift
                reg_user = ShiftHelperFactory.create(shift=shift)
                reg_user.save()
def facility():
    return FacilityFactory.build()