def delete_domain(data): dom = Domain(**data) TargetField.drop(domain_id=dom.id) dom.delete() return {"message": "Collection Deleted", "status": 'success'}, 200
def share_ref_type(ref_type_id, domain_ids): # CHECK IF REF TYPE IS USED ref_type = ReferenceType().load(dict(_id=ref_type_id)) # CHEKC IF REF TYPE IS USED removed_domains_ids = set(domain_ids).union(set( ref_type.domain_ids)).difference(set(domain_ids)) for domain_id in removed_domains_ids: if ref_type.is_used_in_domain(domain_id): domain = Domain().load(dict(_id=domain_id)) if domain.id: return { 'status': 'fail', 'message': f'Cannot remove {ref_type.label} from domain {domain.name}.' }, 409 ref_type.domain_ids = domain_ids ref_type.save() return { 'status': 'success', 'message': 'Reference Type Collection Updated' }, 200
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 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 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 save_field(data, domain_id): dom = Domain(**{'id': domain_id}).load() if dom.id: target_field = TargetField(**data).load(domain_id=domain_id) original_field = copy(target_field) if not target_field.id: identifier = uuid.uuid4().hex.upper() new_dom = TargetField( **{ 'id': identifier, 'created_on': datetime.datetime.utcnow(), 'name': camelCase(data['label']) }) target_field = new_dom target_field.label = data['label'] target_field.description = data.get('description', None) target_field.type = data['type'] target_field.mandatory = data.get('mandatory', False) target_field.editable = data.get('editable', False) target_field.rules = data.get('rules', []) target_field.modified_on = datetime.datetime.utcnow() target_field.ref_type_id = data.get('ref_type_id', None) if original_field.id: if original_field.type != target_field.type: if FlowContext().db().find_one({ "domain_id": domain_id, "columns": { "$in": [target_field.name] } }): return { 'status': 'fail', 'message': 'Unable to change Field Type After Upload' }, 409 target_field.save(domain_id=domain_id) else: return {"status": "fail", "message": "Domain does exist"}, 409 return {"status": "success", "message": "Field Created"}, 200
def save_domain(data): dom = Domain(**data).load() if not dom.id: identifier = uuid.uuid4().hex.upper() new_dom = Domain( **{ **data, **{ 'id': identifier, 'identifier': identifier, 'created_on': datetime.datetime.utcnow() } }) # CREATE NEW TABLES HERE dom = new_dom else: dom.name = data['name'] dom.description = data['description'] dom.save() return dom
def get_domains_by_super_id(super_id): return Domain.get_all(query={'super_domain_id': super_id})
def get_all_domains(): return Domain.get_all()
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_domain(dom_id): return Domain(id=dom_id).load()