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 @@
- - - - + + + +
- +
diff --git a/web/minibase/blueprints/main/__pycache__/utils.cpython-311.pyc b/web/minibase/blueprints/main/__pycache__/utils.cpython-311.pyc index e10b71ee..2845c87f 100644 Binary files a/web/minibase/blueprints/main/__pycache__/utils.cpython-311.pyc and b/web/minibase/blueprints/main/__pycache__/utils.cpython-311.pyc differ diff --git a/web/minibase/blueprints/main/utils.py b/web/minibase/blueprints/main/utils.py index 9557d5f1..d63ada75 100644 --- a/web/minibase/blueprints/main/utils.py +++ b/web/minibase/blueprints/main/utils.py @@ -8,23 +8,47 @@ from minibase.blueprints.main.models import Industries import minibase.blueprints.database.utils as dbUtils -def formFill(item, table, to_ignore=[]): +class accountInfo: + def __init__(self, title, description, short, status, image_file): + self.title = title + self.description = description + self.short = short + self.status = status + self.image_file = image_file + + +# FORM AND MODEL MANAGEMENT +def fill_model(company, form): + for item in form: + modelFill(company, item) + + +def populate_form(form, company): + for item in form: + item.data = formFill(item, company) + + +def formFill(item, table): if item.type not in ["SubmitField", "CSRFTokenField"]: - if item.name not in to_ignore: - attribute = getattr(table, item.name) - if hasattr(attribute, 'name'): - return getattr(attribute, 'name') - else: - return attribute + return getattr(table, item.name) -def modelFill(table, item, to_ignore=[]): +def modelFill(table, item): if item.type not in ["SubmitField", "CSRFTokenField"]: - print(item.type) - if item.name not in to_ignore: + if item.type == "FileField": + if item.data == "None": + print(f"File filed found with no data -> item name {item.name}") + print(f"It will be filled with the existin tbale data -> item name {getattr(table, item.name)}") + setattr(table, item.name, getattr(table, item.name)) + else: setattr(table, item.name, item.data) +# PICTURE MANAGEMENT + +def imageFileLink(imageFileName): + return url_for('static', filename='pics/' + imageFileName) + def save_picture(form_picture): random_hex = secrets.token_hex(8) _, f_ext = os.path.splitext(form_picture.filename) @@ -37,6 +61,7 @@ def save_picture(form_picture): return picture_fn +# MAIL MECHANISMS def send_reset_email(user): token = user.get_reset_token() msg = Message('Password Reset Request', @@ -44,17 +69,16 @@ def send_reset_email(user): recipients=[user.email_account]) msg.body = f'''To reset your password, visit the following link: {url_for('user.reset_token', token=token, _external=True)} -If you didn't make this request, then simply ingnore this email and no chancges will be made. +If you didn't make this request, then simply ingnore this email and no changes will be made. ''' mail.send(msg) +# QUERIES def queryIndustryNames(): choices = Industries.query.order_by(Industries.name.asc()) return choices - def queryIndustryNamesWithDefault(defId): choices = dbUtils.queryNameWithDefaultId(Industries,defId) return choices - diff --git a/web/minibase/blueprints/sensor/__pycache__/forms.cpython-311.pyc b/web/minibase/blueprints/sensor/__pycache__/forms.cpython-311.pyc index 6dcc3c5e..af45123c 100644 Binary files a/web/minibase/blueprints/sensor/__pycache__/forms.cpython-311.pyc and b/web/minibase/blueprints/sensor/__pycache__/forms.cpython-311.pyc differ diff --git a/web/minibase/blueprints/sensor/__pycache__/routes.cpython-311.pyc b/web/minibase/blueprints/sensor/__pycache__/routes.cpython-311.pyc index b239cef3..9bb30bde 100644 Binary files a/web/minibase/blueprints/sensor/__pycache__/routes.cpython-311.pyc and b/web/minibase/blueprints/sensor/__pycache__/routes.cpython-311.pyc differ diff --git a/web/minibase/blueprints/sensor/forms.py b/web/minibase/blueprints/sensor/forms.py index 966b0c6f..c70ed270 100644 --- a/web/minibase/blueprints/sensor/forms.py +++ b/web/minibase/blueprints/sensor/forms.py @@ -99,23 +99,9 @@ class updateNbioDeviceAddForm(FlaskForm): # Defines the form class to be used f content = nbiotDevice.query.filter_by(iccid=input.data).first() if content: raise ValidationError('That ICCID id is taken please choose another one') - + def validate_ip(self, input): content = nbiotDevice.query.filter_by(ip=input.data).first() if content: raise ValidationError('That IP id is taken please choose another one') - -class updateRoleForm(FlaskForm): - id = StringField('ID', render_kw={'disabled':''}) - role = StringField('Role', validators=[DataRequired(), Length(min=4, max=20)]) - description = StringField('Description', validators=[DataRequired(), Length(min=4, max=200)]) - submit = SubmitField() - - -class updateRoleForm(FlaskForm): - id = StringField('ID', render_kw={'disabled':''}) - role = StringField('Role', validators=[DataRequired(), Length(min=4, max=20)]) - description = StringField('Description', validators=[DataRequired(), Length(min=4, max=200)]) - submit = SubmitField() - diff --git a/web/minibase/blueprints/sensor/routes.py b/web/minibase/blueprints/sensor/routes.py index 94d80aa5..b384e1c2 100644 --- a/web/minibase/blueprints/sensor/routes.py +++ b/web/minibase/blueprints/sensor/routes.py @@ -11,7 +11,6 @@ from minibase.app import db import json import os -# TODO: sensor = Blueprint('sensor', __name__, template_folder='templates') @@ -59,6 +58,7 @@ def get_data(): if os.path.exists(DATA_FILE): with open(DATA_FILE, 'r') as f: stored_data = json.load(f) +# TODO: else: stored_data = [] diff --git a/web/minibase/static/pics/a3686f86aa0546c5.png b/web/minibase/static/pics/a3686f86aa0546c5.png new file mode 100644 index 00000000..c4f57038 Binary files /dev/null and b/web/minibase/static/pics/a3686f86aa0546c5.png differ diff --git a/web/minibase/templates/account.html b/web/minibase/templates/account.html new file mode 100644 index 00000000..47587270 --- /dev/null +++ b/web/minibase/templates/account.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% block content %} + +
+
+
+
+ + + + +
+ +
+
+
+ +
+
+ {% include 'form.html' %} +
+
+
+
+
+
+
+
+{% endblock content %} + +