from django.shortcuts import render, get_object_or_404 from django.urls import reverse from django.views.generic.edit import CreateView, FormView from django.views.generic.base import TemplateView from django.views.generic.list import ListView from django.views.generic.detail import DetailView from django.http import HttpResponse 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 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']) # context['form'] = ProductOrderForm( # context['product'].resources.all(), # initial={'product': context['product'].slug, # 'timeframe': context['timeframe'].slug} # ) return context # 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 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 }) context['timeframes'] = context['product'].valid_timeframes() print(context) 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