Use a single form for both creating and updating aliases

master
Pierre Jaury 9 years ago
parent 19707ae3b3
commit 49b33aba88

@ -0,0 +1 @@
- Blueprint pour le routage et les permissions par route

@ -55,14 +55,8 @@ class UserReplyForm(Form):
submit = fields.SubmitField('Update') submit = fields.SubmitField('Update')
class AliasCreateForm(Form): class AliasForm(Form):
localpart = fields.StringField('Alias', [validators.DataRequired()]) localpart = fields.StringField('Alias', [validators.DataRequired()])
destination = fields.StringField('Destination') destination = fields.StringField('Destination')
comment = fields.StringField('Comment') comment = fields.StringField('Comment')
submit = fields.SubmitField('Create') submit = fields.SubmitField('Create')
class AliasEditForm(Form):
destination = fields.StringField('Destination', widget=widgets.TextArea())
comment = fields.StringField('Comment')
submit = fields.SubmitField('Create')

@ -17,8 +17,10 @@ Create alias
{{ macros.form_field(form.submit) }} {{ macros.form_field(form.submit) }}
<script> <script>
$("#destination").tagsinput({ $("#destination").tagsinput({
confirmKeys: [13, 32], confirmKeys: [9, 13, 32],
tagClass: 'label label-primary large',
typeahead: { typeahead: {
afterSelect: function(val) { this.$element.val(""); },
source: {{ current_user.get_managed_addresses()|map('string')|list|tojson }} source: {{ current_user.get_managed_addresses()|map('string')|list|tojson }}
} }
}); });

@ -1,4 +1,4 @@
{% extends "form.html" %} {% extends "alias/create.html" %}
{% block title %} {% block title %}
Edit alias Edit alias

@ -3,6 +3,7 @@ from flask.ext import login as flask_login
import os import os
import flask import flask
import wtforms_components
@app.route('/alias/list/<domain_name>', methods=['GET']) @app.route('/alias/list/<domain_name>', methods=['GET'])
@ -20,12 +21,10 @@ def alias_create(domain_name):
flask.flash('Too many aliases for domain %s' % domain, 'error') flask.flash('Too many aliases for domain %s' % domain, 'error')
return flask.redirect( return flask.redirect(
flask.url_for('.alias_list', domain_name=domain.name)) flask.url_for('.alias_list', domain_name=domain.name))
form = forms.AliasCreateForm() form = forms.AliasForm()
if form.validate_on_submit(): if form.validate_on_submit():
for address in domain.users + domain.aliases: if domain.has_address(form.localpart.data):
if address.localpart == form.localpart.data:
flask.flash('Address %s is already used' % address, 'error') flask.flash('Address %s is already used' % address, 'error')
break
else: else:
alias = models.Alias(localpart=form.localpart.data, domain=domain) alias = models.Alias(localpart=form.localpart.data, domain=domain)
alias.destination = form.destination.data alias.destination = form.destination.data
@ -43,7 +42,8 @@ def alias_create(domain_name):
@flask_login.login_required @flask_login.login_required
def alias_edit(alias): def alias_edit(alias):
alias = utils.get_alias(alias) alias = utils.get_alias(alias)
form = forms.AliasEditForm(obj=alias) form = forms.AliasForm(obj=alias)
wtforms_components.read_only(form.localpart)
if form.validate_on_submit(): if form.validate_on_submit():
alias.destination = form.destination.data alias.destination = form.destination.data
alias.comment = form.comment.data alias.comment = form.comment.data
@ -52,7 +52,8 @@ def alias_edit(alias):
flask.flash('Alias %s updated' % alias) flask.flash('Alias %s updated' % alias)
return flask.redirect( return flask.redirect(
flask.url_for('.alias_list', domain_name=alias.domain.name)) flask.url_for('.alias_list', domain_name=alias.domain.name))
return flask.render_template('alias/edit.html', form=form, alias=alias) return flask.render_template('alias/edit.html',
form=form, alias=alias, domain=alias.domain)
@app.route('/alias/delete/<alias>', methods=['GET']) @app.route('/alias/delete/<alias>', methods=['GET'])

Loading…
Cancel
Save