add model, form and template of sanitization entity

This commit is contained in:
Cayo Puigdefabregas 2023-02-13 20:35:31 +01:00
parent c58dc367e6
commit 8cd7777fc6
5 changed files with 165 additions and 4 deletions

View File

@ -1,10 +1,10 @@
from flask import g
from flask_wtf import FlaskForm
from werkzeug.security import generate_password_hash
from wtforms import BooleanField, EmailField, PasswordField, validators
from wtforms import BooleanField, EmailField, PasswordField, StringField, validators
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.user.models import SanitizationEntity, User
class LoginForm(FlaskForm):
@ -101,3 +101,35 @@ class PasswordForm(FlaskForm):
if commit:
db.session.commit()
return
class SanitizationEntityForm(FlaskForm):
logo = StringField('Logo', render_kw={'class': "form-control"})
company_name = StringField('Company Name', render_kw={'class': "form-control"})
location = StringField('Location', render_kw={'class': "form-control"})
responsable_person = StringField(
'Responsable person', render_kw={'class': "form-control"}
)
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
return False
return True
def save(self, commit=True):
sanitation_data = SanitizationEntity(
logo=self.logo.data,
company_name=self.company_name.data,
location=self.location.data,
responsable_person=self.responsable_person.data,
user=g.user,
)
db.session.add(sanitation_data)
if commit:
db.session.commit()
return

View File

@ -0,0 +1,57 @@
"""sanitization
Revision ID: 4f33137586dd
Revises: 93daff872771
Create Date: 2023-02-13 18:01:00.092527
"""
import sqlalchemy as sa
from alembic import context, op
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '4f33137586dd'
down_revision = '93daff872771'
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(
'sanitization_entity',
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('company_name', sa.String(), nullable=True),
sa.Column('logo', sa.String(), nullable=True),
sa.Column('responsable_person', sa.String(), nullable=True),
sa.Column('location', sa.String(), nullable=True),
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.ForeignKeyConstraint(
['user_id'],
['common.user.id'],
),
schema=f'{get_inv()}',
)
def downgrade():
op.drop_table('sanitization_entity', schema=f'{get_inv()}')

View File

@ -119,3 +119,20 @@ class Session(Thing):
def __str__(self) -> str:
return '{0.token}'.format(self)
class SanitizationEntity(Thing):
id = Column(BigInteger, primary_key=True)
company_name = Column(db.String, nullable=True)
location = Column(db.String, nullable=True)
logo = Column(db.String, nullable=True)
responsable_person = Column(db.String, nullable=True)
user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id))
user = db.relationship(
User,
backref=db.backref('sanitization_entity', lazy=True, collection_class=set),
collection_class=set,
)
def __str__(self) -> str:
return '{0.company_name}'.format(self)

View File

@ -34,7 +34,10 @@
<!-- Bordered Tabs -->
<ul class="nav nav-tabs nav-tabs-bordered">
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#profile-change-password">Change Password</button>
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#profile-change-password">Change Password</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#profile-sanitization-entity">Sanitization Entity</button>
</li>
</ul>
<div class="tab-content pt-2">
@ -65,7 +68,34 @@
<button type="submit" class="btn btn-primary">Change Password</button>
</div>
</form><!-- End Change Password Form -->
</div>
<div class="tab-pane fade pt-3" id="profile-sanitization-entity">
<!-- Sanitization Entity datas Form -->
<form action="{{ url_for('core.set-password') }}" method="post">
{% for f in sanitization_form %}
{% if f == sanitization_form.csrf_token %}
{{ f }}
{% else %}
<div class="row mb-3">
<label class="col-md-4 col-lg-3 col-form-label">{{ f.label }}</label>
<div class="col-md-8 col-lg-9">
{{ f }}
{% if f.errors %}
<p class="text-danger">
{% for error in f.errors %}
{{ error }}<br/>
{% endfor %}
</p>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
<div class="text-center">
<button type="submit" class="btn btn-primary">Change sanitization data</button>
</div>
</form><!-- End Sanitization Entity datas Form -->
</div>
</div><!-- End Bordered Tabs -->

View File

@ -9,7 +9,7 @@ from sqlalchemy import or_
from ereuse_devicehub import __version__, messages
from ereuse_devicehub.db import db
from ereuse_devicehub.forms import LoginForm, PasswordForm
from ereuse_devicehub.forms import LoginForm, PasswordForm, SanitizationEntityForm
from ereuse_devicehub.resources.action.models import Trade
from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.user.models import User
@ -100,10 +100,15 @@ class UserProfileView(GenericMixin):
def dispatch_request(self):
self.get_context()
sanitization_form = SanitizationEntityForm()
if g.user.sanitization_entity:
sanitization = list(g.user.sanitization_entity)[0]
sanitization_form = SanitizationEntityForm(initial=sanitization)
self.context.update(
{
'current_user': current_user,
'password_form': PasswordForm(),
'sanitization_form': sanitization_form,
}
)
@ -127,7 +132,27 @@ class UserPasswordView(View):
return flask.redirect(flask.url_for('core.user-profile'))
class SanitizationEntityView(View):
methods = ['POST']
decorators = [login_required]
def dispatch_request(self):
form = SanitizationEntityForm()
db.session.commit()
if form.validate_on_submit():
form.save(commit=False)
messages.success('Sanitization datas updated successfully!')
else:
messages.error('Error modifying Sanitization datas!')
db.session.commit()
return flask.redirect(flask.url_for('core.user-profile'))
core.add_url_rule('/login/', view_func=LoginView.as_view('login'))
core.add_url_rule('/logout/', view_func=LogoutView.as_view('logout'))
core.add_url_rule('/profile/', view_func=UserProfileView.as_view('user-profile'))
core.add_url_rule('/set_password/', view_func=UserPasswordView.as_view('set-password'))
core.add_url_rule(
'/set_sanitization/', view_func=SanitizationEntityView.as_view('set-sanitization')
)