Untitled
unknown
plain_text
2 years ago
5.5 kB
2
Indexable
def postData(payLoad, configvalues): logging = configlogfile() logging.info("Preparing to save records" ) try: dateColumns = json.loads(configvalues.get('apiconfiguration', 'dateColumns', raw=True)) keysToBeIgnored = json.loads(configvalues.get('apiconfiguration', 'keysToBeIgnored', raw=True)) columnMapping = json.loads(configvalues.get('apiconfiguration', 'columnMapping', raw=True)) validColumns = json.loads(configvalues.get('apiconfiguration', 'validKeys', raw=True)) timeZone = configvalues.get('apiconfiguration', 'timeZone', raw=True) logging.debug("arguments-" + str(payLoad)) logging.debug("validColumns-" + str(validColumns)) logging.debug("payLoad[before]-" + str(payLoad)) if payLoad == None or not isinstance(payLoad, dict) or len(payLoad) == 0: return (("422", ("9005", "Missing required attribute in payLoad. " + ','.join(validColumns) + "."))) if not set(list(payLoad.keys())).issubset(set(list(columnMapping.values()))): return (("422", ("9006", "Field rule violation. Payload having non mapped attribute. " + ','.join(list(set(list(payLoad.keys()))-set(columnMapping.values()))) + "."))) #return ((["9006"], ("Field rule violation. Payload having non mapped attribute. " + ','.join(list(set(list(payLoad.keys())) - set(columnMapping.values()))) + "."))) if not set(validColumns).issubset(set(list(payLoad.keys()))): return (("422", ("9005", "Field rule violation or missing required attribute. "+",".join(list(set(validColumns)-set(list(payLoad.keys()))))))) #return ((["9005"], "Field rule violation or missing required attribute. " + ",".join( #list(set(validColumns) - set(list(payLoad.keys())))))) logging.info("saving records for cart details " + payLoad['businessId'] + "," + payLoad['valueType'] + "," + payLoad['key']) #if 'value' in payLoad.keys() and not (isinstance(payLoad['value'], list) or isinstance(payLoad['value'],dict)): # return (("422", ("9005", "Field rule violation. value field needs to be in list or dictionary format"))) data = {} data = payLoad.copy() if 'createdTime' not in data.keys(): data['createdTime'] = datetime.strftime(datetime.now(pytz.timezone(timeZone)),'%Y-%m-%dT%H:%M:%S.%f%z') if 'updatedTime' not in data.keys(): data['updatedTime'] = datetime.strftime(datetime.now(pytz.timezone(timeZone)),'%Y-%m-%dT%H:%M:%S.%f%z') data = {k: v for k, v in data.items() if v is not None} # removes the fields with null values data = {k: v for k, v in data.items() if k not in keysToBeIgnored} data = {k: v for k, v in data.items() if k in columnMapping.values()} # remove fields not mapped for k, v in columnMapping.items(): if (v in data): data[k] = data.pop(v) insertSql = "" for k, v in data.items(): logging.debug(k) logging.debug(v) logging.debug(type(v)); if k in dateColumns: # formats the date columns in the request logging.debug("processing date columns") try: datetime.strptime(v,'%Y-%m-%dT%H:%M:%S.%f%z') insertSql = insertSql + k + "=datetime.strptime('" + v + "','%Y-%m-%dT%H:%M:%S.%f%z')," except ValueError: return (("422", ("9005", "Field rule violation. " + k + " field needs to be in 2020-01-01T00:00:00.000000+0530 format"))) elif type(v) == str: if k == 'value': logging.debug('value-' + str(v)) insertSql = insertSql + str(k) + "='" + str( str(v))+ "'.encode('utf-8')," # convert the string to bytes to store data in blob column else: if not v.isalnum(): # logic included to check for alphanumeric string and enclose string with double quotes insertSql = insertSql + str(k) + "=\"\"\"" + v + "\"\"\"," #insertSql = insertSql + str(k) + "='" + str(v) + "'," else: insertSql = insertSql + str(k) + "='" + str(v) + "'," elif isinstance(v, list) or isinstance(v, dict): if k == 'value': insertSql = insertSql + str(k) + "=b'" + json.dumps(v) + "'," else: insertSql = insertSql + str(k) + "=" + str(v) + "," elif type(b) == blob: insertSql = insertSql + str(k) + "=textAsBlob('" + str(v) + ")," else: insertSql = insertSql + str(k) + "='" + str(v) + "'," insertSql = insertSql[0:len(insertSql) - 1] insertSql = "global cacheStoreData; cacheStoreData = cache_store(" + insertSql + ")" logging.debug('insertSql') logging.debug(insertSql) #return ((["0000"],insertSql)) exec(insertSql) cacheStoreData.save() gc.collect() logging.info("Successfully saved the cache details") return (("200","Successfully saved the cache details")) return (("200", {k: v for k, v in payLoad.items()})) except Exception as e: gc.collect() logging.error("Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno))) return (("500", "Technical exception"))
Editor is loading...