Browse Source

Fix for app log installation/reinstall issue

master
administrator 11 months ago
parent
commit
a8aae0a52b
  1. 163
      smart_service/apis/app_user_login.py

163
smart_service/apis/app_user_login.py

@ -32,18 +32,16 @@ def custom_rate_limit(limit, seconds):
return 1 return 1
# Temporary Login # Temporary Login
# @frappe.whitelist(allow_guest=True)
# def app_user_validation_test():
@frappe.whitelist(allow_guest=True) # dicData = {}
def app_user_validation_test(): # try:
dicData = {} # doc = frappe.get_doc("Fish", None)
try: # dicData['token'] = doc.apple
doc = frappe.get_doc("Fish", None) # dicData['status'] = 1
dicData['token'] = doc.apple # except:
dicData['status'] = 1 # return {'status': 0, 'error': 'Site Error'}
except: # return dicData
return {'status': 0, 'error': 'Site Error'}
return dicData
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
@ -60,13 +58,12 @@ def app_user_validation(usr=None, pwd=None):
technician_login = preference.technician_login_api technician_login = preference.technician_login_api
# ISmartAuth API - Email available # ISmartAuth API - Email available
try: try:
data = {'LoginID': usr, 'Password': pwd} data = {'LoginID': usr, 'Password': pwd}
headers = {"Content-Type": "application/x-www-form-urlencoded"} headers = {"Content-Type": "application/x-www-form-urlencoded"}
if technician_login == 1 and mahindra_login == 0: if technician_login == 1 and mahindra_login == 0:
response = requests.post( response = requests.post(
"http://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers) "https://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers)
decoded = response.content.decode('utf-8') decoded = response.content.decode('utf-8')
if len(decoded) > 165: if len(decoded) > 165:
@ -85,7 +82,7 @@ def app_user_validation(usr=None, pwd=None):
return {'status': 0, 'error': 'Invalid Credentials'} return {'status': 0, 'error': 'Invalid Credentials'}
elif technician_login == 0 and mahindra_login == 1: elif technician_login == 0 and mahindra_login == 1:
response1 = requests.post("http://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential", response1 = requests.post("https://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential",
data=data, headers=headers) data=data, headers=headers)
decoded1 = response1.content.decode('utf-8') decoded1 = response1.content.decode('utf-8')
node = ET.fromstring(decoded1) node = ET.fromstring(decoded1)
@ -131,10 +128,10 @@ def app_user_validation(usr=None, pwd=None):
elif technician_login == 1 and mahindra_login == 1: elif technician_login == 1 and mahindra_login == 1:
response = requests.post( response = requests.post(
"http://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers) "https://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers)
decoded = response.content.decode('utf-8') decoded = response.content.decode('utf-8')
response1 = requests.post("http://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential", response1 = requests.post("https://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential",
data=data, headers=headers) data=data, headers=headers)
decoded1 = response1.content.decode('utf-8') decoded1 = response1.content.decode('utf-8')
node = ET.fromstring(decoded1) node = ET.fromstring(decoded1)
@ -218,7 +215,6 @@ def grant_user_access(userdata=None, iid=None):
area_name = userdata['AreaName'] area_name = userdata['AreaName']
zone = userdata['Zone'] zone = userdata['Zone']
dealer_code = userdata['Code'] dealer_code = userdata['Code']
dealer_name = userdata['DealerName'] dealer_name = userdata['DealerName']
app_current_version = userdata['AppCurrentVersion'] app_current_version = userdata['AppCurrentVersion']
@ -234,9 +230,7 @@ def grant_user_access(userdata=None, iid=None):
frappe.set_user("Administrator") frappe.set_user("Administrator")
user_name = frappe.db.sql('''SELECT name FROM {0}.tabUser where email='{1}';'''.format( user_name = frappe.db.sql('''SELECT name FROM {0}.tabUser where email='{1}';'''.format(
current_db_name, email), as_dict=1) current_db_name, email), as_dict=1)
# return user_name
if user_name: if user_name:
# token = generate_keys(user_name[0]['name'])
try: try:
""" New Refresh token update """ """ New Refresh token update """
access_token = generate_access_token(user_name[0]['name']) access_token = generate_access_token(user_name[0]['name'])
@ -259,7 +253,6 @@ def grant_user_access(userdata=None, iid=None):
user_name = frappe.db.sql('''SELECT name FROM {0}.tabUser where email='{1}';'''.format( user_name = frappe.db.sql('''SELECT name FROM {0}.tabUser where email='{1}';'''.format(
current_db_name, email), as_dict=1) current_db_name, email), as_dict=1)
if user_name: if user_name:
# token = generate_keys(user_name[0]['name'])
try: try:
""" New Refresh token update """ """ New Refresh token update """
access_token = generate_access_token(user_name[0]['name']) access_token = generate_access_token(user_name[0]['name'])
@ -306,16 +299,6 @@ def grant_user_access(userdata=None, iid=None):
'''Inserting Device data''' '''Inserting Device data'''
if iid == 'None' or iid == 'null': if iid == 'None' or iid == 'null':
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
if frappe.db.exists({'doctype': 'App Device', 'device_id': device_id}): if frappe.db.exists({'doctype': 'App Device', 'device_id': device_id}):
frappe.db.sql( frappe.db.sql(
'''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where device_id = "{0}";'''.format(device_id, '''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where device_id = "{0}";'''.format(device_id,
@ -328,21 +311,27 @@ def grant_user_access(userdata=None, iid=None):
app_device1 = frappe.get_last_doc( app_device1 = frappe.get_last_doc(
'App Device', filters={"device_id": device_id}) 'App Device', filters={"device_id": device_id})
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
app_logs = frappe.new_doc('App Log') app_logs = frappe.new_doc('App Log')
app_logs.user = app_user1.name app_logs.user = app_user1.name
app_logs.device = app_device.name app_logs.device = app_device.name
app_logs.type = 'Reinstalled' app_logs.type = 'Reinstalled'
app_logs.latitude = latitude app_logs.latitude = latitude
app_logs.longitude = longitude app_logs.longitude = longitude
# app_logs.active_status = 'Active'
app_logs.date_time_stamp = now() app_logs.date_time_stamp = now()
app_logs.insert() app_logs.insert()
app_device_tmp = frappe.get_last_doc( app_device_tmp = frappe.get_last_doc(
'App Device', filters={"device_id": device_id}) 'App Device', filters={"device_id": device_id})
frappe.db.sql('''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where name = "{0}";'''.format(
app_device_tmp.name, "Active", current_db_name))
frappe.db.commit()
app_logs3 = frappe.new_doc('App Log') app_logs3 = frappe.new_doc('App Log')
app_logs3.user = app_user1.name app_logs3.user = app_user1.name
@ -354,6 +343,16 @@ def grant_user_access(userdata=None, iid=None):
app_logs3.insert() app_logs3.insert()
else: else:
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
app_user1 = frappe.get_last_doc( app_user1 = frappe.get_last_doc(
'App Users', filters={"email_id": email}) 'App Users', filters={"email_id": email})
app_device1 = frappe.get_last_doc( app_device1 = frappe.get_last_doc(
@ -362,7 +361,6 @@ def grant_user_access(userdata=None, iid=None):
app_logs.user = app_user1.name app_logs.user = app_user1.name
app_logs.device = app_device.name app_logs.device = app_device.name
app_logs.type = 'Installed' app_logs.type = 'Installed'
# app_logs.active_status = 'Active'
app_logs.latitude = latitude app_logs.latitude = latitude
app_logs.longitude = longitude app_logs.longitude = longitude
app_logs.date_time_stamp = now() app_logs.date_time_stamp = now()
@ -378,9 +376,6 @@ def grant_user_access(userdata=None, iid=None):
app_logs4.save() app_logs4.save()
else: else:
# frappe.db.sql(
# '''UPDATE {4}.`tabApp Device` set last_login= '{0}', user_name = '{2}', device_type = '{3}' where name = "{1}";'''.format(
# now(), iid, email, user_type_desc, current_db_name))
frappe.db.sql( frappe.db.sql(
'''UPDATE {3}.`tabApp Device` set last_login= '{0}', device_type = '{2}' where name = "{1}";'''.format( '''UPDATE {3}.`tabApp Device` set last_login= '{0}', device_type = '{2}' where name = "{1}";'''.format(
now(), iid, device_type, current_db_name)) now(), iid, device_type, current_db_name))
@ -406,7 +401,6 @@ def grant_user_access(userdata=None, iid=None):
user_details = {} user_details = {}
user_details['Installation ID'] = lst[0][0] user_details['Installation ID'] = lst[0][0]
user_details['User ID'] = lst1[0][0] user_details['User ID'] = lst1[0][0]
# user_details['token'] = token
user_details['status'] = 1 user_details['status'] = 1
user_details['token_details'] = token_details user_details['token_details'] = token_details
return user_details return user_details
@ -414,6 +408,7 @@ def grant_user_access(userdata=None, iid=None):
except Exception as e1: except Exception as e1:
return {'status': 0, 'error': "Invalid Inputs"} return {'status': 0, 'error': "Invalid Inputs"}
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def data_invalidation(email, device_id, iid=None): def data_invalidation(email, device_id, iid=None):
rate_res = custom_rate_limit(limit=5, seconds=15) rate_res = custom_rate_limit(limit=5, seconds=15)
@ -439,6 +434,7 @@ def data_invalidation(email, device_id, iid=None):
except Exception as e1: except Exception as e1:
return {'status': 0, 'data': [], 'error': 'Failed to verify user'} return {'status': 0, 'data': [], 'error': 'Failed to verify user'}
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def new_app_user_validation(usr=None, pwd=None, iid=None, device_data=None): def new_app_user_validation(usr=None, pwd=None, iid=None, device_data=None):
rate_res = custom_rate_limit(limit=5, seconds=15) rate_res = custom_rate_limit(limit=5, seconds=15)
@ -459,7 +455,7 @@ def new_app_user_validation(usr=None, pwd=None, iid=None, device_data=None):
headers = {"Content-Type": "application/x-www-form-urlencoded"} headers = {"Content-Type": "application/x-www-form-urlencoded"}
if technician_login == 1 and mahindra_login == 0: if technician_login == 1 and mahindra_login == 0:
response = requests.post( response = requests.post(
"http://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers) "https://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers)
decoded = response.content.decode('utf-8') decoded = response.content.decode('utf-8')
if len(decoded) > 165: if len(decoded) > 165:
@ -478,7 +474,7 @@ def new_app_user_validation(usr=None, pwd=None, iid=None, device_data=None):
return {'status': 0, 'error': 'Invalid Credentials'} return {'status': 0, 'error': 'Invalid Credentials'}
elif technician_login == 0 and mahindra_login == 1: elif technician_login == 0 and mahindra_login == 1:
response1 = requests.post("http://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential", response1 = requests.post("https://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential",
data=data, headers=headers) data=data, headers=headers)
decoded1 = response1.content.decode('utf-8') decoded1 = response1.content.decode('utf-8')
node = ET.fromstring(decoded1) node = ET.fromstring(decoded1)
@ -521,10 +517,10 @@ def new_app_user_validation(usr=None, pwd=None, iid=None, device_data=None):
elif technician_login == 1 and mahindra_login == 1: elif technician_login == 1 and mahindra_login == 1:
response = requests.post( response = requests.post(
"http://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers) "https://www.mahindramile.com/ISmartAuth/Service.asmx/ValidateCredential", data=data, headers=headers)
decoded = response.content.decode('utf-8') decoded = response.content.decode('utf-8')
response1 = requests.post("http://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential", response1 = requests.post("https://www.mahindramile.com/WindowAuth/Service.asmx/ValidateCredential",
data=data, headers=headers) data=data, headers=headers)
decoded1 = response1.content.decode('utf-8') decoded1 = response1.content.decode('utf-8')
node = ET.fromstring(decoded1) node = ET.fromstring(decoded1)
@ -684,16 +680,6 @@ def new_grant_user_access(userdata=None, iid=None):
'''Inserting Device data''' '''Inserting Device data'''
if iid == 'None' or iid == 'null': if iid == 'None' or iid == 'null':
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
if frappe.db.exists({'doctype': 'App Device', 'device_id': device_id}): if frappe.db.exists({'doctype': 'App Device', 'device_id': device_id}):
frappe.db.sql( frappe.db.sql(
'''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where device_id = "{0}";'''.format(device_id, '''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where device_id = "{0}";'''.format(device_id,
@ -705,6 +691,15 @@ def new_grant_user_access(userdata=None, iid=None):
'App Users', filters={"email_id": email}) 'App Users', filters={"email_id": email})
app_device1 = frappe.get_last_doc( app_device1 = frappe.get_last_doc(
'App Device', filters={"device_id": device_id}) 'App Device', filters={"device_id": device_id})
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
app_logs = frappe.new_doc('App Log') app_logs = frappe.new_doc('App Log')
app_logs.user = app_user1.name app_logs.user = app_user1.name
@ -712,16 +707,9 @@ def new_grant_user_access(userdata=None, iid=None):
app_logs.type = 'Reinstalled' app_logs.type = 'Reinstalled'
app_logs.latitude = latitude app_logs.latitude = latitude
app_logs.longitude = longitude app_logs.longitude = longitude
# app_logs.active_status = 'Active'
app_logs.date_time_stamp = now() app_logs.date_time_stamp = now()
app_logs.insert() app_logs.insert()
app_device_tmp = frappe.get_last_doc(
'App Device', filters={"device_id": device_id})
frappe.db.sql('''UPDATE {2}.`tabApp Device` SET active_status= "{1}" where name = "{0}";'''.format(
app_device_tmp.name, "Active", current_db_name))
frappe.db.commit()
app_logs3 = frappe.new_doc('App Log') app_logs3 = frappe.new_doc('App Log')
app_logs3.user = app_user1.name app_logs3.user = app_user1.name
app_logs3.device = app_device.name app_logs3.device = app_device.name
@ -732,6 +720,16 @@ def new_grant_user_access(userdata=None, iid=None):
app_logs3.insert() app_logs3.insert()
else: else:
app_device = frappe.new_doc('App Device')
app_device.device_id = device_id
app_device.publish_type = "Global"
app_device.device_type = device_type
app_device.os = os
app_device.os_version = os_version
app_device.app_current_version = app_current_version
app_device.last_login = now()
app_device.insert()
app_user1 = frappe.get_last_doc( app_user1 = frappe.get_last_doc(
'App Users', filters={"email_id": email}) 'App Users', filters={"email_id": email})
app_device1 = frappe.get_last_doc( app_device1 = frappe.get_last_doc(
@ -740,7 +738,6 @@ def new_grant_user_access(userdata=None, iid=None):
app_logs.user = app_user1.name app_logs.user = app_user1.name
app_logs.device = app_device.name app_logs.device = app_device.name
app_logs.type = 'Installed' app_logs.type = 'Installed'
# app_logs.active_status = 'Active'
app_logs.latitude = latitude app_logs.latitude = latitude
app_logs.longitude = longitude app_logs.longitude = longitude
app_logs.date_time_stamp = now() app_logs.date_time_stamp = now()
@ -756,9 +753,6 @@ def new_grant_user_access(userdata=None, iid=None):
app_logs4.save() app_logs4.save()
else: else:
# frappe.db.sql(
# '''UPDATE {4}.`tabApp Device` set last_login= '{0}', user_name = '{2}', device_type = '{3}' where name = "{1}";'''.format(
# now(), iid, email, user_type_desc, current_db_name))
frappe.db.sql( frappe.db.sql(
'''UPDATE {3}.`tabApp Device` set last_login= '{0}', device_type = '{2}' where name = "{1}";'''.format( '''UPDATE {3}.`tabApp Device` set last_login= '{0}', device_type = '{2}' where name = "{1}";'''.format(
now(), iid, device_type, current_db_name)) now(), iid, device_type, current_db_name))
@ -809,16 +803,16 @@ def generate_keys(user_name):
return token return token
@frappe.whitelist() # @frappe.whitelist()
def login_generate_token(doc=None, method=None): # def login_generate_token(doc=None, method=None):
rate_res = custom_rate_limit(limit=5, seconds=15) # rate_res = custom_rate_limit(limit=5, seconds=15)
if rate_res != 1: # if rate_res != 1:
return rate_res # return rate_res
new_tok = generate_keys('cms@guidedsmartservice.com') # new_tok = generate_keys('cms@guidedsmartservice.com')
water = frappe.get_doc("Fish", None) # water = frappe.get_doc("Fish", None)
water.apple = new_tok # water.apple = new_tok
water.save() # water.save()
return water.apple # return water.apple
def input_validation(**kwargs): def input_validation(**kwargs):
@ -881,7 +875,6 @@ def generate_refresh_token():
@frappe.whitelist(allow_guest=1, methods=['POST']) @frappe.whitelist(allow_guest=1, methods=['POST'])
def refresh_token(): def refresh_token():
""" For Refresh API """ """ For Refresh API """
# rate_res = custom_rate_limit(limit=5, seconds=15) # rate_res = custom_rate_limit(limit=5, seconds=15)
# if rate_res != 1: # if rate_res != 1:
@ -891,9 +884,9 @@ def refresh_token():
req = frappe.request.data req = frappe.request.data
req = json.loads(req) req = json.loads(req)
refresh_token= req['refresh_token'] refresh_token = req['refresh_token']
if refresh_token: if refresh_token:
device_id= req['DeviceID'] device_id = req['DeviceID']
ref_token = refresh_token ref_token = refresh_token
doc_details = frappe.db.sql(''' doc_details = frappe.db.sql('''
SELECT * FROM `tabRefresh Token`;''', as_dict=1) SELECT * FROM `tabRefresh Token`;''', as_dict=1)
@ -904,9 +897,7 @@ def refresh_token():
refresh_token = generate_refresh_token() refresh_token = generate_refresh_token()
token_details = update_tokens_in_doc( token_details = update_tokens_in_doc(
d['user'], access_token, refresh_token) d['user'], access_token, refresh_token)
# token_details['status'] = 1
return {'status': 1, 'token_details': token_details} return {'status': 1, 'token_details': token_details}
# return token_details
return {'status': 0, "error": 'Invalid token'} return {'status': 0, "error": 'Invalid token'}
@ -942,7 +933,7 @@ def invalidate_token(refresh_token):
return {'status': 0, "error": 'Failed'} return {'status': 0, "error": 'Failed'}
@frappe.whitelist(allow_guest=1,methods=['POST']) @frappe.whitelist(allow_guest=1, methods=['POST'])
def deactivate_account(refresh_token): def deactivate_account(refresh_token):
rate_res = custom_rate_limit(limit=5, seconds=15) rate_res = custom_rate_limit(limit=5, seconds=15)
if rate_res != 1: if rate_res != 1:
@ -968,10 +959,7 @@ def deactivate_account(refresh_token):
return {'status': 0, "error": 'Failed'} return {'status': 0, "error": 'Failed'}
def update_tokens_in_doc(user_email, access, refresh): def update_tokens_in_doc(user_email, access, refresh):
# access_exp_hours = 15 * 60
# refresh_exp_hrs = 60 * 60 * 24
token_settings = frappe.get_doc('Token Settings') token_settings = frappe.get_doc('Token Settings')
access_exp_hours = token_settings.access_expiry_time access_exp_hours = token_settings.access_expiry_time
@ -994,9 +982,6 @@ def update_tokens_in_doc(user_email, access, refresh):
refresh_doc.access_token = encrypt(access) refresh_doc.access_token = encrypt(access)
refresh_doc.refresh_token = encrypt(refresh) refresh_doc.refresh_token = encrypt(refresh)
# access_exp_time = datetime.now() + timedelta(seconds=access_exp_hours)
# refresh_exp_time = datetime.now() + timedelta(seconds=refresh_exp_hrs)
access_exp_time = datetime.now() + timedelta(minutes=access_exp_hours) access_exp_time = datetime.now() + timedelta(minutes=access_exp_hours)
refresh_exp_time = datetime.now() + timedelta(hours=refresh_exp_hrs) refresh_exp_time = datetime.now() + timedelta(hours=refresh_exp_hrs)
@ -1006,9 +991,9 @@ def update_tokens_in_doc(user_email, access, refresh):
frappe.db.commit() frappe.db.commit()
user_token_details['access_token'] = access user_token_details['access_token'] = access
user_token_details['access_expiry_time'] = access_exp_hours* 60 user_token_details['access_expiry_time'] = access_exp_hours * 60
user_token_details['refresh_token'] = refresh user_token_details['refresh_token'] = refresh
user_token_details['refresh_expiry_time'] = refresh_exp_hrs* 60 * 60 user_token_details['refresh_expiry_time'] = refresh_exp_hrs * 60 * 60
return user_token_details return user_token_details

Loading…
Cancel
Save