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.
101 lines
3.2 KiB
101 lines
3.2 KiB
import os
|
|
import secrets
|
|
from PIL import Image
|
|
from flask import url_for, current_app
|
|
from flask_mail import Message
|
|
from minibase.app import mail
|
|
from minibase.blueprints.main.models import Industries
|
|
import minibase.blueprints.database.utils as dbUtils
|
|
|
|
|
|
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(cur_model, form):
|
|
for item in form:
|
|
modelFill(cur_model, item)
|
|
|
|
|
|
def populate_form(form, cur_model):
|
|
for item in form:
|
|
item.data = formFill(item, cur_model)
|
|
|
|
|
|
#Fills the form with
|
|
def formFill(item, cur_model):
|
|
if item.type not in ["SubmitField", "CSRFTokenField"]:
|
|
# Cheking if the arrtibute exitst, otherwise ignoring
|
|
if hasattr(cur_model, item.name):
|
|
return getattr(cur_model, item.name)
|
|
|
|
|
|
# Fills the model with the data from the Form, if an attribute doesn't exist, the value of that
|
|
# Attribute will be filled with the existing data (May be an extra step, overkill?)
|
|
def modelFill(cur_model, item):
|
|
if hasattr(cur_model, item.name):
|
|
if item.type not in ["SubmitField", "CSRFTokenField"]:
|
|
if item.type == "FileField":
|
|
if item.data == "None":
|
|
print(f"File field found with no data -> item name {item.name}")
|
|
print(f"It will be filled with the existin table data -> item name {getattr(cur_model, item.name)}")
|
|
setattr(cur_model, item.name, getattr(cur_model, item.name))
|
|
else:
|
|
setattr(cur_model, 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)
|
|
picture_fn = random_hex + f_ext
|
|
picture_path = os.path.join(current_app.root_path, 'static/pics', picture_fn)
|
|
output_size = (125, 125)
|
|
i = Image.open(form_picture)
|
|
i.thumbnail(output_size)
|
|
i.save(picture_path)
|
|
return picture_fn
|
|
|
|
|
|
# MAIL MECHANISMS
|
|
def send_reset_email(user):
|
|
token = user.get_reset_token()
|
|
msg = Message('Password Reset Request',
|
|
sender='noreply@demo.com',
|
|
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 changes will be made.
|
|
'''
|
|
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
|
|
def queryIndustryNames():
|
|
return dbUtils.queryWithAttrOrder(Industries, Industries.name, 1)
|
|
|
|
def queryIndustryNamesWithDefault(defId):
|
|
return dbUtils.queryNameWithDefaultId(Industries,defId)
|