user avatars and banner images

This commit is contained in:
rimu 2023-12-14 20:24:08 +13:00
parent 46900390a5
commit b32be0127e
3 changed files with 33 additions and 7 deletions

View file

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% from 'bootstrap/form.html' import render_form %} {% from 'bootstrap/form.html' import render_field %}
{% block app_content %} {% block app_content %}
<div class="row"> <div class="row">
@ -11,9 +11,21 @@
<li class="breadcrumb-item active">{{ _('Edit profile') }}</li> <li class="breadcrumb-item active">{{ _('Edit profile') }}</li>
</ol> </ol>
</nav> </nav>
<h1 class="mt-2">{{ _('Edit profile') }}</h1> <h1 class="mt-2">{{ _('Edit profile of %(name)s', name=user.user_name) }}</h1>
<form method='post'> <form method='post' enctype="multipart/form-data">
{{ render_form(form) }} {{ form.csrf_token() }}
{{ render_field(form.email) }}
{{ render_field(form.password_field) }}
<hr />
{{ render_field(form.about) }}
{{ render_field(form.profile_file) }}
<small class="field_hint">Provide a square image that looks good when small.</small>
{{ render_field(form.banner_file) }}
<small class="field_hint">Provide a wide image - letterbox orientation.</small>
<hr />
{{ render_field(form.bot) }}
{{ render_field(form.submit) }}
</form> </form>
</div> </div>
</div> </div>

View file

@ -11,6 +11,9 @@ class ProfileForm(FlaskForm):
password_field = PasswordField(_l('Set new password'), validators=[Optional(), Length(min=1, max=50)], password_field = PasswordField(_l('Set new password'), validators=[Optional(), Length(min=1, max=50)],
render_kw={"autocomplete": 'Off'}) render_kw={"autocomplete": 'Off'})
about = TextAreaField(_l('Bio'), validators=[Optional(), Length(min=3, max=5000)]) about = TextAreaField(_l('Bio'), validators=[Optional(), Length(min=3, max=5000)])
profile_file = FileField(_('Avatar image'))
banner_file = FileField(_('Top banner image'))
bot = BooleanField(_l('This profile is a bot'))
submit = SubmitField(_l('Save profile')) submit = SubmitField(_l('Save profile'))
def validate_email(self, field): def validate_email(self, field):
@ -20,7 +23,6 @@ class ProfileForm(FlaskForm):
class SettingsForm(FlaskForm): class SettingsForm(FlaskForm):
newsletter = BooleanField(_l('Subscribe to email newsletter')) newsletter = BooleanField(_l('Subscribe to email newsletter'))
bot = BooleanField(_l('This profile is a bot'))
ignore_bots = BooleanField(_l('Hide posts by bots')) ignore_bots = BooleanField(_l('Hide posts by bots'))
nsfw = BooleanField(_l('Show NSFW posts')) nsfw = BooleanField(_l('Show NSFW posts'))
nsfl = BooleanField(_l('Show NSFL posts')) nsfl = BooleanField(_l('Show NSFL posts'))

View file

@ -5,10 +5,12 @@ from flask_login import login_user, logout_user, current_user, login_required
from flask_babel import _ from flask_babel import _
from app import db, cache from app import db, cache
from app.community.util import save_icon_file, save_banner_file
from app.models import Post, Community, CommunityMember, User, PostReply, PostVote, Notification, utcnow from app.models import Post, Community, CommunityMember, User, PostReply, PostVote, Notification, utcnow
from app.user import bp from app.user import bp
from app.user.forms import ProfileForm, SettingsForm from app.user.forms import ProfileForm, SettingsForm
from app.utils import get_setting, render_template, markdown_to_html, user_access, markdown_to_text, shorten_string from app.utils import get_setting, render_template, markdown_to_html, user_access, markdown_to_text, shorten_string, \
is_image_url
from sqlalchemy import desc, or_, text from sqlalchemy import desc, or_, text
@ -47,6 +49,17 @@ def edit_profile(actor):
if form.password_field.data.strip() != '': if form.password_field.data.strip() != '':
current_user.set_password(form.password_field.data) current_user.set_password(form.password_field.data)
current_user.about = form.about.data current_user.about = form.about.data
current_user.bot = form.bot.data
profile_file = request.files['profile_file']
if profile_file and profile_file.filename != '':
file = save_icon_file(profile_file)
if file:
current_user.avatar = file
banner_file = request.files['banner_file']
if banner_file and banner_file.filename != '':
file = save_banner_file(banner_file)
if file:
current_user.cover = file
current_user.flush_cache() current_user.flush_cache()
db.session.commit() db.session.commit()
@ -73,7 +86,6 @@ def change_settings(actor):
form = SettingsForm() form = SettingsForm()
if form.validate_on_submit(): if form.validate_on_submit():
current_user.newsletter = form.newsletter.data current_user.newsletter = form.newsletter.data
current_user.bot = form.bot.data
current_user.ignore_bots = form.ignore_bots.data current_user.ignore_bots = form.ignore_bots.data
current_user.show_nsfw = form.nsfw.data current_user.show_nsfw = form.nsfw.data
current_user.show_nsfl = form.nsfl.data current_user.show_nsfl = form.nsfl.data