예제 #1
0
def edit(username):
    if username != g.user.username:
        print(username, g.user.username)
        flash('You are not authorized to visit this page!')
        return redirect(url_for('index'))
    form = EditForm()
    if form.validate_on_submit():
        if form.username.data != g.user.username and User.query.filter_by(
                username=form.username.data).first() != None:
            flash('This username has been occupied!')
            return redirect(url_for('edit', username=g.user.username))
        else:
            if form.avatar.data:
                # print(form.avatar.data.stream)
                filename = avatars.save(form.avatar.data)
                # print(filename)
                file_url = avatars.url(filename)
                g.user.avatar = file_url
            g.user.username = form.username.data
            g.user.about_me = form.about_me.data
            u = g.user
            db.session.add(u)
            db.session.commit()
            logout_user()
            login_user(
                User.query.filter_by(username=form.username.data).first())
            flash('Your changes have been saved.')
            return redirect(url_for('user', username=form.username.data))
    else:
        form.username.data = g.user.username
        form.about_me.data = g.user.about_me
    return render_template('edit.html',
                           title='Edit your info',
                           user=g.user,
                           form=form)
예제 #2
0
def avatar(user_id, isresident):
    if current_user.id == user_id or current_user.can(
            Permission.UPDATE_OTHERS_INFORMATION):
        if (isresident == 'True'):
            the_user = Residents.query.get_or_404(user_id)
            forder = "resident" + str(user_id)
            face = Face.query.filter(Face.id_number == user_id).all()[0]
        elif (isresident == 'User'):
            the_user = User.query.get_or_404(user_id)
            forder = str(user_id)
            face = the_user
        else:
            the_user = Visitors.query.get_or_404(user_id)
            forder = "visitor" + str(user_id)
            face = Face.query.filter(Face.id_number == user_id).all()[0]

        avatar_edit_form = AvatarEditForm()
        avatar_upload_form = AvatarUploadForm()
        if avatar_upload_form.validate_on_submit():
            if 'avatar' in request.files:
                avatar_name = avatars.save(avatar_upload_form.avatar.data,
                                           folder=forder)
                face.avatar = json.dumps({
                    "use_out_url": False,
                    "url": avatar_name
                })
                db.session.add(face)
                db.session.commit()
                flash(u'头像更新成功!', 'success')
                if (isresident != 'User'):
                    return redirect(
                        url_for('book.people_detail',
                                people_id=user_id,
                                isresident=isresident))
                else:
                    return redirect(url_for('user.detail', user_id=user_id))
        if avatar_edit_form.validate_on_submit():
            the_user.avatar = json.dumps({
                "use_out_url":
                True,
                "url":
                avatar_edit_form.avatar_url.data
            })
            db.session.add(the_user)
            db.session.commit()
            return redirect(url_for('user.detail', user_id=user_id))

        return render_template('avatar_edit.html',
                               user=the_user,
                               face=face,
                               avatar_edit_form=avatar_edit_form,
                               avatar_upload_form=avatar_upload_form,
                               title=u"更换头像")
    else:
        abort(403)
예제 #3
0
def change_avatar():
    form = UploadForm()
    if form.validate_on_submit():
        filename = avatars.save(form.photo.data)
        file_url = avatars.url(filename)
        current_user.avatar = file_url
        db.session.add(current_user)
        db.session.commit()
        flash('修改成功!')
        return redirect(
            url_for('user', username=current_user.username, file_url=file_url))
    return render_template('change_avatar.html',
                           form=form,
                           file_url=current_user.avatar)
예제 #4
0
def add_blog():
    from .forms import BlogForm
    form = BlogForm()
    g.open_article = True
    if form.validate_on_submit():
        try:
            filename = avatars.save(request.files['avatars'])
        except Exception as e:
            flash('上传失败,请检查文件格式')
            return render_template('admin/add_blog.html', form=form)
        file_url = avatars.url(filename)
        form.avatars.data = file_url
        Blog.from_form(form)
        return redirect(url_for('main.index'))
    return render_template('admin/add_blog.html', form=form)
