def find_child_and_grandchild(grandpa, childIndex=0): parent = Subject.find(Q('parents', 'eq', grandpa))[childIndex] try: child = Subject.find(Q('parents', 'eq', parent))[0] except IndexError: return find_child_and_grandchild(grandpa, childIndex=childIndex+1) return parent, child
def find_child_and_grandchild(grandpa, childIndex=0): parent = Subject.find(Q('parents', 'eq', grandpa))[childIndex] try: child = Subject.find(Q('parents', 'eq', parent))[0] except IndexError: return find_child_and_grandchild(grandpa, childIndex=childIndex + 1) return parent, child
def create_subject_rules(): top_levels = Subject.find(Q('parents', 'eq', [])) subA = top_levels[0] subB = top_levels[1] subC = top_levels[2] children_of_A = Subject.find(Q('parents', 'eq', subA)) subD = children_of_A[0] subE = children_of_A[1] subF, subG = find_child_and_grandchild(subB) rules = [([subA._id, subD._id], False), ([subA._id, subE._id], True), ([subB._id, subF._id, subG._id], True), ([subC._id], True)] return rules
def get_queryset(self): parent = self.request.query_params.get( 'filter[parents]', None) or self.request.query_params.get( 'filter[parent]', None) provider = get_object_or_error(PreprintProvider, self.kwargs['provider_id'], display_name='PreprintProvider') if parent: if parent == 'null': return provider.top_level_subjects if provider.subjects.exists(): return provider.subjects.filter(parent___id=parent) else: # TODO: Delet this when all PreprintProviders have a mapping # Calculate this here to only have to do it once. allowed_parents = [ id_ for sublist in provider.subjects_acceptable for id_ in sublist[0] ] allows_children = [ subs[0][-1] for subs in provider.subjects_acceptable if subs[1] ] return [ sub for sub in Subject.find(MQ('parent___id', 'eq', parent)) if provider.subjects_acceptable == [] or self.is_valid_subject(allows_children=allows_children, allowed_parents=allowed_parents, sub=sub) ] return provider.all_subjects
def create_subject_rules(): top_levels = Subject.find(Q('parents', 'eq', [])) subA = top_levels[0] subB = top_levels[1] subC = top_levels[2] children_of_A = Subject.find(Q('parents', 'eq', subA)) subD = children_of_A[0] subE = children_of_A[1] subF, subG = find_child_and_grandchild(subB) rules = [ ([subA._id, subD._id], False), ([subA._id, subE._id], True), ([subB._id, subF._id, subG._id], True), ([subC._id], True) ] return rules
def get_queryset(self): parent = self.request.query_params.get('filter[parents]', None) provider = PreprintProvider.load(self.kwargs['provider_id']) if parent: if parent == 'null': return provider.top_level_subjects # Calculate this here to only have to do it once. allowed_parents = [id_ for sublist in provider.subjects_acceptable for id_ in sublist[0]] allows_children = [subs[0][-1] for subs in provider.subjects_acceptable if subs[1]] return [sub for sub in Subject.find(Q('parents', 'eq', parent)) if provider.subjects_acceptable == [] or self.is_valid_subject(allows_children=allows_children, allowed_parents=allowed_parents, sub=sub)] return provider.all_subjects
def load_bepress(f_path): assert Subject.find().count() == 0 logger.info('Loading BePress...') with open(f_path) as fp: bpress = json.load(fp) validate_map_bepress_correctness(set(bpress.keys())) logger.info('Populating Subjects...') for text in bpress.keys(): Subject(text=text).save() assert Subject.find().count() == len(bpress.keys()) logger.info('Setting parents...') for s in Subject.find(): if bpress[s.text]['lineage']: s.parents = [Subject.find_one(Q('text', 'eq', bpress[s.text]['lineage'][-1]))] s.save() logger.info('Setting children...') for s in Subject.find(): s.children = Subject.find(Q('parents', 'eq', s)) s.save() logger.info('Successfully imported BePress taxonomy.')
def load_bepress(f_path): assert Subject.find().count() == 0 logger.info('Loading BePress...') with open(f_path) as fp: bpress = json.load(fp) validate_map_bepress_correctness(set(bpress.keys())) logger.info('Populating Subjects...') for text in bpress.keys(): Subject(text=text).save() assert Subject.find().count() == len(bpress.keys()) logger.info('Setting parents...') for s in Subject.find(): if bpress[s.text]['lineage']: s.parents = [ Subject.find_one( Q('text', 'eq', bpress[s.text]['lineage'][-1])) ] s.save() logger.info('Setting children...') for s in Subject.find(): s.children = Subject.find(Q('parents', 'eq', s)) s.save() logger.info('Successfully imported BePress taxonomy.')