Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,111 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
@app.route('/')
|
| 8 |
def home():
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
from functools import wraps
|
| 7 |
+
import qrcode
|
| 8 |
|
| 9 |
+
from flask import Flask, render_template, request, Response
|
| 10 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 11 |
+
from flask_admin import Admin, AdminIndexView
|
| 12 |
+
from flask_admin.contrib.sqla import ModelView
|
| 13 |
+
|
| 14 |
+
# --- KONFIGURASI APLIKASI ---
|
| 15 |
app = Flask(__name__)
|
| 16 |
+
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'ganti-dengan-kunci-rahasia-yang-sangat-aman')
|
| 17 |
+
|
| 18 |
+
# --- KONFIGURASI DATABASE ---
|
| 19 |
+
# Menentukan path untuk file database
|
| 20 |
+
basedir = os.path.abspath(os.path.dirname(__file__))
|
| 21 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
|
| 22 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 23 |
+
db = SQLAlchemy(app)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# --- MODEL DATABASE (STRUKTUR TABEL) ---
|
| 27 |
+
class Product(db.Model):
|
| 28 |
+
id = db.Column(db.Integer, primary_key=True)
|
| 29 |
+
name = db.Column(db.String(100), unique=True, nullable=False)
|
| 30 |
+
description = db.Column(db.Text, nullable=False)
|
| 31 |
+
price = db.Column(db.String(50), nullable=False)
|
| 32 |
+
image = db.Column(db.String(100), nullable=True, default='default.jpg')
|
| 33 |
+
|
| 34 |
+
def __repr__(self):
|
| 35 |
+
return f'<Product {self.name}>'
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# --- FUNGSI KEAMANAN UNTUK ADMIN PANEL ---
|
| 39 |
+
def check_auth(username, password):
|
| 40 |
+
"""Fungsi untuk memeriksa username & password admin."""
|
| 41 |
+
ADMIN_USER = os.environ.get('ADMIN_USER', 'admin')
|
| 42 |
+
ADMIN_PASS = os.environ.get('ADMIN_PASS', 'password')
|
| 43 |
+
return username == ADMIN_USER and password == ADMIN_PASS
|
| 44 |
+
|
| 45 |
+
def authenticate():
|
| 46 |
+
"""Mengirim respons 401 Unauthorized."""
|
| 47 |
+
return Response(
|
| 48 |
+
'Could not verify your access level for that URL.\n'
|
| 49 |
+
'You have to login with proper credentials', 401,
|
| 50 |
+
{'WWW-Authenticate': 'Basic realm="Login Required"'})
|
| 51 |
+
|
| 52 |
+
def protected(f):
|
| 53 |
+
@wraps(f)
|
| 54 |
+
def decorated(*args, **kwargs):
|
| 55 |
+
auth = request.authorization
|
| 56 |
+
if not auth or not check_auth(auth.username, auth.password):
|
| 57 |
+
return authenticate()
|
| 58 |
+
return f(*args, **kwargs)
|
| 59 |
+
return decorated
|
| 60 |
|
| 61 |
+
|
| 62 |
+
# --- PENGATURAN ADMIN PANEL ---
|
| 63 |
+
class SecureAdminIndexView(AdminIndexView):
|
| 64 |
+
@protected
|
| 65 |
+
def dispatch_request(self, *args, **kwargs):
|
| 66 |
+
return super(SecureAdminIndexView, self).dispatch_request(*args, **kwargs)
|
| 67 |
+
|
| 68 |
+
class SecureModelView(ModelView):
|
| 69 |
+
@protected
|
| 70 |
+
def dispatch_request(self, *args, **kwargs):
|
| 71 |
+
return super(SecureModelView, self).dispatch_request(*args, **kwargs)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
admin = Admin(app, name='Bit & Bean Admin', template_mode='bootstrap4', index_view=SecureAdminIndexView())
|
| 75 |
+
admin.add_view(SecureModelView(Product, db.session))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# --- FUNGSI QR CODE (TIDAK BERUBAH) ---
|
| 79 |
+
def generate_qr_code(data):
|
| 80 |
+
qr = qrcode.QRCode(version=1, box_size=10, border=4)
|
| 81 |
+
qr.add_data(data)
|
| 82 |
+
qr.make(fit=True)
|
| 83 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
| 84 |
+
buffered = io.BytesIO()
|
| 85 |
+
img.save(buffered, format="PNG")
|
| 86 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 87 |
+
return img_str
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# --- ROUTE UNTUK HALAMAN UTAMA ---
|
| 91 |
@app.route('/')
|
| 92 |
def home():
|
| 93 |
+
"""Mengambil data produk dari database dan menampilkannya."""
|
| 94 |
+
products_from_db = Product.query.all()
|
| 95 |
+
|
| 96 |
+
products_with_qr = []
|
| 97 |
+
for product in products_from_db:
|
| 98 |
+
product_url = f"https://NAMA_SPACE_ANDA.hf.space/product/{product.id}" # Ganti dengan URL Space Anda
|
| 99 |
+
|
| 100 |
+
# Konversi objek database menjadi dictionary agar mudah diolah
|
| 101 |
+
product_data = {
|
| 102 |
+
"id": product.id,
|
| 103 |
+
"name": product.name,
|
| 104 |
+
"description": product.description,
|
| 105 |
+
"price": product.price,
|
| 106 |
+
"image": product.image,
|
| 107 |
+
"qr_code": generate_qr_code(product_url)
|
| 108 |
+
}
|
| 109 |
+
products_with_qr.append(product_data)
|
| 110 |
+
|
| 111 |
+
return render_template('index.html', products=products_with_qr)
|