You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
5.5 KiB
88 lines
5.5 KiB
from flask_wtf import FlaskForm
|
|
from flask_wtf.file import FileField, FileAllowed
|
|
from wtforms import StringField, SubmitField, URLField, IntegerField, SelectField
|
|
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
|
|
import minibase.blueprints.company.utils as companyUtils
|
|
import minibase.blueprints.geography.utils as geoUtils
|
|
import minibase.blueprints.main.utils as mainUtils
|
|
import minibase.theme as theme
|
|
from wtforms_alchemy import ModelForm
|
|
from minibase.blueprints.company.models import Companies
|
|
from minibase.blueprints.sensor.models import nbiotDevice
|
|
from minibase.blueprints.user.models import Users
|
|
|
|
fields = {
|
|
"city_id": SelectField(
|
|
'City',
|
|
validators=[DataRequired()],
|
|
render_kw={"hx-get": "/geography/get_states", "hx-target": "#state_id"}
|
|
),
|
|
}
|
|
|
|
class companyForm(FlaskForm): # Defines the form class to be used for the user update
|
|
name = StringField('Name', validators=[DataRequired(), Length(min=3, max=100)])
|
|
website = URLField('Website', validators=[DataRequired(), Length(min=3, max=100)])
|
|
country_id = SelectField('Country', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_cities", "hx-target": "#city_id"})
|
|
city_id = SelectField('City', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_states", "hx-target": "#state_id"})
|
|
state_id = SelectField('State', validators=[])
|
|
post_code = IntegerField('Post', validators=[DataRequired()])
|
|
street = StringField('Street', validators=[DataRequired()])
|
|
street_no = IntegerField('No', validators=[DataRequired()])
|
|
industry_id = SelectField('Industry', validators=[DataRequired()])
|
|
legal_entity_id = SelectField('Legal Entity', validators=[DataRequired()])
|
|
type_id = SelectField('Type', validators=[DataRequired()])
|
|
relation_id = SelectField('Relation', validators=[DataRequired()])
|
|
status_id = SelectField('Status', validators=[DataRequired()])
|
|
comment = StringField('Comment', validators=[DataRequired(), Length(min=3, max=400)])
|
|
|
|
image_file = FileField('Update company Picture', validators=[FileAllowed(['jpg', 'png'])])
|
|
|
|
submit = SubmitField()
|
|
|
|
def populate_for_updating(self, company):
|
|
self.originalModel = company
|
|
self.submit.label.text = "Update"
|
|
self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNamesWithDefault(company.country_id)]
|
|
|
|
# This is for the htmx implementation. please be careful how of there is data we first take from the database an then from the from(containing th htmx call)
|
|
if self.country_id.data:
|
|
self.city_id.choices = [(row.id, row.name) for row in geoUtils.queryCityNamesWithCountryId(self.country_id.data)]
|
|
self.state_id.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryId(self.country_id.data)]
|
|
else:
|
|
self.city_id.choices = [(row.id, row.name) for row in geoUtils.queryCityNamesWithCountryIdWithDefault(company.city_id, company.country_id)]
|
|
self.state_id.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryIdWithDefault(company.state_id, company.country_id)]
|
|
|
|
self.industry_id.choices = [(row.id, row.name) for row in mainUtils.queryIndustryNamesWithDefault(company.industry_id)]
|
|
self.legal_entity_id.choices = [(row.id, row.name) for row in companyUtils.queryLegalEntityNamesWithDefault(company.legal_entity_id)]
|
|
self.type_id.choices = [(row.id, row.name) for row in companyUtils.queryTypeNamesWithDefault(company.type_id)]
|
|
self.relation_id.choices = [(row.id, row.name) for row in companyUtils.queryRelationNamesWithDefault(company.relation_id)]
|
|
self.status_id.choices = [(row.id, row.name) for row in companyUtils.queryStatusNamesWithDefault(company.status_id)]
|
|
|
|
def populate_for_adding(self, company):
|
|
self.originalModel = company
|
|
self.submit.label.text = "Add"
|
|
del self.image_file
|
|
|
|
self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNames()]
|
|
|
|
# This is for the htmx implementation. please be careful how of there is data we switch the funtion needed
|
|
if self.country_id.data:
|
|
self.city_id.choices = [(row.id, row.name) for row in geoUtils.queryCityNamesWithCountryId(self.country_id.data)]
|
|
self.state_id.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryId(self.country_id.data)]
|
|
else:
|
|
self.city_id.choices = []
|
|
self.state_id.choices = []
|
|
|
|
self.industry_id.choices = [(row.id, row.name) for row in mainUtils.queryIndustryNames()]
|
|
self.legal_entity_id.choices = [(row.id, row.name) for row in companyUtils.queryLegalEntityNames()]
|
|
self.type_id.choices = [(row.id, row.name) for row in companyUtils.queryTypeNames()]
|
|
self.relation_id.choices = [(row.id, row.name) for row in companyUtils.queryRelationNames()]
|
|
self.status_id.choices = [(row.id, row.name) for row in companyUtils.queryStatusNames()]
|
|
|
|
#custom validators specific naming convention: validate_<fieldname>. This is how Flask-WTF knows which field the validator is associated with.
|
|
def validate_name(self, input):
|
|
if input.data != self.originalModel.name:
|
|
if (Companies.query.filter(Companies.name.ilike(f'%{input.data}%')).first()):
|
|
raise ValidationError('This Company already registered')
|
|
|