diff --git a/web/instance/test.db b/web/instance/test.db index 5086d0b2..88f1be94 100644 Binary files a/web/instance/test.db and b/web/instance/test.db differ diff --git a/web/minibase/blueprints/company/__pycache__/forms.cpython-311.pyc b/web/minibase/blueprints/company/__pycache__/forms.cpython-311.pyc index 34012a2a..cd7de6aa 100644 Binary files a/web/minibase/blueprints/company/__pycache__/forms.cpython-311.pyc and b/web/minibase/blueprints/company/__pycache__/forms.cpython-311.pyc differ diff --git a/web/minibase/blueprints/company/__pycache__/routes.cpython-311.pyc b/web/minibase/blueprints/company/__pycache__/routes.cpython-311.pyc index 61004312..62dff425 100644 Binary files a/web/minibase/blueprints/company/__pycache__/routes.cpython-311.pyc and b/web/minibase/blueprints/company/__pycache__/routes.cpython-311.pyc differ diff --git a/web/minibase/blueprints/company/forms.py b/web/minibase/blueprints/company/forms.py index 5b5623c9..6e2d6f62 100644 --- a/web/minibase/blueprints/company/forms.py +++ b/web/minibase/blueprints/company/forms.py @@ -12,7 +12,7 @@ from minibase.blueprints.sensor.models import nbiotDevice from minibase.blueprints.user.models import Users -class updateCompanyForm(FlaskForm): # Defines the form class to be used for the user update +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"}) @@ -30,9 +30,10 @@ class updateCompanyForm(FlaskForm): # Defines the form class to be used for the image_file = FileField('Update company Picture', validators=[FileAllowed(['jpg', 'png'])]) - submit = SubmitField('Update') + submit = SubmitField() - def populate_choices(self, company): + def populate_for_update(self, 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) @@ -48,36 +49,29 @@ class updateCompanyForm(FlaskForm): # Defines the form class to be used for the 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.submit.label.text = "Add" + del self.image_file - # Queries to be made in order to validate the form : If username exists - def validate_companyName(self, company_name): - company = companyUtils.queryByNameFirst(company_name) - if company: - raise ValidationError('That username is taken please choose another one') - + self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNames()] -class addCompanyForm(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 = SelectField('Country', validators=[DataRequired()],choices=[], render_kw={"hx-get": "/geography/get_cities", "hx-target": "#city"}) - city = SelectField('City', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_states", "hx-target": "#state"}) - state = SelectField('State', validators=[]) - post_code = IntegerField('Post', validators=[DataRequired()]) - street = StringField('Street', validators=[DataRequired()]) - street_no = IntegerField('No', validators=[DataRequired()]) - industry = SelectField('Industry', validators=[DataRequired()]) - legal_entity= SelectField('Legal Entity', validators=[DataRequired()]) - type = SelectField('Type', validators=[DataRequired()]) - relation = SelectField('Relation', validators=[DataRequired()]) - status = SelectField('Status', validators=[DataRequired()]) - comment = StringField('Comment', validators=[DataRequired(), Length(min=3, max=400)]) + # 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 = [] - submit = SubmitField('Add') + 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()] # Queries to be made in order to validate the form : If username exists def validate_companyName(self, company_name): company = companyUtils.queryByNameFirst(company_name) if company: raise ValidationError('That username is taken please choose another one') - diff --git a/web/minibase/blueprints/company/routes.py b/web/minibase/blueprints/company/routes.py index 336cab1a..b0a7781e 100644 --- a/web/minibase/blueprints/company/routes.py +++ b/web/minibase/blueprints/company/routes.py @@ -7,11 +7,12 @@ import minibase.blueprints.database.utils as dbUtils import minibase.blueprints.main.utils as mainUtils import minibase.blueprints.geography.utils as geoUtils import minibase.blueprints.company.utils as companyUtils -from minibase.blueprints.company.forms import updateCompanyForm, addCompanyForm +from minibase.blueprints.company.forms import companyForm # Declaring a blueprint company = Blueprint('company', __name__, template_folder='templates') + @company.route("/list", methods=['GET', 'POST']) def list(): page=request.args.get('page', 1, type=int) @@ -24,78 +25,48 @@ def list(): def account(companyId): if companyId: company = Companies.query.get_or_404(companyId) - form = updateCompanyForm() + form = companyForm() + form.populate_for_update(company) - form.populate_choices(company) + _accountInfo = mainUtils.accountInfo( + title=company.name, + description=company.legal_entity.name, + short=company.website, + status=company.type.name, + image_file=mainUtils.imageFileLink(company.image_file) + ) if form.validate_on_submit(): if form.image_file.data: picture_file = mainUtils.save_picture(form.image_file.data) company.image_file = picture_file - for item in form: - mainUtils.modelFill(company, item, ["image_file"]) - + mainUtils.fill_model(company, form) dbUtils.dbCommit() flash('Company Has been successfully updated', 'success') return redirect(url_for('company.account', companyId=companyId)) elif request.method == 'GET': - for item in form: - item.data = mainUtils.formFill(item, company) - - image_file = url_for('static', filename='pics/' + companyUtils.queryImageById(companyId)) + mainUtils.populate_form(form, company) - return render_template('company/account.html', - theme=theme, - image_file=image_file, - form=form) + return render_template('account.html', theme=theme, accountInfo=_accountInfo, form=form) else: flash('You need to select a company id', 'alarm') return redirect(url_for('company.list')) + + @company.route("/add", methods=['GET', 'POST']) @login_required def add(): - form = addCompanyForm() - - form.country.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 form.country.data: - form.city.choices = [(row.id, row.name) for row in geoUtils.queryCityNamesWithCountryId(form.country.data)] - form.state.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryId(form.country.data)] - else: - form.city.choices = [] - form.state.choices = [] - - form.industry.choices = [(row.id, row.name) for row in mainUtils.queryIndustryNames()] - form.legal_entity.choices = [(row.id, row.name) for row in companyUtils.queryLegalEntityNames()] - form.type.choices = [(row.id, row.name) for row in companyUtils.queryTypeNames()] - form.relation.choices = [(row.id, row.name) for row in companyUtils.queryRelationNames()] - form.status.choices = [(row.id, row.name) for row in companyUtils.queryStatusNames()] + company = Companies() + form = companyForm() + form.populate_for_adding(company) if form.validate_on_submit(): - company = Companies( - name = form.name.data, - website = form.website.data, - country_id = form.country.data, - city_id = form.city.data, - state_id = form.state.data, - post_code = form.post_code.data, - street = form.street.data, - street_no = form.street_no.data, - industry_id = form.industry.data, - legal_entity_id = form.legal_entity.data, - type_id = form.type.data, - relation_id = form.relation.data, - status_id = form.status.data, - comment = form.comment.data) + mainUtils.fill_model(company, form) dbUtils.dbAddAndCommit(company) - flash('Company Has been successfully added', 'success') + flash('Company Has been successfully Added', 'success') return redirect(url_for('company.list')) - return render_template('edit.html', - title="Add Company", - theme=theme, - form=form) + return render_template('edit.html', title="Add Company", theme=theme, form=form) diff --git a/web/minibase/blueprints/company/templates/company/account.html b/web/minibase/blueprints/company/templates/company/account.html index 2426f161..47587270 100644 --- a/web/minibase/blueprints/company/templates/company/account.html +++ b/web/minibase/blueprints/company/templates/company/account.html @@ -5,12 +5,12 @@