Final fixes for Android release candidate 1
This commit is contained in:
parent
bb8ba5958e
commit
af4cb5a92e
125
well-api.py
125
well-api.py
@ -1707,8 +1707,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id):
|
|||||||
field_mapping = {
|
field_mapping = {
|
||||||
'role_ids': 'role_ids',
|
'role_ids': 'role_ids',
|
||||||
'access_to_deployments': 'access_to_deployments',
|
'access_to_deployments': 'access_to_deployments',
|
||||||
'email': 'email',
|
'beneficiary_email': 'email',
|
||||||
'new_user_name': 'user_name',
|
'beneficiary_user_name': 'user_name',
|
||||||
'first_name': 'first_name',
|
'first_name': 'first_name',
|
||||||
'last_name': 'last_name',
|
'last_name': 'last_name',
|
||||||
'address_street': 'address_street',
|
'address_street': 'address_street',
|
||||||
@ -1716,9 +1716,7 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id):
|
|||||||
'address_zip': 'address_zip',
|
'address_zip': 'address_zip',
|
||||||
'address_state': 'address_state',
|
'address_state': 'address_state',
|
||||||
'address_country': 'address_country',
|
'address_country': 'address_country',
|
||||||
'phone_number': 'phone_number',
|
'beneficiary_password': 'key'
|
||||||
'picture': 'picture',
|
|
||||||
'key': 'key'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for param_key, db_field in field_mapping.items():
|
for param_key, db_field in field_mapping.items():
|
||||||
@ -1742,8 +1740,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id):
|
|||||||
field_mapping = {
|
field_mapping = {
|
||||||
'role_ids': 'role_ids',
|
'role_ids': 'role_ids',
|
||||||
'access_to_deployments': 'access_to_deployments',
|
'access_to_deployments': 'access_to_deployments',
|
||||||
'email': 'email',
|
'beneficiary_email': 'email',
|
||||||
'new_user_name': 'user_name',
|
'beneficiary_user_name': 'user_name',
|
||||||
'first_name': 'first_name',
|
'first_name': 'first_name',
|
||||||
'last_name': 'last_name',
|
'last_name': 'last_name',
|
||||||
'address_street': 'address_street',
|
'address_street': 'address_street',
|
||||||
@ -1751,9 +1749,7 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id):
|
|||||||
'address_zip': 'address_zip',
|
'address_zip': 'address_zip',
|
||||||
'address_state': 'address_state',
|
'address_state': 'address_state',
|
||||||
'address_country': 'address_country',
|
'address_country': 'address_country',
|
||||||
'phone_number': 'phone_number',
|
'beneficiary_password': 'key'
|
||||||
'picture': 'picture',
|
|
||||||
'key': 'key'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prepare values, using None for empty strings
|
# Prepare values, using None for empty strings
|
||||||
@ -1768,8 +1764,8 @@ def StoreBeneficiary2DB(parameters, editing_user_id, user_id):
|
|||||||
INSERT INTO public.person_details
|
INSERT INTO public.person_details
|
||||||
(role_ids, access_to_deployments, email, user_name, first_name, last_name,
|
(role_ids, access_to_deployments, email, user_name, first_name, last_name,
|
||||||
address_street, address_city, address_zip, address_state, address_country,
|
address_street, address_city, address_zip, address_state, address_country,
|
||||||
time_edit, user_edit, phone_number, picture, key)
|
key, time_edit, user_edit)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
RETURNING user_id
|
RETURNING user_id
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -13503,7 +13499,7 @@ class RequestParser:
|
|||||||
|
|
||||||
# Store in context
|
# Store in context
|
||||||
req.context.form_data = form_data
|
req.context.form_data = form_data
|
||||||
logger.debug(f"RequestParser: Parsed form data: {form_data}")
|
#logger.debug(f"RequestParser: Parsed form data: {form_data}")
|
||||||
|
|
||||||
# Reset the stream with the original content
|
# Reset the stream with the original content
|
||||||
req.stream = io.BytesIO(raw_body)
|
req.stream = io.BytesIO(raw_body)
|
||||||
@ -17002,20 +16998,22 @@ class WellApi:
|
|||||||
try:
|
try:
|
||||||
# Use your existing get_form_data function for URL-encoded
|
# Use your existing get_form_data function for URL-encoded
|
||||||
form_data = get_form_data(req)
|
form_data = get_form_data(req)
|
||||||
logger.debug(f"Form data: {form_data}")
|
#logger.debug(f"Form data: {form_data}")
|
||||||
|
|
||||||
# Add the Base64 photo fix
|
# Add the Base64 photo fix
|
||||||
if 'beneficiary_photo' in form_data:
|
if 'beneficiary_photo_data' in form_data:
|
||||||
photo_fixed = quick_fix_base64_photo(form_data)
|
photo_fixed = quick_fix_base64_photo(form_data, form_data["beneficiary_photo"])
|
||||||
if photo_fixed:
|
if photo_fixed:
|
||||||
|
form_data["beneficiary_photo_data"] = ""
|
||||||
logger.debug("Base64 photo processed successfully")
|
logger.debug("Base64 photo processed successfully")
|
||||||
|
|
||||||
|
logger.debug(f"Form data: {form_data}")
|
||||||
# Your existing processing logic continues here with form_data...
|
# Your existing processing logic continues here with form_data...
|
||||||
|
|
||||||
|
|
||||||
# Get form data using our helper function - but don't read stream again
|
# Get form data using our helper function - but don't read stream again
|
||||||
#form_data = get_form_data(req)
|
#form_data = get_form_data(req)
|
||||||
logger.debug(f"Form data: {form_data}")
|
#logger.debug(f"Form data: {form_data}")
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -18356,7 +18354,7 @@ class WellApi:
|
|||||||
|
|
||||||
deployment = form_data.get('deployment')
|
deployment = form_data.get('deployment')
|
||||||
beneficiary_name = form_data.get('beneficiary_name')
|
beneficiary_name = form_data.get('beneficiary_name')
|
||||||
image_file_name = beneficiary_name.replace(" ","_") + ".jpg"
|
image_file_name = form_data["beneficiary_photo"]
|
||||||
gps_lat = form_data.get('lat')
|
gps_lat = form_data.get('lat')
|
||||||
gps_lng = form_data.get('lng')
|
gps_lng = form_data.get('lng')
|
||||||
time_zone_s = GetTZFromGPS(gps_lat, gps_lng)
|
time_zone_s = GetTZFromGPS(gps_lat, gps_lng)
|
||||||
@ -18364,19 +18362,19 @@ class WellApi:
|
|||||||
|
|
||||||
#debug_received_data(form_data)
|
#debug_received_data(form_data)
|
||||||
# Or better yet, update to handle both cases for backward compatibility:
|
# Or better yet, update to handle both cases for backward compatibility:
|
||||||
if form_data.get("file_sent", "") != "":
|
#if form_data.get("beneficiary_photo", "") != "":
|
||||||
# Old Base64 method
|
# # Old Base64 method
|
||||||
StoreFile2Blob(form_data["file_sent"], image_file_name, "user-pictures")
|
# StoreFile2Blob(form_data["beneficiary_photo"], image_file_name, "user-pictures")
|
||||||
elif "beneficiary_photo" in files:
|
#elif "beneficiary_photo" in files:
|
||||||
# New multipart method
|
## New multipart method
|
||||||
try:
|
#try:
|
||||||
with open('beneficiary.jpg', 'rb') as f:
|
#with open('beneficiary.jpg', 'rb') as f:
|
||||||
image_data = f.read()
|
#image_data = f.read()
|
||||||
image_base64 = base64.b64encode(image_data).decode('utf-8')
|
#image_base64 = base64.b64encode(image_data).decode('utf-8')
|
||||||
StoreFile2Blob(image_base64, image_file_name, "user-pictures")
|
#StoreFile2Blob(image_base64, image_file_name, "user-pictures")
|
||||||
os.remove('beneficiary.jpg')
|
#os.remove('beneficiary.jpg')
|
||||||
except Exception as e:
|
#except Exception as e:
|
||||||
logger.error(f"Failed to process beneficiary photo: {e}")
|
#logger.error(f"Failed to process beneficiary photo: {e}")
|
||||||
|
|
||||||
devices_list = json.loads(devices)
|
devices_list = json.loads(devices)
|
||||||
deployments_id = []
|
deployments_id = []
|
||||||
@ -18402,7 +18400,7 @@ class WellApi:
|
|||||||
if deployment == "NEW":
|
if deployment == "NEW":
|
||||||
if in_db_user_name != None:
|
if in_db_user_name != None:
|
||||||
#this email exists in DB, so cannot be used for NEW deployment!
|
#this email exists in DB, so cannot be used for NEW deployment!
|
||||||
error_string = f"This email cannot be used for new account!"
|
error_string = f"This email already has associated account!"
|
||||||
print(error_string)
|
print(error_string)
|
||||||
payload = {'ok': 0, 'error': error_string}
|
payload = {'ok': 0, 'error': error_string}
|
||||||
resp.media = package_response(payload)
|
resp.media = package_response(payload)
|
||||||
@ -18455,14 +18453,14 @@ class WellApi:
|
|||||||
form_data['picture'] = image_file_name
|
form_data['picture'] = image_file_name
|
||||||
form_data['key'] = password
|
form_data['key'] = password
|
||||||
#update person_details
|
#update person_details
|
||||||
|
#if in_db_user_name == None, than we really need to create new person.
|
||||||
beneficiary_id, if_new = StoreBeneficiary2DB(form_data, editing_user_id, user_id) #this will update or create beneficiary
|
beneficiary_id, if_new = StoreBeneficiary2DB(form_data, editing_user_id, user_id) #this will update or create beneficiary
|
||||||
#lets check if devices listed are not part of existing deployment
|
#lets check if devices listed are not part of existing deployment
|
||||||
if reuse_existing_devices == "0": #do re-use
|
if reuse_existing_devices == "0": #do re-use
|
||||||
success, result = DevicesNotUsed(devices, user_name)
|
success, result = DevicesNotUsed(devices, user_name)
|
||||||
else:
|
else:
|
||||||
success = True
|
success = True
|
||||||
result["deployed"] = []
|
result = {"deployed": [], "not_found": []}
|
||||||
result["not_found"] = []
|
|
||||||
|
|
||||||
if success: #this should always be true!
|
if success: #this should always be true!
|
||||||
if result["deployed"]:
|
if result["deployed"]:
|
||||||
@ -18515,7 +18513,7 @@ class WellApi:
|
|||||||
form_data_temp['wifis'] = form_data.get('wifis')
|
form_data_temp['wifis'] = form_data.get('wifis')
|
||||||
form_data_temp['lat'] = form_data.get('lat')
|
form_data_temp['lat'] = form_data.get('lat')
|
||||||
form_data_temp['lng'] = form_data.get('lng')
|
form_data_temp['lng'] = form_data.get('lng')
|
||||||
form_data_temp['gps_age'] = form_data.get('gps_age')
|
form_data_temp['gps_age'] = str(int(float(form_data.get('gps_age'))))
|
||||||
form_data_temp['time_zone_s'] = time_zone_s
|
form_data_temp['time_zone_s'] = time_zone_s
|
||||||
#["64B7088909FC", "64B7088909B8", "901506CA3DA0", "142B2F81A020", "64B7088905BC", "64B708890898", "64B70888FAD4","64B7088904BC"]
|
#["64B7088909FC", "64B7088909B8", "901506CA3DA0", "142B2F81A020", "64B7088905BC", "64B708890898", "64B70888FAD4","64B7088904BC"]
|
||||||
form_data_temp['devices'] = WellIDs2MAC(form_data.get('devices'))
|
form_data_temp['devices'] = WellIDs2MAC(form_data.get('devices'))
|
||||||
@ -18525,7 +18523,7 @@ class WellApi:
|
|||||||
if is_new_deployment == 1 and if_new:
|
if is_new_deployment == 1 and if_new:
|
||||||
#This is new email, therefore person, so we need to give him access to only this deployment
|
#This is new email, therefore person, so we need to give him access to only this deployment
|
||||||
form_data['access_to_deployments'] = str(editing_deployment_id)
|
form_data['access_to_deployments'] = str(editing_deployment_id)
|
||||||
beneficiary_id, if_new = StoreBeneficiary2DB(form_data, editing_user_id, user_id)
|
beneficiary_id, if_new = StoreBeneficiary2DB(form_data, beneficiary_id, user_id)
|
||||||
|
|
||||||
#deployment_id = deployment_details[0]
|
#deployment_id = deployment_details[0]
|
||||||
#we need to update deployment_history table if devices changed
|
#we need to update deployment_history table if devices changed
|
||||||
@ -18548,14 +18546,24 @@ class WellApi:
|
|||||||
print(devices_in_history_last)
|
print(devices_in_history_last)
|
||||||
ok = 1
|
ok = 1
|
||||||
|
|
||||||
|
|
||||||
|
#at this step, we know deployment_id
|
||||||
|
if form_data.get("beneficiary_photo", "") != "":
|
||||||
|
image_file_name = form_data.get("beneficiary_photo", "")
|
||||||
|
if image_file_name == "NEW.jpg":
|
||||||
|
image_file_name = f"{editing_deployment_id}_{beneficiary_name}"
|
||||||
|
image_file_name = image_file_name.replace(" ", "_")
|
||||||
|
StoreFile2Blob(form_data["beneficiary_photo"], image_file_name, "user-pictures")
|
||||||
|
|
||||||
|
|
||||||
if credentials_changed:
|
if credentials_changed:
|
||||||
if if_new == 1:
|
if if_new == 1:
|
||||||
#we need to call cd ~/mqtt-auth-service/acl_manager.py
|
#we need to call cd ~/mqtt-auth-service/acl_manager.py
|
||||||
#we created new beneficiary and he needs welcome email
|
#we created new beneficiary and he needs welcome email
|
||||||
#if deployment == "NEW":
|
#if deployment == "NEW":
|
||||||
SendWelcomeBeneficiaryEmail(form_data['email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'])
|
SendWelcomeBeneficiaryEmail(form_data['beneficiary_email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'], signature)
|
||||||
else:
|
else:
|
||||||
SendCredentialsChangedEmail(form_data['email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'])
|
SendCredentialsChangedEmail(form_data['beneficiary_email'], form_data['first_name'], form_data['last_name'], devices, form_data['phone_number'], form_data['new_user_name'], form_data['key'], signature)
|
||||||
else:
|
else:
|
||||||
if devices_changed:
|
if devices_changed:
|
||||||
CallICLUpdate()
|
CallICLUpdate()
|
||||||
@ -20063,7 +20071,7 @@ class WellApi:
|
|||||||
|
|
||||||
device_ids, device_list = MACsToWellIds(cur, macs_list_clean)
|
device_ids, device_list = MACsToWellIds(cur, macs_list_clean)
|
||||||
|
|
||||||
devices_details[deployment_id] = deployment
|
devices_details[deployment_id] = (deployment, device_ids, device_list, users_formatted)
|
||||||
|
|
||||||
elif deployment_id in privileges_lst:
|
elif deployment_id in privileges_lst:
|
||||||
deployment = DeploymentDetails(deployment_id)
|
deployment = DeploymentDetails(deployment_id)
|
||||||
@ -20078,29 +20086,34 @@ class WellApi:
|
|||||||
macs_list_clean.append(mac)
|
macs_list_clean.append(mac)
|
||||||
|
|
||||||
device_ids, device_list = MACsToWellIds(cur, macs_list_clean)
|
device_ids, device_list = MACsToWellIds(cur, macs_list_clean)
|
||||||
devices_details[deployment_id] = deployment
|
devices_details[deployment_id] = (deployment, device_ids, device_list, users_formatted)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for entry in result:
|
||||||
|
deployment_id = str(entry[0])
|
||||||
device_list_to_report = []
|
device_list_to_report = []
|
||||||
|
device_list = devices_details[deployment_id][2]
|
||||||
|
users_formatted = devices_details[deployment_id][3]
|
||||||
for device in device_list:
|
for device in device_list:
|
||||||
#we need well_id, location and decription only
|
#we need well_id, location and decription only
|
||||||
device_list_to_report.append((device[0], device[2], device[3]))
|
device_list_to_report.append((device[0], device[2], device[3]))
|
||||||
|
|
||||||
sql = f"SELECT first_name, last_name, email, picture FROM public.person_details WHERE user_id IN ({users_formatted});"
|
sql = f"SELECT first_name, last_name, email, picture FROM public.person_details WHERE user_id IN ({users_formatted});"
|
||||||
print(sql)
|
print(sql)
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
result1 = cur.fetchall()
|
result1 = cur.fetchall()
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
for entry in result:
|
|
||||||
deployment_id = str(entry[0])
|
deployment_id = str(entry[0])
|
||||||
deployment = devices_details[deployment_id]
|
deployment_t = devices_details[deployment_id][0]
|
||||||
first_name, last_name, email, photo = result1[counter]
|
first_name, last_name, email, photo = result1[counter]
|
||||||
deployment["beneficiary_first_name"] = first_name
|
deployment_t["beneficiary_first_name"] = first_name
|
||||||
deployment["beneficiary_last_name"] = last_name
|
deployment_t["beneficiary_last_name"] = last_name
|
||||||
deployment["beneficiary_email"] = email
|
deployment_t["beneficiary_email"] = email
|
||||||
deployment["photo"] = photo
|
deployment_t["photo"] = photo
|
||||||
devices_details[deployment_id] = deployment
|
to_report.append((entry, device_list_to_report, deployment_t))
|
||||||
to_report.append((entry, device_list_to_report, devices_details[deployment_id]))
|
|
||||||
|
|
||||||
|
|
||||||
print(to_report)
|
print(to_report)
|
||||||
@ -21689,9 +21702,9 @@ def parse_multipart_data(request_body, content_type):
|
|||||||
|
|
||||||
return form_data, files
|
return form_data, files
|
||||||
|
|
||||||
def quick_fix_base64_photo(form_data):
|
def quick_fix_base64_photo(form_data, file_name):
|
||||||
"""Fix corrupted Base64 photo from URL-encoded form"""
|
"""Fix corrupted Base64 photo from URL-encoded form"""
|
||||||
photo_b64 = form_data.get('beneficiary_photo')
|
photo_b64 = form_data.get('beneficiary_photo_data')
|
||||||
|
|
||||||
if not photo_b64:
|
if not photo_b64:
|
||||||
return False
|
return False
|
||||||
@ -21710,10 +21723,10 @@ def quick_fix_base64_photo(form_data):
|
|||||||
image_data = base64.b64decode(clean)
|
image_data = base64.b64decode(clean)
|
||||||
|
|
||||||
if image_data[:3] == b'\xff\xd8\xff':
|
if image_data[:3] == b'\xff\xd8\xff':
|
||||||
with open('beneficiary.jpg', 'wb') as f:
|
with open(file_name, 'wb') as f:
|
||||||
f.write(image_data)
|
f.write(image_data)
|
||||||
|
|
||||||
print(f"Base64 photo beneficiary.jpg fixed and saved: {len(image_data)} bytes")
|
print(f"Base64 photo {file_name} fixed and saved: {len(image_data)} bytes")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logger.error("Invalid JPEG header in Base64 data")
|
logger.error("Invalid JPEG header in Base64 data")
|
||||||
@ -21987,7 +22000,7 @@ def SendWelcomeCaretakerEmail(email, first_name, last_name, devices, phone_numbe
|
|||||||
redis_conn.xadd('messaging_requests_stream', queue_data)
|
redis_conn.xadd('messaging_requests_stream', queue_data)
|
||||||
|
|
||||||
|
|
||||||
def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_number, user_name, password):
|
def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_number, user_name, password, signature):
|
||||||
logger.error(f"Requesting welcome beneficiary email to {email}")
|
logger.error(f"Requesting welcome beneficiary email to {email}")
|
||||||
queue_data = {
|
queue_data = {
|
||||||
'function': "new_beneficiary",
|
'function': "new_beneficiary",
|
||||||
@ -22004,7 +22017,7 @@ def SendWelcomeBeneficiaryEmail(email, first_name, last_name, devices, phone_num
|
|||||||
}
|
}
|
||||||
redis_conn.xadd('messaging_requests_stream', queue_data)
|
redis_conn.xadd('messaging_requests_stream', queue_data)
|
||||||
|
|
||||||
def SendCredentialsChangedEmail(email, first_name, last_name, devices, phone_number, user_name, password):
|
def SendCredentialsChangedEmail(email, first_name, last_name, devices, phone_number, user_name, password, signature):
|
||||||
logger.error(f"Requesting credentials changed of beneficiary email to {email}")
|
logger.error(f"Requesting credentials changed of beneficiary email to {email}")
|
||||||
queue_data = {
|
queue_data = {
|
||||||
'function': "credentials_updated",
|
'function': "credentials_updated",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user