def add_product(request): if request.method == "POST": form = ProductForm(request.POST) if form.is_valid(): cleaned = (form.cleaned_data) name = cleaned['name'] price = cleaned['price'] incoming_prod = Product.objects.filter(name="name") if incoming_prod.exists(): print('product exists') if request.FILES['image']: image = request.FILES['image'] new_product = Product(name=name, price=price, image=image) new_product.save() print(new_product.image.path) shrink(new_product.image.path) else: try: if request.FILES['image']: image = request.FILES['image'] shrink(image) new_product = Product(name=name, price=price, image=image) new_product.save() except: new_product = Product(name=name, price=price) new_product.save() context = {"webaddress": webaddress[0]} return tables(request)
def handle(self, *args, **options): Product.objects.all().delete() now = timezone.now() Product( pk=1, name='Mineral Water Strawberry', description= 'Natural-flavored strawberry with an anti-oxidant kick.', price=1.00, photo='products/mineralwater-strawberry.jpg', ).save() Product( pk=2, name='Mineral Water Raspberry', description='Flavoured with raspberry, loaded with anti-oxidants.', price=2.00, photo='products/mineralwater-raspberry.jpg', sale_start=now + timedelta(days=20), sale_end=None, ).save() Product( pk=3, name='Vitamin A 10,000 IU (125 caplets)', price=3.00, description= 'Vitamin A is essential for normal and night vision, and helps maintain healthy skin and mucous membranes.', sale_start=now - timedelta(days=10), sale_end=None, photo='products/vitamin-a.jpg', ).save() Product( pk=4, name='Vitamin B-Complex (100 caplets)', price=3.00, description= 'Contains a combination of essential B vitamins that help convert food to energy.', sale_start=now, sale_end=now + timedelta(days=10), photo='products/vitamin-bcomplex.jpg', ).save() Order.objects.all().delete() for i in range(1, 11): Order(pk=i, product_id=i % 4 + 1, quantity=random.randint(1, 20)).save()
def test_product(self): product = Product() product.name = "A solution book" product.price = 200 product.digital = True product.save() record = Product.objects.get(pk=1) self.assertEqual(record, product)
def addproduct(request): product_name = request.POST.get("name") product_price = request.POST.get("price") new_product = Product(label=product_name, price=float(product_price)) new_product.save() context = {} context["products"] = Product.objects.all() return render(request, "store/products.html", context)
def mail(request): if request.method == "POST": name = (request.POST["name"]) price = request.POST["price"] incoming_prod = Product.objects.filter(name="name") if incoming_prod.exists(): print('email exists') else: new_product = Product(name=name, price=price) new_product.save() context = {"webaddress": webaddress[0]} return HttpResponse({"webaddress": webaddress[0]})
def setUp(self): # Create a product with its image file self.product_data = { 'name': 'test product name', 'description': 'test product description', 'base_price': 49.99, } with open(f'{settings.BASE_DIR}/store/tests/_test_file_1.png', 'rb') as image_file: image = ImageFile(image_file) self.product = Product(**self.product_data) self.product.image.save(new_image_path(self.product, image.name), image) self.product.save()
def test_orderItem(self): order = OrderItem() order.quantity = 2 #Quantity of item order.product = Product() order.product.name = "A solution book" order.product.price = 200 #Price of the product order.product.digital = True order.product.save() order.save() record = OrderItem.objects.get(pk=1) self.assertEqual(record.get_total, 600) #Check if expected total equal to returned total
def edit_product(request): if request.method == 'POST' and request.FILES['image']: image = request.FILES['image'] title = request.POST['title'] price = request.POST['price'] description = request.POST['description'] obj = Product() obj.title = title obj.description = description obj.price = price obj.image = image obj.save() return redirect('/custom-admin') return render(request, template_name='store/add_product.html')
def createproduct(request): data = {} if request.method != "POST": form = ProductForm() data['form'] = form return render(request, 'store/createproduct.html', data) form = ProductForm(request.POST, request.FILES) data['form'] = form if form.is_valid(): try: pr = Product(**form.cleaned_data) pr.save() return redirect('createproduct') except Exception: warning(request, 'Product was not created due to some issue') return render(request, 'store/createproduct.html', data)
def add_product(request): if request.method == 'POST': form = ProductForm(request.POST, request.FILES) product = Product() if form.is_valid(): # product.sub_category = 'ENGINE OIL DIESEL' product.name = form.cleaned_data['name'] product.price = form.cleaned_data['price'] product.image = form.cleaned_data['image'] product.author = request.user product.save() print(request) return redirect('products_list') else: messages.error(request, 'Post error') return redirect('add_product') form = ProductForm() context = {'form': form} return render(request, 'staff/product-add.html', context)
def setUp(self): """ Create a product with its required fields and media files before each tests """ self.django_cat = Category.objects.create(name='django') self.product_data = { 'name': 'test product name', 'description': 'test product description', 'base_price': 49.99, } with open(f'{settings.BASE_DIR}/store/tests/_test_file_1.png', 'rb') as image_file: image = ImageFile(image_file) self.product = Product(**self.product_data) self.product.image.save( new_image_path(self.product, image.name), image) self.product.category = self.django_cat self.product.save()
def handle(self, *args, **options): if Category.objects.exists() or Category.objects.exists(): print('Product already loaded...existing.') print(ALREADY_LOADED_ERROR_MESSAGE) return print('Creating category data') for name in CATEGORY_IMAGE_NAMES.keys(): category = Category(categoryName=name) category.categoryImgPath = CATEGORY_IMAGE_NAMES[name] category.save() print('Loading product data from csv file') for line in DictReader(open('./product_data.csv')): product = Product() product.productName = line['productName'] product.productPrice = line['productPrice'] product.productDescription = line['productDescription'] product.productImgPath = line['productImgPath'] product.category = Category.objects.get( categoryName=line['productCategory']) product.save()
def qchange(request, pk): id = request.session['logid'] q = int(request.POST.get("quantity", "")) pl = Plist.objects.all().filter(tfield=pk) for i in pl: a = 0 p = Plist() i.quantity = q j = Product() p = Product.objects.all() for j in p: if (j == i.product_id): i.price = q * j.price j.quantity -= q i.save() j.save() if (id == 64): return redirect('/bill') else: return redirect('/cart') return redirect('/cart')
import csv from store.models import Category, SubCategory, Product data = csv.reader(open('/Users/Mahmoud Dawlatly/Documents/FYP/Product.csv', encoding='utf8'), delimiter=',') a = 0 for row in data: if row[0] != 'Product Category': product = Product() if row[0] == 'Technology': product.product_Category_id = 3 elif row[0] == 'Furniture': product.product_Category_id = 4 elif row[0] == 'Office Supplies': product.product_Category_id = 2 else: product.product_Category_id = 5 if row[1] == 'Telephones and Communication': product.product_SubCategory_id = 1 elif row[1] == 'Office Machines': product.product_SubCategory_id = 2 elif row[1] == 'Computer Peripherals': product.product_SubCategory_id = 3 elif row[1] == 'Copiers and Fax': product.product_SubCategory_id = 4 elif row[1] == 'Bookcases': product.product_SubCategory_id = 5 elif row[1] == 'Chairs & Chairmats': product.product_SubCategory_id = 6
for i in range(0, len(movies)): if (i == 0): continue lst = movies[i].split("\t") id = lst[0] title = lst[1] genre = lst[2] runtime = lst[3] adult = lst[4] bob = False if (adult == "Yes"): bob = True url = lst[5] disc = lst[6] p = Product(ID=id, originalTitle=title, isAdult=bob, runtime=runtime, genres=genre, poster=url, description=disc, isBooked=False, price=random.randint(80, 101)) p.save()