From 3d0f7bdb89f93026609f530e7f1c7a5ddc44bfd3 Mon Sep 17 00:00:00 2001 From: venkataakhil Date: Tue, 16 May 2023 17:16:41 +0530 Subject: [PATCH] base url append --- smart_service/apis/addon_api.py | 48 ++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/smart_service/apis/addon_api.py b/smart_service/apis/addon_api.py index 5d92505..1348fd1 100644 --- a/smart_service/apis/addon_api.py +++ b/smart_service/apis/addon_api.py @@ -5,8 +5,12 @@ import base64 import os from frappe.utils import cstr from smart_service.apis.app_user_login import input_validation +site_name = cstr(frappe.local.site) +base_url = os.path.expanduser( + "~") + "/frappe-bench/sites/" + site_name + "/public" current_db_name = frappe.conf.get("db_name") + def custom_rate_limit(limit, seconds): ip_based = True ip = frappe.local.request_ip if ip_based is True else None @@ -23,7 +27,8 @@ def custom_rate_limit(limit, seconds): 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() def addon(args=None): rate_res = custom_rate_limit(limit=5, seconds=15) @@ -31,8 +36,8 @@ def addon(args=None): return rate_res # if args == None: # return {"status": 0, "error": "Parameter missing: args"} - val=input_validation(args=args) - if val!='': + val = input_validation(args=args) + if val != '': return {"status": 0, "error": "Input parameter Missing: " + val} if args: @@ -70,7 +75,8 @@ def addon(args=None): elif args == "teknet_module": api = frappe.db.get_list( "Teknet Module", - fields=["name", "teknet_module_name", "active_status", "flvl_id"], + fields=["name", "teknet_module_name", + "active_status", "flvl_id"], ) for i in api: if i["active_status"] == "Active": @@ -82,7 +88,8 @@ def addon(args=None): api = frappe.get_doc('User Manual') else: - api = {"status": 0, "error": "No such function. Check argument: args"} + api = {"status": 0, + "error": "No such function. Check argument: args"} except Exception as e: api = {"status": 0, "error": "Module not available"} @@ -98,8 +105,8 @@ def bookmark(args=None, request=None): if rate_res != 1: return rate_res - val=input_validation(args=args,request=request) - if val!='': + val = input_validation(args=args, request=request) + if val != '': return {"status": 0, "error": "Input parameter Missing: " + val} # if args == None and request == None: # return {"status": 0, "error": "Parameter's missing: args & request"} @@ -160,8 +167,8 @@ def feedback(args=None, request=None): rate_res = custom_rate_limit(limit=5, seconds=15) if rate_res != 1: return rate_res - val=input_validation(args=args,request=request) - if val!='': + val = input_validation(args=args, request=request) + if val != '': return {"status": 0, "error": "Input parameter Missing: " + val} # if args == None and request == None: # return {"status": 0, "error": "Parameter's missing: args & request"} @@ -176,8 +183,8 @@ def feedback(args=None, request=None): request = json.loads(request) api = frappe.db.sql( '''SELECT name,feedback_type,subject,status,concat(DATE(date),"T",time(date)) as date,user_name,attachments,description FROM {0}.`tabUser Feedback` where user_name = '{1}';''' - .format(current_db_name, - frappe.db.get_value('App Users', {'user_id': request['UserID']}, ['name'])), + .format(current_db_name, + frappe.db.get_value('App Users', {'user_id': request['UserID']}, ['name'])), as_dict=True) for i in api: @@ -217,7 +224,7 @@ def feedback(args=None, request=None): file_name = x file_data = f[x] - file_url = "/home/ubuntu/frappe-bench/sites/" + site_name + "/public/files/" + file_name + file_url = base_url+"/files/" + file_name with open(file_url, "wb") as img: img.write(base64.b64decode(file_data)) @@ -231,7 +238,8 @@ def feedback(args=None, request=None): ret.is_private = 0 ret.insert(ignore_permissions=True) file_response.append(ret) - api = {"isSuccessful": True, "name": usr_fdbk.name, "files": file_response} + api = {"isSuccessful": True, + "name": usr_fdbk.name, "files": file_response} except Exception as e: api = {"status": 0, "error": str(frappe.get_traceback())} else: @@ -246,8 +254,8 @@ def model_hit(args=None, request=None): rate_res = custom_rate_limit(limit=5, seconds=15) if rate_res != 1: return rate_res - val=input_validation(args=args,request=request) - if val!='': + val = input_validation(args=args, request=request) + if val != '': return {"status": 0, "error": "Input parameter Missing: " + val} # if args == None and request == None: # return {"status": 0, "error": "Parameter's missing: args & request"} @@ -275,7 +283,8 @@ def model_hit(args=None, request=None): model_hit.download_vehicle_date = i['DownloadVehicleDate'] model_hit.download_vehicle_language_id = i['DownloadVehicleLanguageID'] model_hit.download_vehicle_current_version = i['DownloadVehicleCurrentVersion'] - model_hit.insert(ignore_if_duplicate=True, ignore_mandatory=True) + model_hit.insert(ignore_if_duplicate=True, + ignore_mandatory=True) return {"isSuccessful": True} except Exception as e: @@ -292,8 +301,8 @@ def model_usage(args=None, request=None): rate_res = custom_rate_limit(limit=5, seconds=15) if rate_res != 1: return rate_res - val=input_validation(args=args,request=request) - if val!='': + val = input_validation(args=args, request=request) + if val != '': return {"status": 0, "error": "Input parameter Missing: " + val} # if args == None and request == None: # return {"status": 0, "error": "Parameter's missing: args & request"} @@ -326,7 +335,8 @@ def model_usage(args=None, request=None): model_usage.steps_id = i['StepsID'] model_usage.menu_id = i['MenuID'] model_usage.language = i['Language'] - model_usage.insert(ignore_if_duplicate=True, ignore_mandatory=True) + model_usage.insert( + ignore_if_duplicate=True, ignore_mandatory=True) return {"isSuccessful": True} except Exception as e: