def save(self, commit=True, **kwargs): """When save a journey form, you have to provide an user.""" user = self.user if "user" in kwargs: assert isinstance(kwargs["user"], User) user = kwargs.get("user") journey_template = super(SmartJourneyTemplateForm, self).save(commit=False) journey_template.user = user journey_template.driver = user if self.cleaned_data[ "i_am_driver"] else None # Smart origin, destiny and kind origin = self.cleaned_data["origin"] destiny = self.cleaned_data["destiny"] attribute_selector = { Residence: "residence", Campus: "campus", } attribute = attribute_selector[origin.__class__] setattr(journey_template, attribute, origin) attribute = attribute_selector[destiny.__class__] setattr(journey_template, attribute, destiny) journey_template.kind = GOING if isinstance(origin, Residence) else RETURN if commit: journey_template.save() # Expand journey recurrence expand(journey_template) return journey_template
def test_expand(self): journey = JourneyTemplateFactory() journey.departure = journey.departure.replace(day=1, month=1) journey.save() pattern = rrule(dtstart=make_naive(journey.departure), until=make_naive(journey.departure) + datetime.timedelta(days=20), freq=DAILY) journey.recurrence = str(pattern) journey.save() journeys = expand(journey) self.assertEquals(21, len(journeys))
def test_smart_create_no_transport(self): """Test smart create of a journey without transport.""" user = UserFactory() origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplate.objects.smart_create( user=user, origin=origin, destination=destination, departure=timezone.now() + datetime.timedelta(days=1) ) journeys = expand(template) journey = journeys[0] self.assertEquals(Journey.objects.count(), 1) self.assertEquals(Journey.objects.first(), journey)
def test_expand_september(self): journey = JourneyFactory() journey.departure = journey.departure.replace(month=9) journey.save() rule = recurrence.Rule(recurrence.DAILY) pattern = recurrence.Recurrence( dtstart=make_naive(journey.departure) + datetime.timedelta(days=1), dtend=make_naive(journey.departure) + datetime.timedelta(days=20), rrules=[ rule, ]) journey.recurrence = pattern journey.save() journeys = expand(journey) self.assertEquals(22, len(journeys))
def test_smart_create(self): """Test smart create of a journey with transport.""" user = UserFactory() transport = TransportFactory(user=user) origin = ResidenceFactory(user=user) destination = CampusFactory() template = JourneyTemplate.objects.smart_create( user=user, origin=origin, destination=destination, departure=timezone.now() + datetime.timedelta(days=1), transport=transport ) journeys = expand(template) journey = journeys[0] self.assertEquals(Journey.objects.count(), 1) self.assertEquals(Journey.objects.first(), journey) self.assertEquals(journey.free_places, transport.default_places) self.assertEquals(journey.current_free_places(), transport.default_places) self.assertIsNotNone(journey.driver) self.assertEquals(user, journey.driver)