# Path: depot/models.py
# class Depot(models.Model):
#     """
#     A depot has a name and many depot managers.
# 
#     :author: Leo Tappe
#     :author: Benedikt Seidl
#     """
# 
#     name = models.CharField(max_length=32)
#     description = models.CharField(max_length=256, blank=True)
#     organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
#     manager_users = models.ManyToManyField(User, blank=True)
#     manager_groups = models.ManyToManyField(Group, blank=True)
#     active = models.BooleanField(default=True)
# 
#     def managed_by(self, user):
#         """
#         Depots are managed by superusers, the organization's managers,
#         any manager user and any user in a manager group.
#         """
# 
#         return (user.is_superuser
#                 or self.organization.managed_by(user)
#                 or self.manager_users.filter(id=user.id).exists()
#                 or self.manager_groups.filter(id__in=user.groups.all()).exists())
# 
#     def show_internal_items(self, user):
#         """
#         Internal items can be seen by superusers and organization members.
#         """
# 
#         return user.is_superuser or self.organization.is_member(user)
# 
#     def visible_items(self, user):
#         """
#         Return the list of items the user is allowed to see in this depot.
#         """
# 
#         if self.show_internal_items(user):
#             return self.active_items.all()
#         else:
#             return self.public_items.all()
# 
#     @property
#     def managers(self):
#         """
#         The list of users explicitly listed as managers of this depot.
#         Does not include any organization managers or superusers which are
#         not added to the depot.
#         """
# 
#         return User.objects.filter(
#             models.Q(id__in=self.manager_users.all())
#             | models.Q(groups__in=self.manager_groups.all())
#         ).distinct()
# 
#     @property
#     def public_items(self):
#         """
#         List all items with the visibility set to public.
#         """
# 
#         return self.item_set.filter(visibility=Item.VISIBILITY_PUBLIC)
# 
#     @property
#     def active_items(self):
#         return self.item_set.filter(
#             models.Q(visibility=Item.VISIBILITY_PUBLIC)
#             | models.Q(visibility=Item.VISIBILITY_INTERNAL)
#         )
# 
#     @staticmethod
#     def filter_by_user(user):
#         """
#         Filter for depots managed by the given user
#         """
# 
#         return (models.Q(organization__managers__id=user.id)
#                 | models.Q(manager_users__id=user.id)
#                 | models.Q(manager_groups__id__in=user.groups.all()))
# 
#     def __str__(self):
#         return self.name
# 
# Path: rental/models.py
# class Rental(models.Model):
#     """
#     A rental defines the amount of items and has a start and return date.
#     Only items from one depot can be requested for rental at once.
# 
#     When requesting a rental, the user has to enter his full name and email
#     address as well as describing the purpose of his rental. If they are
#     logged in, the user's id will be stored as well.
# 
#     After creating a rental request, it is in the PENDING state. From this,
#     it can be either APPROVED or DECLINED by a depot manager or REVOKED by
#     the requesting user. If all items were returned correctly, it can be
#     set to RETURNED to finish the rental process.
# 
#     :author: Benedikt Seidl
#     """
# 
#     STATE_PENDING = '1'
#     STATE_APPROVED = '2'
#     STATE_DECLINED = '3'
#     STATE_REVOKED = '4'
#     STATE_RETURNED = '5'
#     STATES = (
#         (STATE_PENDING, _('pending')),
#         (STATE_APPROVED, _('approved')),
#         (STATE_DECLINED, _('declined')),
#         (STATE_REVOKED, _('revoked')),
#         (STATE_RETURNED, _('returned')),
#     )
# 
#     uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
#     depot = models.ForeignKey(Depot, on_delete=models.CASCADE)
#     items = models.ManyToManyField(Item, through='ItemRental')
#     firstname = models.CharField(max_length=32)
#     lastname = models.CharField(max_length=32)
#     email = models.EmailField()
#     purpose = models.CharField(max_length=256)
#     user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
#     start_date = models.DateTimeField()
#     return_date = models.DateTimeField()
#     state = models.CharField(max_length=1, choices=STATES, default=STATE_PENDING)
# 
#     def clean(self):
#         if not self.depot.active:
#             raise ValidationError({'depot': 'The depot has to be active.'})
# 
#         if self.start_date > self.return_date:
#             raise ValidationError({
#                 'start_date': 'The start date must be before the return date.'
#             })
# 
#         if self.start_date < datetime.now() and self.state == self.STATE_PENDING:
#             raise ValidationError({
#                 'start_date': 'The start date must be in the future for new rentals.'
#             })
# 
#     def __str__(self):
#         return 'Rental by %s %s' % (self.firstname, self.lastname)


from depot.models import Depot
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, render
from django.views import View
from rental.models import Rental


class DepotRentalsView(View):
    """
    Provide an overview over all rentals for one depot

    Show labels to visually differentiate the state a
    rental is in.

    :author: Florian Stamer
    """

    def get(self, request, depot_id):

