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 .models import * from .forms import * from .services import * class ProductOneTimeOrderView(FormView): form_class = ProductOneTimeOrderForm template_name = 'app/productorder_form.html' def get_success_url(self): return "/" def get_form_kwargs(self): kwargs = super().get_form_kwargs() # Set the product so the form can retrieve the resources product = get_object_or_404(Product, slug=self.kwargs['product']) kwargs['resources'] = product.resources.all() return kwargs def get_initial(self): """ Initial values for the form """ initial = super().get_initial() initial['product'] = self.kwargs['product'] if 'timeframe' in self.kwargs: initial['timeframe'] = self.kwargs['timeframe'] return initial def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['product'] = get_object_or_404(Product, slug=self.kwargs['product']) if 'timeframe' in context: context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe']) return context def form_valid(self, form): 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------") for f in form.fields: print(f) print(form.cleaned_data) 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) return super().form_valid(form) class ProductOrderView(ProductOneTimeOrderView): form_class = ProductOrderForm 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() context['has_one_time_price'] = context['product'].has_one_time_price() print(context) return context class ProductListView(ListView): model = Product class ProductSelectView(CreateView): model = ProductOrder fields = ['product' ] class IndexView(TemplateView): template_name = "app/index.html" 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