Skip to content

Commit

Permalink
added email attribute for users
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricbonhomme committed Apr 7, 2020
1 parent 83e8ff8 commit 4bb0433
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
30 changes: 30 additions & 0 deletions migrations/versions/029c7f524121_added_email_property_for_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""added email property for user
Revision ID: 029c7f524121
Revises: 863c39a3dc7e
Create Date: 2020-04-07 10:33:31.459049
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '029c7f524121'
down_revision = '863c39a3dc7e'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('email', sa.String(length=256), nullable=True))
op.get_bind().execute('UPDATE "user" SET email=%s WHERE email IS NULL;', ('[email protected]'))
op.alter_column('user', 'email', nullable=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('user', 'email')
# ### end Alembic commands ###
8 changes: 8 additions & 0 deletions mosp/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask_login import UserMixin
from sqlalchemy.orm import validates
from werkzeug.security import check_password_hash
from validate_email import validate_email

from mosp.bootstrap import db

Expand All @@ -23,6 +24,7 @@ class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(30), unique=True, nullable=False)
pwdhash = db.Column(db.String(), nullable=False)
email = db.Column(db.String(256), nullable=False)
created_at = db.Column(db.DateTime(), default=datetime.utcnow)
last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
apikey = db.Column(db.String(), default=secrets.token_urlsafe(50))
Expand Down Expand Up @@ -69,3 +71,9 @@ def __str__(self):
def validates_login(self, key, value):
assert 3 <= len(value) <= 30, AssertionError("maximum length for login: 30")
return re.sub("[^a-zA-Z0-9_.]", "", value.strip())

@validates("email")
def validates_email(self, key, value):
assert 3 <= len(value) <= 30, AssertionError("maximum length for email: 256")
if validate_email(value):
return value
5 changes: 5 additions & 0 deletions mosp/templates/admin/edit_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ <h2>{{ action | safe }}</h2>
{{ form.password(class_="form-control") }} {% for error in form.password.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
</div>

<div class="form-group">
{{ form.email.label }}
{{ form.email(class_="form-control") }} {% for error in form.email.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
</div>

<div class="form-check form-check-inline">
{{ form.public_profile(class_="form-check-input") }} {% for error in form.public_profile.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
{{ form.public_profile.label(class_="form-check-label") }}
Expand Down
3 changes: 3 additions & 0 deletions mosp/templates/edit_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ <h2>{{ action | safe }}</h2>
{{ form.login.label }}
{{ form.login(class_="form-control") }} {% for error in form.login.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}

{{ form.email.label }}
{{ form.email(class_="form-control") }} {% for error in form.email.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}

{{ form.password.label }}
{{ form.password(class_="form-control") }} {% for error in form.password.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}

Expand Down
6 changes: 5 additions & 1 deletion mosp/web/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
SubmitField,
validators,
HiddenField,
SelectMultipleField,
SelectMultipleField
)
from wtforms.fields.html5 import EmailField
from wtforms.validators import Email, InputRequired
from werkzeug.exceptions import NotFound, HTTPException
from flask_babel import lazy_gettext

Expand Down Expand Up @@ -164,6 +166,7 @@ class UserForm(FlaskForm):
],
)
password = PasswordField(lazy_gettext("Password"))
email = EmailField("Email", [InputRequired("Please enter your email address."), Email("Please enter your email address.")])
public_profile = BooleanField(lazy_gettext("Public profile"), default=True)
is_active = BooleanField(lazy_gettext("Active"), default=True)
is_admin = BooleanField(lazy_gettext("Admin"), default=False)
Expand Down Expand Up @@ -218,5 +221,6 @@ class ProfileForm(FlaskForm):
],
)
password = PasswordField(lazy_gettext("Password"))
email = EmailField("Email", [InputRequired("Please enter your email address."), Email("Please enter your email address.")])
public_profile = BooleanField(lazy_gettext("Public profile"), default=True)
submit = SubmitField(lazy_gettext("Save"))
1 change: 1 addition & 0 deletions mosp/web/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def process_user_form(user_id=None):
# Create a new user
new_user = User(
login=form.login.data,
email=form.email.data,
public_profile=form.public_profile.data,
is_active=form.is_active.data,
is_admin=form.is_admin.data,
Expand Down

0 comments on commit 4bb0433

Please sign in to comment.