def item_form(request, id=None): if id: item = get_object_or_404(Item, id=id) scenario = 'Update' else: item = Item() scenario = 'Create' if request.POST: form = ItemForm(data=request.POST, instance=item, user=request.user) if form.is_valid(): item = form.save(commit=False) item.save(account_no=form.cleaned_data['account_no'], opening_balance=form.cleaned_data['opening_balance']) if request.is_ajax(): return render(request, 'callback.html', {'obj': ItemSerializer(item).data}) return redirect('/inventory/items/') else: form = ItemForm(instance=item, user=request.user) if request.is_ajax(): base_template = 'modal.html' else: base_template = 'inventory_base.html' return render(request, 'item_form.html', { 'scenario': scenario, 'form': form, 'base_template': base_template, })
class InvoiceItemSerializer(serializers.ModelSerializer): item = ItemSerializer(many=False) class Meta: model = InvoiceItem fields = ("id", "quantity", "item")
class ListRequestSerializer(serializers.ModelSerializer): # returns item and employee dicts in response item = ItemSerializer(read_only=True) employee = EmployeeSerializer(read_only=True) class Meta: model = Request fields = ("id", "employee", "item", "quantity", "date", "active")
def ingridient_items_listing(request, uuid=None): ingredient = Ingredient.objects.get(uuid=uuid) items = Item.objects.filter(ingredient_uuid=uuid) print(ingredient) response = { 'Ingredients': IngredientSerializer(ingredient).to_dict(), 'number_of_ingredients': len(items), 'Items': [ItemSerializer(item).to_dict() for item in items], } return JsonResponse(response, safe=False)
def company_items_listing(request, uuid=None): company = Company.objects.get(uuid=uuid) items = Item.objects.filter(company_uuid_id=uuid) print(company) response = { 'company': CompanySerializer(company).to_dict(), 'number_of_items': len(items), 'items': [ItemSerializer(item).to_dict() for item in items] } return JsonResponse(response, safe=False)
def get(self, request): try: product_code = request.GET.get('product_code') inventory = Item.objects.get(product_code=product_code) serializer = ItemSerializer(inventory) response_dict = { 'item': serializer.get_json_data(), 'status_code': 200 } except Item.DoesNotExist: response_dict = { 'message': 'No item found for product_code {}'.format(product_code), 'status_code': 404 } except: print(traceback.format_exc()) response_dict = { 'message': 'Internal server error', 'status_code': 500 } return Response(response_dict, status=response_dict['status_code'])
def item_list(request): if request.method == 'GET': items = Item.objects.all() serializer = ItemSerializer(items, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ItemSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: inventory = json.loads(request.body) serializer = ItemSerializer(data=inventory) if serializer.is_valid(): serializer.save() response_dict = { 'message': serializer.data, 'status_code': 200 } else: response_dict = { 'message': serializer.errors, 'status_code': 401 } except: print(traceback.format_exc()) response_dict = { 'message': 'Internal server error', 'status_code': 500 } return Response(response_dict, status=response_dict['status_code'])
def item_details(request, pk): try: item = Item.objects.get(pk=pk) except Item.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ItemSerializer(item) return Response(serializer.data) elif request.method == 'PUT': serializer = ItemSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': item.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def items_listing(request): items = Item.objects.all()[:40] response = [ItemSerializer(item).to_dict() for item in items] return JsonResponse(response, safe=False)
def items_as_json(request): items = Item.objects.all() items_data = ItemSerializer(items).data return JsonResponse(items_data, safe=False)
class QuoteItemSerializer(serializers.ModelSerializer): item = ItemSerializer(many=False) class Meta: model = QuoteItem fields = "__all__"
def items(self, request, pk): items = Item.objects.filter(parent__exact=pk) serializer = ItemSerializer(items, many=True, context={'request': request}) return Response(serializer.data)
def all_items(request): serializer = ItemSerializer(Item.objects.all(), many=True) return JsonResponse({'results': serializer.data}, safe=False)