save datas and initialized form

This commit is contained in:
Cayo Puigdefabregas 2023-02-14 12:15:21 +01:00
parent 8cd7777fc6
commit 8207ca9ab2
5 changed files with 33 additions and 7 deletions

View File

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

View File

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

View File

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

View File

@ -72,7 +72,7 @@
<div class="tab-pane fade pt-3" id="profile-sanitization-entity">
<!-- Sanitization Entity datas Form -->
<form action="{{ url_for('core.set-password') }}" method="post">
<form action="{{ url_for('core.set-sanitization') }}" method="post">
{% for f in sanitization_form %}
{% if f == sanitization_form.csrf_token %}
{{ f }}

View File

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