def user_edit(user_email): user = models.User.query.get(user_email) or flask.abort(404) # Handle the case where user quota is more than allowed max_quota_bytes = user.domain.max_quota_bytes if max_quota_bytes and user.quota_bytes > max_quota_bytes: max_quota_bytes = user.quota_bytes # Create the form form = forms.UserForm(obj=user) wtforms_components.read_only(form.localpart) form.localpart.validators = [] if max_quota_bytes: form.quota_bytes.validators = [ wtforms.validators.NumberRange(max=max_quota_bytes) ] if form.validate_on_submit(): form.populate_obj(user) if form.pw.data: user.set_password(form.pw.data) models.db.session.commit() flask.flash('User %s updated' % user) return flask.redirect( flask.url_for('.user_list', domain_name=user.domain.name)) return flask.render_template('user/edit.html', form=form, user=user, domain=user.domain, max_quota_bytes=max_quota_bytes)
def user_create(domain_name): domain = models.Domain.query.get(domain_name) or flask.abort(404) if not domain.max_users == -1 and len(domain.users) >= domain.max_users: flask.flash('Too many users for domain %s' % domain, 'error') return flask.redirect( flask.url_for('.user_list', domain_name=domain.name)) form = forms.UserForm() form.pw.validators = [wtforms.validators.DataRequired()] if domain.max_quota_bytes: form.quota_bytes.validators = [ wtforms.validators.NumberRange(max=domain.max_quota_bytes) ] if form.validate_on_submit(): if domain.has_email(form.localpart.data): flask.flash('Email is already used', 'error') else: user = models.User(domain=domain) form.populate_obj(user) user.set_password(form.pw.data) models.db.session.add(user) models.db.session.commit() user.send_welcome() flask.flash('User %s created' % user) return flask.redirect( flask.url_for('.user_list', domain_name=domain.name)) return flask.render_template('user/create.html', domain=domain, form=form)