Update calculate.py
Browse files- calculate.py +67 -66
calculate.py
CHANGED
|
@@ -1,66 +1,67 @@
|
|
| 1 |
-
from flask import Flask, request, redirect, session, url_for
|
| 2 |
-
from data.static import generate as sg
|
| 3 |
-
from data.dynamic import generate as dg
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
app
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
<
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
<input
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
<
|
| 53 |
-
<a href='/
|
| 54 |
-
<a href='/
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, session, url_for
|
| 2 |
+
from data.static import generate as sg
|
| 3 |
+
from data.dynamic import generate as dg
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from data.data import default_hari
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
app.secret_key = "secret_key_aman_diganti"
|
| 10 |
+
|
| 11 |
+
# Data login hardcoded
|
| 12 |
+
VALID_USER = os.getenv("VALID_USER")
|
| 13 |
+
VALID_PASS = os.getenv("VALID_PASS")
|
| 14 |
+
|
| 15 |
+
# Middleware: Cek login sebelum semua request (kecuali /login dan /static file)
|
| 16 |
+
@app.before_request
|
| 17 |
+
def require_login():
|
| 18 |
+
allowed_routes = ["login", "static"]
|
| 19 |
+
if request.endpoint not in allowed_routes and "user" not in session: return redirect(url_for("login"))
|
| 20 |
+
|
| 21 |
+
# Form login
|
| 22 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 23 |
+
def login():
|
| 24 |
+
if request.method == "POST":
|
| 25 |
+
user = request.form.get("username")
|
| 26 |
+
password = request.form.get("password")
|
| 27 |
+
print(user, VALID_USER, password, VALID_PASS)
|
| 28 |
+
if user == VALID_USER and password == VALID_PASS:
|
| 29 |
+
session["user"] = user
|
| 30 |
+
return redirect(url_for("home"))
|
| 31 |
+
return "<h3>Login gagal!</h3><a href='/login'>Coba lagi</a>"
|
| 32 |
+
|
| 33 |
+
return """
|
| 34 |
+
<h2>Login</h2>
|
| 35 |
+
<form method='post'>
|
| 36 |
+
Username: <input name='username'><br>
|
| 37 |
+
Password: <input name='password' type='password'><br>
|
| 38 |
+
<input type='submit' value='Login'>
|
| 39 |
+
</form>
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# Logout
|
| 43 |
+
@app.route("/logout")
|
| 44 |
+
def logout():
|
| 45 |
+
session.pop("user", None)
|
| 46 |
+
return redirect(url_for("login"))
|
| 47 |
+
|
| 48 |
+
# Halaman utama
|
| 49 |
+
@app.route("/")
|
| 50 |
+
def home():
|
| 51 |
+
return """
|
| 52 |
+
<h2>Halaman Utama</h2>
|
| 53 |
+
<a href='/static/7'>Static 7 Hari</a><br>
|
| 54 |
+
<a href='/dynamic'>Dynamic</a><br>
|
| 55 |
+
<a href='/logout'>Logout</a>
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
@app.route("/static")
|
| 59 |
+
def static_page(): return f"""<a href="/">β Kembali</a>{sg(90, request.args.get("img"))}"""
|
| 60 |
+
|
| 61 |
+
@app.route("/static/<int:days>")
|
| 62 |
+
def static_pages(days): return f"""<a href="/">β Kembali</a>{sg(days, request.args.get("img"))}"""
|
| 63 |
+
|
| 64 |
+
@app.route("/dynamic")
|
| 65 |
+
def dynamic_page(): return f"""<a href="/">β Kembali</a>{dg(int(request.args.get("d") or default_hari), request.args.get("img"))}"""
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__": app.run(host=os.getenv("HOST") or "0.0.0.0", port=os.getenv("PORT") or 8501)
|