# 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


import re
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404
from depot.models import Depot


def get_depot_if_allowed(depot_id, user):
    """
    Return the depot for the given if the user is allowed to access it

    Otherwise throws a PermissionDenied exception.

    :author: Benedikt Seidl
    """


# 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: depot/helpers.py
import re
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404
from depot.models import Depot



def get_depot_if_allowed(depot_id, user):
    """
    Return the depot for the given if the user is allowed to access it

    Otherwise throws a PermissionDenied exception.

    :author: Benedikt Seidl
    """


    depot = get_object_or_404(Depot, pk=depot_id)