def delete_product(request, pid): with open("product.txt", "r+") as f: d = f.readlines() f.seek(0) for i in d: i = i.split(" ") if i[1] != pid: i = " ".join(i) f.write(i) f.truncate() with open("index.txt", "r+") as f: d = f.readlines() f.seek(0) for i in d: i = i.split(" ") p_id = pid + "\n" if i[1] != p_id: i = " ".join(i) f.write(i) f.truncate() with open("secindex.txt", "r+") as f: d = f.readlines() f.seek(0) for i in d: i = i.split(" ") if i[0] != pid: i = " ".join(i) f.write(i) f.truncate() sort_index() sort_secindex() messages.success(request, ('Item has been deleted')) return redirect('productapp:view_all')
def add_product(request): if request.method == "POST": product_form = AddProduct(data=request.POST) index = None if product_form.is_valid(): pname = str(product_form.cleaned_data['pname']) pname = pname.split(" ") pname = "-".join(pname) pid = str(product_form.cleaned_data['pid']) with open("product.txt", "r") as file: for line in file: arr = line.split(" ") if pid.upper() == arr[1]: messages.success(request, ('Item Already Exists')) return redirect("productapp:add") index = int(arr[0]) with open("product.txt", "a") as file: if index is None: index = 0 else: index = index + 1 file.write(str(index)) file.write(" ") file.write((str(product_form.cleaned_data['pid'])).upper()) file.write(" ") file.write(pname.upper()) file.write(" ") file.write(str(product_form.cleaned_data['quantity'])) file.write(" ") file.write(str(product_form.cleaned_data['price'])) file.write("\n") with open("index.txt", "a") as file: file.write(str(index)) file.write(" ") file.write((str(product_form.cleaned_data['pid'])).upper()) file.write("\n") with open("secindex.txt", "a") as file: file.write((str(product_form.cleaned_data['pid'])).upper()) file.write(" ") file.write(pname.upper()) file.write("\n") #sorting index on basis of primary key sort_index() sort_secindex() messages.success(request, ('Item has been Added')) return redirect("productapp:view_all") else: product_form = AddProduct() return render(request, "productapp/add_product.html", {'product_form': product_form})
def update_product(request, pid): if request.method == "POST": pid1 = request.POST.get('pid') pname = request.POST.get('pname') pname = pname.split(" ") pname = "-".join(pname) quantity = request.POST.get('quantity') price = request.POST.get('price') with open("product.txt", "r") as file: for line in file: arr1 = line.split(" ") if pid.upper() == arr1[1]: arr1 = line.split(" ") break with open("product.txt", "r") as file: data = file.readlines() new_data = [] index_data = [] secindex_data = [] for line in data: arr = line.split(" ") if arr[1] == pid1.upper() and arr[1] != pid.upper(): messages.success(request, ('Product Id already exists')) return render(request, "productapp/update.html", {'arr': arr1}) if arr[1] == pid: line = arr[0] + " " + pid1.upper() + " " + pname.upper( ) + " " + quantity + " " + price + "\n" index = arr[0] + " " + pid1.upper() + "\n" secindex = pid1.upper() + " " + pname.upper() + "\n" new_data.append(line) index_data.append(index) secindex_data.append(secindex) else: new_data.append(line) index = arr[0] + " " + arr[1].upper() + "\n" secindex = arr[1].upper() + " " + arr[2].upper() + "\n" index_data.append(index) secindex_data.append(secindex) with open("product.txt", "w") as file: file.writelines(new_data) with open("index.txt", "w") as file: file.writelines(index_data) with open("secindex.txt", "w") as file: file.writelines(secindex_data) sort_index() sort_secindex() messages.success(request, ('Item has been Updated')) return redirect("productapp:view_all") with open("product.txt", "r") as file: for line in file: arr1 = line.split(" ") if pid == arr1[1]: return render(request, "productapp/update.html", {'arr': arr1}) else: messages.success(request, ('No Item Exists')) return redirect("productapp:search")