updating ui
This commit is contained in:
68
app.py
68
app.py
@@ -1,22 +1,26 @@
|
||||
from flask import Flask, jsonify, request, send_file
|
||||
from flask import Flask, jsonify, request, send_file, render_template, flash
|
||||
import os
|
||||
import json
|
||||
from admin import admin_bp
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['STATIC_FOLDER'] = './static'
|
||||
app.config['SECRET_KEY'] = 'dev-key-for-demo-only'
|
||||
|
||||
# Sample in-memory data for item attributes
|
||||
ITEM_ATTRIBUTES = {
|
||||
"123456": [
|
||||
{"fieldName": "_id", "label": "Item ID", "value": "123456", "editable": "false", "fieldType": "TEXT"},
|
||||
{"fieldName": "_price", "label": "Sale Price", "value": "$49.99", "editable": "true", "fieldType": "TEXT"},
|
||||
{"fieldName": "_image", "label": "Image", "value": "/images/123456.png", "editable": "false", "fieldType": "IMAGE_URI"}
|
||||
],
|
||||
"789012": [
|
||||
{"fieldName": "_id", "label": "Item ID", "value": "789012", "editable": "false", "fieldType": "TEXT"},
|
||||
{"fieldName": "_price", "label": "Sale Price", "value": "$59.99", "editable": "true", "fieldType": "TEXT"},
|
||||
{"fieldName": "_image", "label": "Image", "value": "/images/789012.png", "editable": "false", "fieldType": "IMAGE_URI"}
|
||||
]
|
||||
}
|
||||
# Register blueprints
|
||||
app.register_blueprint(admin_bp)
|
||||
|
||||
# Load product data from products.json
|
||||
def load_products():
|
||||
try:
|
||||
with open('static/products.json', 'r') as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/login', methods=['GET'])
|
||||
def login():
|
||||
@@ -36,16 +40,46 @@ def get_ar_content_fields():
|
||||
@app.route('/arinfo', methods=['GET'])
|
||||
def get_ar_info():
|
||||
barcode = request.args.get('barcode')
|
||||
if not barcode or barcode not in ITEM_ATTRIBUTES:
|
||||
products = load_products()
|
||||
if not barcode or barcode not in products:
|
||||
return jsonify({"error": "Item not found"}), 404
|
||||
return jsonify(ITEM_ATTRIBUTES[barcode]), 200
|
||||
return jsonify(products[barcode]), 200
|
||||
|
||||
@app.route('/images/<path:filename>', methods=['GET'])
|
||||
def serve_image(filename):
|
||||
image_path = os.path.join(app.config['STATIC_FOLDER'], filename)
|
||||
image_path = os.path.join(app.config['STATIC_FOLDER'], 'images', filename)
|
||||
if os.path.exists(image_path):
|
||||
return send_file(image_path)
|
||||
return jsonify({"error": "Image not found"}), 404
|
||||
|
||||
@app.route('/barcodes/<path:filename>', methods=['GET'])
|
||||
def serve_barcode(filename):
|
||||
barcode_path = os.path.join(app.config['STATIC_FOLDER'], 'barcodes', filename)
|
||||
if os.path.exists(barcode_path):
|
||||
return send_file(barcode_path)
|
||||
return jsonify({"error": "Barcode not found"}), 404
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 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['STATIC_FOLDER'], 'barcodes'), exist_ok=True)
|
||||
|
||||
# If products.json doesn't exist, create it with initial data
|
||||
products_file = os.path.join(app.config['STATIC_FOLDER'], 'products.json')
|
||||
if not os.path.exists(products_file):
|
||||
initial_data = {
|
||||
"123456": [
|
||||
{"fieldName": "_id", "label": "Item ID", "value": "123456", "editable": "false", "fieldType": "TEXT"},
|
||||
{"fieldName": "_price", "label": "Sale Price", "value": "$49.99", "editable": "true", "fieldType": "TEXT"},
|
||||
{"fieldName": "_image", "label": "Image", "value": "/images/123456.png", "editable": "false", "fieldType": "IMAGE_URI"}
|
||||
],
|
||||
"789012": [
|
||||
{"fieldName": "_id", "label": "Item ID", "value": "789012", "editable": "false", "fieldType": "TEXT"},
|
||||
{"fieldName": "_price", "label": "Sale Price", "value": "$59.99", "editable": "true", "fieldType": "TEXT"},
|
||||
{"fieldName": "_image", "label": "Image", "value": "/images/789012.png", "editable": "false", "fieldType": "IMAGE_URI"}
|
||||
]
|
||||
}
|
||||
with open(products_file, 'w') as f:
|
||||
json.dump(initial_data, f, indent=2)
|
||||
|
||||
app.run(port=5555, host="0.0.0.0", debug=True)
|
||||
|
||||
Reference in New Issue
Block a user