fix migrations

This commit is contained in:
Cayo Puigdefabregas 2022-09-30 17:54:26 +02:00
parent d36008934a
commit 6357a15f01
5 changed files with 96 additions and 4 deletions

View File

@ -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()

View File

@ -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;")

View File

@ -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)

View File

@ -23,6 +23,7 @@
<div class="pt-4 pb-2">
<h5 class="card-title text-center pb-0 fs-4">Register as a new User</h5>
{% if not form._token %}
<p class="text-center small">Enter an Email & password for to do a new register.</p>
{% if form.form_errors %}
<p class="text-danger">
@ -71,6 +72,14 @@
<p class="small mb-0">Don't have account? <a href="#TODO" onclick="$('#exampleModal').modal('show')" data-toggle="modal" data-target="#exampleModal">Create an account</a></p>
</div>
</form>
{% else %}
<div class="col-12">
We have sent you a validation email. Please check your email.
</div>
<div class="col-12">
{{ form._token }}
</div>
{% endif %}
</div>
</div>

View File

@ -116,8 +116,6 @@ class UserRegistrationView(View):
form = UserNewRegisterForm()
if form.validate_on_submit():
form.save()
messages.success('User created successfully!')
return flask.redirect(flask.url_for('core.login'))
context = {'form': form, 'version': __version__}
return flask.render_template(self.template_name, **context)