2022-01-14 23:26:35 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.urls import reverse
|
2022-01-02 17:29:35 +00:00
|
|
|
|
2022-01-16 16:13:59 +00:00
|
|
|
from django.views.generic.edit import CreateView, FormView
|
2022-01-14 23:26:35 +00:00
|
|
|
from django.views.generic.base import TemplateView
|
|
|
|
from django.views.generic.list import ListView
|
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
|
2022-01-16 16:13:59 +00:00
|
|
|
from .models import *
|
|
|
|
from .forms import *
|
2022-01-30 12:00:58 +00:00
|
|
|
from .services import *
|
2022-01-16 16:13:59 +00:00
|
|
|
|
2022-01-30 12:00:58 +00:00
|
|
|
class ProductOneTimeOrderView(FormView):
|
|
|
|
form_class = ProductOneTimeOrderForm
|
2022-01-16 16:13:59 +00:00
|
|
|
template_name = 'app/productorder_form.html'
|
2022-01-30 12:00:58 +00:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return "/"
|
2022-01-16 16:13:59 +00:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
2022-01-30 09:11:22 +00:00
|
|
|
|
|
|
|
# Set the product so the form can retrieve the resources
|
2022-01-16 16:13:59 +00:00
|
|
|
product = get_object_or_404(Product, slug=self.kwargs['product'])
|
|
|
|
kwargs['resources'] = product.resources.all()
|
|
|
|
return kwargs
|
2022-01-14 23:26:35 +00:00
|
|
|
|
2022-01-30 09:11:22 +00:00
|
|
|
def get_initial(self):
|
2022-01-30 12:00:58 +00:00
|
|
|
"""
|
|
|
|
Initial values for the form
|
|
|
|
"""
|
|
|
|
|
2022-01-30 09:11:22 +00:00
|
|
|
initial = super().get_initial()
|
|
|
|
|
|
|
|
initial['product'] = self.kwargs['product']
|
2022-01-30 12:00:58 +00:00
|
|
|
|
|
|
|
if 'timeframe' in self.kwargs:
|
|
|
|
initial['timeframe'] = self.kwargs['timeframe']
|
2022-01-30 09:11:22 +00:00
|
|
|
return initial
|
|
|
|
|
2022-01-14 23:26:35 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['product'] = get_object_or_404(Product, slug=self.kwargs['product'])
|
2022-01-30 12:00:58 +00:00
|
|
|
if 'timeframe' in context:
|
|
|
|
context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
|
2022-01-14 23:26:35 +00:00
|
|
|
return context
|
|
|
|
|
2022-01-30 09:11:22 +00:00
|
|
|
def form_valid(self, form):
|
2022-01-30 12:00:58 +00:00
|
|
|
product = get_object_or_404(Product, slug=form.cleaned_data['product'])
|
|
|
|
|
|
|
|
print("We got a valid form, let's create the order, listing fields:\n------")
|
2022-01-30 09:11:22 +00:00
|
|
|
for f in form.fields:
|
|
|
|
print(f)
|
|
|
|
print(form.cleaned_data)
|
2022-01-30 12:00:58 +00:00
|
|
|
|
|
|
|
if 'timeframe' in form.cleaned_data:
|
|
|
|
timeframe = get_object_or_404(TimeFrame, slug=form.cleaned_data['timeframe'])
|
|
|
|
else:
|
|
|
|
timeframe = None
|
|
|
|
|
|
|
|
order_product(product, timeframe, form.cleaned_data)
|
|
|
|
|
2022-01-30 09:11:22 +00:00
|
|
|
return super().form_valid(form)
|
2022-01-16 16:13:59 +00:00
|
|
|
|
2022-01-30 12:00:58 +00:00
|
|
|
class ProductOrderView(ProductOneTimeOrderView):
|
|
|
|
form_class = ProductOrderForm
|
2022-01-14 23:26:35 +00:00
|
|
|
|
|
|
|
class ProductDetailView(DetailView):
|
|
|
|
model = Product
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['productorder'] = reverse('product-order', kwargs={'product': self.object.slug })
|
2022-01-15 22:57:55 +00:00
|
|
|
context['timeframes'] = context['product'].valid_timeframes()
|
2022-01-30 12:00:58 +00:00
|
|
|
context['has_one_time_price'] = context['product'].has_one_time_price()
|
2022-01-15 22:57:55 +00:00
|
|
|
print(context)
|
2022-01-14 23:26:35 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class ProductListView(ListView):
|
|
|
|
model = Product
|
|
|
|
|
|
|
|
|
|
|
|
class ProductSelectView(CreateView):
|
|
|
|
model = ProductOrder
|
|
|
|
fields = ['product' ]
|
|
|
|
|
2022-01-30 13:47:01 +00:00
|
|
|
class IndexView(TemplateView):
|
|
|
|
template_name = "app/index.html"
|
2022-01-14 23:26:35 +00:00
|
|
|
|
|
|
|
class Yearly(TemplateView):
|
|
|
|
template_name = "app/config_product.html"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(Yearly, self).get_context_data(**kwargs)
|
|
|
|
context['current_year'] = self.current_year
|
|
|
|
context['current_month'] = self.current_month
|
|
|
|
return context
|