# Path: depot/models.py
# class Depot(models.Model):
#     """
#     A depot has a name and many depot managers.
# 
#     :author: Leo Tappe
#     :author: Benedikt Seidl
#     """
# 
#     name = models.CharField(max_length=32)
#     description = models.CharField(max_length=256, blank=True)
#     organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
#     manager_users = models.ManyToManyField(User, blank=True)
#     manager_groups = models.ManyToManyField(Group, blank=True)
#     active = models.BooleanField(default=True)
# 
#     def managed_by(self, user):
#         """
#         Depots are managed by superusers, the organization's managers,
#         any manager user and any user in a manager group.
#         """
# 
#         return (user.is_superuser
#                 or self.organization.managed_by(user)
#                 or self.manager_users.filter(id=user.id).exists()
#                 or self.manager_groups.filter(id__in=user.groups.all()).exists())
# 
#     def show_internal_items(self, user):
#         """
#         Internal items can be seen by superusers and organization members.
#         """
# 
#         return user.is_superuser or self.organization.is_member(user)
# 
#     def visible_items(self, user):
#         """
#         Return the list of items the user is allowed to see in this depot.
#         """
# 
#         if self.show_internal_items(user):
#             return self.active_items.all()
#         else:
#             return self.public_items.all()
# 
#     @property
#     def managers(self):
#         """
#         The list of users explicitly listed as managers of this depot.
#         Does not include any organization managers or superusers which are
#         not added to the depot.
#         """
# 
#         return User.objects.filter(
#             models.Q(id__in=self.manager_users.all())
#             | models.Q(groups__in=self.manager_groups.all())
#         ).distinct()
# 
#     @property
#     def public_items(self):
#         """
#         List all items with the visibility set to public.
#         """
# 
#         return self.item_set.filter(visibility=Item.VISIBILITY_PUBLIC)
# 
#     @property
#     def active_items(self):
#         return self.item_set.filter(
#             models.Q(visibility=Item.VISIBILITY_PUBLIC)
#             | models.Q(visibility=Item.VISIBILITY_INTERNAL)
#         )
# 
#     @staticmethod
#     def filter_by_user(user):
#         """
#         Filter for depots managed by the given user
#         """
# 
#         return (models.Q(organization__managers__id=user.id)
#                 | models.Q(manager_users__id=user.id)
#                 | models.Q(manager_groups__id__in=user.groups.all()))
# 
#     def __str__(self):
#         return self.name
# 
# Path: rental/models.py
# class Rental(models.Model):
#     """
#     A rental defines the amount of items and has a start and return date.
#     Only items from one depot can be requested for rental at once.
# 
#     When requesting a rental, the user has to enter his full name and email
#     address as well as describing the purpose of his rental. If they are
#     logged in, the user's id will be stored as well.
# 
#     After creating a rental request, it is in the PENDING state. From this,
#     it can be either APPROVED or DECLINED by a depot manager or REVOKED by
#     the requesting user. If all items were returned correctly, it can be
#     set to RETURNED to finish the rental process.
# 
#     :author: Benedikt Seidl
#     """
# 
#     STATE_PENDING = '1'
#     STATE_APPROVED = '2'
#     STATE_DECLINED = '3'
#     STATE_REVOKED = '4'
#     STATE_RETURNED = '5'
#     STATES = (
#         (STATE_PENDING, _('pending')),
#         (STATE_APPROVED, _('approved')),
#         (STATE_DECLINED, _('declined')),
#         (STATE_REVOKED, _('revoked')),
#         (STATE_RETURNED, _('returned')),
#     )
# 
#     uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
#     depot = models.ForeignKey(Depot, on_delete=models.CASCADE)
#     items = models.ManyToManyField(Item, through='ItemRental')
#     firstname = models.CharField(max_length=32)
#     lastname = models.CharField(max_length=32)
#     email = models.EmailField()
#     purpose = models.CharField(max_length=256)
#     user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
#     start_date = models.DateTimeField()
#     return_date = models.DateTimeField()
#     state = models.CharField(max_length=1, choices=STATES, default=STATE_PENDING)
# 
#     def clean(self):
#         if not self.depot.active:
#             raise ValidationError({'depot': 'The depot has to be active.'})
# 
#         if self.start_date > self.return_date:
#             raise ValidationError({
#                 'start_date': 'The start date must be before the return date.'
#             })
# 
#         if self.start_date < datetime.now() and self.state == self.STATE_PENDING:
#             raise ValidationError({
#                 'start_date': 'The start date must be in the future for new rentals.'
#             })
# 
#     def __str__(self):
#         return 'Rental by %s %s' % (self.firstname, self.lastname)


# Path: depot/views/depot_rentals_view.py
from depot.models import Depot
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, render
from django.views import View
from rental.models import Rental



class DepotRentalsView(View):
    """
    Provide an overview over all rentals for one depot

    Show labels to visually differentiate the state a
    rental is in.

    :author: Florian Stamer
    """

    def get(self, request, depot_id):

        depot = get_object_or_404(Depot, pk=depot_id)