Use populate_obj to update objects

master
Pierre Jaury 9 years ago
parent d3b13c2412
commit 493fcf3a58

@ -11,8 +11,8 @@ class LoginForm(Form):
class DomainForm(Form): class DomainForm(Form):
name = fields.StringField('Domain name', [validators.DataRequired()]) name = fields.StringField('Domain name', [validators.DataRequired()])
max_users = fields_.DecimalField('Maximum user count', default=10) max_users = fields_.IntegerField('Maximum user count', default=10)
max_aliases = fields_.DecimalField('Maximum alias count', default=10) max_aliases = fields_.IntegerField('Maximum alias count', default=10)
comment = fields.StringField('Comment') comment = fields.StringField('Comment')
submit = fields.SubmitField('Create') submit = fields.SubmitField('Create')

@ -24,12 +24,10 @@ def alias_create(domain_name):
form = forms.AliasForm() form = forms.AliasForm()
if form.validate_on_submit(): if form.validate_on_submit():
if domain.has_email(form.localpart.data): if domain.has_email(form.localpart.data):
# TODO email is not declared flask.flash('Email is already used', 'error')
flask.flash('Email %s is already used' % email, 'error')
else: else:
alias = models.Alias(localpart=form.localpart.data, domain=domain) alias = models.Alias(domain=domain)
alias.destination = form.destination.data form.populate_obj(alias)
alias.comment = form.comment.data
db.session.add(alias) db.session.add(alias)
db.session.commit() db.session.commit()
flask.flash('Alias %s created' % alias) flask.flash('Alias %s created' % alias)
@ -46,8 +44,7 @@ def alias_edit(alias):
form = forms.AliasForm(obj=alias) form = forms.AliasForm(obj=alias)
wtforms_components.read_only(form.localpart) wtforms_components.read_only(form.localpart)
if form.validate_on_submit(): if form.validate_on_submit():
alias.destination = form.destination.data form.populate_obj(alias)
alias.comment = form.comment.data
db.session.add(alias) db.session.add(alias)
db.session.commit() db.session.commit()
flask.flash('Alias %s updated' % alias) flask.flash('Alias %s updated' % alias)

@ -21,10 +21,8 @@ def domain_create():
if models.Domain.query.get(form.name.data): if models.Domain.query.get(form.name.data):
flask.flash('Domain %s is already used' % form.name.data, 'error') flask.flash('Domain %s is already used' % form.name.data, 'error')
else: else:
domain = models.Domain(name=form.name.data) domain = models.Domain()
domain.max_users = int(form.max_users.data) form.populate_obj(domain)
domain.max_aliases = int(form.max_aliases.data)
domain.comment = form.comment.data
db.session.add(domain) db.session.add(domain)
db.session.commit() db.session.commit()
flask.flash('Domain %s created' % domain) flask.flash('Domain %s created' % domain)
@ -40,9 +38,7 @@ def domain_edit(domain_name):
form = forms.DomainForm(obj=domain) form = forms.DomainForm(obj=domain)
wtforms_components.read_only(form.name) wtforms_components.read_only(form.name)
if form.validate_on_submit(): if form.validate_on_submit():
domain.max_users = int(form.max_users.data) form.populate_obj(domain)
domain.max_aliases = int(form.max_aliases.data)
domain.comment = form.comment.data
db.session.add(domain) db.session.add(domain)
db.session.commit() db.session.commit()
flask.flash('Domain %s saved' % domain) flask.flash('Domain %s saved' % domain)
@ -60,9 +56,3 @@ def domain_delete(domain_name):
db.session.commit() db.session.commit()
flask.flash('Domain %s deleted' % domain) flask.flash('Domain %s deleted' % domain)
return flask.redirect(flask.url_for('.domain_list')) return flask.redirect(flask.url_for('.domain_list'))
@app.route('/domain/admins/<domain_name>', methods=['GET'])
@flask_login.login_required
def domain_admins(domain_name):
domain = utils.get_domain_admin(domain_name)

