def retrieve_description(db, requested_db, requested_coll): """Retrieve inventory for named collection db -- LMFDB connection to inventory db requested_db -- name of database the named collection belongs to requested_coll -- name of collection to fetch inventory for """ try: _id = idc.get_db(db, requested_db)['id'] coll_record = idc.get_coll(db, _id, requested_coll) _c_id = coll_record['id'] info = coll_record['data']['INFO'] info['nice_name'] = coll_record['data']['nice_name'] specials = {'INFO': info, 'NOTES':coll_record['data']['NOTES']} request = {'coll_id': _c_id} fields_auto = inv.ALL_STRUC.get_fields('auto') fields_human = inv.ALL_STRUC.get_fields('human') collection = db[fields_auto[inv.STR_NAME]] descr_auto = collection.find(request) collection = db[fields_human[inv.STR_NAME]] descr_human = collection.find(request) return {'data':patch_records(descr_auto, descr_human), 'specials': specials, 'scrape_date':coll_record['data']['scan_date']} except Exception as e: inv.log_dest.error("Error retrieving inventory "+requested_db+'.'+requested_coll+' '+str(e)) return {'data':None, 'specials':None, 'scrape_date':None}
def retrieve_description(requested_db, requested_coll): """Retrieve inventory for named collection requested_db -- name of database the named collection belongs to requested_coll -- name of collection to fetch inventory for """ try: _id = idc.get_db(requested_db)['id'] coll_record = idc.get_coll(_id, requested_coll) _c_id = coll_record['id'] info = coll_record['data']['INFO'] info['nice_name'] = coll_record['data']['nice_name'] specials = {'INFO': info, 'NOTES':coll_record['data']['NOTES']} request = {'table_id': _c_id} fields_auto = 'inv_fields_auto' fields_human ='inv_fields_human' descr_auto = db[fields_auto].search(request) descr_human = db[fields_human].search(request) return {'data':patch_records(descr_auto, descr_human), 'specials': specials, 'scrape_date':coll_record['data']['scan_date']} except: return {'data':None, 'specials':None, 'scrape_date':None}
def capture_rollback(db_id, db_name, coll_name, change, coll_id = None): """"Capture diff which will allow roll-back of edits db_id -- ID of DB change applies to db_name -- Name of DB change applies to coll_name -- Name of collection change applies to change -- The change to be made, as a diff item ( entry in diff['diffs']) coll_id -- Supply if this is a field edit and so coll_id is known Roll-backs can be applied using apply_rollback. Their format is a diff, with extra 'post' field storing the state after change, and the live field which should be unset if they are applied """ return {} is_record = False #Fetch the current state if coll_id is None and coll_name is not None: current_record = idc.get_coll(db_id, coll_name) elif coll_id is None: current_record = idc.get_db(db_name) else: try: current_record = idc.get_field(coll_id, change['item'], type = 'human') #Try as a field first assert current_record is not None and current_record['err'] is False except: #Now try as a record current_record = idc.get_record(coll_id, change['item']) is_record = True if current_record is None: #Should not happen really, but if it does we can't do anything return None #Create a roll-back document field = change["field"] prior = change.copy() if coll_id is None and coll_name is not None: if ih.is_special_field(change["item"]): prior['content'] = current_record['data'][change["item"][2:-2]][field] elif ih.is_toplevel_field(change['item']): prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data'][change["item"]][field] elif coll_id is None: prior['content'] = current_record['data'][field] elif is_record: prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data']['data'][field] #This can be applied like the diffs from the web-frontend, but has an extra field rollback_diff = {"db":db_name, "collection":coll_name, "diffs":[prior], "post":change, "live":True} return rollback_diff
def capture_rollback(inv_db, db_id, db_name, coll_name, change, coll_id = None): """"Capture diff which will allow roll-back of edits inv_db -- connection to inventory_db db_id -- ID of DB change applies to db_name -- Name of DB change applies to coll_name -- Name of collection change applies to change -- The change to be made, as a diff item ( entry in diff['diffs']) coll_id -- Supply if this is a field edit and so coll_id is known Roll-backs can be applied using apply_rollback. Their format is a diff, with extra 'post' field storing the state after change, and the live field which should be unset if they are applied """ is_record = False #Fetch the current state if coll_id is None and coll_name is not None: current_record = idc.get_coll(inv_db, db_id, coll_name) elif coll_id is None: current_record = idc.get_db(inv_db, db_name) else: try: current_record = idc.get_field(inv_db, coll_id, change['item'], type = 'human') #Try as a field first assert current_record is not None and current_record['err'] is False except: #Now try as a record current_record = idc.get_record(inv_db, coll_id, change['item']) is_record = True if current_record is None: #Should not happen really, but if it does we can't do anything return None #Create a roll-back document field = change["field"] prior = change.copy() if coll_id is None and coll_name is not None: if ih.is_special_field(change["item"]): prior['content'] = current_record['data'][change["item"][2:-2]][field] elif ih.is_toplevel_field(change['item']): prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data'][change["item"]][field] elif coll_id is None: prior['content'] = current_record['data'][field] elif is_record: prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data']['data'][field] #This can be applied like the diffs from the web-frontend, but has an extra field rollback_diff = {"db":db_name, "collection":coll_name, "diffs":[prior], "post":change, "live":True} return rollback_diff
def get_nicename(db_name, table_name): """Return the nice_name string for given db/table pair""" try: if table_name: return idc.get_table(table_name)['nice_name'] else: return idc.get_db(db_name)['nice_name'] except Exception as e: inv.log_dest.error('Failed to get nice name for '+db_name+' '+table_name+' '+str(e)) #Can't return nice name so return None return None
def collate_collection_info(db_name): """Fetches and collates viewable info for collections in named db """ db_info = idc.get_db(db_name) if not db_info['exist']: return colls_info = idc.get_all_colls(db_info['id']) for coll in colls_info: rec_info = idc.count_records_and_types(db, coll['_id']) coll['records'] = comma(rec_info) return colls_info
def get_nicename(db_name, collection_name): """Return the nice_name string for given db/coll pair""" try: if collection_name: db_id = idc.get_db_id(db_name) coll_rec = idc.get_coll(db_id['id'], collection_name) nice_name = coll_rec['data']['nice_name'] else: db_rec = idc.get_db(db_name) #print db_rec nice_name = db_rec['data']['nice_name'] return nice_name except: #Can't return nice name so return None return None
def upload_collection_indices(db, db_name, coll_name, structure_dat): """Extract index data and upload""" try: db_info = invc.get_db(db, db_name) coll_info = invc.get_coll(db, db_info['id'], coll_name) except Exception as e: inv.log_dest.error("Failed to get db or coll id "+str(e)) return {'err':True, 'mess':'Failed to get db or coll'} #Probably should rethrow try: data = structure_dat[db_name][coll_name]['indices'] #err = upload_indices(db, coll_info['id'], data) upload_indices(db, coll_info['id'], data) # TODO rethrow if err except Exception as e: inv.log_dest.error("Failed to upload index "+str(e)) return {'err':True, 'mess':'Failed to upload'} return {'err':False, 'mess':''}
def capture_rollback(db_id, db_name, table_name, change, table_id = None): """"Capture diff which will allow roll-back of edits db_id -- ID of DB change applies to db_name -- Name of DB change applies to table_name -- Name of table change applies to change -- The change to be made, as a diff item ( entry in diff['diffs']) table_id -- Supply if this is a field edit and so table_id is known Roll-backs can be applied using apply_rollback. Their format is a diff, with extra 'post' field storing the state after change, and the live field which should be unset if they are applied """ #Fetch the current state if table_id is None and table_name is not None: current_record = idc.get_table(table_name) elif table_id is None: current_record = idc.get_db(db_name) else: current_record = idc.get_field(table_id, change['item'], type = 'human') if current_record is None: #Should not happen really, but if it does we can't do anything return None #Create a roll-back document field = change["field"] prior = change.copy() if table_id is None and table_name is not None: if ih.is_special_field(change["item"]): prior['content'] = current_record['data'][change["item"][2:-2]][field] elif ih.is_toplevel_field(change['item']): prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data'][change["item"]][field] elif table_id is None: prior['content'] = current_record['data'][field] else: prior['content'] = current_record['data']['data'][field] #This can be applied like the diffs from the web-frontend, but has an extra field rollback_diff = {"db":db_name, "table":table_name, "diffs":[prior], "post":change, "live":True} return rollback_diff
def upload_collection_indices(db, db_name, coll_name, structure_dat): """Extract index data and upload""" try: db_info = invc.get_db(db, db_name) coll_info = invc.get_coll(db, db_info['id'], coll_name) except: return { 'err': True, 'mess': 'Failed to get db or coll' } #Probably should rethrow try: data = structure_dat[db_name][coll_name]['indices'] #err = upload_indices(db, coll_info['id'], data) upload_indices(db, coll_info['id'], data) # TODO rethrow if err except: return {'err': True, 'mess': 'Failed to upload'} return {'err': False, 'mess': ''}
def collate_collection_info(db_name): """Fetches and collates viewable info for collections in named db """ try: inv.setup_internal_client() db = inv.int_client[inv.ALL_STRUC.name] except Exception: raise ih.ConnectOrAuthFail("") return None db_info = idc.get_db(db, db_name) if not db_info['exist']: return colls_info = idc.get_all_colls(db, db_info['id']) for coll in colls_info: rec_info = idc.count_records_and_types(db, coll['_id']) coll['records'] = comma(rec_info) return colls_info
def get_nicename(db_name, collection_name): """Return the nice_name string for given db/coll pair""" try: inv.setup_internal_client() db = inv.int_client[inv.ALL_STRUC.name] except Exception as e: raise ih.ConnectOrAuthFail("") return None try: if collection_name: db_id = idc.get_db_id(db, db_name) coll_rec = idc.get_coll(db, db_id['id'], collection_name) nice_name = coll_rec['data']['nice_name'] else: db_rec = idc.get_db(db, db_name) #print db_rec nice_name = db_rec['data']['nice_name'] return nice_name except Exception as e: inv.log_dest.error('Failed to get nice name for '+db_name+' '+collection_name+' '+str(e)) #Can't return nice name so return None return None
def retrieve_description(db, requested_db, requested_coll): """Retrieve inventory for named collection db -- LMFDB connection to inventory db requested_db -- name of database the named collection belongs to requested_coll -- name of collection to fetch inventory for """ try: _id = idc.get_db(db, requested_db)['id'] coll_record = idc.get_coll(db, _id, requested_coll) _c_id = coll_record['id'] info = coll_record['data']['INFO'] info['nice_name'] = coll_record['data']['nice_name'] specials = {'INFO': info, 'NOTES': coll_record['data']['NOTES']} request = {'coll_id': _c_id} fields_auto = inv.ALL_STRUC.get_fields('auto') fields_human = inv.ALL_STRUC.get_fields('human') collection = db[fields_auto[inv.STR_NAME]] descr_auto = collection.find(request) collection = db[fields_human[inv.STR_NAME]] descr_human = collection.find(request) return { 'data': patch_records(descr_auto, descr_human), 'specials': specials, 'scrape_date': coll_record['data']['scan_date'] } except Exception as e: inv.log_dest.error("Error retrieving inventory " + requested_db + '.' + requested_coll + ' ' + str(e)) return {'data': None, 'specials': None, 'scrape_date': None}
def get_nicename(db_name, collection_name): """Return the nice_name string for given db/coll pair""" try: inv.setup_internal_client() db = inv.int_client[inv.ALL_STRUC.name] except Exception as e: raise ih.ConnectOrAuthFail("") return None try: if collection_name: db_id = idc.get_db_id(db, db_name) coll_rec = idc.get_coll(db, db_id['id'], collection_name) nice_name = coll_rec['data']['nice_name'] else: db_rec = idc.get_db(db, db_name) print db_rec nice_name = db_rec['data']['nice_name'] return nice_name except Exception as e: inv.log_dest.error('Failed to get nice name for ' + db_name + ' ' + collection_name + ' ' + str(e)) #Can't return nice name so return None return None