2018-10-18 15:57:43 +02:00
|
|
|
from mailu import models
|
2017-09-24 12:00:39 +02:00
|
|
|
from mailu.ui import ui, forms, access
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
import flask
|
2016-03-22 21:45:30 +02:00
|
|
|
import wtforms_components
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/alias/list/<domain_name>', methods=['GET'])
|
2016-08-28 15:23:57 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-03-19 21:37:48 +02:00
|
|
|
def alias_list(domain_name):
|
2016-08-28 15:23:57 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('alias/list.html', domain=domain)
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/alias/create/<domain_name>', methods=['GET', 'POST'])
|
2016-08-28 15:23:57 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-03-19 21:37:48 +02:00
|
|
|
def alias_create(domain_name):
|
2016-08-28 15:23:57 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2019-01-05 14:45:55 +02:00
|
|
|
if not domain.max_aliases == -1 and len(domain.aliases) >= domain.max_aliases:
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Too many aliases for domain %s' % domain, 'error')
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(
|
|
|
|
flask.url_for('.alias_list', domain_name=domain.name))
|
2016-03-22 21:45:30 +02:00
|
|
|
form = forms.AliasForm()
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2016-05-01 20:04:40 +02:00
|
|
|
if domain.has_email(form.localpart.data):
|
2016-05-01 21:12:08 +02:00
|
|
|
flask.flash('Email is already used', 'error')
|
2016-03-19 21:37:48 +02:00
|
|
|
else:
|
2016-05-01 21:12:08 +02:00
|
|
|
alias = models.Alias(domain=domain)
|
|
|
|
form.populate_obj(alias)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.add(alias)
|
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Alias %s created' % alias)
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.alias_list', domain_name=domain.name))
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('alias/create.html',
|
|
|
|
domain=domain, form=form)
|
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@ui.route('/alias/edit/<path:alias>', methods=['GET', 'POST'])
|
2016-08-28 15:23:57 +02:00
|
|
|
@access.domain_admin(models.Alias, 'alias')
|
2016-03-19 21:37:48 +02:00
|
|
|
def alias_edit(alias):
|
2016-08-28 15:23:57 +02:00
|
|
|
alias = models.Alias.query.get(alias) or flask.abort(404)
|
2016-03-22 21:45:30 +02:00
|
|
|
form = forms.AliasForm(obj=alias)
|
|
|
|
wtforms_components.read_only(form.localpart)
|
2017-01-25 01:05:03 +02:00
|
|
|
form.localpart.validators = []
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2016-05-01 21:12:08 +02:00
|
|
|
form.populate_obj(alias)
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Alias %s updated' % alias)
|
|
|
|
return flask.redirect(
|
2016-03-20 16:36:56 +02:00
|
|
|
flask.url_for('.alias_list', domain_name=alias.domain.name))
|
2016-03-22 21:45:30 +02:00
|
|
|
return flask.render_template('alias/edit.html',
|
|
|
|
form=form, alias=alias, domain=alias.domain)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@ui.route('/alias/delete/<path:alias>', methods=['GET', 'POST'])
|
2016-08-28 15:23:57 +02:00
|
|
|
@access.domain_admin(models.Alias, 'alias')
|
|
|
|
@access.confirmation_required("delete {alias}")
|
2016-03-19 21:37:48 +02:00
|
|
|
def alias_delete(alias):
|
2016-08-28 15:23:57 +02:00
|
|
|
alias = models.Alias.query.get(alias) or flask.abort(404)
|
2016-11-12 16:47:48 +02:00
|
|
|
domain = alias.domain
|
2018-10-18 15:57:43 +02:00
|
|
|
models.db.session.delete(alias)
|
|
|
|
models.db.session.commit()
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Alias %s deleted' % alias)
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(
|
2016-11-12 16:47:48 +02:00
|
|
|
flask.url_for('.alias_list', domain_name=domain.name))
|