From 44ac3d5e92b440e22e3eab0414c324dccf43a6d4 Mon Sep 17 00:00:00 2001 From: onggiabayluon Date: Wed, 3 Nov 2021 16:00:32 +0700 Subject: [PATCH] First Commit --- .flaskenv | 2 + .gitignore | 14 +++++++ .vscode/settings.json | 3 ++ readme.md | 14 +++++++ requirements.txt | 0 saleapp/__init__.py | 4 ++ saleapp/data/categories.json | 10 +++++ saleapp/data/products.json | 26 +++++++++++++ saleapp/routes.py | 54 +++++++++++++++++++++++++++ saleapp/static/css/style.css | 0 saleapp/templates/base.html | 14 +++++++ saleapp/templates/layout/navbar.html | 35 +++++++++++++++++ saleapp/templates/pages/home.html | 41 ++++++++++++++++++++ saleapp/templates/pages/products.html | 45 ++++++++++++++++++++++ saleapp/templates/readme.md | 2 + saleapp/utils.py | 17 +++++++++ setup.py | 10 +++++ 17 files changed, 291 insertions(+) create mode 100644 .flaskenv create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 readme.md create mode 100644 requirements.txt create mode 100644 saleapp/__init__.py create mode 100644 saleapp/data/categories.json create mode 100644 saleapp/data/products.json create mode 100644 saleapp/routes.py create mode 100644 saleapp/static/css/style.css create mode 100644 saleapp/templates/base.html create mode 100644 saleapp/templates/layout/navbar.html create mode 100644 saleapp/templates/pages/home.html create mode 100644 saleapp/templates/pages/products.html create mode 100644 saleapp/templates/readme.md create mode 100644 saleapp/utils.py create mode 100644 setup.py 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 %} +{{ title }} + + + + +{% endblock head %} + + +{% block body %} +
+
+

Bán hàng trực tuyến

+
+ {% for product in products %} +
+
+ Card image cap +
+
{{ product.name }}
+

{{ product.description }}

+ Details +
+
+
+ {% endfor %} +
+
+
+{% endblock body %} + +{% block scripts %} + + + + + + +{% endblock scripts %} \ No newline at end of file diff --git a/saleapp/templates/pages/products.html b/saleapp/templates/pages/products.html new file mode 100644 index 0000000..4f9ad12 --- /dev/null +++ b/saleapp/templates/pages/products.html @@ -0,0 +1,45 @@ +{% extends 'base.html'%} + +{% block head %} +{{ title }} + + + + +{% endblock head %} + + +{% block body %} +
+
+ +
+ {% if products %} + {% for product in products %} +
+
+ Card image cap +
+
{{ product.name }}
+

{{ product.description }}

+ Details +
+
+
+ {% endfor %} + {% else %} +

Không tìm thấy kết quả nào

+ {% endif %} +
+
+
+{% endblock body %} + + +{% block scripts %} + + + + + +{% endblock scripts %} \ No newline at end of file diff --git a/saleapp/templates/readme.md b/saleapp/templates/readme.md new file mode 100644 index 0000000..4d64773 --- /dev/null +++ b/saleapp/templates/readme.md @@ -0,0 +1,2 @@ +# Folder structure +Note: This saleapp [templates Folder structure] Similar to [sass folder structure] diff --git a/saleapp/utils.py b/saleapp/utils.py new file mode 100644 index 0000000..2531c79 --- /dev/null +++ b/saleapp/utils.py @@ -0,0 +1,17 @@ +import json +import os + +from saleapp import app + + +# Read Json file +def read_json(path): + with open(path, "r") as file: + return json.load(file) + +# load data +def load_data(dataPath): + return read_json(os.path.join(app.root_path, dataPath)) + +# Transform python object back into json +# output_json = json.dumps(output_products) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..9f7418a --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +from setuptools import setup + +setup( + name='saleapp', + packages=['saleapp'], + include_package_data=True, + install_requires=[ + 'flask', + ], +) \ No newline at end of file