# Path: depot/views/depot_create_rental_view.py
# class DepotCreateRentalView(View):
#     """
#     Show a form to create a new rental for the given depot
# 
#     :author: Benedikt Seidl
#     """
# 
#     def get(self, request, depot_id):
#         depot = get_depot_if_allowed(depot_id, request.user)
# 
#         # configure time frame
#         start_date, return_date = self.get_start_return_date(request.GET)
# 
#         item_list = depot.visible_items(request.user)
# 
#         availability = Availability(
#             datetime.combine(start_date.date() - timedelta(days=1), datetime.min.time()),
#             datetime.combine(return_date.date() + timedelta(days=1), datetime.min.time()),
#             depot_id
#         )
# 
#         availability_data = []
# 
#         for item in item_list:
#             intervals = availability.get_availability_intervals(item)
# 
#             availability_data.append((
#                 item,
#                 self.get_chart_data(intervals),
#                 min(intervals).value
#             ))
# 
#         errors = request.session.pop('errors', None)
#         data = request.session.pop('data', {})
# 
#         return render(request, 'depot/create-rental.html', {
#             'depot': depot,
#             'show_visibility': depot.show_internal_items(request.user),
#             'availability_data': availability_data,
#             'errors': errors,
#             'data': data,
#             'item_quantities': extract_item_quantities(data),
#             'start_date': start_date,
#             'return_date': return_date,
#             'start_date_formatted': start_date.strftime('%Y-%m-%d %H:%M'),
#             'return_date_formatted': return_date.strftime('%Y-%m-%d %H:%M')
#         })
# 
#     def get_start_return_date(self, data):
#         """
#         Return the start and return dates from the request
# 
#         If required data is missing or invalid, default values are returned.
#         """
# 
#         try:
#             start_date = datetime.strptime(data.get('start_date'), '%Y-%m-%d %H:%M')
#         except (ValueError, TypeError):
#             start_date = datetime.now() + timedelta(days=1)
# 
#         try:
#             return_date = datetime.strptime(data.get('return_date'), '%Y-%m-%d %H:%M')
#         except (ValueError, TypeError):
#             return_date = start_date + timedelta(days=3)
# 
#         return (start_date, max(start_date, return_date))
# 
#     def get_chart_data(self, intervals):
#         """
#         Generate the data the JavaScript can render
#         """
# 
#         data = []
# 
#         for interval in intervals:
#             data.append({
#                 "x": interval.begin.isoformat(),
#                 "y": interval.value
#             })
#             data.append({
#                 "x": interval.end.isoformat(),
#                 "y": interval.value
#             })
# 
#         return data
# 
# Path: depot/views/depot_detail_view.py
# class DepotDetailView(View):
#     """
#     Provide a detailed overview of all items in a depot
# 
#     Archived depots can only be accessed by superusers and organization managers.
#     The internal items of a depot can be seen by all members of this organization.
# 
#     :author: Florian Stamer
#     """
# 
#     def get(self, request, depot_id):
#         depot = get_depot_if_allowed(depot_id, request.user)
#         item_list = depot.visible_items(request.user)
# 
#         return render(request, 'depot/detail.html', {
#             'depot': depot,
#             'item_list': item_list,
#             'show_visibility': depot.show_internal_items(request.user),
#             'managed_by_user': depot.managed_by(request.user),
#             'start_date': datetime.now() + timedelta(days=1),
#             'return_date': datetime.now() + timedelta(days=4)
#         })
# 
# Path: depot/views/depot_index_view.py
# class DepotIndexView(View):
#     """
#     Show an index page of all organizations and their depots
# 
#     Only organization managers get a link to the admin interface of their
#     organization so that they can change the name and add new depots.
# 
#     :author: Florian Stamer
#     :author: Benedikt Seidl
#     """
# 
#     def get(self, request):
#         organization_depots = []
# 
#         for organization in Organization.objects.all():
#             managed_by_user = organization.managed_by(request.user)
#             depots = organization.active_depots.all()
# 
#             if depots:
#                 organization_depots.append({
#                     'model': organization,
#                     'managed_by_user': managed_by_user,
#                     'depots': depots
#                 })
# 
#         return render(request, 'depot/index.html', {
#             'organization_depots': organization_depots
#         })
# 
# Path: depot/views/depot_rentals_view.py
# 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)
# 
#         if not depot.managed_by(request.user):
#             return HttpResponseForbidden('Not a manager of this depot')
# 
#         rentals = Rental.objects.filter(
#             depot_id=depot.id,
#             state__in=[Rental.STATE_PENDING, Rental.STATE_APPROVED]
#         )
# 
#         return render(request, 'depot/rentals.html', {
#             'rentals': rentals,
#             'depot': depot,
#         })


from django.urls import path
from .views.depot_create_rental_view import DepotCreateRentalView
from .views.depot_detail_view import DepotDetailView
from .views.depot_index_view import DepotIndexView
from .views.depot_rentals_view import DepotRentalsView

