def test_make_more_than_one_reservation(self): self.user_quota.number_of_reservations = 5 self.user_quota.save() self.assertTrue( Quota.can_make_new_reservation(self.user, self.machine_type)) for reservation_number in range(5): self.check_reservation_valid( self.create_reservation( timedelta(days=reservation_number, hours=1), timedelta(days=reservation_number, hours=2)), "User should be able to make as many reservations as allowed") self.assertFalse( Quota.can_make_new_reservation(self.user, self.machine_type))
def get_context_data(self, year, week, machine): """ Create the context required for the controls and the information to be displayed :param year: The year to show the calendar for :param week: The week to show the calendar for :param machine: The machine object to show the calendar for :return: context required to show the reservation calendar with controls """ context = super().get_context_data() context.update({ "can_make_reservations": False, "can_make_more_reservations": False, "can_ignore_rules": False, "other_machines": Machine.objects.exclude(pk=machine.pk).filter( machine_type=machine.machine_type), "machine": machine, "year": year, "week": week, "date": year_and_week_to_monday(year, week), }) if self.request.user.is_authenticated: context.update({ "can_make_reservations": machine.machine_type.can_user_use(self.request.user), "can_make_more_reservations": Quota.can_make_new_reservation(self.request.user, machine.machine_type), "can_ignore_rules": any( quota.can_make_more_reservations(self.request.user) for quota in Quota.get_user_quotas( self.request.user, machine.machine_type).filter( ignore_rules=True)) }) return context
def get_context_data(self, machine_type, **kwargs): context_data = super().get_context_data(**kwargs) context_data.update({ "machine_type": machine_type, "rules": ReservationRule.objects.filter(machine_type=machine_type), }) if not self.request.user.is_anonymous: context_data.update({ "quotas": Quota.get_user_quotas(self.request.user, machine_type), }) return context_data
def get_machine_data(request, machine, reservation=None): return JsonResponse({ "reservations": [{"start_date": c_reservation.start_time, "end_date": c_reservation.end_time} for c_reservation in Reservation.objects.filter(end_time__gte=timezone.now(), machine=machine) if c_reservation != reservation], "canIgnoreRules": any(quota.ignore_rules and quota.can_make_more_reservations(request.user) for quota in Quota.get_user_quotas(request.user, machine.machine_type)), "rules": [ { "periods": [ [ day + rule.start_time.hour / 24 + rule.start_time.minute / 1440, (day + rule.days_changed + rule.end_time.hour / 24 + rule.end_time.minute / 1440) % 7 ] for day, _ in enumerate(bin(rule.start_days)[2:][::-1]) if _ == "1" ], "max_hours": rule.max_hours, "max_hours_crossed": rule.max_inside_border_crossed, } for rule in ReservationRule.objects.filter(machine_type=machine.machine_type)] })
def get_context_data(self, year, week, machine): """ Create the context required for the given machine in the given week of the given year. The context also includes the year and week in case they changed. :param year: The year to show the calendar for :param week: The week to show the calendar for :param machine: The machine object to show the calendar for :return: context for the reservation calendar """ if not is_valid_week(year, week): year, week = get_next_week(year, week, 1) context = { 'week_days': self.get_week_days_with_reservations(year, week, machine), "week": week, "year": year, "machine": machine, "now": timezone.now(), "max_reservation_time": 1, "can_make_reservations": False, "can_make_more_reservations": False, "can_ignore_rules": "false", "rules": [{ "periods": [[ day + rule.start_time.hour / 24 + rule.start_time.minute / 1440, (day + rule.days_changed + rule.end_time.hour / 24 + rule.end_time.minute / 1440) % 7 ] for day, _ in enumerate(bin(rule.start_days)[2:][::-1]) if _ == "1"], "max_hours": rule.max_hours, "max_hours_crossed": rule.max_inside_border_crossed, } for rule in ReservationRule.objects.filter( machine_type=machine.machine_type)] } if self.request.user.is_authenticated: context[ "can_make_more_reservations"] = Quota.can_make_new_reservation( self.request.user, machine.machine_type) context[ "can_make_reservations"] = machine.machine_type.can_user_use( self.request.user) context["can_ignore_rules"] = str( any( quota.can_make_more_reservations(self.request.user) for quota in Quota.get_user_quotas( self.request.user, machine.machine_type).filter( ignore_rules=True))).lower() return context