venkata akhil
1 year ago
2 changed files with 4 additions and 322 deletions
@ -1,321 +0,0 @@ |
|||
import frappe |
|||
from frappe.model.document import Document |
|||
import json |
|||
import os |
|||
from frappe.utils import cstr |
|||
|
|||
from smart_service.apis.app_user_login import input_validation |
|||
|
|||
JSON_EXT = ".json" |
|||
JSON_INT_PATH = "/files/json_files/internal/" |
|||
JSON_FULL_INT_PATH = "/files/json_files/full_update/internal/" |
|||
JSON_GLOABL_PATH = "/files/json_files/global/" |
|||
JSON_FULL_GLOBAL_PATH = "/files/json_files/full_update/" |
|||
STATUS = "status" |
|||
ERROR = "error" |
|||
UPDATE_AVAILABLE = 'IsUpdateAvailable' |
|||
UPDATE_FAILED = "Update checking Failed" |
|||
PARAM_MISSING = "Parameter missing :" |
|||
VERSION = 'version' |
|||
CUR_VERSION = 'CurrentVersion' |
|||
DIS = 'Discription' |
|||
FILE_ERROR = 'File failed to load' |
|||
LANGUAGE = 'language' |
|||
FILE_SIZE = 'FileSize' |
|||
JSON_URL = 'JsonURL' |
|||
REMARKS = 'remarks' |
|||
|
|||
|
|||
site_name = cstr(frappe.local.site) |
|||
base_url = os.path.expanduser( |
|||
"~") + "/frappe-bench/sites/" + site_name + "/public" |
|||
|
|||
|
|||
def custom_rate_limit(limit, seconds): |
|||
ip_based = True |
|||
ip = frappe.local.request_ip if ip_based is True else None |
|||
identity = None |
|||
|
|||
identity = ip |
|||
cache_key = f"rl:{frappe.form_dict.cmd}:{identity}" |
|||
value = frappe.cache().get(cache_key) or 0 |
|||
if not value: |
|||
frappe.cache().setex(cache_key, seconds, 0) |
|||
|
|||
value = frappe.cache().incrby(cache_key, 1) |
|||
if value > limit: |
|||
frappe.local.response["http_status_code"] = 429 |
|||
return "You hit the rate limit because of too many requests. Please try after sometime." |
|||
return 1 |
|||
|
|||
|
|||
@frappe.whitelist(allow_guest=1) |
|||
def check_all_vehicle_updates(vehicle_list=None): |
|||
""" Validate Input's """ |
|||
val = input_validation(vehicle_list=vehicle_list) |
|||
|
|||
""" Error response based on missing Input arguements """ |
|||
if val != '': |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: PARAM_MISSING+val} |
|||
|
|||
if vehicle_list: |
|||
""" Validate Input keys """ |
|||
try: |
|||
vehicle_req_list = [] |
|||
v_list = json.loads(vehicle_list) |
|||
language = v_list['LanguageID'] |
|||
iid = v_list['InstallationId'] |
|||
vehicle_data = v_list['VehicleReqList'] |
|||
publish_type = frappe.db.sql( |
|||
'''SELECT publish_type FROM `tabApp Device` where name='{}';'''.format(iid), as_list=1) |
|||
|
|||
if not publish_type: |
|||
""" Error Response for Invalid Publish Type """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: "Publish Type not set "} |
|||
|
|||
for v in vehicle_data: |
|||
vehicle_id = get_vehicle_id(v['Vehicle']) |
|||
publish_response = get_all_vehicle_publish_data( |
|||
v['Vehicle'], language, float(v['CurrentVersion']), publish_type[0][0], vehicle_id) |
|||
|
|||
vehicle_req_list.append(publish_response) |
|||
|
|||
""" Vehicle List Response """ |
|||
return {"LanguageID": language, |
|||
"VehicleReqList": vehicle_req_list, STATUS: 1, ERROR: 'No Error'} |
|||
|
|||
except Exception as e: |
|||
""" Error Response for Input key's validation """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: str(e)} |
|||
|
|||
else: |
|||
""" Error Response for Empty Vehicle List """ |
|||
return {"LanguageID": None, |
|||
"VehicleReqList": [], STATUS: 0, ERROR: "Check Arugument: vehicle list"} |
|||
|
|||
|
|||
def get_vehicle_id(vehicle): |
|||
vehicle_id = frappe.db.sql( |
|||
'''select myid as vehicle_id from `tabVehicle` where vehicle ='{0}'; '''.format(vehicle), as_dict=1) |
|||
if vehicle_id: |
|||
return vehicle_id[0]['vehicle_id'] |
|||
else: |
|||
False |
|||
|
|||
|
|||
def get_all_vehicle_publish_data(vehicle, l_id, current_version, publish_type, vehicle_id): |
|||
""" Response Structure """ |
|||
vehicle_response_data = { |
|||
"name": None, |
|||
"version": 0, |
|||
"vehicle_id": vehicle_id, |
|||
"language": l_id, |
|||
"description": None, |
|||
ERROR: None, |
|||
UPDATE_AVAILABLE: False, |
|||
"CurrentVersion": current_version |
|||
} |
|||
latest_global_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and publish_type='Global' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
latest_publish_version = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id,language,release_description as description |
|||
FROM `tabPublish` where vehicle='{0}' and language='{1}' and publish_status='Published' and vehicle_status='Active' |
|||
and publish_module='Automotive System' and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and vehicle='{0}' and language='{1}' and version > {2}); |
|||
'''.format(vehicle, l_id, current_version), as_dict=1) |
|||
|
|||
if not latest_publish_version: |
|||
""" Latest File already downloaded Global/Internal """ |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Latest Version" |
|||
|
|||
else: |
|||
if not latest_global_version or (current_version >= float(latest_global_version[0]['version'])): |
|||
|
|||
""" Only Internal available """ |
|||
|
|||
if publish_type == 'Internal': |
|||
value = round( |
|||
float(latest_publish_version[0]['version']) - current_version, 2) |
|||
|
|||
if value == 0.01: |
|||
""" 1 Internal File """ |
|||
data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
|
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Full Internal File """ |
|||
data = frappe.db.sql('''SELECT name, format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' and publish_type='Internal' |
|||
and vehicle='{0}' and language='{1}' ); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_FULL_INT_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
if os.path.exists(base_url + file_name): |
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Internal Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "No Update available" |
|||
|
|||
elif current_version < float(latest_global_version[0]['version']): |
|||
""" Only Global available """ |
|||
|
|||
if (float(latest_global_version[0]['version']) - int(current_version)) > 1: |
|||
""" Full Global File """ |
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
if data: |
|||
file_name = JSON_FULL_GLOBAL_PATH + vehicle + "/" + vehicle + \ |
|||
"-" + l_id + "-"+"full_v" + \ |
|||
str(data[0][VERSION] |
|||
) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
|
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global Full Update available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
# vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
# vehicle_response_data[REMARKS] = "Full Update Global available" |
|||
else: |
|||
|
|||
""" Single Global File """ |
|||
# vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
# vehicle_response_data[REMARKS] = "Global available" |
|||
|
|||
data = frappe.db.sql('''SELECT name,format(version,2) as version,vehicle_id, language, |
|||
release_description as description FROM `tabPublish` where vehicle='{0}' and language='{1}' and |
|||
publish_status='Published' and vehicle_status='Active' and publish_module='Automotive System' |
|||
and version = (select max(version) as version from `tabPublish` |
|||
where publish_module='Automotive System' |
|||
and vehicle='{0}' and language='{1}' and publish_type ='Global'); |
|||
'''.format(vehicle, l_id), as_dict=1) |
|||
|
|||
if data: |
|||
file_name = JSON_GLOABL_PATH+vehicle+"/"+vehicle + \ |
|||
"-" + l_id + "_v" + \ |
|||
str(data[0][VERSION]) + JSON_EXT |
|||
|
|||
if os.path.exists(base_url + file_name): |
|||
|
|||
vehicle_response_data['name'] = data[0]['name'] |
|||
vehicle_response_data['version'] = data[0]['version'] |
|||
# vehicle_response_data['vehicle_id'] = data[0]['vehicle_id'] |
|||
vehicle_response_data['language'] = data[0]['language'] |
|||
vehicle_response_data['description'] = data[0]['description'] |
|||
|
|||
# vehicle_response_data['FileSize'] = get_file_size( |
|||
# file_name) |
|||
# vehicle_response_data['JsonURL'] = file_name |
|||
|
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
|
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
vehicle_response_data[ERROR] = "Failed to read file" |
|||
else: |
|||
vehicle_response_data[UPDATE_AVAILABLE] = False |
|||
vehicle_response_data[REMARKS] = "Global available" |
|||
vehicle_response_data[ERROR] = "Failed to get Publish version" |
|||
|
|||
else: |
|||
""" Unknow condition Dont Know Yet """ |
|||
value = {'latest_publish_version': latest_publish_version[0]['version'], |
|||
" latest_global_version": latest_global_version[0]['version']} |
|||
frappe.log_error('New check Update', str( |
|||
f'Unknow Condition{value}')) |
|||
vehicle_response_data[UPDATE_AVAILABLE] = True |
|||
vehicle_response_data[REMARKS] = "New Condition" |
|||
|
|||
return vehicle_response_data |
Loading…
Reference in new issue