diff --git a/.flaskenv b/.flaskenv new file mode 100644 index 0000000..5917a39 --- /dev/null +++ b/.flaskenv @@ -0,0 +1,2 @@ +export FLASK_APP=saleapp +export FLASK_ENV=development \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..148ff97 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +venv/ + +*.pyc +__pycache__/ + +instance/ + +.pytest_cache/ +.coverage +htmlcov/ + +dist/ +build/ +*.egg-info/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f2d90cb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.linting.enabled": true +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e125237 --- /dev/null +++ b/readme.md @@ -0,0 +1,14 @@ +# Install Dependency +1. pip install Flask +2. pip install python-dotenv + +# Chạy File bằng cách gõ câu lệnh bên dưới vào command line +Flask run + +[Link Localhost](http://127.0.0.1:5000/) + +# File .flaskenv đã được config environment variables đường dẫn đến thư mục gốc là saleapp ( nếu muốn sửa tên thư mục gốc là src thì sửa lại FLASK_APP=src ) + +FLASK_APP=saleapp +FLASK_ENV=development + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/saleapp/__init__.py b/saleapp/__init__.py new file mode 100644 index 0000000..ff865f9 --- /dev/null +++ b/saleapp/__init__.py @@ -0,0 +1,4 @@ +from flask import Flask +app = Flask(__name__) + +import saleapp.routes \ No newline at end of file diff --git a/saleapp/data/categories.json b/saleapp/data/categories.json new file mode 100644 index 0000000..5050f4b --- /dev/null +++ b/saleapp/data/categories.json @@ -0,0 +1,10 @@ +[ + { + "id": 1, + "name": "mobile" + }, + { + "id": 2, + "name": "tablet" + } +] \ No newline at end of file diff --git a/saleapp/data/products.json b/saleapp/data/products.json new file mode 100644 index 0000000..37d8f5b --- /dev/null +++ b/saleapp/data/products.json @@ -0,0 +1,26 @@ +[ + { + "id": 1, + "name": "iPhone 7 Plus", + "description": "Apple, 32GB, RAM: 3GB, iOS13", + "price": 17000000, + "image": "https://cdn.tgdd.vn/Products/Images/42/78124/iphone-7-plus-gold-400x460-400x460.png", + "category_id": 1 + }, + { + "id": 2, + "name": "iPad Pro 2020", + "description": "Apple, 128GB, RAM: 6GB", + "price": 37000000, + "image": "https://cdn.tgdd.vn/Products/Images/522/221775/ipad-pro-12-9-inch-wifi-128gb-2020-xam-400x460-1-400x460.png", + "category_id": 2 + }, + { + "id": 3, + "name": "Galaxy Note 10 Plus", + "description": "Samsung, 64GB, RAML: 6GB", + "price": 24000000, + "image": "https://cdn.tgdd.vn/Products/Images/42/214909/samsung-galaxy-note-10-lite-chi-tiet-1-400x460.png", + "category_id": 1 + } +] \ No newline at end of file diff --git a/saleapp/routes.py b/saleapp/routes.py new file mode 100644 index 0000000..78b7acc --- /dev/null +++ b/saleapp/routes.py @@ -0,0 +1,54 @@ +from flask.templating import render_template +from flask import request +from saleapp import app, utils +import sys, json + +@app.route('/') +def home(): + # Load all Products + products = utils.load_data("data/products.json") + return render_template( + "./pages/home.html", + title="Tất cả sản phẩm", + products=products, + categories=utils.load_data("data/categories.json") + ) + + +# products Route +@app.route('/products', methods=['GET']) +def products(): + # get parameters + _id = int(request.args.get('category_id')) if request.args.get('category_id') else None + + # Transform json input to python objects + input_products = utils.load_data("data/products.json") + + # Filter python objects with list comprehensions + output_products = [product for product in input_products if product["category_id"] == _id ] + + return render_template( + "./pages/products.html", + title="Products", + products=output_products, + ) + +# search Route +@app.route('/search', methods=['GET']) +def search(): + # get parameters + keyword = request.args.get('keyword').lower() + + # Transform json input to python objects + input_products = utils.load_data("data/products.json") + + # Filter python objects with list comprehensions + output_products = [ + product for product in input_products if product["name"].lower().find(keyword) >= 0 + ] + + return render_template( + "./pages/products.html", + title='%s Giá tốt nhất' % keyword, + products=output_products, + ) diff --git a/saleapp/static/css/style.css b/saleapp/static/css/style.css new file mode 100644 index 0000000..e69de29 diff --git a/saleapp/templates/base.html b/saleapp/templates/base.html new file mode 100644 index 0000000..a09be90 --- /dev/null +++ b/saleapp/templates/base.html @@ -0,0 +1,14 @@ + + +
+ + + + {% block head %}{% endblock head %} + + + {% include './layout/navbar.html' %} + {% block body %}{% endblock body %} + {% block scripts %}{% endblock scripts %} + + \ No newline at end of file diff --git a/saleapp/templates/layout/navbar.html b/saleapp/templates/layout/navbar.html new file mode 100644 index 0000000..35a28d0 --- /dev/null +++ b/saleapp/templates/layout/navbar.html @@ -0,0 +1,35 @@ + + diff --git a/saleapp/templates/pages/home.html b/saleapp/templates/pages/home.html new file mode 100644 index 0000000..6138c2b --- /dev/null +++ b/saleapp/templates/pages/home.html @@ -0,0 +1,41 @@ +{% extends 'base.html'%} + +{% block head %} +