Improve forms for user creation an deletion

master
Pierre Jaury 9 years ago
parent 8d6a4cb654
commit 398e7c1183

@ -22,17 +22,13 @@ class DomainEditForm(Form):
submit = fields.SubmitField('Save') submit = fields.SubmitField('Save')
class UserCreateForm(Form): class UserForm(Form):
localpart = fields.StringField('E-mail', [validators.DataRequired()]) localpart = fields.StringField('E-mail', [validators.DataRequired()])
pw = fields.PasswordField('Password', [validators.DataRequired()]) pw = fields.PasswordField('Password', [validators.DataRequired()])
pw2 = fields.PasswordField('Confirm password', [validators.EqualTo('pw')])
quota_bytes = fields_.DecimalSliderField('Quota', default=1000000000)
comment = fields.StringField('Comment') comment = fields.StringField('Comment')
submit = fields.SubmitField('Create') submit = fields.SubmitField('Save')
class UserEditForm(Form):
quota_bytes = fields.IntegerField('Quota')
comment = fields.StringField('Comment')
submit = fields.SubmitField('Create')
class UserSettingsForm(Form): class UserSettingsForm(Form):

@ -39,6 +39,13 @@ class Domain(Base):
max_users = db.Column(db.Integer, nullable=True) max_users = db.Column(db.Integer, nullable=True)
max_aliases = db.Column(db.Integer, nullable=True) max_aliases = db.Column(db.Integer, nullable=True)
def has_address(self, localpart):
for address in self.users + self.aliases:
if address.localpart == localpart:
return True
else:
return False
def __str__(self): def __str__(self):
return self.name return self.name

@ -1,5 +1,5 @@
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/utils.html" as utils %} {% import "bootstrap/utils.html" as utils %}
{% import "macros.html" as macros %}
{% extends "bootstrap/base.html" %} {% extends "bootstrap/base.html" %}
{% block styles %} {% block styles %}

@ -18,7 +18,7 @@ Domain list
<th>Domain name</th> <th>Domain name</th>
<th>Mailbox count</th> <th>Mailbox count</th>
<th>Alias count</th> <th>Alias count</th>
<th>Comemnt</th> <th>Comment</th>
<th>Created</th> <th>Created</th>
<th>Last edit</th> <th>Last edit</th>
</tr> </tr>

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block box_content %} {% block box_content %}
{{ wtf.quick_form(form) }} {{ macros.form(form) }}
{% endblock %} {% endblock %}

@ -0,0 +1,57 @@
{% macro form_errors(form) %}
{% if form.errors %}
{% for fieldname, errors in form.errors.items() %}
{% if bootstrap_is_hidden_field(form[fieldname]) %}
{% for error in errors %}
<p class="error">{{error}}</p>
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endmacro %}
{% macro form_fields(fields, prepend='', append='', label=True) %}
{% set width = (12 / fields|length)|int %}
<div class="form-group">
<div class="row">
{% for field in fields %}
<div class="col-lg-{{ width }} col-xs-12 {{ 'has-error' if field.errors else '' }}">
{{ field.label if label else '' }}
{% if field.errors %}
{% for error in field.errors %}
<p class="help-block inline">{{ error }}</p>
{% endfor %}
{% endif %}
{% if prepend or append %}<div class="input-group">{% endif %}
{{ prepend|safe }}
{{ field(class_="form-control", **kwargs) }}
{{ append|safe }}
{% if prepend or append %}</div>{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endmacro %}
{% macro form_field(field) %}
{% if field.type == 'SubmitField' %}
{{ form_fields((field,), label=False, class="btn btn-default", **kwargs) }}
{% elif field.type == 'HiddenField' %}
{{ form_fields((field,), label=False, **kwargs) }}
{% else %}
{{ form_fields((field,), **kwargs) }}
{% endif %}
{% endmacro %}
{% macro form(form) %}
<form class="form" method="post" role="form">
{{ form.hidden_tag() }}
{% for field in form %}
{% if bootstrap_is_hidden_field(field) %}
{{ field() }}
{% else %}
{{ form_field(field) }}
{% endif %}
{% endfor %}
</form>
{% endmacro %}

@ -9,5 +9,14 @@ New user
{% endblock %} {% endblock %}
{% block box_content %} {% block box_content %}
{{ wtf.quick_form(form) }} <form class="form" method="post" role="form">
{{ form.hidden_tag() }}
{{ macros.form_field(form.localpart, append='<span class="input-group-addon">@'+domain.name+'</span>') }}
{{ macros.form_fields((form.pw, form.pw2)) }}
{{ macros.form_field(form.quota_bytes, step=1000000000, max=50000000000,
prepend='<span class="input-group-addon"><span id="quota">'+(form.quota_bytes.data//1000000000).__str__()+'</span> GiB</span>',
oninput='$("#quota").text(this.value/1000000000);') }}
{{ macros.form_field(form.comment) }}
{{ macros.form_field(form.submit) }}
</form>
{% endblock %} {% endblock %}

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

@ -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('/user/list/<domain_name>', methods=['GET']) @app.route('/user/list/<domain_name>', methods=['GET'])
@ -20,15 +21,14 @@ def user_create(domain_name):
flask.flash('Too many users for domain %s' % domain, 'error') flask.flash('Too many users for domain %s' % domain, 'error')
return flask.redirect( return flask.redirect(
flask.url_for('.user_list', domain_name=domain.name)) flask.url_for('.user_list', domain_name=domain.name))
form = forms.UserCreateForm() form = forms.UserForm()
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:
user = models.User(localpart=form.localpart.data, domain=domain) user = models.User(localpart=form.localpart.data, domain=domain)
user.comment = form.comment.data user.comment = form.comment.data
user.quota_bytes = int(form.quota_bytes.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()
@ -43,16 +43,18 @@ def user_create(domain_name):
@flask_login.login_required @flask_login.login_required
def user_edit(user_email): def user_edit(user_email):
user = utils.get_user(user_email, True) user = utils.get_user(user_email, True)
form = forms.UserEditForm(obj=user) form = forms.UserForm(obj=user)
wtforms_components.read_only(form.localpart)
form.pw.validators = []
if form.validate_on_submit(): if form.validate_on_submit():
user.quota_bytes = form.quota_bytes.data user.quota_bytes = int(form.quota_bytes.data)
user.comment = form.comment.data user.comment = form.comment.data
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
flask.flash('User %s updated' % user) flask.flash('User %s updated' % user)
return flask.redirect( return flask.redirect(
flask.url_for('.user_list', domain_name=user.domain.name)) flask.url_for('.user_list', domain_name=user.domain.name))
return flask.render_template('user/edit.html', form=form, user=user) return flask.render_template('user/edit.html', form=form, user=user, domain=user.domain)
@app.route('/user/delete/<user_email>', methods=['GET']) @app.route('/user/delete/<user_email>', methods=['GET'])

Loading…
Cancel
Save