def settings_profile(): """Allows users to customize their profile direct from this view.""" # Create the form and initialize the `select` field this can not be done # in the template. form = ChangeProfileForm( feed_pagination_size=(current_user.get('feed_pagination_size') or app.config.get('FEED_ITEMS_PER_PAGE')), replies_pagination_size=(current_user.get('replies_pagination_size') or app.config.get('REPLIES_ITEMS_PER_PAGE')), alerts_pagination_size=(current_user.get('alerts_pagination_size') or app.config.get('ALERT_ITEMS_PER_PAGE')), homepage=current_user.get('homepage', ''), location=current_user.get('location', '') ) if request.method == 'POST': form = ChangeProfileForm() if form.validate(): # If there is an uploaded File pass it on else pass nothing if form.upload.data: # Pass the BytesIO stream to the backend. upload = form.upload.data.stream else: upload = None # Update the current user in the session current_user['about'] = form.about.data current_user['hide_feed_images'] = form.hide_feed_images.data current_user['feed_pagination_size'] = \ int(form.feed_pagination_size.data) current_user['replies_pagination_size'] = \ int(form.replies_pagination_size.data) current_user['alerts_pagination_size'] = \ int(form.alerts_pagination_size.data) current_user['homepage'] = form.homepage.data current_user['location'] = form.location.data # Update the user in the database user = update_profile_settings( current_user.get('_id'), about=form.about.data, hide_feed_images=form.hide_feed_images.data, feed_size=form.feed_pagination_size.data, replies_size=form.replies_pagination_size.data, alerts_size=form.alerts_pagination_size.data, homepage=form.homepage.data, location=form.location.data, upload=upload ) # Reload the current_user current_user.update(user) flash('Your profile has been updated', 'success') else: flash('Oh no! There are errors in your form', 'error') return render_template('settings_profile.html', form=form)
def settings_profile(): """Allows users to customize their profile direct from this view.""" # Create the form and initialize the `select` field this can not be done # in the template. form = ChangeProfileForm( feed_pagination_size=(current_user.get('feed_pagination_size') or app.config.get('FEED_ITEMS_PER_PAGE')), replies_pagination_size=(current_user.get('replies_pagination_size') or app.config.get('REPLIES_ITEMS_PER_PAGE')), alerts_pagination_size=(current_user.get('alerts_pagination_size') or app.config.get('ALERT_ITEMS_PER_PAGE')), homepage=current_user.get('homepage', ''), location=current_user.get('location', '')) if request.method == 'POST': form = ChangeProfileForm() if form.validate(): # If there is an uploaded File pass it on else pass nothing if form.upload.data: # Pass the BytesIO stream to the backend. upload = form.upload.data.stream else: upload = None # Update the current user in the session current_user['about'] = form.about.data current_user['hide_feed_images'] = form.hide_feed_images.data current_user['feed_pagination_size'] = \ int(form.feed_pagination_size.data) current_user['replies_pagination_size'] = \ int(form.replies_pagination_size.data) current_user['alerts_pagination_size'] = \ int(form.alerts_pagination_size.data) # Sort order but be 1 or -1 due to MongoDB requirements if form.reply_sort_order.data: reply_sort_order = 1 else: reply_sort_order = -1 current_user['reply_sort_order'] = reply_sort_order current_user['homepage'] = form.homepage.data current_user['location'] = form.location.data current_user['default_permission'] = int(form.permission.data) # Update the user in the database user = update_profile_settings( current_user.get('_id'), about=form.about.data, hide_feed_images=form.hide_feed_images.data, feed_size=form.feed_pagination_size.data, replies_size=form.replies_pagination_size.data, alerts_size=form.alerts_pagination_size.data, reply_sort_order=reply_sort_order, homepage=form.homepage.data, location=form.location.data, upload=upload, permission=form.permission.data) # Reload the current_user current_user.update(user) flash('Your profile has been updated', 'success') else: flash('Oh no! There are errors in your form', 'error') return render_template('settings_profile.html', form=form)