def all(cls, instance_name=None): if 'dbcache' not in this_thread.misc: this_thread.misc['dbcache'] = {} if instance_name: listobj = DAList(instance_name, object_type=cls) else: listobj = DAList(object_type=cls) listobj.set_random_instance_name() for db_entry in list( cls._session.query(cls._model).order_by(cls._model.id).all()): if cls._model.__name__ in this_thread.misc[ 'dbcache'] and db_entry.id in this_thread.misc['dbcache'][ cls._model.__name__]: listobj.append(this_thread.misc['dbcache'][cls._model.__name__] [db_entry.id]) else: obj = listobj.appendObject() obj.id = db_entry.id db_values = {} for column in cls._model.__dict__.keys(): if column == 'id' or column.startswith('_'): continue db_values[column] = getattr(db_entry, column) if db_values[column] is not None: obj.db_set(column, db_values[column]) obj._orig = db_values obj.db_cache() listobj.gathered = True return listobj
def classified_entries(self, key=None): self._initialize() results = DAList() results.gathered = True results.set_random_instance_name() if key is None: query = MachineLearning.query.filter_by( group_id=self.group_id, active=True).order_by(MachineLearning.id).all() else: query = MachineLearning.query.filter_by( group_id=self.group_id, active=True, key=key).order_by(MachineLearning.id).all() for entry in query: results.appendObject( MachineLearningEntry, ml=self, id=entry.id, independent=fix_pickle_obj( codecs.decode( bytearray(entry.independent, encoding='utf-8'), 'base64')), dependent=fix_pickle_obj( codecs.decode(bytearray(entry.dependent, encoding='utf-8'), 'base64')), info=fix_pickle_obj( codecs.decode(bytearray(entry.info, encoding='utf-8'), 'base64')) if entry.info is not None else None, create_time=entry.create_time, key=entry.key) return results
def children(self): """ Returns list of direct children/step/foster children (not grandchildren) based on the relationship attribute.""" children = DAList(object_type=Individual, auto_gather=False, gathered=True) for person in self.elements: if person.relationship.lower() in ['child','son','daughter','foster child','stepchild','step child']: children.append(person) return children
def unclassified_entries(self, key=None): self._initialize() results = DAList()._set_instance_name_for_method() results.gathered = True if key is None: query = db.session.execute( select(MachineLearning).filter_by( group_id=self.group_id, active=False).order_by(MachineLearning.id)).scalars() else: query = db.session.execute( select(MachineLearning).filter_by( group_id=self.group_id, key=key, active=False).order_by(MachineLearning.id)).scalars() for entry in query: results.appendObject( MachineLearningEntry, ml=self, id=entry.id, independent=fix_pickle_obj( codecs.decode( bytearray(entry.independent, encoding='utf-8'), 'base64')), create_time=entry.create_time, key=entry.key, info=fix_pickle_obj( codecs.decode(bytearray(entry.info, encoding='utf-8'), 'base64')) if entry.info is not None else None) return results
def get_parent(self, rel_name, instance_name=None): if not self.ready(): raise Exception("get_parent: cannot retrieve data") info = self._parent_mapping[rel_name] model = info['relationship_class']._model if instance_name: results = DAList(instance_name, object_type=info['parent_class']) else: results = [] indexno = 0 if instance_name is None: for db_entry in list( self._session.query(model).filter( model.getattr(info['child_column']) == self.id).all()): results.append(info['parent_class'].by_id( db_entry.getattr(info['parent_column']))) else: for db_entry in list( self._session.query(model).filter( model.getattr(info['child_column']) == self.id).all()): results.append(info['parent_class'].by_id( db_entry.getattr(info['parent_column'])), instance_name=instance_name + '[' + str(indexno) + ']') indexno += 1 return results
def filter(cls, instance_name, **kwargs): if 'dbcache' not in this_thread.misc: this_thread.misc['dbcache'] = {} listobj = DAList(instance_name, object_type=cls, auto_gather=False) filters = [] for key, val in kwargs.items(): if not hasattr(cls._model, key): raise Exception("filter: class " + cls.__name__ + " does not have column " + key) filters.append(getattr(cls._model, key) == val) for db_entry in list( cls._session.query(cls._model).filter(*filters).order_by( cls._model.id).all()): if cls._model.__name__ in this_thread.misc[ 'dbcache'] and db_entry.id in this_thread.misc['dbcache'][ cls._model.__name__]: listobj.append(this_thread.misc['dbcache'][cls._model.__name__] [db_entry.id]) else: obj = listobj.appendObject() obj.id = db_entry.id db_values = {} for column in cls._model.__dict__.keys(): if column == 'id' or column.startswith('_'): continue db_values[column] = getattr(db_entry, column) if db_values[column] is not None: obj.db_set(column, db_values[column]) obj._orig = db_values obj.db_cache() listobj.gathered = True return listobj
def dalist_of_individuals(self, directory: list): """ Converts a list of Python dicts into a DAList of Individual instances. Args: directory (list): List of dicts where each dict defines a person. Returns: DAList: List of instances of Individual """ individuals = [] for entry in directory: address = Address() address.init(address=entry.get('address'), city=entry.get('city'), state='TX', zip=entry.get('zip code'), country='US') name = IndividualName() name.init(first=entry.get('first name'), middle=entry.get('middle name'), last=entry.get('last name'), suffix=entry.get('suffix')) individual = Individual() individual.init(name=name, address=address, title=entry.get('title'), phone=entry.get('phone'), email=entry.get('email'), ou=entry.get('court')) individuals.append(individual) result = DAList() result.init(object_type=Individual, elements=individuals) return result
def guardians(self): """Returns legal guardians or parents""" guardians = DAList(object_type=Individual, auto_gather=False,gathered=True) for person in self.elements: if person.relationship.lower() in ['guardian','father','mother', 'legal guardian','step father','step mother','stepparent','step parent']: guardians.append(person) return guardians
def unclassified_entries(self, key=None): self._initialize() results = DAList()._set_instance_name_for_method() results.gathered = True if key is None: query = MachineLearning.query.filter_by(group_id=self.group_id, active=False).order_by(MachineLearning.id).all() else: query = MachineLearning.query.filter_by(group_id=self.group_id, key=key, active=False).order_by(MachineLearning.id).all() for entry in query: results.appendObject(MachineLearningEntry, ml=self, id=entry.id, independent=pickle.loads(codecs.decode(entry.independent, 'base64')), create_time=entry.create_time, key=entry.key) return results
def in_category_not_subcategory(self, category_id, check_access=False, user=None): filtered = [ form for form in self.elements if hasattr(form, 'fields') and ( form.fields.get('CategoryLookupId') == category_id) and ( not form.fields.get('SubcategoryLookupId')) ] if len(filtered): return filtered else: return DAList(there_are_any=False)
def has_relationship(self, relationships): """Return a list of company memberships with the specified relationship attribute. Relationship may be a string or list of strings.""" related = DAList(object_type=Individual, auto_gather=False, gathered=True) for person in self.elements: if hasattr(person, 'relationship'): if isinstance(relationships, list): if person.relationship.lower() in relationships: related.append(person) else: if person.relationship.lower() == relationships: related.append(person) return related
def import_yaml_to_DA(database, factors, cases, model, case_collection='docassemble_openlcbr_output', domain_model='docassemble_openlcbr_output'): # First, take the content of the yaml file and turn it into Python data. stream = open(database, 'r') data = yaml.load(stream) new_factors = {} # Load the Factors into the factors object for f in data['factors']: #new_factor = DAObject() new_factor = factors.appendObject() new_factor.id = f new_factor.side = side_to_word(data['factors'][f]['favored_side']) new_factor.long_desc = data['factors'][f]['description'] new_factor.name = data['factors'][f]['proposition'] #factors.append(new_factor.copy_shallow('factors')) new_factors[f] = new_factor factors.gathered = True #factors.auto_gather = False # Load the Cases into the Cases Object for c in data['case_collections'][case_collection]['cases']: #new_case = DAIBPCase() new_case = cases.appendObject(DAIBPCase) new_case.id = c['id'] if 'name' in c: new_case.name = c['name'] else: new_case.name = c['citation'] new_case.year = c['year'] new_case.cite = c['citation'] new_case.winner = side_to_word(c['winner']) for f in c['factors']: #new_case.factors.append(new_factors[f].copy_shallow('factors')) for factor in factors: if factor.id == f: # See notes below about adding factors to issues. Bug. new_case.factors.append(factor) break new_case.factors.gathered = True #new_case.factors.auto_gather = False #cases.append(new_case) cases.gathered = True #cases.auto_gather = False # Load the model into the model Object new_issues = { } # So that we can come back to it and use it to add branches. top_issue = None # To keep track of which issue is the root issue. issues = data['domain_models'][domain_model]['issues'] for i in issues: new_issue = DAIBPIssue() new_issue.initializeAttribute('factors', DAList) new_issue.initializeAttribute('branches', DAList) new_issue.id = issues[i]['id'] new_issue.text = issues[i]['proposition'] new_issue.type = issues[i]['type'] if new_issue.type == "top" or new_issue.type == "top_level_issue": top_issue = new_issue if 'winner_if_unraised' in issues[i]: new_issue.default = issues[i]['winner_if_unraised'] if 'antecedents' in issues[i]: if 'disjoint_antecedents' in issues[i]: new_issue.join_type = "disjunctive" else: new_issue.join_type = "conjunctive" new_issue.factors.gathered = True #new_issue.factors.auto_gather = False new_issue.complete = True new_issue.branches.gathered = True new_issue.branches.object_type = DAIBPIssue #new_issue.branches.auto_gather = False new_issues[i] = new_issue # Do the recursive naming prior to adding factors and antecedents. top_issue._set_instance_name_recursively('model.issues') for i in issues: if 'factors' in issues[i]: for f in issues[i]['factors']: for factor in factors: if factor.id == f: new_issues[i].factors.append(factor) break if 'antecedents' in issues[i]: for a in issues[i]['antecedents']: new_issues[i].branches.append(new_issues[a]) model.ko_factors = DAList('model.ko_factors') for kof in data['domain_models'][domain_model]['ko_factors']: for factor in factors: if factor.id == kof: model.ko_factors.append(factor) model.ko_factors.gathered = True #model.ko_factors.auto_gather = False model.issues = top_issue model.issues.build = True
def matches_category(self, category): return DAList(elements=[ key for key, value in self.iteritems() if value.category == category ])
def unchecked_values(self): return DAList(elements=[ key for key, value in self.iteritems() if value.checked is False ])
def get_contacts(self, upn, default_address='home'): """ Return a list of contacts from the given user's Universal Principal Name (i.e., [email protected] for most organizations, or [email protected]). Does not paginate--will return the first 100 contacts only for now. You can choose whether to default to 'home' or 'business' address and phone.""" contacts_url = "https://graph.microsoft.com/v1.0/users/" + upn + "/contacts" res = self.get_request(contacts_url) people = DAList(object_type=Individual, auto_gather=False, gathered=True) for p_res in res.get('value', []): person = people.appendObject() person.name.first = p_res.get('givenName', '') person.name.last = p_res.get('surname', '') if p_res.get('middleName'): person.name.middle = p_res.get('middleName') person.jobTitle = p_res.get('jobTitle') person.title = p_res.get('title') person.business_phones = p_res.get('businessPhones', []) person.home_phones = p_res.get('homePhones', []) person.mobile_number = p_res.get('mobilePhone') person.initializeAttribute('home_address', Address) person.home_address.address = p_res.get( 'homeAddress', {}).get('street') person.home_address.city = p_res.get('homeAddress', {}).get('city') person.home_address.state = p_res.get( 'homeAddress', {}).get('state') person.home_address.zip = p_res.get( 'homeAddress', {}).get('postalCode') # if not p_res.get('homeAddress',{}).get('countryOrRegion') is None: # person.home_address.country = p_res.get('homeAddress',{}).get('countryOrRegion') person.initializeAttribute('business_address', Address) person.business_address.address = p_res.get( 'businessAddress', {}).get('street') person.business_address.city = p_res.get( 'businessAddress', {}).get('city') person.business_address.state = p_res.get( 'businessAddress', {}).get('state') person.business_address.zip = p_res.get( 'businessAddress', {}).get('postalCode') # if not p_res.get('businessAddress',{}).get('countryOrRegion') is None: # person.business_address.country = p_res.get('businessAddress',{}).get('countryOrRegion') person.emails = p_res.get('emailAddresses', []) if p_res.get('emailAddresses'): person.email = next(iter(person.emails), []).get( 'address', None) # take the first email # Try to respect the address kind the user wants, but if not present, use the address we have (which might be null) # Information is often put in the business phone/address fields if the contact only has one address/phone if default_address == 'home': if p_res.get('homeAddress'): person.address = person.home_address else: person.address = person.business_address else: if p_res.get('businessAddress'): person.address = person.business_address else: person.address = person.home_address if default_address == 'home': if p_res.get('homePhones'): # just take the first home phone in the list person.phone_number = next(iter(person.home_phones), '') else: person.phone_number = next( iter(person.business_phones), '') else: if p_res.get('businessPhones'): # just take the first business phone person.phone_number = next( iter(person.business_phones), '') else: person.phone_number = next(iter(person.home_phones), '') return people
def in_subcategory(self, subcategory_id, check_access=False, user=None): return [ form for form in self.elements if hasattr(form, 'fields') and form.fields.get('SubcategoryLookupId', False) and form.fields.get('SubcategoryLookupId') == subcategory_id ] or DAList(there_are_any=False)
def in_category(self, category_id, check_access=False, user=None): return [ form for form in self.elements if hasattr(form, 'fields') and category_id == form.fields.get('CategoryLookupId') ] or DAList(there_are_any=False)