Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,29 +7,54 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|
| 7 |
from threading import Lock
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
-
app.secret_key = os.environ.get("SECRET_KEY", "
|
| 11 |
|
| 12 |
-
# ---
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
DB_REPO = os.environ.get("DB_REPO")
|
| 15 |
DB_FILE = "db.json"
|
| 16 |
api = HfApi(token=HF_TOKEN)
|
| 17 |
db_lock = Lock()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def load_db():
|
|
|
|
| 20 |
if not HF_TOKEN or not DB_REPO:
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
path = hf_hub_download(repo_id=DB_REPO, filename=DB_FILE, repo_type="dataset", token=HF_TOKEN)
|
| 24 |
with open(path, 'r') as f:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def save_db(data):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with db_lock:
|
|
|
|
| 31 |
with open(DB_FILE, 'w') as f:
|
| 32 |
json.dump(data, f)
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
api.upload_file(
|
| 35 |
path_or_fileobj=DB_FILE,
|
|
@@ -38,11 +63,16 @@ def save_db(data):
|
|
| 38 |
repo_type="dataset",
|
| 39 |
commit_message="Sync DB"
|
| 40 |
)
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"
|
|
|
|
|
|
|
| 43 |
|
|
|
|
| 44 |
DATA_CACHE = load_db()
|
| 45 |
|
|
|
|
| 46 |
@app.route('/')
|
| 47 |
def index():
|
| 48 |
return render_template('index.html', pins=DATA_CACHE.get('pins', []), user=session.get('user'))
|
|
@@ -51,38 +81,68 @@ def index():
|
|
| 51 |
def signup():
|
| 52 |
username = request.form['username']
|
| 53 |
password = request.form['password']
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
return redirect(url_for('index'))
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return redirect(url_for('index'))
|
| 61 |
|
| 62 |
@app.route('/login', methods=['POST'])
|
| 63 |
def login():
|
| 64 |
username = request.form['username']
|
| 65 |
password = request.form['password']
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
session['user'] = username
|
|
|
|
| 69 |
else:
|
| 70 |
-
flash("Invalid
|
|
|
|
| 71 |
return redirect(url_for('index'))
|
| 72 |
|
| 73 |
@app.route('/logout')
|
| 74 |
def logout():
|
| 75 |
session.pop('user', None)
|
|
|
|
| 76 |
return redirect(url_for('index'))
|
| 77 |
|
| 78 |
@app.route('/add_pin', methods=['POST'])
|
| 79 |
def add_pin():
|
| 80 |
if 'user' not in session: return redirect(url_for('index'))
|
|
|
|
| 81 |
img_url = request.form['img_url']
|
| 82 |
caption = request.form['caption']
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
save_db(DATA_CACHE)
|
|
|
|
| 86 |
return redirect(url_for('index'))
|
| 87 |
|
| 88 |
if __name__ == '__main__':
|
|
|
|
| 7 |
from threading import Lock
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
+
app.secret_key = os.environ.get("SECRET_KEY", "super_secret_key_123")
|
| 11 |
|
| 12 |
+
# --- CONFIGURATION ---
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
DB_REPO = os.environ.get("DB_REPO")
|
| 15 |
DB_FILE = "db.json"
|
| 16 |
api = HfApi(token=HF_TOKEN)
|
| 17 |
db_lock = Lock()
|
| 18 |
|
| 19 |
+
# --- STARTER DATA (So the site isn't empty!) ---
|
| 20 |
+
DEFAULT_PINS = [
|
| 21 |
+
{"id": 1, "url": "https://images.unsplash.com/photo-1541963463532-d68292c34b19", "caption": "Nature Vibes", "author": "System"},
|
| 22 |
+
{"id": 2, "url": "https://images.unsplash.com/photo-1493246507139-91e8fad9978e", "caption": "Alpine Lake", "author": "System"},
|
| 23 |
+
{"id": 3, "url": "https://images.unsplash.com/photo-1511497584788-876760111969", "caption": "Forest Mist", "author": "System"},
|
| 24 |
+
{"id": 4, "url": "https://images.unsplash.com/photo-1682687982501-1e58ab814714", "caption": "Desert Life", "author": "System"},
|
| 25 |
+
{"id": 5, "url": "https://images.unsplash.com/photo-1472214103451-9374bd1c798e", "caption": "Green Valley", "author": "System"}
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
def load_db():
|
| 29 |
+
# If secrets are missing, warn the user but load default images
|
| 30 |
if not HF_TOKEN or not DB_REPO:
|
| 31 |
+
print("WARNING: HF_TOKEN or DB_REPO secrets are missing. Site is Read-Only.")
|
| 32 |
+
return {"users": {}, "pins": DEFAULT_PINS}
|
| 33 |
+
|
| 34 |
try:
|
| 35 |
path = hf_hub_download(repo_id=DB_REPO, filename=DB_FILE, repo_type="dataset", token=HF_TOKEN)
|
| 36 |
with open(path, 'r') as f:
|
| 37 |
+
data = json.load(f)
|
| 38 |
+
# If DB has no pins, mix in our defaults
|
| 39 |
+
if not data.get('pins'):
|
| 40 |
+
data['pins'] = DEFAULT_PINS
|
| 41 |
+
return data
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"DB Load Error (Using defaults): {e}")
|
| 44 |
+
return {"users": {}, "pins": DEFAULT_PINS}
|
| 45 |
|
| 46 |
def save_db(data):
|
| 47 |
+
# Check permissions before trying to save
|
| 48 |
+
if not HF_TOKEN or not DB_REPO:
|
| 49 |
+
flash("Error: Cannot save. Secrets (HF_TOKEN/DB_REPO) are missing!")
|
| 50 |
+
return False
|
| 51 |
+
|
| 52 |
with db_lock:
|
| 53 |
+
# 1. Save locally
|
| 54 |
with open(DB_FILE, 'w') as f:
|
| 55 |
json.dump(data, f)
|
| 56 |
+
|
| 57 |
+
# 2. Push to Hugging Face
|
| 58 |
try:
|
| 59 |
api.upload_file(
|
| 60 |
path_or_fileobj=DB_FILE,
|
|
|
|
| 63 |
repo_type="dataset",
|
| 64 |
commit_message="Sync DB"
|
| 65 |
)
|
| 66 |
+
return True
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"Sync Error: {e}")
|
| 69 |
+
flash(f"Database Sync Failed: {str(e)}")
|
| 70 |
+
return False
|
| 71 |
|
| 72 |
+
# Load data on startup
|
| 73 |
DATA_CACHE = load_db()
|
| 74 |
|
| 75 |
+
# --- ROUTES ---
|
| 76 |
@app.route('/')
|
| 77 |
def index():
|
| 78 |
return render_template('index.html', pins=DATA_CACHE.get('pins', []), user=session.get('user'))
|
|
|
|
| 81 |
def signup():
|
| 82 |
username = request.form['username']
|
| 83 |
password = request.form['password']
|
| 84 |
+
|
| 85 |
+
# 1. Check if user exists
|
| 86 |
+
if username in DATA_CACHE.get('users', {}):
|
| 87 |
+
flash("User already exists!")
|
| 88 |
return redirect(url_for('index'))
|
| 89 |
+
|
| 90 |
+
# 2. Add user
|
| 91 |
+
DATA_CACHE.setdefault('users', {})[username] = generate_password_hash(password)
|
| 92 |
+
|
| 93 |
+
# 3. Save to DB
|
| 94 |
+
success = save_db(DATA_CACHE)
|
| 95 |
+
if success:
|
| 96 |
+
session['user'] = username
|
| 97 |
+
flash("Account created successfully!")
|
| 98 |
+
else:
|
| 99 |
+
# If save failed, remove the user from cache so they try again
|
| 100 |
+
del DATA_CACHE['users'][username]
|
| 101 |
+
|
| 102 |
return redirect(url_for('index'))
|
| 103 |
|
| 104 |
@app.route('/login', methods=['POST'])
|
| 105 |
def login():
|
| 106 |
username = request.form['username']
|
| 107 |
password = request.form['password']
|
| 108 |
+
users = DATA_CACHE.get('users', {})
|
| 109 |
+
|
| 110 |
+
if username in users and check_password_hash(users[username], password):
|
| 111 |
session['user'] = username
|
| 112 |
+
flash("Logged in!")
|
| 113 |
else:
|
| 114 |
+
flash("Invalid username or password")
|
| 115 |
+
|
| 116 |
return redirect(url_for('index'))
|
| 117 |
|
| 118 |
@app.route('/logout')
|
| 119 |
def logout():
|
| 120 |
session.pop('user', None)
|
| 121 |
+
flash("Logged out")
|
| 122 |
return redirect(url_for('index'))
|
| 123 |
|
| 124 |
@app.route('/add_pin', methods=['POST'])
|
| 125 |
def add_pin():
|
| 126 |
if 'user' not in session: return redirect(url_for('index'))
|
| 127 |
+
|
| 128 |
img_url = request.form['img_url']
|
| 129 |
caption = request.form['caption']
|
| 130 |
+
|
| 131 |
+
# Basic validation
|
| 132 |
+
if not img_url:
|
| 133 |
+
flash("Image URL is required")
|
| 134 |
+
return redirect(url_for('index'))
|
| 135 |
+
|
| 136 |
+
new_pin = {
|
| 137 |
+
"id": int(time.time()),
|
| 138 |
+
"url": img_url,
|
| 139 |
+
"caption": caption,
|
| 140 |
+
"author": session['user']
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
DATA_CACHE.setdefault('pins', []).insert(0, new_pin)
|
| 144 |
save_db(DATA_CACHE)
|
| 145 |
+
flash("Pin added!")
|
| 146 |
return redirect(url_for('index'))
|
| 147 |
|
| 148 |
if __name__ == '__main__':
|