app_name = 'depot'
urlpatterns = [
    path('', DepotIndexView.as_view(), name='index'),

# Path: depot/views/depot_create_rental_view.py
# class DepotCreateRentalView(View):
#     """
#     Show a form to create a new rental for the given depot
# 
#     :author: Benedikt Seidl
#     """
# 
#     def get(self, request, depot_id):
#         depot = get_depot_if_allowed(depot_id, request.user)
# 
#         # configure time frame
#         start_date, return_date = self.get_start_return_date(request.GET)
# 
#         item_list = depot.visible_items(request.user)
# 
#         availability = Availability(
#             datetime.combine(start_date.date() - timedelta(days=1), datetime.min.time()),
#             datetime.combine(return_date.date() + timedelta(days=1), datetime.min.time()),
#             depot_id
#         )
# 
#         availability_data = []
# 
#         for item in item_list:
#             intervals = availability.get_availability_intervals(item)
# 
#             availability_data.append((
#                 item,
#                 self.get_chart_data(intervals),
#                 min(intervals).value
#             ))
# 
#         errors = request.session.pop('errors', None)
#         data = request.session.pop('data', {})
# 
#         return render(request, 'depot/create-rental.html', {
#             'depot': depot,
#             'show_visibility': depot.show_internal_items(request.user),
#             'availability_data': availability_data,
#             'errors': errors,
#             'data': data,
#             'item_quantities': extract_item_quantities(data),
#             'start_date': start_date,
#             'return_date': return_date,
#             'start_date_formatted': start_date.strftime('%Y-%m-%d %H:%M'),
#             'return_date_formatted': return_date.strftime('%Y-%m-%d %H:%M')
#         })
# 
#     def get_start_return_date(self, data):
#         """
#         Return the start and return dates from the request
# 
#         If required data is missing or invalid, default values are returned.
#         """
# 
#         try:
#             start_date = datetime.strptime(data.get('start_date'), '%Y-%m-%d %H:%M')
#         except (ValueError, TypeError):
#             start_date = datetime.now() + timedelta(days=1)
# 
#         try:
#             return_date = datetime.strptime(data.get('return_date'), '%Y-%m-%d %H:%M')
#         except (ValueError, TypeError):
#             return_date = start_date + timedelta(days=3)
# 
#         return (start_date, max(start_date, return_date))
# 
#     def get_chart_data(self, intervals):
#         """
#         Generate the data the JavaScript can render
#         """
# 
#         data = []
# 
#         for interval in intervals:
#             data.append({
#                 "x": interval.begin.isoformat(),
#                 "y": interval.value
#             })
#             data.append({
#                 "x": interval.end.isoformat(),
#                 "y": interval.value
#             })
# 
#         return data
# 
# Path: depot/views/depot_detail_view.py
# class DepotDetailView(View):
#     """
#     Provide a detailed overview of all items in a depot
# 
#     Archived depots can only be accessed by superusers and organization managers.
#     The internal items of a depot can be seen by all members of this organization.
# 
#     :author: Florian Stamer
#     """
# 
#     def get(self, request, depot_id):
#         depot = get_depot_if_allowed(depot_id, request.user)
#         item_list = depot.visible_items(request.user)
# 
#         return render(request, 'depot/detail.html', {
#             'depot': depot,
#             'item_list': item_list,
#             'show_visibility': depot.show_internal_items(request.user),
#             'managed_by_user': depot.managed_by(request.user),
#             'start_date': datetime.now() + timedelta(days=1),
#             'return_date': datetime.now() + timedelta(days=4)
#         })
# 
# Path: depot/views/depot_index_view.py
# class DepotIndexView(View):
#     """
#     Show an index page of all organizations and their depots
# 
#     Only organization managers get a link to the admin interface of their
#     organization so that they can change the name and add new depots.
# 
#     :author: Florian Stamer
#     :author: Benedikt Seidl
#     """
# 
#     def get(self, request):
#         organization_depots = []
# 
#         for organization in Organization.objects.all():
#             managed_by_user = organization.managed_by(request.user)
#             depots = organization.active_depots.all()
# 
#             if depots:
#                 organization_depots.append({
#                     'model': organization,
#                     'managed_by_user': managed_by_user,
#                     'depots': depots
#                 })
# 
#         return render(request, 'depot/index.html', {
#             'organization_depots': organization_depots
#         })
# 
# Path: depot/views/depot_rentals_view.py
# 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)
# 
#         if not depot.managed_by(request.user):
#             return HttpResponseForbidden('Not a manager of this depot')
# 
#         rentals = Rental.objects.filter(
#             depot_id=depot.id,
#             state__in=[Rental.STATE_PENDING, Rental.STATE_APPROVED]
#         )
# 
#         return render(request, 'depot/rentals.html', {
#             'rentals': rentals,
#             'depot': depot,
#         })


# Path: depot/urls.py
from django.urls import path
from .views.depot_create_rental_view import DepotCreateRentalView
from .views.depot_detail_view import DepotDetailView
from .views.depot_index_view import DepotIndexView
from .views.depot_rentals_view import DepotRentalsView


app_name = 'depot'
urlpatterns = [
    path('', DepotIndexView.as_view(), name='index'),

    path('<int:depot_id>/', DepotDetailView.as_view(), name='detail'),