def _validate_borrow_timeframe(borrower, bike, start, finish, is_lender, exclude=None): if not start: raise ValidationError(_("ERROR_IMPOSSIBLE_START_DATE")) if not finish: raise ValidationError(_("ERROR_IMPOSSIBLE_FINISH_DATE")) if finish < start: raise ValidationError(_("ERROR_FINISH_BEFORE_START")) today = datetime.datetime.now().date() minstart = today + datetime.timedelta(days=MIN_DAYS) if not is_lender and start < minstart: raise ValidationError(_("ERROR_START_LESS_THEN_MINIMUM")) if len(control.active_borrows_in_timeframe(bike, start, finish, exclude=exclude)): raise ValidationError(_("ERROR_OTHER_BORROW_IN_TIMEFRAME")) args = (borrower, start, finish, exclude) if not is_lender and len(control.borrows_requested_in_timeframe(*args)): raise ValidationError(_("ERROR_ALREADY_REQUESTED_BORROW_IN_TIMEFRAME"))
def clean(self): cleaned_data = super(Respond, self).clean() bike = self.borrow.bike start = self.borrow.start finish = self.borrow.finish if cleaned_data.get("response") != "REJECTED": today = datetime.datetime.now().date() if self.borrow.finish <= today: raise ValidationError(_("ERROR_TO_LATE_TO_ACCEPT")) if not self.borrow.bike.active: raise ValidationError(_("ERROR_BIKE_NOT_ACTIVE")) if not self.borrow.bike.station: raise ValidationError(_("ERROR_BIKE_STATION_UNKNOWN")) if not self.borrow.bike.station.active: raise ValidationError(_("ERROR_BIKE_STATION_INACTIVE")) if control.active_borrows_in_timeframe(bike, start, finish): raise ValidationError(_("ERROR_OTHER_BORROW_IN_TIMEFRAME")) return cleaned_data