def new(db_handle, db_table, add_modified_field=True, add_archived_field=True): record = copy.deepcopy(db[db_table]) db_fk_settings(record, add_archived_field) record["__db__name__"] = db_table timestamp, timestamp_str = utils._get_current_timestamp() record["rec_timestamp"] = timestamp record["rec_timestamp_str"] = timestamp_str if add_modified_field: record["last_modified_timestamp"] = "" record["last_modified_timestamp_str"] = "" if add_archived_field: _db_add_archive_field(record) random_hex = os.urandom(24).hex() random_int = random.randint(config.G_RANDOM_START, config.G_RANDOM_END) req_id = random_hex + "-" + str(random_int) record["_id"] = ObjectId() record["ipkey"] = str(record["_id"]) record["pkey"] = record["ipkey"] + "-" + req_id mongo_record_model = model.mongo_model(record, record, db_handle) return mongo_record_model
def _deep_unlink_update_constructor(self, fk_structure, fk_pkey, record_ref_key, touch_timestamp=True): # construct update for collection fk_structure_type = type(fk_structure) if fk_structure_type == list: if len(fk_structure) > 0 and type( fk_structure[0]) == dict: # list of dictionary reference fk_update = {"pkey": fk_pkey} else: # list of pkeys reference fk_update = fk_pkey update = {"$pull": {record_ref_key: fk_update}} elif fk_structure_type == dict or fk_structure_type == str: fk_update = fk_structure update = {"$set": {record_ref_key: fk_update}} if touch_timestamp: if "$set" not in update: update["$set"] = {} timestamp, timestamp_str = utils._get_current_timestamp() update["$set"]["last_modified_timestamp"] = timestamp update["$set"]["last_modified_timestamp_str"] = timestamp_str return update
def upload(self, response, file, params={}): filename = secure_filename(file.filename) if self.service == WASABI: timestamp, timestamp_str = utils._get_current_timestamp("%Y-%m-%d") bucket = params[ "bucket"] if "bucket" in params else config.G_WSB_IMAGE_BUCKET folder = params[ "folder"] if "folder" in params else config.G_WSB_MAIN_FOLDER full_file_path = folder + timestamp_str + "/" + str( timestamp) + "-" + filename file_response = wasabi_lib.store_file({ "bucket": bucket, "file_data": file, "file_name": full_file_path }) if file_response["message_action"] != "ADD_WASABI_FILE_SUCCESS": response.put("status", "ADD_WASABI_FILE_FAILED") response.put("desc", file_response["message_desc"]) response.put("status_code", config.G_STATUS["WASABI_UPLOAD_ERROR"]['CODE']) return None wasabi_url = config.G_WSB_URL if config.G_WSB_URL.endswith( "/") else config.G_WSB_URL + '/' file_url = wasabi_url + bucket + '/' + full_file_path return file_url
def new_record(db_handle, db_table, db_table_fks, add_modified_field = config.G_RECORD_ADD_MODIFIED_TIMESTAMP, add_archived_field = config.G_RECORD_ADD_ARCHIVED_TIMESTAMP): record = copy.deepcopy(db[db_table]) record = _clean_record(record, db_table_fks[db_table], add_archived_field) record["__db__name__" ] = db_table timestamp, timestamp_str = utils._get_current_timestamp() record["rec_timestamp" ] = timestamp record["rec_timestamp_str"] = timestamp_str if add_modified_field: record["last_modified_timestamp"] = "" record["last_modified_timestamp_str"] = "" if add_archived_field: _db_add_archive_field(record) random_hex = os.urandom(24).hex() random_int = random.randint( config.G_RANDOM_START , config.G_RANDOM_END ) req_id = random_hex + "-" + str(random_int) record["_id" ] = ObjectId() record["ipkey"] = str( record["_id"] ) record["pkey" ] = record["ipkey"] + "-" + req_id mongo_record_model = model.mongo_model( record , record , db_handle ) return mongo_record_model
def restore(self, params): try: collection = params["collection"] db_readable_name = database.get_readable_name(collection) process_name = "RESTORE_" + db_readable_name.upper().replace( " ", "_") proc_success_status = process_name + "_SUCCESS" proc_failed_status = process_name + "_FAILED" response = helper.response_msg( status=proc_success_status, desc="Restoring {} record successful.".format( db_readable_name), data={}) ##### PAYLOAD ##### pkey = params["pkey"] ##### VALIDATION ##### record = validation.find_one(response, self.db_handle, collection, {"pkey": pkey}) if record == None: return response ##### DB OPERATION ##### timestamp, timestamp_str = utils._get_current_timestamp() bulk_action = bulk_db_action.bulk_db_action({ "db_handle": self.db_handle, "app": self.webapp }) bulk_action.deep_update(collection, {"pkey": pkey}, { "$set": { "archived_timestamp": "", "archived_timestamp_str": "" } }) bulk_action.execute({}) ##### RESPONSE ##### record["archived_timestamp"] = "" record["archived_timestamp_str"] = "" response.put("status_code", config.G_STATUS['SUCCESS']['CODE']) response.put("data", record) except: self.webapp.logger.debug(traceback.format_exc()) response.put("status_code", config.G_STATUS['UNEXPECTED_ERROR']['CODE']) response.put("status", proc_failed_status) response.put("desc", config.G_STATUS['UNEXPECTED_ERROR']['DESC']) # end try return response
def get_record(db_table): record = copy.deepcopy(db[db_table]) db_fk_settings(record) timestamp, timestamp_str = utils._get_current_timestamp() record["rec_timestamp"] = timestamp record["rec_timestamp_str"] = timestamp_str record["_id"] = ObjectId() record["pkey"] = str(record["_id"]) return copy.deepcopy(record)
def _deep_link_update_constructor( self, fk_structure, record, record_ref_key, touch_timestamp=config.G_RECORD_ADD_MODIFIED_TIMESTAMP): # construct update for collection fk_structure_type = type(fk_structure) if fk_structure_type == str: # pkey reference only fk_update = record["pkey"] update = {"$set": {record_ref_key: fk_update}} elif fk_structure_type == dict: # dict reference fk_update = {} for key in fk_structure: fk_update[key] = record[key] update = {"$set": {record_ref_key: fk_update}} elif fk_structure_type == list: # list of reference if len(fk_structure) > 0 and type( fk_structure[0]) == dict: # list of dict reference fk_update = {} for key in fk_structure[0]: fk_update[key] = record[key] else: # list of pkeys fk_update = record["pkey"] update = {"$push": {record_ref_key: fk_update}} if touch_timestamp: if "$set" not in update: update["$set"] = {} timestamp, timestamp_str = utils._get_current_timestamp() update["$set"]["last_modified_timestamp"] = timestamp update["$set"]["last_modified_timestamp_str"] = timestamp_str return update
def _global_unlink_reference(self, main, sub, touch_timestamp=True, add_archived_field=True): main_collection = main["collection"] # db_main sub_collection = sub["collection"] # db_sub main_record = main["record"] sub_record = sub["record"] main_fk_field = main[ "fk_field"] if "fk_field" in main else None # if not None, then you want to update if main_fk_field: tbl_fks = self.webapp.db_table_fks[sub_collection] fk_found = False for tbl_fk in tbl_fks: fk_path, fk_update = copy.deepcopy(next(iter(tbl_fk.items()))) if fk_path.replace("$[elem].", "").rstrip(".").endswith(main_fk_field): main_fk_path = fk_path fk_found = True break if fk_found: if add_archived_field: database._db_add_archive_field(fk_update) if ".$[elem]." in fk_path: # meaning it is a list of fk fk_update = {"pkey": main_record["pkey"]} ref_key = fk_path.replace(".$[elem].", "") db_operation = "$pull" else: ref_key = fk_path.rstrip(".") db_operation = "$set" update = {db_operation: {ref_key: fk_update}} if touch_timestamp: if "$set" not in update: update["$set"] = {} timestamp, timestamp_str = utils._get_current_timestamp() update["$set"]["last_modified_timestamp"] = timestamp update["$set"][ "last_modified_timestamp_str"] = timestamp_str self.add(UPDATE, sub_collection, {"pkey": sub_record["pkey"]}, update, multi_operation=True) tbl_update_context = self.webapp.db_update_context[ main_collection] for tbl in tbl_update_context: collection, tbl_fks = next(iter(tbl.items())) for tbl_fk in tbl_fks: fk_path, fk_update = copy.deepcopy( next(iter(tbl_fk.items()))) if not fk_path.endswith(main_fk_path): continue sub_path = fk_path[:-len(main_fk_path)] sub_path_keys = sub_path.split('.') sub_path_keys_len = len(sub_path_keys) if not sub_path_keys_len > 1: continue key_pointer = -2 if sub_path_keys[key_pointer] == "$[]": sub_path_keys[key_pointer] = "$[elem]" key_pointer -= 1 if key_pointer * -1 > sub_path_keys_len: continue sub_ref_names = database.get_referenced_names( sub_collection) if sub_path_keys[key_pointer] not in sub_ref_names: continue #tests passed if add_archived_field: database._db_add_archive_field(fk_update) new_sub_path = ".".join(sub_path_keys) if ".$[elem]." in main_fk_path: # meaning it is a list of fk fk_update = {"pkey": main_record["pkey"]} ref_key = new_sub_path + main_fk_path.replace( ".$[elem].", "") db_operation = "$pull" else: ref_key = new_sub_path + main_fk_path.rstrip(".") db_operation = "$set" update = {db_operation: {ref_key: fk_update}} if "$[elem]" in new_sub_path: # we need to use subarray to query sub_query = { ref_key.split("$[elem]")[0].replace( ".$[].", ".") + "pkey": sub_record["pkey"] } sub_array_filters = [{ "elem.pkey": sub_record["pkey"] }] self.add(UPDATE, collection, sub_query, update, multi_operation=True, array_filters=sub_array_filters) else: # sub_query = [{ new_sub_path + "pkey" : sub_record["pkey"]}] sub_query = { new_sub_path + "pkey": sub_record["pkey"] } self.add(UPDATE, collection, sub_query, update, multi_operation=True)
def _global_link_reference( self, main, sub, touch_timestamp=config.G_RECORD_ADD_MODIFIED_TIMESTAMP, add_archived_field=config.G_RECORD_ADD_ARCHIVED_TIMESTAMP): main_collection = main["collection"] # db_main sub_collection = sub["collection"] # db_sub main_record = main["record"] sub_record = sub["record"] main_set_fields = main["set_fields"] if "set_fields" in main else None main_fk_field = main[ "fk_field"] if "fk_field" in main else None # if not None, then you want to update # sub_fk_field = sub["fk_field" ] if "fk_field" in sub else None # if not None, then you want to update if main_fk_field: # db_wp_taxonomy -- fk_wp_taxonomy tbl_fks = self.webapp.db_table_fks[sub_collection] fk_found = False for tbl_fk in tbl_fks: fk_path, fk_update = copy.deepcopy(next(iter(tbl_fk.items()))) if fk_path.replace("$[elem].", "").rstrip(".").endswith(main_fk_field): main_fk_path = fk_path fk_found = True break if fk_found: if add_archived_field: database._db_add_archive_field(fk_update) main_clean_record = database.simple_load(main_collection, complete=True) fk_update = self.__assign_fk_values(copy.deepcopy(fk_update), copy.deepcopy(main_record), main_clean_record, main_set_fields) """ Cases: 1. Walang $[elem] or $[] 2. Me $[elem] walang $[] 3. Me $[elem] me $[] 4. Ends with $[elem] 5. Does not end with $[elem] research kung need pa ba ung $[] ANG $[] AT $[ELEM] ay hindi ginagamit sa query! """ """ 1. endswith dapat ung unang condition 2. replace middle elem with $[] -- ang intention ay kung ung fk ay nasa loob ng array -- lahat mauupdate -nope???? -- OR do we need to replace middle $[] with elem PANG SET LANG UNG ARRAY FILTER! """ array_filter = None # if ".$[elem]." in fk_path: # meaning it is a list of fk #NOTE: 1. endswith ba dapat?? if fk_path.endswith(".$[elem]."): ref_key = fk_path.replace( ".$[elem].", "") #NOTE: 2. replace din ba ung $[] db_operation = "$push" # pull old reference TODO: improve to updating instead of pulling self.add( UPDATE, sub_collection, {"pkey": sub_record["pkey"]}, {"$pull": { ref_key: { "pkey": main_record["pkey"] } }}, #NOTE: 3. checkout ref_key multi_operation=True) else: ref_key = fk_path.rstrip( ".") #NOTE: 4. same, papalitan ba ung elem at $[] db_operation = "$set" if ".$[elem]." in fk_path: # array in middle of path array_filter = [{ 'elem' + fk_path.split("$[elem]")[-1] + 'pkey': { "$in": ["", main_record["pkey"]] } }] update = {db_operation: {ref_key: fk_update}} if touch_timestamp: if "$set" not in update: update["$set"] = {} timestamp, timestamp_str = utils._get_current_timestamp() update["$set"]["last_modified_timestamp"] = timestamp update["$set"][ "last_modified_timestamp_str"] = timestamp_str if array_filter != None: self.add(UPDATE, sub_collection, {"pkey": sub_record["pkey"]}, update, multi_operation=True, array_filters=array_filter) else: self.add(UPDATE, sub_collection, {"pkey": sub_record["pkey"]}, update, multi_operation=True) tbl_update_context = self.webapp.db_update_context[ main_collection] for tbl in tbl_update_context: collection, tbl_fks = next(iter(tbl.items())) for tbl_fk in tbl_fks: fk_path, fk_update = copy.deepcopy( next(iter(tbl_fk.items()))) if not fk_path.endswith(main_fk_path): continue sub_path = fk_path[:-len(main_fk_path)] sub_path_keys = sub_path.split('.') sub_path_keys_len = len(sub_path_keys) if not sub_path_keys_len > 1: continue key_pointer = -2 if sub_path_keys[ key_pointer] == "$[elem]" or sub_path_keys[ key_pointer] == "$[]": sub_path_keys[key_pointer] = "$[elem]" key_pointer -= 1 if key_pointer * -1 > sub_path_keys_len: continue sub_ref_names = database.get_referenced_names( sub_collection) # print(sub_ref_names, sub_collection, sub_path_keys, sub_path_keys[key_pointer]) if sub_path_keys[key_pointer] not in sub_ref_names: continue #tests passed if add_archived_field: database._db_add_archive_field(fk_update) fk_update = self.__assign_fk_values( copy.deepcopy(fk_update), copy.deepcopy(main_record), main_clean_record, main_set_fields) new_sub_path = ".".join(sub_path_keys) # if ".$[elem]." in main_fk_path: # meaning it is a list of fk if main_fk_path.endswith(".$[elem]."): ref_key = new_sub_path + main_fk_path.replace( ".$[elem].", "") db_operation = "$push" else: ref_key = new_sub_path + main_fk_path.rstrip(".") db_operation = "$set" update = {db_operation: {ref_key: fk_update}} # if "$[elem]" in new_sub_path: # we need to use subarray to query if "$[elem]" in ref_key: # we need to use subarray to query sub_array_filters = [{ "elem.pkey": sub_record["pkey"] }] # sub_array_ref_key = ref_key.split("$[elem]")[-1] # sub_array_ref_key = sub_array_ref_key + '.' if not sub_array_ref_key.endswith('.') else sub_array_ref_key # sub_array_ref_key = '.' + sub_array_ref_key if not sub_array_ref_key.startswith('.') else sub_array_ref_key # sub_array_filters = [{ 'elem' + sub_array_ref_key + 'pkey' : sub_record["pkey"]}] sub_query = { ref_key.split("$[elem]")[0].replace( ".$[].", ".") + "pkey": sub_record["pkey"] } # pull old reference TODO: improve to updating instead of pulling if db_operation == "$push": self.add( UPDATE, collection, sub_query, { "$pull": { ref_key: { "pkey": main_record["pkey"] } } }, multi_operation=True, array_filters=sub_array_filters) self.add(UPDATE, collection, sub_query, update, multi_operation=True, array_filters=sub_array_filters) else: # sub_query = [{ new_sub_path + "pkey" : sub_record["pkey"]}] sub_query = { new_sub_path + "pkey": sub_record["pkey"] } # pull old reference TODO: improve to updating instead of pulling if db_operation == "$push": self.add( UPDATE, collection, sub_query, { "$pull": { ref_key: { "pkey": main_record["pkey"] } } }, multi_operation=True) self.add(UPDATE, collection, sub_query, update, multi_operation=True)