def add(): context = {} has_category = False if request.method == "POST": # convert name to lower case and remove leading # and trailing spaces name = request.form["name"].lower().strip() # case 1: do not allow adding empty category name if is_empty_str(name): flash(notify_warning("Category name cannot be empty")) return redirect(url_for("category.add")) # case 2: do not allow category name uncategorised # not sure if this is needed since if we add this # during initialization then this check will be covered # by case 3 if name == "uncategorised" or name == "uncategorized": flash(notify_warning("Category cannot be named as uncategorised")) return redirect(url_for("category.add")) has_category = Category.category_exists(name) # case 3: do not allow adding existing category name if has_category: flash(notify_warning(f'Category "{name}" already exists')) return render_template("category/add.html", **context) # case 4: sucessfully add the category category = Category(name=name) try: if "photo" in request.files: file = request.files["photo"] filename = unique_sec_filename(file.filename) file.filename = filename categoryphotos.save(file) category.resources.append( Resource( type="image", filename=filename, category="category_image", ) ) except flask_uploads.UploadNotAllowed as e: pass category.save() flash(notify_success(f'Category "{name}" added successfully')) return render_template("category/add.html", **context) context["has_category"] = str(has_category) return render_template("category/add.html", **context)
def add_women_category(): with app.app_context(): category = Category(name="Women") subcategory1 = SubCategory(name="Sneakers") subcategory2 = SubCategory(name="Air Jordan") subcategory3 = SubCategory(name="Slides") subcategory4 = SubCategory(name="Airmax") subcategory2.save(commit=False) subcategory3.save(commit=False) subcategory4.save(commit=False) subcategory1.save(commit=False) category.subcategories.extend( [subcategory1, subcategory2, subcategory3, subcategory4]) category.save()
def test_category_add_existing_subcat_with_spaces_post(self, test_client): category = Category(name="category1") subcategory = SubCategory(name="subcategory1") category.subcategories.append(subcategory) category.save() response = test_client.post( url_for("category.add_sub", category_name="category1"), data=dict(name=" subcategory1 "), follow_redirects=True, ) subcategories = SubCategory.query.count() assert response.status_code == 200 assert b"Name already exists for category" in response.data assert subcategories == 1
def test_category_delete_cat_with_subcategory_get(self, test_client): # add a category with subcategoires category = Category(name="category") subcategory = SubCategory(name="subcategory") category.subcategories.append(subcategory) category.save() response = test_client.get( url_for("category.delete", name="category"), follow_redirects=True, ) assert response.status_code == 200 assert request.path == url_for("category.dashboard") assert ( b'Please delete all subcategories for category "category"' in response.data )
def add_uncategorised_category(): with app.app_context(): category = Category(name="uncategorised") subcategory = SubCategory(name="uncategorised") p1 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Apple", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p1.colors = colors p1.sizes = sizes p2 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Pear", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p2.colors = colors p2.sizes = sizes p3 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Peach", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p3.colors = colors p3.sizes = sizes subcategory.products.extend([p1, p2, p3]) category.subcategories.append(subcategory) category.save()
def add_men_category(): with app.app_context(): category = Category(name="Men") subcategory1 = SubCategory(name="Sneakers") subcategory2 = SubCategory(name="Air Jordan") subcategory3 = SubCategory(name="Slides") subcategory4 = SubCategory(name="Airmax") p1 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Demo Shoe 1", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p1.sizes = sizes p1.colors = colors p2 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Demo Shoe 2", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p2.sizes = sizes p2.colors = colors p3 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Demo Shoe 3", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p3.sizes = sizes p3.colors = colors p4 = Product( barcode=str(uuid.uuid1()), price=10.0, name="Demo Shoe 4", in_stock=50, selling_price=15.0, discontinued=False, description="", ) p4.sizes = sizes p4.colors = colors subcategory2.save(commit=False) subcategory3.save(commit=False) subcategory4.save(commit=False) subcategory1.products.extend([p1, p2, p3, p4]) category.subcategories.extend( [subcategory1, subcategory2, subcategory3, subcategory4]) category.save()