def post(request, category_id): form = CategoryForm(request.POST) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: ProductCategory.edit_category( category_id, { 'name': form.cleaned_data.get('name'), }) return HttpResponseRedirect('/admin/categories') except: pass category = ProductCategory.get_by_id(category_id) return TemplateResponse( request, 'category.html', { 'category': category, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), })
def post(request): form = CSVUploadForm(request.POST, request.FILES) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): form_data = TextIOWrapper(form.cleaned_data.get('csv'), encoding='utf-8') try: csv_file = csv.reader(form_data) next(csv_file) add_product = [] for c in csv_file: already_product = Product.get_by_jan_code(c[4]) if already_product is None: add_product.append( Product( product_name=c[1], brand=c[0], model_number=c[2], category=ProductCategory.get_by_name(c[3]), jan_code=c[4], )) Product.objects.bulk_create(add_product) except: products = Product.get_all() return TemplateResponse( request, 'products.html', { 'products': products, 'error_messages': get_error_message(request), }) return HttpResponseRedirect('/admin/products')
def post(request, product_id): form = ProductForm(request.POST) if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: Product.edit_product( product_id, { 'brand': form.cleaned_data.get('brand'), 'product_name': form.cleaned_data.get('product_name'), 'model_number': form.cleaned_data.get('model_number'), 'category': ProductCategory.get_by_id( form.cleaned_data.get('category')), 'jan_code': form.cleaned_data.get('jan_code'), }) return HttpResponseRedirect('/admin/products') except: pass categories = ProductCategory.get_all() return TemplateResponse( request, 'products_create.html', { 'categories': categories, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), })