Exemplo n.º 1
0
 def all_subjects(self):
     q = []
     for rule in self.subjects_acceptable:
         if rule[1]:
             q.append(Q('parents', 'eq', Subject.load(rule[0][-1])))
             if len(rule[0]) == 1:
                 potential_parents = Subject.find(Q('parents', 'eq', Subject.load(rule[0][-1])))
                 for parent in potential_parents:
                     q.append(Q('parents', 'eq', parent))
         for sub in rule[0]:
             q.append(Q('_id', 'eq', sub))
     return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (Subject.find(q[0]) if len(q) else Subject.find())
Exemplo n.º 2
0
 def all_subjects(self):
     q = []
     for rule in self.subjects_acceptable:
         if rule[1]:
             q.append(Q('parents', 'eq', Subject.load(rule[0][-1])))
             if len(rule[0]) == 1:
                 potential_parents = Subject.find(
                     Q('parents', 'eq', Subject.load(rule[0][-1])))
                 for parent in potential_parents:
                     q.append(Q('parents', 'eq', parent))
         for sub in rule[0]:
             q.append(Q('_id', 'eq', sub))
     return Subject.find(reduce(lambda x, y: x | y, q)) if len(q) > 1 else (
         Subject.find(q[0]) if len(q) else Subject.find())
Exemplo n.º 3
0
def main():
    dry_run = '--dry' in sys.argv
    if not dry_run:
        script_utils.add_file_logger(logger, __file__)
    set_up_storage([Subject], storage.MongoStorage)
    with TokuTransaction():
        update_taxonomies('bepress_taxonomy.json')
        # Now that all subjects have been added to the db, compute and set
        # the 'children' field for every subject
        logger.info('Setting "children" field for each Subject')
        for subject in Subject.find():
            subject.children = Subject.find(Q('parents', 'eq', subject))
            subject.save()

        if dry_run:
            raise RuntimeError('Dry run, transaction rolled back')
Exemplo n.º 4
0
    def setUp(self):
        super(TestTaxonomy, self).setUp()

        # Subject 1 has 3 children
        self.subject1 = SubjectFactory()
        self.subject1_child1 = SubjectFactory(parent=self.subject1)
        self.subject1_child2 = SubjectFactory(parent=self.subject1)

        self.subjects = Subject.find()
        self.url = '/{}taxonomies/'.format(API_BASE)
        self.res = self.app.get(self.url)
        self.data = self.res.json['data']
Exemplo n.º 5
0
    def test_taxonomy_filter_top_level(self):
        top_level_subjects = Subject.find(Q('parents', 'eq', []))
        top_level_url = self.url + '?filter[parents]=null'

        res = self.app.get(top_level_url)
        assert_equal(res.status_code, 200)

        data = res.json['data']
        assert_equal(len(top_level_subjects), len(data))
        assert len(top_level_subjects) > 0
        for subject in data:
            assert_equal(subject['attributes']['parents'], [])
Exemplo n.º 6
0
    def test_taxonomy_filter_top_level(self):
        top_level_subjects = Subject.find(
            Q('parents', 'eq', [])
        )
        top_level_url = self.url + '?filter[parents]=null'

        res = self.app.get(top_level_url)
        assert_equal(res.status_code, 200)

        data = res.json['data']
        assert_equal(len(top_level_subjects), len(data))
        assert len(top_level_subjects) > 0
        for subject in data:
            assert_equal(subject['attributes']['parents'], [])
Exemplo n.º 7
0
    def setUp(self):
        super(TestTaxonomy, self).setUp()

        # Subject 1 has 3 children
        self.subject1 = SubjectFactory()
        self.subject1_child1 = SubjectFactory(parents=[self.subject1])
        self.subject1_child2 = SubjectFactory(parents=[self.subject1])

        # Subject 2 has a child whose parent is both subject 1 and subject 2
        self.subject2 = SubjectFactory()
        self.subject2_child1_subject1_child3 = SubjectFactory(parents=[self.subject1, self.subject2])

        self.subjects = Subject.find()
        self.url = '/{}taxonomies/'.format(API_BASE)
        self.res = self.app.get(self.url)
        self.data = self.res.json['data']
Exemplo n.º 8
0
    def test_taxonomy_filter_by_parent(self):
        children_subjects = Subject.find(
            Q('parents', 'eq', self.subject1._id)
        )
        children_url = self.url + '?filter[parents]={}'.format(self.subject1._id)

        res = self.app.get(children_url)
        assert_equal(res.status_code, 200)

        data = res.json['data']
        assert_equal(len(children_subjects), len(data))

        for subject in data:
            parents_ids = []
            for parent in subject['attributes']['parents']:
                parents_ids.append(parent['id'])
            assert_in(self.subject1._id, parents_ids)
Exemplo n.º 9
0
 def get_queryset(self):
     return Subject.find(self.get_query_from_request())
Exemplo n.º 10
0
 def top_level_subjects(self):
     if len(self.subjects_acceptable) == 0:
         return Subject.find(Q('parents', 'eq', []))
     tops = set([sub[0][0] for sub in self.subjects_acceptable])
     return [Subject.load(sub) for sub in tops]
Exemplo n.º 11
0
 def get_queryset(self):
     return Subject.find(self.get_query_from_request())
Exemplo n.º 12
0
 def top_level_subjects(self):
     if len(self.subjects_acceptable) == 0:
         return Subject.find(Q('parents', 'eq', []))
     tops = set([sub[0][0] for sub in self.subjects_acceptable])
     return [Subject.load(sub) for sub in tops]