just adapting user to new templates and form decalarations

master
Kerem Yollu 10 months ago
parent bf3fab9045
commit 081d496de9

Binary file not shown.

@ -73,4 +73,3 @@ def create_app():
# (APP) Returning the initialised app # (APP) Returning the initialised app
return app return app

@ -11,6 +11,13 @@ from minibase.blueprints.company.models import Companies
from minibase.blueprints.sensor.models import nbiotDevice from minibase.blueprints.sensor.models import nbiotDevice
from minibase.blueprints.user.models import Users 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 class companyForm(FlaskForm): # Defines the form class to be used for the user update
name = StringField('Name', validators=[DataRequired(), Length(min=3, max=100)]) name = StringField('Name', validators=[DataRequired(), Length(min=3, max=100)])
@ -34,7 +41,6 @@ class companyForm(FlaskForm): # Defines the form class to be used for the user
def populate_for_updating(self, company): def populate_for_updating(self, company):
self.originalModel = company self.originalModel = company
print(f"company.name={company.name}")
self.submit.label.text = "Update" self.submit.label.text = "Update"
self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNamesWithDefault(company.country_id)] self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNamesWithDefault(company.country_id)]

@ -23,6 +23,10 @@ def queryNameWithDefaultId(table,defId):
choices = table.query.order_by(case((table.id == defId, 0),else_=1),table.name.asc()) choices = table.query.order_by(case((table.id == defId, 0),else_=1),table.name.asc())
return choices return choices
def queryItemWithId(table,defId):
choices = table.query.order_by(case((table.id == defId, 0),else_=1),table.name.asc())
return choices
def dbAdd(dbClass): def dbAdd(dbClass):
db.session.add(dbClass) db.session.add(dbClass)

@ -18,30 +18,33 @@ class accountInfo:
# FORM AND MODEL MANAGEMENT # FORM AND MODEL MANAGEMENT
def fill_model(company, form): def fill_model(cur_model, form):
for item in form: for item in form:
modelFill(company, item) modelFill(cur_model, item)
def populate_form(form, company): def populate_form(form, cur_model):
for item in form: for item in form:
item.data = formFill(item, company) item.data = formFill(item, cur_model)
def formFill(item, table): def formFill(item, cur_model):
if item.type not in ["SubmitField", "CSRFTokenField"]: if item.type not in ["SubmitField", "CSRFTokenField"]:
return getattr(table, item.name) # Cheking if the arrtibute exitst, otherwise ignoring
if hasattr(cur_model, item.name):
return getattr(cur_model, item.name)
def modelFill(table, item): def modelFill(cur_model, item):
if hasattr(cur_model, item.name):
if item.type not in ["SubmitField", "CSRFTokenField"]: if item.type not in ["SubmitField", "CSRFTokenField"]:
if item.type == "FileField": if item.type == "FileField":
if item.data == "None": if item.data == "None":
print(f"File filed found with no data -> item name {item.name}") print(f"File field 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)}") print(f"It will be filled with the existin tbale data -> item name {getattr(cur_model, item.name)}")
setattr(table, item.name, getattr(table, item.name)) setattr(cur_model, item.name, getattr(cur_model, item.name))
else: else:
setattr(table, item.name, item.data) setattr(cur_model, item.name, item.data)
# PICTURE MANAGEMENT # PICTURE MANAGEMENT
@ -73,6 +76,18 @@ If you didn't make this request, then simply ingnore this email and no changes w
''' '''
mail.send(msg) mail.send(msg)
def send_sensor_email(email, data):
msg = Message('You have recived an ALARM',
sender='noreply@demo.com',
recipients=[email])
msg.body = f'''The Sensor Has the following DATA:
{data}
Please contact your support person as soon as possible
'''
mail.send(msg)
# QUERIES # QUERIES
def queryIndustryNames(): def queryIndustryNames():

