def product_upload(): form = AddProduct() if form.validate_on_submit(): sub_category = session.query(SubCategory).filter_by( name=form.sub_category.data).first() shop = session.query(Shop).filter_by(id=current_user.id).first() if sub_category: #save product product = Product(form.name.data, sub_category, shop) if product: #upload photo to the server front_picture = save_picture(form.front_pic.data, "static/vendor_product_pictures", 800, 800) back_picture = save_picture(form.back_pic.data, "static/vendor_product_pictures", 800, 800) #save product details ProductDetails(product_type=form.product_type.data, brand=form.brand.data, color=form.color.data, size=form.size.data, front_picture=front_picture, back_picture=back_picture, key_features=form.key_features.data, quantity=form.quantity.data, prize=form.prize.data, weight=form.weight.data, description=form.description.data, warranty=form.warranty.data, guarantee=form.guarantee.data, product=product) flash("Your product has been saved successfully!!", "success") return redirect(url_for("vendors.product_upload")) return render_template('vendor/product-upload.html', form=form)
class AddProduct(FlaskForm): name = StringField('Name', validators=[InputRequired(), Length(min=3, max=20)]) category = SelectField('Category Name', validators=[InputRequired()], choices=[ (category.name, category.name) for category in session.query(Category).all() ]) sub_category = SelectField( 'Sub_Category Name', validators=[InputRequired()], choices=[(sub_cat.name, sub_cat.name) for sub_cat in session.query(SubCategory).all()]) brand = SelectField('Brand', validators=[InputRequired()], choices=[(brand.name, brand.name) for brand in session.query(Brand).all()]) product_type = StringField('Product Type', validators=[Optional(), Length(min=3, max=20)]) size = StringField('Size', validators=[Optional(), Length(min=3, max=20)]) color = StringField('Color', validators=[InputRequired()]) quantity = StringField('quantity', validators=[InputRequired(), Length(min=1, max=20)]) weight = StringField('weight', validators=[Optional(), Length(min=3, max=20)]) front_pic = FileField('Front Image', validators=[FileAllowed(['jpg', 'png'])]) back_pic = FileField('Back Image', validators=[FileAllowed(['jpg', 'png'])]) description = TextAreaField('Total Product Description', validators=[InputRequired()]) prize = StringField('Price', validators=[Optional(), Length(min=3, max=20)]) key_features = TextAreaField('Products Key Features', validators=[Optional()]) warranty = StringField('Warranty', validators=[Optional(), Length(min=3, max=20)]) guarantee = StringField('Guarantee', validators=[Optional(), Length(min=3, max=20)]) submit = SubmitField('Add Product')
def login(): if current_user.is_authenticated: return redirect(url_for("customers.home")) form = LoginForm() if form.validate_on_submit(): customer = session.query(Customer).filter_by( email=form.email.data).first() if customer: if customer.verify_password(form.password.data): period['account_type'] = 'customer' login_user(customer) flash("You have been Logged in!!", "success") return redirect(url_for("customers.home")) else: flash( 'Login not successful. Please check your email and password', 'danger') return redirect(url_for("customers.login")) else: flash( 'User does not exist. You must create an account to access the site', 'info') return redirect(url_for("customers.login")) return render_template('customer/login.html', form=form)
def filter(): if request.method == "GET": category_id = request.args['category'] if category_id == "All": return redirect(url_for('brand.get_brands')) else: categories = Category.read(Category) brands = session.query(Brand).filter_by(category_id=category_id) return render_template("manager/brands.html", mod=module, brands=brands, categories=categories)
def before_request(): categories = session.query(Category)\ .options( lazyload("sub_category")\ .lazyload("product") ).all() c = [x.serialize() for x in categories] period["categories_menu"] = c cart_items = [] try: cart_items = AddToCart.read_customer_cart_items( AddToCart, current_user.id) except: pass period["cart_len"] = len(cart_items)
def home(): form = LoginForm() # cart_items = AddToCart.read_customer_cart_items(AddToCart, current_user.id) if request.method == "GET": images = session.query(HomeSlideShowImages)\ .order_by(HomeSlideShowImages.order).all() ordered_categories = Category.getHomeDisplayCategories(Category) page_number = request.args.get("page", 1, type=int) pages = Paginate(ProductDetails.read_products_details(ProductDetails), page_number, 10) prev_url = url_for( "customers.home", page=pages.previous_page) if pages.has_previous else None next_url = url_for("customers.home", page=pages.next_page) if pages.has_next else None return render_template('customer/index.html', form=form, pages=pages, next_url=next_url, prev_url=prev_url, images=images, enumerate=enumerate, ordered_categories=ordered_categories)
def login(): if current_user.is_authenticated: return redirect(url_for("vendors.product_upload")) form = LoginForm() if form.validate_on_submit(): vendor = session.query(Shop).filter_by(email=form.email.data).first() if vendor: if vendor.verify_password(form.password.data): period['account_type'] = 'vendor' login_user(vendor) flash("You have been Logged in!", "success") return redirect(url_for("vendors.sales")) else: flash( 'Login not successful. Please check your email and password', "danger") return redirect(url_for("vendors.login")) else: flash( 'User does not exist. You must create an account to access the site', "info") return redirect(url_for("vendors.login")) return render_template('vendor/login.html', form=form)
def load_user(user_id): return session.query(User).filter_by(id=user_id).first()