def category_update(request, pk): category = get_object_or_404(Category, pk=pk) if request.user.is_superuser or request.user == category.restaurant.manager: pass else: messages.warning(request, "Você não tem permissão.") return redirect('dashboard') if request.method == 'POST': form = CategoryForm(request.POST, instance=category) try: if form.is_valid(): form.save() messages.success(request, "Categoria atualizada") return redirect('category_update', pk=pk) except Exception as e: messages.warning(request, 'Ocorreu um erro ao atualizar: {}'.format(e)) else: form = CategoryForm(instance=category) return render(request, 'products/category_update.html', {'form': form})
def add_a_category(request): if request.method == "POST": category_form = CategoryForm(request.POST, request.FILES) if category_form.is_valid(): category_form.save() return redirect(dashboard_product) else: category_form = CategoryForm() return render(request, "dashboardaddcategory.html", {'category_form': category_form})
def category_edit(request, pk=None): instance = get_object_or_404(Category, id=pk) form = CategoryForm(request.POST or None, instance=instance) if form.is_valid(): form.save() return HttpResponse( '<script>opener.closePopup(window, "%s", "%s", "#id_author");</script>' % (instance.pk, instance)) return render(request, 'dashboard/ajax_calls/popup_form.html', {"form": form})
def createCategoryPopup(request): form = CategoryForm(request.POST or None) if form.is_valid(): instance = form.save() return HttpResponse( '<script>opener.closePopup(window, "%s", "%s", "#id_category");</script>' % (instance.pk, instance)) return render(request, 'dashboard/ajax_calls/popup_form.html', {"form": form})
def category_create(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): # Category.objects.create( # name=form.cleaned_data.get('name'), # description=form.cleaned_data.get('description') # ) form.save() return redirect( reverse('products:index') ) return render( request, 'categories/create.html', { 'form': form, 'page_title': 'Create category' } )
def category_update(request, pk): obj = get_object_or_404(Category, pk=pk) form = CategoryForm(instance=obj) if request.method == 'POST': form = CategoryForm( request.POST, instance=obj ) if form.is_valid(): form.save() return redirect( reverse('products:index') ) return render( request, 'categories/update.html', { 'form': form, 'page_title': 'Update category' } )
def mutate_and_get_payload(cls, input, context, info): # check user check_superuser(context) category_id = input.get('id', None) category = get_node(category_id, context, info, Category) if not category: raise ResponseError("Invalid Category", code='invalid_category') parent_id = input.get('parent_id') parent = get_node(parent_id, context, info, Category) # update category form = CategoryForm(input, instance=category) if form.is_valid(): form.save() category.parent = parent category.save() else: raise FormError(form.errors) return UpdateCategoryMutation(category=category)
def add_category(request): form = CategoryForm(request.POST or None) c = Category.objects.all() if form.is_valid(): category = form.save(commit=False) category.save() messages.success(request, "Category added successfully") return render(request, 'products/categories.html', {'categories': c}) context = {'form': form} return render(request, 'products/add_category.html', context)
def mutate_and_get_payload(cls, input, context, info): # check user check_superuser(context) parent_id = input.get('parent_id') parent = get_node(parent_id, context, info, Category) # create category form = CategoryForm(input) if form.is_valid(): new_category = form.save(commit=False) new_category.parent = parent new_category.save() else: raise FormError(form.errors) return CreateCategoryMutation(category=new_category)
def category_new(request): try: restaurant = Restaurant.objects.get(manager=request.user) except Restaurant.DoesNotExist: messages.warning(request, "Você precisa cadastrar um restaurante") return redirect('new_restaurant') if request.method == "POST": form = CategoryForm(request.POST) if form.is_valid(): category = form.save(commit=False) category.restaurant = restaurant category.save() messages.success(request, "Nova categoria cadastrada.") return redirect(categories_list) else: form = CategoryForm() return render(request, 'products/category_new.html', {'form': form})