def db_updater(self): rc = self.rc key = rc.projectum_id filterid = {'_id': key} found_projectum = rc.client.find_one(rc.database, rc.coll, filterid) if not found_projectum: pra = fragment_retrieval(self.gtx["projecta"], ["_id"], key) if len(pra) == 0: raise RuntimeError( "Please input a valid projectum id or a valid fragment of a projectum id" ) print("Projectum not found. Projecta with similar names: ") for i in range(len(pra)): print(f"{pra[i].get('_id')} status:{pra[i].get('status')}") print("Please rerun the helper specifying the complete ID.") return found_projectum.update({'status': 'finished'}) if rc.end_date: found_projectum.update( {'end_date': date_parser.parse(rc.end_date).date()}) else: found_projectum.update({'end_date': dt.date.today()}) found_projectum['kickoff'].update({'status': 'finished'}) found_projectum['deliverable'].update({'status': 'finished'}) for i in found_projectum: if i == 'milestones': for field in found_projectum['milestones']: field.update({'status': 'finished'}) rc.client.update_one(rc.database, rc.coll, filterid, found_projectum) print(f"{rc.projectum_id} status has been updated to finished") return
def db_updater(self): rc = self.rc found_contacts = fragment_retrieval(self.gtx['contacts'], ["_id", "aka", "name"], rc.fragmentname) found_contacts.sort(key=lambda x: x['_id'], reverse=False) index_list = list(range(2, (len(found_contacts) + 2))) if not rc.index: print("Please rerun the helper by hitting up arrow and adding '-i list-index' " "to update the list item 'list-index', e.g., 'regolith helper eins -i 2'. " "For new contacts --name (-n) and --institution (-o) are required:") print(f"{1}. {rc.fragmentname} as a new contact") for i, j in zip(index_list, found_contacts): print(f"{i}. {j.get('name')}\n" f" id: {j.get('_id')}\n" f" email: {j.get('email')}\n" f" institution: {j.get('institution')}\n" f" department: {j.get('department')}\n" f" notes: {j.get('notes')}\n" f" aliases: {j.get('aka')}") return pdoc = {} if int(rc.index) == 1: if not rc.institution or not rc.name: raise RuntimeError("Institution and name are required to create a new contact") name = HumanName(rc.name) if not rc.id: key = str(name.first[0].lower().replace(" ", "") + name.last.lower().replace(" ", "")) else: key = rc.id pdoc.update({"name": name.full_name}) pdoc.update({"date": dt.date.today()}) pdoc.update({"institution": rc.institution}) notes = [] aliases = [] uniqueidentifier = str(uuid.uuid4()) pdoc.update({'uuid': uniqueidentifier}) else: current = found_contacts[rc.index-2] key = current.get('_id') notes = current.get('notes', []) aliases = current.get('aka', []) if not rc.date: now = dt.datetime.now() else: now = date_parser.parse(rc.date).date() if rc.aliases: aliases.extend(rc.aliases) if rc.notes: if isinstance(rc.notes, str): rc.notes.list() notes.extend(rc.notes) if rc.department: pdoc.update({"department":rc.department}) pdoc.update({"aka": aliases}) pdoc.update({"notes": notes}) pdoc.update({'updated': now}) rc.client.update_one(rc.database, rc.coll, {'_id': key}, pdoc) print("{} has been added/updated in contacts".format(key)) return
def db_updater(self): rc = self.rc key = rc.projectum_id filterid = {'_id': key} found_projectum = rc.client.find_one(rc.database, rc.coll, filterid) # id exists if found_projectum is not None: rc.client.update_one(rc.database, rc.coll, filterid, {'log_url': rc.log_url}) print( f"{rc.projectum_id} has been updated with a log_url of {rc.log_url}" ) else: # find all similar projectum ids pra = fragment_retrieval(self.gtx["projecta"], ["_id"], rc.projectum_id) # no matches to id fragment and id does not exist if len(pra) == 0: raise RuntimeError( "Please input a valid projectum id or a valid fragment of a projectum id" ) # id fragment and no inputted number elif not rc.number: print( "There does not seem to be a projectum with this exact name in this database." ) print("However, there are projecta with similar names: ") for i in range(len(pra)): print( f"{i + 1}. {pra[i].get('_id')} current url: {pra[i].get('log_url')}" ) print( "Please rerun the u_logurl helper with the same name as previously inputted, " "but with the addition of -n followed by a number corresponding to one of the above listed " "projectum ids that you would like to update.") # id fragment and inputted number else: if int(rc.number) < 1 or int(rc.number) > len(pra): raise RuntimeError("Sorry, you picked an invalid number.") else: filterid = {'_id': pra[int(rc.number) - 1].get('_id')} rc.client.update_one(rc.database, rc.coll, filterid, {'log_url': rc.log_url}) print( f"{pra[int(rc.number) - 1].get('_id')} has been updated with a log_url of {rc.log_url}" ) return
def db_updater(self): rc = self.rc key = rc.institution_id filterid = {'_id': key} target_inst = rc.client.find_one(rc.database, rc.coll, filterid) now = dt.datetime.today() pdoc = {} departments = {} schools = {} if target_inst: if rc.aka: current_aka = target_inst.get('aka') current_aka.extend(rc.aka) pdoc.update({'aka': current_aka}) departments = target_inst.get('departments', {}) schools = target_inst.get('schools', {}) else: inst = fragment_retrieval(self.gtx["institutions"], ["_id", "name", "aka"], rc.institution_id) inst.sort(key=lambda x: x['_id'], reverse=False) if not rc.index: print( "Please rerun the helper specifying '-n list-index' to update item number 'list-index':" ) print(f"1. {key} as a new institution.") for i in range(len(inst)): print( f"{i+2}. {inst[i].get('_id')} {inst[i].get('name')}." ) return if rc.index < 1 or rc.index > len(inst) + 1: raise RuntimeError("Sorry, you picked an invalid number.") if rc.index == 1: if not rc.name or not rc.city or not rc.country: raise RuntimeError( "Name, city, and country are required for a new institution." ) if rc.country == 'US': if not rc.zip or not rc.state: raise RuntimeError( "Zip and state are required for a new institution " "if institutions is in the US.") pdoc.update({'name': rc.name, 'uuid': str(uuid.uuid4())}) if rc.aka: pdoc.update({'aka': rc.aka}) if rc.date: pdoc.update({'date': rc.date}) else: pdoc.update({'date': now.date()}) else: chosen_inst = inst[rc.index - 2] key = chosen_inst.get('_id') if rc.aka: current_aka = chosen_inst.get('aka') current_aka.extend(rc.aka) pdoc.update({'aka': current_aka}) current_departments = chosen_inst.get('departments') for k, v in current_departments.items(): info_department = { 'name': v.get('name'), 'aka': v.get('aka') } departments.update({k: info_department}) current_schools = chosen_inst.get('schools') for k, v in current_schools.items(): info_school = {'name': v.get('name'), 'aka': v.get('aka')} schools.update({k: info_school}) if rc.city: pdoc.update({'city': rc.city}) if rc.state: pdoc.update({'state': rc.state}) if rc.country: pdoc.update({'country': rc.country}) if rc.zip: pdoc.update({'zip': rc.zip}) if rc.date: pdoc.update({'updated': rc.date}) else: pdoc.update({'updated': now}) # departments: if rc.dept_id: dep = {} if rc.dept_id in departments: doc = departments.get(rc.dept_id) current_dept_aka = doc.get('aka') if rc.dept_name: dep.update({'name': rc.dept_name}) if rc.dept_aka: current_dept_aka.extend(rc.dept_aka) doc.update({'aka': current_dept_aka}) departments.update({rc.dept_id: doc}) else: if not rc.dept_name: raise RuntimeError( "Name is required for a new department.") dep.update({'name': rc.dept_name}) if rc.dept_aka: dep.update({'aka': rc.dept_aka}) departments[rc.dept_id] = dep pdoc.update({'departments': departments}) #schools if rc.school_id: school = {} if rc.school_id in schools: doc = schools.get(rc.school_id) if rc.school_name: doc.update({'name': rc.school_name}) current_sc_aka = doc.get('aka') if rc.dept_aka: current_sc_aka.extend(rc.school_aka) doc.update({'aka': current_sc_aka}) schools.update({rc.school_id: doc}) else: if not rc.school_name: raise RuntimeError("Name is required for a new school.") school.update({'name': rc.school_name}) if rc.school_aka: school.update({'aka': rc.school_aka}) schools[rc.school_id] = school pdoc.update({'schools': schools}) rc.client.update_one(rc.database, rc.coll, {'_id': key}, pdoc) print(f"{key} has been updated/added in institutions") return
def db_updater(self): rc = self.rc key = rc.projectum_id if rc.date: now = date_parser.parse(rc.date).date() else: now = dt.date.today() filterid = {'_id': key} target_prum = rc.client.find_one(rc.database, rc.coll, filterid) if not target_prum: pra = fragment_retrieval(self.gtx["projecta"], ["_id"], rc.projectum_id) if len(pra) == 0: raise RuntimeError( "Please input a valid projectum id or a valid fragment of a projectum id" ) print("Projecta not found. Projecta with similar names: ") for i in range(len(pra)): print(f"{pra[i].get('_id')}") print("Please rerun the helper specifying the complete ID.") return milestones = target_prum.get('milestones') deliverable = target_prum.get('deliverable') kickoff = target_prum.get('kickoff') deliverable['identifier'] = 'deliverable' kickoff['identifier'] = 'kickoff' for item in milestones: item['identifier'] = 'milestones' all_milestones = [deliverable, kickoff] all_milestones.extend(milestones) for i in all_milestones: i['due_date'] = get_due_date(i) all_milestones.sort(key=lambda x: x['due_date'], reverse=False) index_list = list(range(2, (len(all_milestones) + 2))) if not rc.index: deliverable['name'] = 'deliverable' print("Please choose from one of the following to update/add:") print("1. new milestone") for i, j in zip(index_list, all_milestones): if rc.current: if j.get("status") not in CURRENT_STATI: del j['identifier'] continue print(f"{i}. {j.get('name')} due date: {j.get('due_date')}" f" status: {j.get('status')}") if rc.verbose: if j.get("audience"): print(f" audience: {j.get('audience')}") if j.get("objective"): print(f" objective: {j.get('objective')}") if j.get("type"): print(f" type: {j.get('type')}") if j.get('notes'): print(f" notes:") for note in j.get('notes', []): print(f" - {note}") if j.get('identifier') == 'deliverable': del j['name'] del j['identifier'] return if rc.type and rc.type not in (list( chain.from_iterable( (k, v) for k, v in ALLOWED_TYPES.items()))): raise KeyError( f"please rerun specifying --type with a value from {ALLOWED_TYPES}" ) if rc.status and rc.status not in (list( chain.from_iterable( (k, v) for k, v in ALLOWED_STATI.items()))): raise KeyError( f"please rerun specifying --status with a value from {ALLOWED_STATI}" ) rc.index = rc.index.replace(" ", "") if "-" in rc.index: idx_parsed = [ i for i in range(int(rc.index.split('-')[0]), int(rc.index.split('-')[1]) + 1) ] elif "," in rc.index: idx_parsed = [int(i) for i in rc.index.split(',')] else: idx_parsed = [int(rc.index)] for idx in idx_parsed: pdoc = {} if idx == 1: mil = {} if not rc.due_date or not rc.name or not rc.objective: raise RuntimeError( "name, objective, and due date are required for a new milestone" ) mil.update({'due_date': rc.due_date}) mil['due_date'] = get_due_date(mil) mil.update({'objective': rc.objective, 'name': rc.name}) if rc.audience: mil.update({'audience': rc.audience}) else: mil.update({'audience': ['lead', 'pi', 'group_members']}) if rc.type: if rc.type in ALLOWED_TYPES: mil.update({'type': ALLOWED_TYPES.get(rc.type)}) else: mil.update({'type': rc.type}) else: mil.update({'status': 'proposed'}) if rc.status: if rc.status in ALLOWED_STATI: mil.update({'status': ALLOWED_STATI.get(rc.status)}) else: mil.update({'status': rc.status}) else: mil.update({'type': 'meeting'}) if rc.notes: mil.update({'notes': rc.notes}) milestones.append(mil) pdoc = {'milestones': milestones} if idx > 1: doc = all_milestones[idx - 2] identifier = doc['identifier'] if not doc.get( 'type') and not rc.type and identifier == 'milestones': raise RuntimeError( "ERROR: This milestone does not have a type set and this is required. " "Please rerun your command adding '--type' " f"and typing a type from this list: {ALLOWED_TYPES}") if rc.type: if rc.type in ALLOWED_TYPES: doc.update({'type': ALLOWED_TYPES.get(rc.type)}) else: doc.update({'type': rc.type}) if rc.finish: rc.status = "f" doc.update({'end_date': now}) if identifier == 'deliverable': name = 'deliverable' else: name = doc['name'] print( "The milestone {} has been marked as finished in prum {}" .format(name, key)) if rc.status: if rc.status in ALLOWED_STATI: doc.update({'status': ALLOWED_STATI.get(rc.status)}) else: doc.update({'status': rc.status}) if rc.audience: doc.update({'audience': rc.audience}) if rc.due_date: doc.update({'due_date': rc.due_date}) doc['due_date'] = get_due_date(doc) if rc.notes: notes = doc.get("notes", []) notes.extend(rc.notes) doc["notes"] = notes if identifier == 'milestones': if rc.name: doc.update({'name': rc.name}) if rc.objective: doc.update({'objective': rc.objective}) new_mil = [] for i, j in zip(index_list, all_milestones): if j['identifier'] == 'milestones' and i != idx: new_mil.append(j) new_mil.append(doc) pdoc.update({'milestones': new_mil}) else: pdoc.update({identifier: doc}) rc.client.update_one(rc.database, rc.coll, filterid, pdoc) for i in all_milestones: del i['identifier'] print("{} has been updated in projecta".format(key)) return
def test_fragment_retrieval(input, expected): assert (fragment_retrieval(input[0], input[1], input[2], case_sensitive=input[3]) == expected)