示例#1
0
    def _migrate_categories(self, **options):
        total_migrated = 0

        # Migrate categories without a parent first
        for lc in legacy.Category.objects.order_by('-parent').iterator():
            kwargs = {
                'name__iexact': lc.name,
            }
            # Filter by parent if one exists since categories with the same
            # name can exists as sub-categories.
            if lc.parent_id:
                kwargs['parent__name'] = lc.parent.name

            if DataCategory.objects.filter(**kwargs).exists():
                print '"{0}" already exists. Skipping...'.format(lc)
                continue

            if lc.parent_id:
                try:
                    p = DataCategory.objects.get(name=lc.parent.name)
                except DataCategory.DoesNotExist:
                    print(
                        'Parent category "{0}" does not exist for "{1}". '
                        'Skipping...'.format(lc.parent.name, lc.name))
                    continue
            else:
                p = None

            c = DataCategory(name=lc.name, parent=p, order=lc.order)
            c.save()
            print 'Migrated "{0}"'.format(c)
            total_migrated += 1

        print u'Categories migrated:\t{0}'.format(total_migrated)
示例#2
0
    def setUp(self):
        super(ConceptResourceTestCase, self).setUp()

        self.name_field = DataField.objects.get_by_natural_key(
            'tests', 'title', 'name')
        self.salary_field = DataField.objects.get_by_natural_key(
            'tests', 'title', 'salary')
        self.boss_field = DataField.objects.get_by_natural_key(
            'tests', 'title', 'boss')

        cat = DataCategory(name='Test', published=True, order=1.0)
        cat.save()

        c1 = DataConcept(name='Title', published=True, category=cat)
        c1.save()
        DataConceptField(concept=c1, field=self.name_field, order=1).save()
        DataConceptField(concept=c1, field=self.salary_field, order=2).save()
        DataConceptField(concept=c1, field=self.boss_field, order=3).save()

        # Not published
        c2 = DataConcept(name='Salary', category=cat)
        c2.save()
        DataConceptField(concept=c2, field=self.salary_field, order=1).save()
        DataConceptField(concept=c2, field=self.boss_field, order=2).save()

        c3 = DataConcept(name='Name', published=True, category=cat)
        c3.save()
        DataConceptField(concept=c3, field=self.name_field, order=1).save()

        self.c1 = c1
        self.c2 = c2
        self.c3 = c3
示例#3
0
    def _migrate_categories(self, **options):
        total_migrated = 0

        # Migrate categories without a parent first
        for lc in legacy.Category.objects.order_by('-parent').iterator():
            kwargs = {
                'name__iexact': lc.name,
            }
            # Filter by parent if one exists since categories with the same
            # name can exists as sub-categories.
            if lc.parent_id:
                kwargs['parent__name'] = lc.parent.name

            if DataCategory.objects.filter(**kwargs).exists():
                print '"{0}" already exists. Skipping...'.format(lc)
                continue

            if lc.parent_id:
                try:
                    p = DataCategory.objects.get(name=lc.parent.name)
                except DataCategory.DoesNotExist:
                    print('Parent category "{0}" does not exist for "{1}". '
                          'Skipping...'.format(lc.parent.name, lc.name))
                    continue
            else:
                p = None

            c = DataCategory(name=lc.name, parent=p, order=lc.order)
            c.save()
            print 'Migrated "{0}"'.format(c)
            total_migrated += 1

        print u'Categories migrated:\t{0}'.format(total_migrated)
示例#4
0
    def setUp(self):
        super(CategoryResourceTestCase, self).setUp()

        c1 = DataCategory(name='Title', published=True)
        c1.save()

        c2 = DataCategory(name='Other', published=False)
        c2.save()
示例#5
0
 def setUp(self):
     management.call_command('avocado', 'init', 'tests', publish=False,
                             concepts=False, quiet=True)
     self.is_manager = DataField.objects.get_by_natural_key(
         'tests', 'employee', 'is_manager')
     self.salary = DataField.objects.get_by_natural_key(
         'tests', 'title', 'salary')
     c = DataCategory(published=False)
     c.save()
     self.category = DataCategory.objects.get(pk=c.pk)