@ -6,6 +6,7 @@ import minibase.blueprints.sensor.utils as sensorUtils
from minibase.blueprints.sensor.forms import nbiotDeviceForm from minibase.blueprints.sensor.forms import nbiotDeviceForm
import minibase.blueprints.database.utils as dbUtils import minibase.blueprints.database.utils as dbUtils
import minibase.blueprints.main.utils as mainUtils import minibase.blueprints.main.utils as mainUtils
import minibase.blueprints.user.utils as userUtils
from minibase.app import db from minibase.app import db
import json import json
import os import os
@ -47,10 +48,13 @@ def callback():
"srcPort": src_port, "srcPort": src_port,
"payload": payload "payload": payload
} }
sensorUtils.decode_payload(payload)
sensorUtils.decode_payload(payload)
sensor = sensorUtils.queryByImsi(src_imsi)
mail=userUtils.queryMailById(sensor.device_user_id)
print(f"Sensor Model: {sensor.model}, user e-mail: {mail}")
#mainUtils.send_sensor_email(mail, data)
return jsonify(response_data), 200 return jsonify(response_data), 200
#return 'data recieved', 200
@sensor.route('/data', methods=['GET']) @sensor.route('/data', methods=['GET'])
@ -112,10 +116,9 @@ def edit(deviceId):
def add(): def add():
device = nbiotDevice() device = nbiotDevice()
form = nbiotDeviceForm() form = nbiotDeviceForm()
print(f"Route : manufacturer_id : { form.manufacturer_id.data }") print(f"Route : manufacturer_id : { form.company_manufacturer_id.data }")
form.populate_for_adding(device) form.populate_for_adding(device)
if request.method == 'GET': if request.method == 'GET':
form.populate_for_adding(device) form.populate_for_adding(device)

@ -7,10 +7,15 @@ def queryImageById(id):
selected = nbiotDevice.query.filter_by(id=id).first() selected = nbiotDevice.query.filter_by(id=id).first()
return selected.image_file return selected.image_file
def queryById(id): def queryById(id):
return nbiotDevice.query.filter_by(id=id).first() return nbiotDevice.query.filter_by(id=id).first()
def queryByImsi(id):
return nbiotDevice.query.filter_by(imsi=id).first()
def queryStatusNamesWithDefault(defId): def queryStatusNamesWithDefault(defId):
choices = dbUtils.queryNameWithDefaultId(nbiotDeviceStatus, defId) choices = dbUtils.queryNameWithDefaultId(nbiotDeviceStatus, defId)
return choices return choices

