From 493fcf3a580a706a7a9d1cb43e2fe9f762e4d1d3 Mon Sep 17 00:00:00 2001 From: Pierre Jaury Date: Sun, 1 May 2016 21:12:08 +0200 Subject: [PATCH] Use populate_obj to update objects --- admin/freeposte/admin/forms.py | 4 ++-- admin/freeposte/admin/views/aliases.py | 11 ++++------- admin/freeposte/admin/views/domains.py | 16 +++------------- admin/freeposte/admin/views/fetches.py | 14 ++------------ admin/freeposte/admin/views/users.py | 22 ++++++---------------- 5 files changed, 17 insertions(+), 50 deletions(-) diff --git a/admin/freeposte/admin/forms.py b/admin/freeposte/admin/forms.py index b1f22944..10477c6e 100644 --- a/admin/freeposte/admin/forms.py +++ b/admin/freeposte/admin/forms.py @@ -11,8 +11,8 @@ class LoginForm(Form): class DomainForm(Form): name = fields.StringField('Domain name', [validators.DataRequired()]) - max_users = fields_.DecimalField('Maximum user count', default=10) - max_aliases = fields_.DecimalField('Maximum alias count', default=10) + max_users = fields_.IntegerField('Maximum user count', default=10) + max_aliases = fields_.IntegerField('Maximum alias count', default=10) comment = fields.StringField('Comment') submit = fields.SubmitField('Create') diff --git a/admin/freeposte/admin/views/aliases.py b/admin/freeposte/admin/views/aliases.py index 11749507..4c684e98 100644 --- a/admin/freeposte/admin/views/aliases.py +++ b/admin/freeposte/admin/views/aliases.py @@ -24,12 +24,10 @@ def alias_create(domain_name): form = forms.AliasForm() if form.validate_on_submit(): if domain.has_email(form.localpart.data): - # TODO email is not declared - flask.flash('Email %s is already used' % email, 'error') + flask.flash('Email is already used', 'error') else: - alias = models.Alias(localpart=form.localpart.data, domain=domain) - alias.destination = form.destination.data - alias.comment = form.comment.data + alias = models.Alias(domain=domain) + form.populate_obj(alias) db.session.add(alias) db.session.commit() flask.flash('Alias %s created' % alias) @@ -46,8 +44,7 @@ def alias_edit(alias): form = forms.AliasForm(obj=alias) wtforms_components.read_only(form.localpart) if form.validate_on_submit(): - alias.destination = form.destination.data - alias.comment = form.comment.data + form.populate_obj(alias) db.session.add(alias) db.session.commit() flask.flash('Alias %s updated' % alias) diff --git a/admin/freeposte/admin/views/domains.py b/admin/freeposte/admin/views/domains.py index 2e4cbe94..34838d37 100644 --- a/admin/freeposte/admin/views/domains.py +++ b/admin/freeposte/admin/views/domains.py @@ -21,10 +21,8 @@ def domain_create(): if models.Domain.query.get(form.name.data): flask.flash('Domain %s is already used' % form.name.data, 'error') else: - domain = models.Domain(name=form.name.data) - domain.max_users = int(form.max_users.data) - domain.max_aliases = int(form.max_aliases.data) - domain.comment = form.comment.data + domain = models.Domain() + form.populate_obj(domain) db.session.add(domain) db.session.commit() flask.flash('Domain %s created' % domain) @@ -40,9 +38,7 @@ def domain_edit(domain_name): form = forms.DomainForm(obj=domain) wtforms_components.read_only(form.name) if form.validate_on_submit(): - domain.max_users = int(form.max_users.data) - domain.max_aliases = int(form.max_aliases.data) - domain.comment = form.comment.data + form.populate_obj(domain) db.session.add(domain) db.session.commit() flask.flash('Domain %s saved' % domain) @@ -60,9 +56,3 @@ def domain_delete(domain_name): db.session.commit() flask.flash('Domain %s deleted' % domain) return flask.redirect(flask.url_for('.domain_list')) - - -@app.route('/domain/admins/', methods=['GET']) -@flask_login.login_required -def domain_admins(domain_name): - domain = utils.get_domain_admin(domain_name) diff --git a/admin/freeposte/admin/views/fetches.py b/admin/freeposte/admin/views/fetches.py index 75aae6bb..f79c5696 100644 --- a/admin/freeposte/admin/views/fetches.py +++ b/admin/freeposte/admin/views/fetches.py @@ -22,12 +22,7 @@ def fetch_create(user_email): form = forms.FetchForm() if form.validate_on_submit(): fetch = models.Fetch(user=user) - fetch.protocol = form.protocol.data - 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 + form.populate_obj(fetch) db.session.add(fetch) db.session.commit() flask.flash('Fetch configuration created') @@ -42,12 +37,7 @@ def fetch_edit(fetch_id): fetch = utils.get_fetch(fetch_id) form = forms.FetchForm(obj=fetch) if form.validate_on_submit(): - fetch.protocol = form.protocol.data - 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 + form.populate_obj(fetch) db.session.add(fetch) db.session.commit() flask.flash('Fetch configuration updated') diff --git a/admin/freeposte/admin/views/users.py b/admin/freeposte/admin/views/users.py index 5f0ba750..db365146 100644 --- a/admin/freeposte/admin/views/users.py +++ b/admin/freeposte/admin/views/users.py @@ -24,14 +24,10 @@ def user_create(domain_name): form = forms.UserForm() if form.validate_on_submit(): if domain.has_email(form.localpart.data): - # TODO: email is not declared - flask.flash('Email %s is already used' % email, 'error') + flask.flash('Email is already used', 'error') else: - user = models.User(localpart=form.localpart.data, domain=domain) - user.comment = form.comment.data - user.quota_bytes = int(form.quota_bytes.data) - user.enable_imap = form.enable_imap.data - user.enable_pop = form.enable_pop.data + user = models.User(domain=domain) + form.populate_obj(user) user.set_password(form.pw.data) db.session.add(user) db.session.commit() @@ -50,10 +46,7 @@ def user_edit(user_email): wtforms_components.read_only(form.localpart) form.pw.validators = [] if form.validate_on_submit(): - user.comment = form.comment.data - user.quota_bytes = int(form.quota_bytes.data) - user.enable_imap = form.enable_imap.data - user.enable_pop = form.enable_pop.data + form.populate_obj(user) if form.pw.data: user.set_password(form.pw.data) db.session.add(user) @@ -82,9 +75,7 @@ def user_settings(user_email): user = utils.get_user(user_email) form = forms.UserSettingsForm(obj=user) if form.validate_on_submit(): - user.displayed_name = form.displayed_name.data - user.spam_enabled = form.spam_enabled.data - user.spam_threshold = form.spam_threshold.data + form.populate_obj(user) db.session.add(user) db.session.commit() flask.flash('Settings updated for %s' % user) @@ -141,8 +132,7 @@ def user_reply(user_email): user = utils.get_user(user_email) form = forms.UserReplyForm(obj=user) if form.validate_on_submit(): - user.reply_subject = form.reply_subject.data - user.reply_body = form.reply_body.data + form.populate_obj(user) db.session.add(user) db.session.commit() flask.flash('Auto-reply message updated for %s' % user)