mirror of
https://framagit.org/la-chariotte/la-chariotte.git
synced 2025-05-17 11:11:49 +02:00
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
import csv
|
|
import json
|
|
|
|
from django import http
|
|
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.utils import timezone
|
|
from django.views import generic
|
|
from django_weasyprint import WeasyTemplateResponseMixin
|
|
from icalendar import Calendar, Event, vCalAddress, vText
|
|
|
|
from la_chariotte.order.models import GroupedOrder
|
|
from la_chariotte.order.views.mixins import UserIsOrgaMixin
|
|
|
|
from ..forms import PlaceForm
|
|
from ..models import Place
|
|
|
|
|
|
class PlaceIndexView(LoginRequiredMixin, generic.ListView):
|
|
"""View showing all the distribution places managed by the authenticated user"""
|
|
|
|
template_name = "place/index.html"
|
|
context_object_name = "context"
|
|
|
|
def get_queryset(self):
|
|
places = Place.objects.filter(orga=self.request.user)
|
|
print(places)
|
|
|
|
# Let's filter orders by distribution place (for UI grouping)
|
|
orders = dict()
|
|
for place in places:
|
|
# TODO: maybe filter out finished GroupedOrder?
|
|
if place.orders.all():
|
|
orders[place.code] = place.orders.all()
|
|
# orders[place.code] = orders
|
|
|
|
return {
|
|
"places": places,
|
|
"orders": orders,
|
|
}
|
|
|
|
|
|
class PlaceUpdateView(UserIsOrgaMixin, generic.UpdateView):
|
|
"""View showing details and allowing updates to a distribution place"""
|
|
|
|
model = Place
|
|
template_name = "place/place_update.html"
|
|
context_object_name = "place"
|
|
form_class = PlaceForm
|
|
|
|
# Prevent URL change after creation
|
|
form_class.base_fields["code"].disabled = True
|
|
|
|
def get_object(self, queryset=None):
|
|
return get_object_or_404(Place, code=self.kwargs.get("code"))
|
|
|
|
def get_form_kwargs(self):
|
|
kwargs = super().get_form_kwargs()
|
|
kwargs["user"] = self.request.user
|
|
|
|
return kwargs
|
|
|
|
|
|
class PlaceCreateView(LoginRequiredMixin, generic.CreateView):
|
|
"""View for creating a new distribution place"""
|
|
|
|
model = Place
|
|
form_class = PlaceForm
|
|
template_name = "place/place_create.html"
|
|
|
|
# Allow setting URL for creation
|
|
form_class.base_fields["code"].disabled = False
|
|
|
|
def get_form_kwargs(self):
|
|
kwargs = super().get_form_kwargs()
|
|
kwargs["user"] = self.request.user
|
|
return kwargs
|
|
|
|
def form_valid(self, form):
|
|
self.object = form.save()
|
|
return super().form_valid(form)
|