From 8207ca9ab2cd256fb56b0f147cad8a9dc9ad5d47 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 14 Feb 2023 12:15:21 +0100 Subject: [PATCH] save datas and initialized form --- ereuse_devicehub/forms.py | 28 +++++++++++++++++-- .../versions/4f33137586dd_sanitization.py | 4 ++- ereuse_devicehub/resources/user/models.py | 4 ++- .../ereuse_devicehub/user_profile.html | 2 +- ereuse_devicehub/views.py | 2 +- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index dff3278d..087fd165 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -1,7 +1,15 @@ +from boltons.urlutils import URL from flask import g from flask_wtf import FlaskForm from werkzeug.security import generate_password_hash -from wtforms import BooleanField, EmailField, PasswordField, StringField, validators +from wtforms import ( + BooleanField, + EmailField, + PasswordField, + StringField, + URLField, + validators, +) from ereuse_devicehub.db import db from ereuse_devicehub.resources.user.models import SanitizationEntity, User @@ -105,12 +113,25 @@ class PasswordForm(FlaskForm): class SanitizationEntityForm(FlaskForm): - logo = StringField('Logo', render_kw={'class': "form-control"}) + logo = URLField( + 'Logo', + [validators.Optional(), validators.URL()], + 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"} ) + supervisor_person = StringField( + 'Supervisor person', render_kw={'class': "form-control"} + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # import pdb; pdb.set_trace() + if isinstance(self.logo.data, URL): + self.logo.data = self.logo.data.to_text() def validate(self, extra_validators=None): is_valid = super().validate(extra_validators) @@ -122,10 +143,11 @@ class SanitizationEntityForm(FlaskForm): def save(self, commit=True): sanitation_data = SanitizationEntity( - logo=self.logo.data, + logo=URL(self.logo.data), company_name=self.company_name.data, location=self.location.data, responsable_person=self.responsable_person.data, + supervisor_person=self.supervisor_person.data, user=g.user, ) db.session.add(sanitation_data) diff --git a/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py b/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py index 9489dd27..fa5e5fd3 100644 --- a/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py +++ b/ereuse_devicehub/migrations/versions/4f33137586dd_sanitization.py @@ -6,6 +6,7 @@ Create Date: 2023-02-13 18:01:00.092527 """ import sqlalchemy as sa +import teal from alembic import context, op from sqlalchemy.dialects import postgresql @@ -40,8 +41,9 @@ def upgrade(): nullable=False, ), sa.Column('company_name', sa.String(), nullable=True), - sa.Column('logo', sa.String(), nullable=True), + sa.Column('logo', teal.db.URL(), nullable=True), sa.Column('responsable_person', sa.String(), nullable=True), + sa.Column('supervisor_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'), diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index e00c577c..14e1c7ad 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -5,7 +5,7 @@ from flask_login import UserMixin from sqlalchemy import BigInteger, Boolean, Column, Sequence from sqlalchemy.dialects.postgresql import UUID from sqlalchemy_utils import EmailType, PasswordType -from teal.db import IntEnum +from teal.db import URL, IntEnum from ereuse_devicehub.db import db from ereuse_devicehub.resources.enums import SessionType @@ -126,7 +126,9 @@ class SanitizationEntity(Thing): company_name = Column(db.String, nullable=True) location = Column(db.String, nullable=True) logo = Column(db.String, nullable=True) + logo = Column(URL(), nullable=True) responsable_person = Column(db.String, nullable=True) + supervisor_person = Column(db.String, nullable=True) user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id)) user = db.relationship( User, diff --git a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html index 0a0609b7..462fc6ec 100644 --- a/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html +++ b/ereuse_devicehub/templates/ereuse_devicehub/user_profile.html @@ -72,7 +72,7 @@
-
+ {% for f in sanitization_form %} {% if f == sanitization_form.csrf_token %} {{ f }} diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index ef837556..f2cd4bc1 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -103,7 +103,7 @@ class UserProfileView(GenericMixin): sanitization_form = SanitizationEntityForm() if g.user.sanitization_entity: sanitization = list(g.user.sanitization_entity)[0] - sanitization_form = SanitizationEntityForm(initial=sanitization) + sanitization_form = SanitizationEntityForm(obj=sanitization) self.context.update( { 'current_user': current_user,