
from random import random
from PiLE.extensions import db
from PiLE.tables import User, ProfilePhoto
from flask import current_app, g, redirect, render_template, Blueprint, request, flash, g, url_for
from PiLE.auth import login_required
from werkzeug.exceptions import abort
from PiLE.blog import get_random_filename
import os
from datetime import datetime
from PiLE.utils import get_profile_photo

bp = Blueprint('user', __name__, url_prefix='/user')

#用户资料
@bp.route('/<int:user_id>/profile')
def profile(user_id):
    user = db.session.get(User, user_id)

    #获取照片文件名，如果没有就使用默认的
    photo_filename = get_profile_photo(user)

    return render_template('/user/profile.html', user=user, photo_filename=photo_filename)

@bp.route('<int:user_id>/edit', methods=['GET', 'POST'])
@login_required
def edit(user_id):
    if user_id != g.user.id:
        abort(403, '只能更改自己的资料。')
    else:
        user = db.session.get(User, user_id)
        
        
        #更新头像
        if request.method == 'POST':
            profile_photo = request.files.getlist('profile_photo')[0]

            if profile_photo.filename != '':
                root, ext = os.path.splitext(profile_photo.filename)
                if ext not in current_app.config['ALLOWED_IMAGE_EXT']:
                    error = '只允许png, jpg, jpeg, bmp图片。'
                    flash(error)
                else:
                    random_name = get_random_filename(root)
                    photo = ProfilePhoto(created=datetime.today(), filename=random_name+ext, active=True)
                    path = os.path.join(current_app.config['PROFILEPHOTO_DIR'], random_name+ext)
                    profile_photo.save(path)
                    #把以前的头像的状态改为False
                    for p in user.profile_photos:
                        p.active = False
                    user.profile_photos.append(photo)
                    db.session.commit()

                    return redirect(url_for('user.profile', user_id=user.id))

                


        return render_template('user/edit.html', user=user)

from random import random
from PiLE.extensions import db
from PiLE.tables import User, ProfilePhoto
from flask import current_app, g, redirect, render_template, Blueprint, request, flash, g, url_for
from PiLE.auth import login_required
from werkzeug.exceptions import abort
from PiLE.blog import get_random_filename
import os
from datetime import datetime
from PiLE.utils import get_profile_photo

bp = Blueprint('user', __name__, url_prefix='/user')

#用户资料
@bp.route('/<int:user_id>/profile')
def profile(user_id):
    user = db.session.get(User, user_id)

    #获取照片文件名，如果没有就使用默认的
    photo_filename = get_profile_photo(user)

    return render_template('/user/profile.html', user=user, photo_filename=photo_filename)

@bp.route('<int:user_id>/edit', methods=['GET', 'POST'])
@login_required
def edit(user_id):
    if user_id != g.user.id:
        abort(403, '只能更改自己的资料。')
    else:
        user = db.session.get(User, user_id)
        
        
        #更新头像
        if request.method == 'POST':
            profile_photo = request.files.getlist('profile_photo')[0]

            if profile_photo.filename != '':
                root, ext = os.path.splitext(profile_photo.filename)
                if ext not in current_app.config['ALLOWED_IMAGE_EXT']:
                    error = '只允许png, jpg, jpeg, bmp图片。'
                    flash(error)
                else:
                    random_name = get_random_filename(root)
                    photo = ProfilePhoto(created=datetime.today(), filename=random_name+ext, active=True)
                    path = os.path.join(current_app.config['PROFILEPHOTO_DIR'], random_name+ext)
                    profile_photo.save(path)
                    #把以前的头像的状态改为False
                    for p in user.profile_photos:
                        p.active = False
                    user.profile_photos.append(photo)
                    db.session.commit()

                    return redirect(url_for('user.profile', user_id=user.id))

                


        return render_template('user/edit.html', user=user)
#用户资料 #获取照片文件名，如果没有就使用默认的 #更新头像 #把以前的头像的状态改为False