organizing the project
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,6 +1,6 @@
|
|||||||
venv/
|
venv/
|
||||||
desktop.ini
|
desktop.ini
|
||||||
static/
|
data/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
*.pyo
|
*.pyo
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ except ImportError:
|
|||||||
|
|
||||||
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
|
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
|
||||||
|
|
||||||
PRODUCTS_FILE = 'static/products.json'
|
DATA_FOLDER = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
UPLOAD_FOLDER = 'static/images'
|
PRODUCTS_FILE = os.path.join(DATA_FOLDER, 'products.json')
|
||||||
BARCODE_FOLDER = 'static/barcodes'
|
UPLOAD_FOLDER = os.path.join(DATA_FOLDER, 'images')
|
||||||
|
BARCODE_FOLDER = os.path.join(DATA_FOLDER, 'barcodes')
|
||||||
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
||||||
|
|
||||||
def allowed_file(filename):
|
def allowed_file(filename):
|
||||||
@@ -4,7 +4,8 @@ import json
|
|||||||
from admin import admin_bp
|
from admin import admin_bp
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['STATIC_FOLDER'] = './static'
|
# Set DATA_FOLDER to the absolute path of the data directory inside src
|
||||||
|
app.config['DATA_FOLDER'] = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
app.config['SECRET_KEY'] = 'dev-key-for-demo-only'
|
app.config['SECRET_KEY'] = 'dev-key-for-demo-only'
|
||||||
|
|
||||||
# Register blueprints
|
# Register blueprints
|
||||||
@@ -13,7 +14,8 @@ app.register_blueprint(admin_bp)
|
|||||||
# Load product data from products.json
|
# Load product data from products.json
|
||||||
def load_products():
|
def load_products():
|
||||||
try:
|
try:
|
||||||
with open('static/products.json', 'r') as f:
|
products_file = os.path.join(app.config['DATA_FOLDER'], 'products.json')
|
||||||
|
with open(products_file, 'r') as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
except (FileNotFoundError, json.JSONDecodeError):
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
return {}
|
return {}
|
||||||
@@ -47,25 +49,25 @@ def get_ar_info():
|
|||||||
|
|
||||||
@app.route('/images/<path:filename>', methods=['GET'])
|
@app.route('/images/<path:filename>', methods=['GET'])
|
||||||
def serve_image(filename):
|
def serve_image(filename):
|
||||||
image_path = os.path.join(app.config['STATIC_FOLDER'], 'images', filename)
|
image_path = os.path.join(app.config['DATA_FOLDER'], 'images', filename)
|
||||||
if os.path.exists(image_path):
|
if os.path.exists(image_path):
|
||||||
return send_file(image_path)
|
return send_file(image_path)
|
||||||
return jsonify({"error": "Image not found"}), 404
|
return jsonify({"error": "Image not found"}), 404
|
||||||
|
|
||||||
@app.route('/barcodes/<path:filename>', methods=['GET'])
|
@app.route('/barcodes/<path:filename>', methods=['GET'])
|
||||||
def serve_barcode(filename):
|
def serve_barcode(filename):
|
||||||
barcode_path = os.path.join(app.config['STATIC_FOLDER'], 'barcodes', filename)
|
barcode_path = os.path.join(app.config['DATA_FOLDER'], 'barcodes', filename)
|
||||||
if os.path.exists(barcode_path):
|
if os.path.exists(barcode_path):
|
||||||
return send_file(barcode_path)
|
return send_file(barcode_path)
|
||||||
return jsonify({"error": "Barcode not found"}), 404
|
return jsonify({"error": "Barcode not found"}), 404
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Ensure the required directories exist
|
# Ensure the required directories exist
|
||||||
os.makedirs(os.path.join(app.config['STATIC_FOLDER'], 'images'), exist_ok=True)
|
os.makedirs(os.path.join(app.config['DATA_FOLDER'], 'images'), exist_ok=True)
|
||||||
os.makedirs(os.path.join(app.config['STATIC_FOLDER'], 'barcodes'), exist_ok=True)
|
os.makedirs(os.path.join(app.config['DATA_FOLDER'], 'barcodes'), exist_ok=True)
|
||||||
|
|
||||||
# If products.json doesn't exist, create it with initial data
|
# If products.json doesn't exist, create it with initial data
|
||||||
products_file = os.path.join(app.config['STATIC_FOLDER'], 'products.json')
|
products_file = os.path.join(app.config['DATA_FOLDER'], 'products.json')
|
||||||
if not os.path.exists(products_file):
|
if not os.path.exists(products_file):
|
||||||
initial_data = {
|
initial_data = {
|
||||||
"123456": [
|
"123456": [
|
||||||
Reference in New Issue
Block a user