@ -22,12 +22,7 @@ def fetch_create(user_email):
form = forms.FetchForm() form = forms.FetchForm()
if form.validate_on_submit(): if form.validate_on_submit():
fetch = models.Fetch(user=user) fetch = models.Fetch(user=user)
fetch.protocol = form.protocol.data form.populate_obj(fetch)
fetch.host = form.host.data
fetch.port = form.port.data
fetch.tls = form.tls.data
fetch.username = form.username.data
fetch.password = form.password.data
db.session.add(fetch) db.session.add(fetch)
db.session.commit() db.session.commit()
flask.flash('Fetch configuration created') flask.flash('Fetch configuration created')
@ -42,12 +37,7 @@ def fetch_edit(fetch_id):
fetch = utils.get_fetch(fetch_id) fetch = utils.get_fetch(fetch_id)
form = forms.FetchForm(obj=fetch) form = forms.FetchForm(obj=fetch)
if form.validate_on_submit(): if form.validate_on_submit():
fetch.protocol = form.protocol.data form.populate_obj(fetch)
fetch.host = form.host.data
fetch.port = form.port.data
fetch.tls = form.tls.data
fetch.username = form.username.data
fetch.password = form.password.data
db.session.add(fetch) db.session.add(fetch)
db.session.commit() db.session.commit()
flask.flash('Fetch configuration updated') flask.flash('Fetch configuration updated')

@ -24,14 +24,10 @@ def user_create(domain_name):
form = forms.UserForm() form = forms.UserForm()
if form.validate_on_submit(): if form.validate_on_submit():
if domain.has_email(form.localpart.data): if domain.has_email(form.localpart.data):
# TODO: email is not declared flask.flash('Email is already used', 'error')
flask.flash('Email %s is already used' % email, 'error')
else: else:
user = models.User(localpart=form.localpart.data, domain=domain) user = models.User(domain=domain)
user.comment = form.comment.data form.populate_obj(user)
user.quota_bytes = int(form.quota_bytes.data)
user.enable_imap = form.enable_imap.data
user.enable_pop = form.enable_pop.data
user.set_password(form.pw.data) user.set_password(form.pw.data)
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
@ -50,10 +46,7 @@ def user_edit(user_email):
wtforms_components.read_only(form.localpart) wtforms_components.read_only(form.localpart)
form.pw.validators = [] form.pw.validators = []
if form.validate_on_submit(): if form.validate_on_submit():
user.comment = form.comment.data form.populate_obj(user)
user.quota_bytes = int(form.quota_bytes.data)
user.enable_imap = form.enable_imap.data
user.enable_pop = form.enable_pop.data
if form.pw.data: if form.pw.data:
user.set_password(form.pw.data) user.set_password(form.pw.data)
db.session.add(user) db.session.add(user)
@ -82,9 +75,7 @@ def user_settings(user_email):
user = utils.get_user(user_email) user = utils.get_user(user_email)
form = forms.UserSettingsForm(obj=user) form = forms.UserSettingsForm(obj=user)
if form.validate_on_submit(): if form.validate_on_submit():
user.displayed_name = form.displayed_name.data form.populate_obj(user)
user.spam_enabled = form.spam_enabled.data
user.spam_threshold = form.spam_threshold.data
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
flask.flash('Settings updated for %s' % user) flask.flash('Settings updated for %s' % user)
@ -141,8 +132,7 @@ def user_reply(user_email):
user = utils.get_user(user_email) user = utils.get_user(user_email)
form = forms.UserReplyForm(obj=user) form = forms.UserReplyForm(obj=user)
if form.validate_on_submit(): if form.validate_on_submit():
user.reply_subject = form.reply_subject.data form.populate_obj(user)
user.reply_body = form.reply_body.data
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
flask.flash('Auto-reply message updated for %s' % user) flask.flash('Auto-reply message updated for %s' % user)

Loading…
Cancel
Save