@ -1,31 +1,97 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, PasswordField, SubmitField, BooleanField, URLField from wtforms import StringField, PasswordField, SubmitField, BooleanField, URLField, IntegerField, SelectField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from flask_login import current_user from flask_login import current_user
from minibase.blueprints.user.models import Users, User_Roles from minibase.blueprints.user.models import Users, User_Roles
import minibase.blueprints.geography.utils as geoUtils
class registrationForm(FlaskForm): # Defines the form class to be used for the user registretion class registrationForm(FlaskForm): # Defines the form class to be used for the user registretion
# Decalarion of the fields for the form and it's propereties # Decalarion of the fields for the form and it's propereties
username = StringField('User Name', validators=[DataRequired(), Length(min=4, max=20)]) username = StringField('User Name', validators=[DataRequired(), Length(min=4, max=20)])
email = StringField('Email', validators=[DataRequired(), Email()]) name = StringField('Name', validators=[DataRequired(), Length(min=4, max=20)])
surname = StringField('Surname', validators=[DataRequired(), Length(min=4, max=20)])
email_account = StringField('Email: Account', validators=[DataRequired(), Email()])
email_comm = StringField('Email: Communication', validators=[DataRequired(), Email()])
street = StringField('Street', validators=[DataRequired()])
street_no = IntegerField('No', validators=[DataRequired()])
post_code = IntegerField('Post', validators=[DataRequired()])
city_id = SelectField('City', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_states", "hx-target": "#state_id"})
state_id = SelectField('State', validators=[])
country_id = SelectField('Country', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_cities", "hx-target": "#city_id"})
password = PasswordField('Password', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()])
password_confirm= PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')]) password_confirm= PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
submit = SubmitField('Sing Up') submit = SubmitField()
def populate_for_adding(self, user):
self.originalModel = user
self.submit.label.text = "Add"
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 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(user.city_id, user.country_id)]
self.state_id.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryIdWithDefault(user.state_id, user.country_id)]
# Queries to be made in order to validate the form : If username exists # Queries to be made in order to validate the form : If username exists
def validate_username(self, username): def validate_username(self, input):
user = Users.query.filter_by(username=username.data).first() # Database Querry if input.data != self.originalModel.username:
if user: if (Users.query.filter(Users.username.ilike(f'%{input.data}%')).first()):
raise ValidationError('That username is taken please choose another one') raise ValidationError('This User Name is alredy registered')
# Queries to be made in order to validate the form : If Email exists # Queries to be made in order to validate the form : If username exists
def validate_email(self, email): def validate_email_account(self, input):
email = Users.query.filter_by(email_account=email.data).first() # Database Querry if input.data != self.originalModel.email_account:
if email: if (Users.query.filter(Users.email_account.ilike(f'%{input.data}%')).first()):
raise ValidationError('That email is taken do you have an acocunt ?') raise ValidationError('This E-mail is alredy registered')
class accountUpdateForm(FlaskForm): # Defines the form class to be used for the user registretion
# Decalarion of the fields for the form and it's propereties
username = StringField('User Name', validators=[DataRequired(), Length(min=4, max=20)])
name = StringField('Name', validators=[DataRequired(), Length(min=4, max=20)])
surname = StringField('Surname', validators=[DataRequired(), Length(min=4, max=20)])
email_account = StringField('Email: Account', validators=[DataRequired(), Email()])
email_comm = StringField('Email: Communication', validators=[DataRequired(), Email()])
street = StringField('Street', validators=[DataRequired()])
street_no = IntegerField('No', validators=[DataRequired()])
post_code = IntegerField('Post', validators=[DataRequired()])
city_id = SelectField('City', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_states", "hx-target": "#state_id"})
state_id = SelectField('State', validators=[])
country_id = SelectField('Country', validators=[DataRequired()], render_kw={"hx-get": "/geography/get_cities", "hx-target": "#city_id"})
image_file = FileField('Update Avatar', validators=[FileAllowed(['jpg', 'png'])])
password = PasswordField('Password')
password_confirm= PasswordField('Confirm Password', validators=[EqualTo('password')])
submit = SubmitField()
def populate_for_update(self, user):
self.originalModel = user
self.submit.label.text = "Update"
self.country_id.choices = [(row.id, row.name) for row in geoUtils.queryCountryNamesWithDefault(user.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(user.city_id, user.country_id)]
self.state_id.choices = [(row.id, row.name) for row in geoUtils.queryStateNamesWithCountryIdWithDefault(user.state_id, user.country_id)]
# Queries to be made in order to validate the form : If username exists
def validate_username(self, input):
if input.data != self.originalModel.username:
if (Users.query.filter(Users.username.ilike(f'%{input.data}%')).first()):
raise ValidationError('This User Name is alredy registered')
# Queries to be made in order to validate the form : If username exists
def validate_email_account(self, input):
if input.data != self.originalModel.email_account:
if (Users.query.filter(Users.email_account.ilike(f'%{input.data}%')).first()):
raise ValidationError('This E-mail is alredy registered')
class loginForm(FlaskForm): # Defines the form class to be used for the user login class loginForm(FlaskForm): # Defines the form class to be used for the user login
email = StringField('Email', validators=[DataRequired(), Email()]) email = StringField('Email', validators=[DataRequired(), Email()])

@ -4,7 +4,6 @@ from minibase.app import db, login_manager
from flask_login import UserMixin from flask_login import UserMixin
from datetime import datetime from datetime import datetime
# The Default User Loading proccess # The Default User Loading proccess
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):

@ -5,26 +5,32 @@ import minibase.theme as theme
from minibase.blueprints.user.models import Users, User_Roles from minibase.blueprints.user.models import Users, User_Roles
import minibase.blueprints.database.utils as dbUtils import minibase.blueprints.database.utils as dbUtils
import minibase.blueprints.user.utils as UserUtils import minibase.blueprints.user.utils as UserUtils
from minibase.blueprints.user.forms import registrationForm, loginForm, updateAccountForm, resetPasswordForm, requestResetForm, updateRoleForm from minibase.blueprints.user.forms import registrationForm, loginForm, updateAccountForm, resetPasswordForm, requestResetForm, updateRoleForm, accountUpdateForm
import minibase.blueprints.main.utils as mainUtils import minibase.blueprints.main.utils as mainUtils
# Declaring a blueprint # Declaring a blueprint
user = Blueprint('user', __name__, template_folder='templates') user = Blueprint('user', __name__, template_folder='templates')
@user.route("/register", methods=['GET', 'POST']) @user.route("/register", methods=['GET', 'POST'])
def register(): def register():
if current_user.is_authenticated: if current_user.is_authenticated:
flash('You are already logged in', 'success')
return redirect(url_for('main.index')) return redirect(url_for('main.index'))
user = Users()
form = registrationForm() form = registrationForm()
form.populate_for_adding(user)
if request.method == 'GET':
form.populate_for_adding(user)
if form.validate_on_submit(): if form.validate_on_submit():
hashed_pw = bcrypt.generate_password_hash(form.password.data).decode('utf-8') mainUtils.fill_model(user, form)
dbUtils.dbAddAndCommit(Users(username=form.username.data, email_account=form.email.data, email_comm=form.email.data, password=hashed_pw)) user.password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
flash(f'{"Your account has been created you can now log in!"}', 'success') dbUtils.dbAddAndCommit(user)
return redirect(url_for('user.login')) flash('User has been successfully added', 'success')
return render_template('user/register.html', return render_template('edit.html',
theme=theme, theme=theme,
form=form) form=form)
@ -57,29 +63,43 @@ def logout():
@user.route("/account", methods=['GET', 'POST']) @user.route("/account", methods=['GET', 'POST'])
@login_required @login_required
def account(): def account():
form = updateAccountForm() form = accountUpdateForm()
form.populate_for_update(current_user)
_accountInfo = mainUtils.accountInfo(
title=current_user.username,
description=current_user.email_account,
short=current_user.role,
status=current_user.name,
image_file=mainUtils.imageFileLink(current_user.image_file)
)
if form.validate_on_submit(): if form.validate_on_submit():
if form.picture.data: mainUtils.fill_model(current_user, form)
picture_file = mainUtils.save_picture(form.picture.data)
if form.image_file.data:
picture_file = mainUtils.save_picture(form.image_file.data)
current_user.image_file = picture_file current_user.image_file = picture_file
current_user.username = form.username.data
current_user.email_account = form.email_account.data if form.password.data:
current_user.email_comm = form.email_comm.data print(f"Passsword is : {form.password.data}")
print(f"User pass id : {current_user.password}")
hashed = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
current_user.password = hashed
print(f"Hashed is : {hashed}")
print(f"Hashed is : {current_user.password}")
db.session.commit() db.session.commit()
flash('Your account has been updated!', 'success') flash('Your account has been updated!', 'success')
return redirect(url_for('user.account')) return redirect(url_for('user.accountt'))
elif request.method == 'GET': elif request.method == 'GET':
form.username.data = current_user.username mainUtils.populate_form(form, current_user)
form.email_account.data = current_user.email_account
form.email_comm.data = current_user.email_comm return render_template('account.html',
image_file = url_for('static', filename='pics/' + current_user.image_file)
return render_template('user/account.html',
theme=theme, theme=theme,
image_file=image_file, accountInfo=_accountInfo,
form=form) form=form)
@user.route("/reset_password", methods=['GET', 'POST']) @user.route("/reset_password", methods=['GET', 'POST'])
def reset_request(): def reset_request():
if current_user.is_authenticated: if current_user.is_authenticated:

@ -6,6 +6,8 @@ from sqlalchemy import case
def dbGetMailFirst(mail): def dbGetMailFirst(mail):
return Users.query.filter_by(email_account=mail).first() return Users.query.filter_by(email_account=mail).first()
def queryMailById(id):
return (Users.query.filter_by(id=id).first()).email_comm
def queryRoleById(id): def queryRoleById(id):
return User_Roles.query.get_or_404(id) return User_Roles.query.get_or_404(id)

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

@ -85,7 +85,7 @@ class menu:
"sublinks": [ "sublinks": [
{"text": "List", "url": "sensor.list"}, {"text": "List", "url": "sensor.list"},
{"decoration": "line"}, {"decoration": "line"},
{"text": "Add", "url": "main.index"}, {"text": "Add", "url": "sensor.add"},
{"decoration": "line"}, {"decoration": "line"},
{"text": "Edit", "url": "main.index"}, {"text": "Edit", "url": "main.index"},
],}, ],},

Loading…
Cancel
Save