class ProductPage(TemplateView):
category_form_class = CategoryForm
product_form_class = ProductForm
template_name = 'personal_area/pages/products.html'
def post(self, request):
post_data = request.POST or None
profile_id = request.user.id
category_form = self.category_form_class(post_data, profile_id=profile_id, prefix='category')
product_form = self.product_form_class(post_data, prefix='product')
product_form.fields['category'].choices = user_category(request.user.id)
context = self.get_context_data(
category_form=category_form,
product_form=product_form,
product_list=Product.objects.filter(profile_id=request.user.id),
category_list=Category.objects.filter(profile_id=request.user.id)
)
if category_form.is_valid():
self.form_save(form=category_form, profile_id=profile_id)
if product_form.is_valid():
self.form_save(form=product_form, profile_id=profile_id)
return self.render_to_response(context)
@staticmethod
def form_save(form, profile_id):
form_commit = form.save(commit=False)
form_commit.profile_id = profile_id
form_commit.save()
return form_commit
def get(self, request, *args, **kwargs):
return self.post(request)