Example #1
0
    def post(self, request, *args, **kwargs):
        cat_name_form = ModuleCategoryCreateNameForm(request.POST)
        if cat_name_form.is_valid():
            self.category.name = cat_name_form.cleaned_data['name']
            if cat_name_form.cleaned_data['order'] != self.category.order:
                shift_category_orders(self.category,
                                      cat_name_form.cleaned_data['order'])
            self.category.save()

        cat_form = self.form_class(request.POST)
        CategoryProduct.objects.filter(category=self.category).delete()
        for product_form in cat_form.cleaned_data:
            try:
                product = Product.objects.get(
                    pk=product_form['product'].split('/')[0])
                if product.unit:
                    quantity = int(product_form['quantity'])
                else:
                    quantity = 1
                CategoryProduct.objects.create(category=self.category,
                                               product=product,
                                               quantity=quantity)
            except ObjectDoesNotExist:
                pass
            except KeyError:
                pass
        return redirect(self.get_success_url())
Example #2
0
    def post(self, request, *args, **kwargs):
        cat_name_form = ModuleCategoryCreateNameForm(request.POST)
        if cat_name_form.is_valid():
            self.category.name = cat_name_form.cleaned_data['name']
            self.category.save()

        cat_form = self.form_class(request.POST)
        CategoryProduct.objects.filter(category=self.category).delete()
        for product_form in cat_form.cleaned_data:
            try:
                product = Product.objects.get(pk=product_form['product'].split('/')[0])
                if product.unit:
                    quantity = int(product_form['quantity'])
                else:
                    quantity = 1
                CategoryProduct.objects.create(
                    category=self.category,
                    product=product,
                    quantity=quantity
                )
            except ObjectDoesNotExist:
                pass
            except KeyError:
                pass
        return redirect(self.get_success_url())
Example #3
0
 def get(self, request, *args, **kwargs):
     context = self.get_context_data(**kwargs)
     cat_form_data = [{
         'product':
         str(category_product.product.pk) + '/' +
         str(category_product.product.get_unit_display()),
         'quantity':
         category_product.quantity
     } for category_product in self.category.categoryproduct_set.all()]
     context['cat_form'] = self.form_class(initial=cat_form_data)
     context['cat_name_form'] = ModuleCategoryCreateNameForm(
         initial={'name': self.category.name})
     return render(request, self.template_name, context=context)
Example #4
0
    def post(self, request, *args, **kwargs):
        cat_name_form = ModuleCategoryCreateNameForm(request.POST)
        if cat_name_form.is_valid():
            category = Category.objects.create(
                name=cat_name_form.cleaned_data['name'], module=self.module)

        cat_form = self.form_class(request.POST)
        for product_form in cat_form.cleaned_data:
            try:
                product = Product.objects.get(
                    pk=product_form['product'].split('/')[0])
                if product.unit:
                    quantity = int(product_form['quantity'])
                else:
                    quantity = 1
                CategoryProduct.objects.create(category=category,
                                               product=product,
                                               quantity=quantity)
            except ObjectDoesNotExist:
                pass
            except KeyError:
                pass
        return redirect(self.get_success_url())
Example #5
0
 def get(self, request, *args, **kwargs):
     context = self.get_context_data(**kwargs)
     context['cat_form'] = self.form_class()
     context['cat_name_form'] = ModuleCategoryCreateNameForm(
         initial={'order': self.module.categories.all().count()})
     return render(request, self.template_name, context=context)
Example #6
0
 def get(self, request, *args, **kwargs):
     context = self.get_context_data(**kwargs)
     context['cat_form'] = self.form_class()
     context['cat_name_form'] = ModuleCategoryCreateNameForm()
     return render(request, self.template_name, context=context)