def save_super_domain(data): dom = SuperDomain(**data).load() if not dom.id: identifier = camelCase(data['name']) next_iter = get_next_iteration(SuperDomain().db().find( {'identifier': { '$regex': f"{identifier}(_[1-9]+)?" }}, {'identifier': 1})) if next_iter > 0: identifier = f"{identifier}_{str(next_iter)}" new_dom = SuperDomain( **{ **data, **{ 'id': identifier, 'identifier': identifier, 'created_on': datetime.datetime.utcnow() } }) # CREATE NEW TABLES HERE dom = new_dom dom.name = data['name'] dom.description = data.get('description') dom.modified_on = datetime.datetime.utcnow() # CHECK IF NAME IS USE if SuperDomain().db().find_one({'_id': {'$ne': dom.id}, 'name': dom.name}): return {"status": 'fail', "message": 'Domain name already used'}, 409 dom.save() return {"status": 'success', "message": 'Domain saved'}, 201
def delete_super_domain(data): super_dom = SuperDomain(**data).load() if super_dom.id: dms = Domain.get_all(query={'super_domain_id': super_dom.id}) dm: Domain for dm in dms: TargetField.drop(domain_id=dm.id) dm.delete() User.remove_domain_for_users(super_dom.id) super_dom.delete() return super_dom
def save_domain(data): super_domain_id = data['super_domain_id'] super_dom = SuperDomain(**{'id': super_domain_id}).load() if super_dom.id: dom = Domain(**data).load() if not dom.id: identifier = super_dom.id + '_' + camelCase(data['name']) max = get_next_iteration(Domain().db().find( {'identifier': { '$regex': f"{identifier}(_[1-9]+)?" }}, {'identifier': 1})) if max > 0: identifier = f"{identifier}_{str(max)}" new_dom = Domain( **{ **data, **{ 'id': identifier, 'identifier': identifier, 'created_on': datetime.datetime.utcnow(), 'super_domain_id': super_domain_id } }) # CREATE NEW TABLES HERE dom = new_dom dom.name = data.get('name', None) dom.description = data.get('description', None) dom.classification = data.get('classification', None) dom.enableDF = data.get('enableDF', False) dom.modified_on = datetime.datetime.utcnow() if Domain().db().find_one({ '_id': { '$ne': dom.id }, 'name': dom.name, 'super_domain_id': super_dom.id }): return { "status": 'fail', "message": 'Collection name already used in this Domain' }, 409 dom.save() else: return { "status": "fail", "message": "No collection with provided ID found" }, 409 return {"status": "success", "message": "Collection saved"}, 201
def get_domains_hierarchy(user_rights={}): query_result = SuperDomain().db().aggregate([{ "$match": get_user_query(user_rights) }, { "$lookup": { 'from': Domain().db().name, 'localField': 'identifier', 'foreignField': 'super_domain_id', 'as': 'domains' } }]) hierarchy = [] for sd in query_result: hierarchy.append( SuperDomain(**{ **sd, 'domains': [Domain(**d) for d in sd['domains']] })) return hierarchy
def get_domains_grouped_by_super_domains(user_rights=None): query = get_user_query(user_rights) super_domains = SuperDomain.get_all(query=query) domains = Domain.get_all() res = {} for super_dom in super_domains: res[super_dom.name] = [] for dom in domains: if super_dom.id == dom.super_domain_id: dom_copy = dom.to_dict().copy() del dom_copy["created_on"] try: del dom_copy["modified_on"] except: print("key not found modified_on") res[super_dom.name].append(dom_copy) return res
def get_all_super_domains(user_rights=None): return SuperDomain.get_all(query=get_user_query(user_rights))
def get_super_dom(id): return SuperDomain(**dict(id=id)).load()