add user settings
This commit is contained in:
@ -3,16 +3,50 @@ from flask import (
|
||||
)
|
||||
from functools import wraps
|
||||
from ..db import db
|
||||
from ..models import Users, Sessions, Subscriptions
|
||||
from ..lib.babycode import babycode_to_html
|
||||
from ..models import Users, Sessions, Subscriptions, Avatars
|
||||
from ..constants import InfoboxKind, PermissionLevel
|
||||
from ..auth import digest, verify
|
||||
from wand.image import Image
|
||||
from wand.exceptions import WandException
|
||||
import secrets
|
||||
import time
|
||||
import re
|
||||
import os
|
||||
|
||||
bp = Blueprint("users", __name__, url_prefix = "/users/")
|
||||
|
||||
|
||||
def validate_and_create_avatar(input_image, filename):
|
||||
try:
|
||||
with Image(blob=input_image) as img:
|
||||
img.strip()
|
||||
img.gravity = 'center'
|
||||
|
||||
width, height = img.width, img.height
|
||||
min_dim = min(width, height)
|
||||
if min_dim > 256:
|
||||
ratio = 256.0 / min_dim
|
||||
new_width = int(width * ratio)
|
||||
new_height = int(height * ratio)
|
||||
img.resize(new_width, new_height)
|
||||
|
||||
width, height = img.width, img.height
|
||||
crop_size = min(width, height)
|
||||
x_offset = (width - crop_size) // 2
|
||||
y_offset = (height - crop_size) // 2
|
||||
img.crop(left=x_offset, top=y_offset,
|
||||
width=crop_size, height=crop_size)
|
||||
|
||||
img.resize(256, 256)
|
||||
img.format = 'webp'
|
||||
img.compression_quality = 85
|
||||
img.save(filename=filename)
|
||||
return True
|
||||
except WandException:
|
||||
return False
|
||||
|
||||
|
||||
def is_logged_in():
|
||||
return "pyrom_session_key" in session
|
||||
|
||||
@ -197,10 +231,92 @@ def page(username):
|
||||
return render_template("users/user.html", target_user = target_user)
|
||||
|
||||
|
||||
@bp.get("/<username>/setings")
|
||||
@bp.get("/<username>/settings")
|
||||
@login_required
|
||||
def settings(username):
|
||||
return "stub"
|
||||
target_user = Users.find({'username': username})
|
||||
if target_user.id != get_active_user().id:
|
||||
return redirect('.settings', username = get_active_user().username)
|
||||
|
||||
return render_template('users/settings.html')
|
||||
|
||||
|
||||
@bp.post('/<username>/settings')
|
||||
@login_required
|
||||
def settings_form(username):
|
||||
# we silently ignore the passed username
|
||||
# and grab the correct user from the session
|
||||
user = get_active_user()
|
||||
topic_sort_by = request.form.get('topic_sort_by', default='activity')
|
||||
if topic_sort_by == 'activity' or topic_sort_by == 'thread':
|
||||
sort_by = session['sort_by'] = topic_sort_by
|
||||
status = request.form.get('status', default="")[:100]
|
||||
original_sig = request.form.get('signature', default='')
|
||||
rendered_sig = babycode_to_html(original_sig)
|
||||
session['subscribe_by_default'] = request.form.get('subscribe_by_default', default='on') == 'on'
|
||||
|
||||
user.update({
|
||||
'status': status,
|
||||
'signature_original_markup': original_sig,
|
||||
'signature_rendered': rendered_sig,
|
||||
})
|
||||
flash('Settings updated.', InfoboxKind.INFO)
|
||||
return redirect(url_for('.settings', username=user.username))
|
||||
|
||||
|
||||
@bp.post('/<username>/set_avatar')
|
||||
@login_required
|
||||
def set_avatar(username):
|
||||
user = get_active_user()
|
||||
if user.is_guest():
|
||||
return 'no'
|
||||
if 'avatar' not in request.files:
|
||||
return 'no!...'
|
||||
|
||||
file = request.files['avatar']
|
||||
|
||||
if file.filename == '':
|
||||
return 'no..?'
|
||||
|
||||
file_bytes = file.read()
|
||||
|
||||
now = int(time.time())
|
||||
filename = f"u{user.id}d{now}.webp"
|
||||
output_path = os.path.join(current_app.config['AVATAR_UPLOAD_PATH'], filename)
|
||||
proxied_filename = f"/static/avatars/{filename}"
|
||||
res = validate_and_create_avatar(file_bytes, output_path)
|
||||
if res:
|
||||
flash('Avatar updated.', InfoboxKind.INFO)
|
||||
avatar = Avatars.create({
|
||||
'file_path': proxied_filename,
|
||||
'uploaded_at': now,
|
||||
})
|
||||
old_avatar = Avatars.find({'id': user.avatar_id})
|
||||
user.update({'avatar_id': avatar.id})
|
||||
if int(old_avatar.id) != 1:
|
||||
# delete old avi, but not default
|
||||
filename = os.path.join(current_app.config['AVATAR_UPLOAD_PATH'], os.path.basename(old_avatar.file_path))
|
||||
os.remove(filename)
|
||||
old_avatar.delete()
|
||||
return redirect(url_for('.settings', username=user.username))
|
||||
else:
|
||||
return 'uhhhh no'
|
||||
|
||||
|
||||
@bp.post('/<username>/clear_avatar')
|
||||
@login_required
|
||||
def clear_avatar(username):
|
||||
user = get_active_user()
|
||||
if user.is_default_avatar():
|
||||
return 'no'
|
||||
|
||||
old_avatar = Avatars.find({'id': user.avatar_id})
|
||||
user.update({'avatar_id': 1})
|
||||
# delete old avi
|
||||
filename = os.path.join(current_app.config['AVATAR_UPLOAD_PATH'], os.path.basename(old_avatar.file_path))
|
||||
os.remove(filename)
|
||||
old_avatar.delete()
|
||||
return redirect(url_for('.settings', username=user.username))
|
||||
|
||||
|
||||
@bp.post("/log_out")
|
||||
|
Reference in New Issue
Block a user