diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 36b3f91f..b31f22f9 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -12,7 +12,7 @@ from wtforms import ( from ereuse_devicehub.db import db from ereuse_devicehub.resources.agent.models import Person -from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.resources.user.models import User, UserValidation class LoginForm(FlaskForm): @@ -152,11 +152,20 @@ class UserNewRegisterForm(FlaskForm): return True def save(self, commit=True): - user = User(email=self.email.data, password=self.password.data) + user = User(email=self.email.data, password=self.password.data, active=False) + person = Person( email=self.email.data, name=self.name.data, telephone=self.telephone.data ) + user.individuals.add(person) db.session.add(user) + + user_validation = UserValidation( + user=user, + ) + self._token = user_validation.token + db.session.add(user_validation) + if commit: db.session.commit() diff --git a/ereuse_devicehub/migrations/versions/abba37ff5c80_user_validation.py b/ereuse_devicehub/migrations/versions/abba37ff5c80_user_validation.py new file mode 100644 index 00000000..223a9634 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/abba37ff5c80_user_validation.py @@ -0,0 +1,60 @@ +"""user validation + +Revision ID: abba37ff5c80 +Revises: e919fe0611ff +Create Date: 2022-09-30 10:01:19.761864 + +""" +import sqlalchemy as sa +from alembic import context, op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = 'abba37ff5c80' +down_revision = 'e919fe0611ff' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + + +def upgrade(): + op.create_table( + 'user_validation', + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column( + 'updated', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column( + 'created', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column('joined_at', sa.TIMESTAMP(timezone=True), nullable=True), + sa.Column('expired', sa.BigInteger(), nullable=False), + sa.Column('token', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.ForeignKeyConstraint( + ['user_id'], + ['common.user.id'], + ), + sa.UniqueConstraint('token'), + schema=f'{get_inv()}', + ) + + op.execute(f"CREATE SEQUENCE {get_inv()}.user_validation_seq;") + + +def downgrade(): + op.drop_table('user_validation', schema=f'{get_inv()}') + op.execute(f"DROP SEQUENCE {get_inv()}.user_validation_seq;") diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index 5eadb21d..a65554ee 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -119,3 +119,19 @@ class Session(Thing): def __str__(self) -> str: return '{0.token}'.format(self) + + +class UserValidation(Thing): + id = Column(BigInteger, Sequence('user_validation_seq'), primary_key=True) + expired = Column(BigInteger, default=0) + joined_at = db.Column(db.DateTime()) + token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False) + user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id)) + user = db.relationship( + User, + backref=db.backref('user_validation', lazy=True, collection_class=set), + collection_class=set, + ) + + def __str__(self) -> str: + return '{0.token}'.format(self) diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html b/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html index 92c07c23..5a1babcc 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_registration.html @@ -23,6 +23,7 @@
Enter an Email & password for to do a new register.
{% if form.form_errors %}@@ -71,6 +72,14 @@
Don't have account? Create an account