diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 0f4cefbe..dff3278d 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -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 diff --git a/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py b/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py new file mode 100644 index 00000000..9489dd27 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py @@ -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()}') diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index 5eadb21d..e00c577c 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -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) diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html index fd8b8804..0a0609b7 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html @@ -34,7 +34,10 @@
@@ -65,7 +68,34 @@
+ +
+ +
+ {% for f in sanitization_form %} + {% if f == sanitization_form.csrf_token %} + {{ f }} + {% else %} +
+ +
+ {{ f }} + {% if f.errors %} +

+ {% for error in f.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %} +
+
+ {% endif %} + {% endfor %} +
+ +
+
diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index fd852da0..ef837556 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -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') +)