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 django.http import HttpResponse
|
2022-01-14 21:23:39 +00:00
|
|
|
|
2022-01-16 16:13:59 +00:00
|
|
|
from .models import *
|
|
|
|
from .forms import *
|
|
|
|
|
|
|
|
#class ProductOrderView(CreateView):
|
|
|
|
class ProductOrderView(FormView):
|
|
|
|
form_class = ProductOrderForm
|
|
|
|
template_name = 'app/productorder_form.html'
|
|
|
|
success_url = '/'
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
# print(self)
|
|
|
|
# print(self.request)
|
|
|
|
print(self.kwargs)
|
|
|
|
#context = self.get_context_data(**kwargs)
|
|
|
|
#kwargs['resources'] = self.request['object'].resources.all()
|
|
|
|
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
|
|
|
|
|
|
|
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-15 22:57:55 +00:00
|
|
|
context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
|
2022-01-16 16:13:59 +00:00
|
|
|
# context['form'] = ProductOrderForm(
|
|
|
|
# context['product'].resources.all(),
|
|
|
|
# initial={'product': context['product'].slug,
|
|
|
|
# 'timeframe': context['timeframe'].slug}
|
|
|
|
# )
|
2022-01-14 23:26:35 +00:00
|
|
|
return context
|
|
|
|
|
2022-01-16 16:13:59 +00:00
|
|
|
# v2
|
|
|
|
# def post(self, request, *args, **kwargs):
|
|
|
|
# context = self.get_context_data()
|
|
|
|
# if context["form"].is_valid():
|
|
|
|
# print("form good")
|
|
|
|
# else:
|
|
|
|
# print("form not good")
|
|
|
|
# print(context["form"].errors)
|
|
|
|
# return HttpResponse("All good")
|
|
|
|
|
|
|
|
|
|
|
|
# v1
|
|
|
|
# def get_context_data(self, **kwargs):
|
|
|
|
# context = super().get_context_data(**kwargs)
|
|
|
|
# context['product'] = get_object_or_404(Product, slug=self.kwargs['product'])
|
|
|
|
# context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
|
|
|
|
|
|
|
|
# res_price = []
|
|
|
|
# for res in context['product'].resources.all():
|
|
|
|
# price = res.price_per_time.filter(timeframe=context['timeframe'])[0].price
|
|
|
|
# currency = res.price_per_time.filter(timeframe=context['timeframe'])[0].currency
|
|
|
|
|
|
|
|
# res_price.append((res, price, currency))
|
|
|
|
# context['res_price'] = res_price
|
|
|
|
|
|
|
|
# print(context)
|
|
|
|
# print(self.kwargs)
|
|
|
|
# return context
|
|
|
|
|
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()
|
|
|
|
print(context)
|
2022-01-14 23:26:35 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class ProductListView(ListView):
|
|
|
|
model = Product
|
|
|
|
|
|
|
|
|
|
|
|
class ProductSelectView(CreateView):
|
|
|
|
model = ProductOrder
|
|
|
|
fields = ['product' ]
|
|
|
|
|
|
|
|
|
|
|
|
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
|