diff options
| author | jdlugosz963 <jdlugosz963@gmail.com> | 2023-07-17 01:47:57 +0200 |
|---|---|---|
| committer | jdlugosz963 <jdlugosz963@gmail.com> | 2023-07-17 01:52:26 +0200 |
| commit | c21f4b3dacd597a15a5ec39d525df1dfe1b70376 (patch) | |
| tree | 0dfc51d2ffeb7b5022d9ad852f2fd3620c72196a /restaurant_orders/dashboard/views.py | |
| parent | 2c6f98aeef4fa1aba5678fe17c8e762a11db7b40 (diff) | |
| download | restaurant-orders-main.tar.gz restaurant-orders-main.zip | |
Add project.main
Diffstat (limited to 'restaurant_orders/dashboard/views.py')
| -rw-r--r-- | restaurant_orders/dashboard/views.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/restaurant_orders/dashboard/views.py b/restaurant_orders/dashboard/views.py new file mode 100644 index 0000000..cfbc757 --- /dev/null +++ b/restaurant_orders/dashboard/views.py | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | from django.shortcuts import render, redirect, reverse | ||
| 2 | from django.contrib.auth.mixins import LoginRequiredMixin | ||
| 3 | from django.views.generic.list import ListView, View | ||
| 4 | from django.views.generic.edit import UpdateView | ||
| 5 | from django.contrib.messages.views import SuccessMessageMixin | ||
| 6 | from django.core import serializers | ||
| 7 | from django.contrib import messages | ||
| 8 | |||
| 9 | |||
| 10 | from dashboard.forms import OrderStatusForm, AddToBillForm | ||
| 11 | from core.tasks import create_order_and_send_notification | ||
| 12 | from core.models import Restaurant, Order | ||
| 13 | |||
| 14 | class Home(LoginRequiredMixin, View): | ||
| 15 | def get(self, request): | ||
| 16 | redirect_url = 'dashboard:restaurant_dashboard' | ||
| 17 | restaurants = Restaurant.get_user_restaurants(request.user) | ||
| 18 | |||
| 19 | if len(restaurants) == 1: | ||
| 20 | return redirect(redirect_url, restaurant_pk=restaurants[0].pk) | ||
| 21 | |||
| 22 | return render(request, template_name='restaurants_choice.html', context={ | ||
| 23 | 'title': 'Dashboard', | ||
| 24 | 'restaurants': restaurants, | ||
| 25 | 'redirect_url': redirect_url | ||
| 26 | }) | ||
| 27 | |||
| 28 | class DashboardView(LoginRequiredMixin, ListView): | ||
| 29 | template_name = 'dashboard/dashboard.html' | ||
| 30 | model = Order | ||
| 31 | paginate_by = 4 | ||
| 32 | |||
| 33 | def get_queryset(self, *args, **kwargs): | ||
| 34 | restaurant = Restaurant.get_user_restaurant_or_404(pk=self.kwargs.get('restaurant_pk'), | ||
| 35 | user=self.request.user) | ||
| 36 | |||
| 37 | status = self.request.GET.get('status') | ||
| 38 | queryset = {} | ||
| 39 | if status: | ||
| 40 | queryset['wp_status'] = status | ||
| 41 | |||
| 42 | obj = super().get_queryset(*args, **kwargs).filter( | ||
| 43 | restaurant=restaurant, | ||
| 44 | can_display=True, | ||
| 45 | **queryset | ||
| 46 | ).order_by('-wp_id') | ||
| 47 | |||
| 48 | return obj | ||
| 49 | |||
| 50 | |||
| 51 | class DashboardOrderView(LoginRequiredMixin, View): | ||
| 52 | def get(self, request, pk): | ||
| 53 | order = Order.get_order(pk, request.user) | ||
| 54 | orderStatusForm = OrderStatusForm(instance=order) | ||
| 55 | addToBillForm = AddToBillForm(pk, request.user) | ||
| 56 | |||
| 57 | return render(request, 'dashboard/dashboard_order.html', context={ | ||
| 58 | 'order': order, | ||
| 59 | 'orderStatusForm': orderStatusForm, | ||
| 60 | 'addToBillForm': addToBillForm | ||
| 61 | }) | ||
| 62 | |||
| 63 | |||
| 64 | class ChangeOrderStatusView(LoginRequiredMixin, SuccessMessageMixin, UpdateView): | ||
| 65 | form_class = OrderStatusForm | ||
| 66 | model = Order | ||
| 67 | success_message = 'Zapisano!' | ||
| 68 | slug_field='order_pk' | ||
| 69 | |||
| 70 | def get_queryset(self, *args, **kwargs): | ||
| 71 | return super().get_queryset(*args, **kwargs).filter( | ||
| 72 | pk=self.kwargs['pk'], | ||
| 73 | can_display=True, | ||
| 74 | restaurant__users=self.request.user.pk | ||
| 75 | ) | ||
| 76 | |||
| 77 | def get_success_url(self): | ||
| 78 | return reverse('dashboard:order_dashboard', args=(self.kwargs['pk'], )) | ||
| 79 | |||
| 80 | class AddToBillView(LoginRequiredMixin, View): | ||
| 81 | def post(self, request, pk, *args, **kwargs): | ||
| 82 | addToBillForm = AddToBillForm(pk, request.user, request.POST) | ||
| 83 | |||
| 84 | if addToBillForm.is_valid(): | ||
| 85 | order = Order.get_order(pk, request.user) | ||
| 86 | order = serializers.serialize('json', (order, )) | ||
| 87 | email = True if addToBillForm.data.get('send_mail') else False | ||
| 88 | phone = True if addToBillForm.data.get('send_sms') else False | ||
| 89 | items = [(wp_pk, price) for wp_pk, price in request.POST.items() if price.isdigit()] | ||
| 90 | |||
| 91 | # TODO: Za duzo tych jebanych argumentow ! | ||
| 92 | create_order_and_send_notification.delay(order, items, email, phone, request.user.pk) | ||
| 93 | |||
| 94 | |||
| 95 | return redirect('dashboard:order_dashboard', pk) | ||