예제 #5
0
 def patch(self):  # 用户上传头像
     current_user = get_jwt_identity()
     username = current_user.get('username')
     user = User.query.filter_by(username=username).first()
     file = request.files['avatar']
     print(file.filename)
     filename = username + '.' + file.filename.rsplit('.', 1)[1]
     file_path = os.path.join(current_app.config['UPLOADED_AVATARS_DEST'],
                              filename)
     try:
         if os.path.exists(file_path):
             os.remove(file_path)
         filename = avatars.save(request.files['avatar'],
                                 name=username + '.')
         user.avatar_src = os.path.join(request.url_root, 'static',
                                        'avatar', filename)
     except Exception:
         abort(400, '文件格式错误或文件名全为中文字符')
     user.save_to_db()
     return {"user_id": user.id, "avatar_src": user.avatar_src}
예제 #6
0
def avatar(user_id):
    if current_user.id == user_id or current_user.can(Permission.UPDATE_OTHERS_INFORMATION):
        the_user = User.query.get_or_404(user_id)
        avatar_edit_form = AvatarEditForm()
        avatar_upload_form = AvatarUploadForm()
        if avatar_upload_form.validate_on_submit():
            if 'avatar' in request.files:
                forder = str(user_id)
                avatar_name = avatars.save(avatar_upload_form.avatar.data, folder=forder)
                the_user.avatar = json.dumps({"use_out_url": False, "url": avatar_name})
                db.session.add(the_user)
                db.session.commit()
                flash(u'Avatar updated successfully!', 'success')
                return redirect(url_for('user.detail', user_id=user_id))
        if avatar_edit_form.validate_on_submit():
            the_user.avatar = json.dumps({"use_out_url": True, "url": avatar_edit_form.avatar_url.data})
            db.session.add(the_user)
            db.session.commit()
            return redirect(url_for('user.detail', user_id=user_id))
        return render_template('avatar_edit.html', user=the_user, avatar_edit_form=avatar_edit_form,
                               avatar_upload_form=avatar_upload_form, title=u"Change avatar")
    else:
        abort(403)
예제 #7
0
def upload_avatar():
    user = current_user._get_current_object()
    if request.method == 'POST' and 'avatar' in request.files:
        try:
            filename = avatars.save(request.files['avatar'])
        except UploadNotAllowed:
            flash('请选择要上传的文件!')
        else:
            # 如果用户已经有头像了,就删除原先头像文件
            avatar_path = os.path.join(
                basedir, 'app',
                user.avatar[1:])  # user.avatar 第一个字符‘/’会使得join 不能正确拼接路径,需要去掉
            if os.path.exists(
                    avatar_path
            ) and user.avatar != '/static/img/avatar/akkarin.jpg':
                os.remove(avatar_path)
            user.avatar = url_for('static',
                                  filename='img/upload/avatar/' + filename)
            db.session.add(user)
            db.session.commit()
            flash('头像已更新')
            return redirect(url_for('user_page.user', username=user.username))
    return render_template('user/upload_avatar.html', user=user)
예제 #8
0
def avatar(user_id):
    if current_user.id == user_id or current_user.can(Permission.UPDATE_OTHERS_INFORMATION):
        the_user = User.query.get_or_404(user_id)
        avatar_edit_form = AvatarEditForm()
        avatar_upload_form = AvatarUploadForm()
        if avatar_upload_form.validate_on_submit():
            if 'avatar' in request.files:
                forder = str(user_id)
                avatar_name = avatars.save(avatar_upload_form.avatar.data, folder=forder)
                the_user.avatar = json.dumps({"use_out_url": False, "url": avatar_name})
                db.session.add(the_user)
                db.session.commit()
                flash(u'头像更新成功!', 'success')
                return redirect(url_for('user.detail', user_id=user_id))
        if avatar_edit_form.validate_on_submit():
            the_user.avatar = json.dumps({"use_out_url": True, "url": avatar_edit_form.avatar_url.data})
            db.session.add(the_user)
            db.session.commit()
            return redirect(url_for('user.detail', user_id=user_id))
        return render_template('avatar_edit.html', user=the_user, avatar_edit_form=avatar_edit_form,
                               avatar_upload_form=avatar_upload_form, title=u"更换头像")
    else:
        abort(403)