示例#6
0
class CategoryResourceTestCase(BaseTestCase):
    def setUp(self):
        super(CategoryResourceTestCase, self).setUp()

        self.c1 = DataCategory(name='Title', published=True)
        self.c1.save()

        self.c2 = DataCategory(name='Other', published=False)
        self.c2.save()

    def test_get_all(self):
        response = self.client.get('/api/categories/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 1)

    def test_get_one(self):
        response = self.client.get('/api/categories/999/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.not_found)

        response = self.client.get('/api/categories/{0}/'.format(self.c1.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertTrue(json.loads(response.content))
        self.assertEqual(
            response['Link-Template'],
            ('<http://testserver/api/categories/{id}/>; rel="self", '
             '<http://testserver/api/categories/{parent_id}/>; rel="parent"'))

        event = Log.objects.filter(event='read', object_id=self.c1.pk)
        self.assertTrue(event.exists())

    def test_get_privileged(self):
        self.client.login(username='******', password='******')

        response = self.client.get('/api/categories/?unpublished=1',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(len(json.loads(response.content)), 2)

        response = self.client.get('/api/categories/{0}/?unpublished=1'.format(
            self.c2.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertTrue(json.loads(response.content))

        # Make sure the unpublished categories are only exposed when explicitly
        # asked for even when a superuser makes the request.
        response = self.client.get('/api/categories/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(len(json.loads(response.content)), 1)

        response = self.client.get('/api/categories/{0}/'.format(self.c2.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.not_found)
示例#7
0
class CategoryResourceTestCase(BaseTestCase):
    def setUp(self):
        super(CategoryResourceTestCase, self).setUp()

        self.c1 = DataCategory(name='Title', published=True)
        self.c1.save()

        self.c2 = DataCategory(name='Other', published=False)
        self.c2.save()

    def test_get_all(self):
        response = self.client.get('/api/categories/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 1)

    def test_get_one(self):
        response = self.client.get('/api/categories/999/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.not_found)

        response = self.client.get('/api/categories/{0}/'.format(self.c1.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertTrue(json.loads(response.content))
        self.assertEqual(response['Link-Template'], (
            '<http://testserver/api/categories/{id}/>; rel="self", '
            '<http://testserver/api/categories/{parent_id}/>; rel="parent"'
        ))

        event = Log.objects.filter(event='read', object_id=self.c1.pk)
        self.assertTrue(event.exists())

    def test_get_privileged(self):
        self.client.login(username='******', password='******')

        response = self.client.get('/api/categories/?unpublished=1',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(len(json.loads(response.content)), 2)

        response = self.client.get('/api/categories/{0}/?unpublished=1'
                                   .format(self.c2.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertTrue(json.loads(response.content))

        # Make sure the unpublished categories are only exposed when explicitly
        # asked for even when a superuser makes the request.
        response = self.client.get('/api/categories/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(len(json.loads(response.content)), 1)

        response = self.client.get('/api/categories/{0}/'.format(self.c2.pk),
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.not_found)
示例#8
0
    def setUp(self):
        management.call_command("avocado", "init", "search", quiet=True)

        # Create categories for test purposes.
        category = DataCategory(name="Avocado", published=True)
        category.save()

        DataField.objects.update(category=category)
        DataConcept.objects.update(category=category)

        self._haystack = OPTIONAL_DEPS["haystack"]
        OPTIONAL_DEPS["haystack"] = None
示例#9
0
文件: tests.py 项目: rysdyk/avocado
    def setUp(self):
        management.call_command('avocado', 'init', 'search', quiet=True)

        # Create categories for test purposes.
        category = DataCategory(name='Avocado', published=True)
        category.save()

        DataField.objects.update(category=category)
        DataConcept.objects.update(category=category)

        self._haystack = OPTIONAL_DEPS['haystack']
        OPTIONAL_DEPS['haystack'] = None
示例#10
0
 def setUp(self):
     management.call_command('avocado',
                             'init',
                             'tests',
                             publish=False,
                             concepts=False,
                             quiet=True)
     self.is_manager = DataField.objects.get_by_natural_key(
         'tests', 'employee', 'is_manager')
     self.salary = DataField.objects.get_by_natural_key(
         'tests', 'title', 'salary')
     c = DataCategory(published=False)
     c.save()
     self.category = DataCategory.objects.get(pk=c.pk)
示例#11
0
    def setUp(self):
        management.call_command("avocado", "init", "search", quiet=True)

        # Create categories for test purposes
        parent = DataCategory(name="Harvest", published=True)
        parent.save()

        category = DataCategory(name="Avocado", parent=parent, published=True)
        category.save()

        DataField.objects.update(category=category)
        DataConcept.objects.update(category=category)

        management.call_command("rebuild_index", interactive=False, verbosity=0)
示例#12
0
文件: tests.py 项目: rysdyk/avocado
    def setUp(self):
        management.call_command('avocado', 'init', 'search', quiet=True)

        # Create categories for test purposes
        parent = DataCategory(name='Harvest', published=True)
        parent.save()

        category = DataCategory(name='Avocado', parent=parent, published=True)
        category.save()

        DataField.objects.update(category=category)
        DataConcept.objects.update(category=category)

        management.call_command('rebuild_index', interactive=False,
                                verbosity=0)
示例#13
0
    def test_get_all_category_sort(self):
        # Create some temporary concepts and categories.
        cat1 = DataCategory(name='Category1', order=2.0, published=True)
        cat1.save()

        c1 = DataConcept(name='B', published=True, category=cat1, order=1)
        c1.save()
        field1 = DataConceptField(concept=c1, field=self.name_field, order=1)
        field1.save()

        c2 = DataConcept(name='C', published=True, category=cat1, order=2)
        c2.save()
        field2 = DataConceptField(concept=c2, field=self.name_field, order=1)
        field2.save()

        c3 = DataConcept(name='A', published=True, category=cat1, order=3)
        c3.save()
        field3 = DataConceptField(concept=c3, field=self.name_field, order=1)
        field3.save()

        # Check that category ordering is happening by default.
        response = self.client.get('/api/concepts/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['Title', 'Name', 'B', 'C', 'A'])

        # Reverse the ordering of the categories.
        response = self.client.get('/api/concepts/',
                                   {'order': 'desc'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['B', 'C', 'A', 'Title', 'Name'])

        # Order by concept name in addition to category.
        response = self.client.get('/api/concepts/',
                                   {'sort': 'name'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['Name', 'Title', 'A', 'B', 'C'])

        # Reverse the name and category sorting.
        response = self.client.get('/api/concepts/',
                                   {'sort': 'name', 'order': 'desc'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, codes.ok)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['C', 'B', 'A', 'Title', 'Name'])

        c1.delete()
        c2.delete()
        c3.delete()
        field1.delete()
        field2.delete()
        field3.delete()
        cat1.delete()
示例#14
0
文件: concept.py 项目: rysdyk/serrano
    def test_get_all_category_sort(self):
        # Create some temporary concepts and categories
        cat1 = DataCategory(name='Category1', order=1.0, published=True)
        cat1.save()

        c1 = DataConcept(name='B', published=True, category=cat1)
        c1.save()
        field1 = DataConceptField(concept=c1, field=self.name_field, order=1)
        field1.save()

        c2 = DataConcept(name='C', published=True, category=cat1)
        c2.save()
        field2 = DataConceptField(concept=c2, field=self.name_field, order=1)
        field2.save()

        c3 = DataConcept(name='A', published=True, category=cat1)
        c3.save()
        field3 = DataConceptField(concept=c3, field=self.name_field, order=1)
        field3.save()

        # Check that category ordering is happening by default
        response = self.client.get('/api/concepts/',
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['Title', 'Name', 'B', 'C', 'A'])

        # Reverse the ordering of the categories
        response = self.client.get('/api/concepts/',
                                   {'order': 'desc'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['B', 'C', 'A', 'Title', 'Name'])

        # Order by concept name in addition to category
        response = self.client.get('/api/concepts/',
                                   {'sort': 'name'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['Name', 'Title', 'A', 'B', 'C'])

        # Reverse the name and category sorting
        response = self.client.get('/api/concepts/',
                                   {'sort': 'name', 'order': 'desc'},
                                   HTTP_ACCEPT='application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(json.loads(response.content)), 5)
        names = [concept.get('name', '') for concept in
                 json.loads(response.content)]
        self.assertEqual(names, ['C', 'B', 'A', 'Title', 'Name'])

        c1.delete()
        c2.delete()
        c3.delete()
        field1.delete()
        field2.delete()
        field3.delete()
        cat1.delete()