Ejemplo n.º 1
0
    def handle(self, *args, **options):
        items_to_create = options['categories']
        min_level = options['minlevel']

        categories = Category.objects.all_categories(True)

        copy_acl_from = list(Category.objects.all_categories())[0]

        categories = categories.filter(level__gte=min_level)
        fake = Factory.create()

        message = 'Creating %s fake categories...\n'
        self.stdout.write(message % items_to_create)

        message = '\n\nSuccessfully created %s fake categories in %s'

        created_count = 0
        start_time = time.time()
        show_progress(self, created_count, items_to_create)

        while created_count < items_to_create:
            parent = random.choice(categories)

            new_category = Category()
            if random.randint(1, 100) > 75:
                new_category.set_name(fake.catch_phrase().title())
            else:
                new_category.set_name(fake.street_name())

            if random.randint(1, 100) > 50:
                if random.randint(1, 100) > 80:
                    new_category.description = '\r\n'.join(fake.paragraphs())
                else:
                    new_category.description = fake.paragraph()

            new_category.insert_at(parent,
                position='last-child',
                save=True,
            )

            copied_acls = []
            for acl in copy_acl_from.category_role_set.all():
                copied_acls.append(RoleCategoryACL(
                    role_id=acl.role_id,
                    category=new_category,
                    category_role_id=acl.category_role_id,
                ))

            if copied_acls:
                RoleCategoryACL.objects.bulk_create(copied_acls)

            created_count += 1
            show_progress(
                self, created_count, items_to_create, start_time)

        acl_version.invalidate()

        total_time = time.time() - start_time
        total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
        self.stdout.write(message % (created_count, total_humanized))
Ejemplo n.º 2
0
    def test_all_categories(self):
        """all_categories returns queryset with categories tree"""
        root = Category.objects.root_category()

        test_category_a = Category(name='Test')
        test_category_a.insert_at(root, position='last-child', save=True)

        test_category_b = Category(name='Test 2')
        test_category_b.insert_at(root, position='last-child', save=True)

        all_categories_from_db = list(Category.objects.all_categories(True))

        self.assertIn(test_category_a, all_categories_from_db)
        self.assertIn(test_category_b, all_categories_from_db)
    def test_all_categories(self):
        """all_categories returns queryset with categories tree"""
        root = Category.objects.root_category()

        test_category_a = Category(name='Test')
        test_category_a.insert_at(root, position='last-child', save=True)

        test_category_b = Category(name='Test 2')
        test_category_b.insert_at(root, position='last-child', save=True)

        all_categories_from_db = list(Category.objects.all_categories(True))

        self.assertIn(test_category_a, all_categories_from_db)
        self.assertIn(test_category_b, all_categories_from_db)
Ejemplo n.º 4
0
    def handle(self, *args, **options):
        try:
            fake_cats_to_create = int(args[0])
        except IndexError:
            fake_cats_to_create = 5
        except ValueError:
            self.stderr.write("\nOptional argument should be integer.")
            sys.exit(1)

        categories = Category.objects.all_categories(True)

        try:
            min_level = int(args[1])
        except (IndexError):
            min_level = 0
        except ValueError:
            self.stderr.write("\nSecond optional argument should be integer.")
            sys.exit(1)

        copy_acl_from = list(Category.objects.all_categories())[0]

        categories = categories.filter(level__gte=min_level)
        fake = Factory.create()

        message = 'Creating %s fake categories...\n'
        self.stdout.write(message % fake_cats_to_create)

        message = '\n\nSuccessfully created %s fake categories in %s'

        created_count = 0
        start_time = time.time()
        show_progress(self, created_count, fake_cats_to_create)
        for i in range(fake_cats_to_create):
            parent = random.choice(categories)

            new_category = Category()
            if random.randint(1, 100) > 75:
                new_category.set_name(fake.catch_phrase().title())
            else:
                new_category.set_name(fake.street_name())

            if random.randint(1, 100) > 50:
                if random.randint(1, 100) > 80:
                    new_category.description = '\r\n'.join(fake.paragraphs())
                else:
                    new_category.description = fake.paragraph()

            new_category.insert_at(parent,
                position='last-child',
                save=True,
            )

            copied_acls = []
            for acl in copy_acl_from.category_role_set.all():
                copied_acls.append(RoleCategoryACL(
                    role_id=acl.role_id,
                    category=new_category,
                    category_role_id=acl.category_role_id,
                ))

            if copied_acls:
                RoleCategoryACL.objects.bulk_create(copied_acls)

            created_count += 1
            show_progress(
                self, created_count, fake_cats_to_create, start_time)

        acl_version.invalidate()

        total_time = time.time() - start_time
        total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
        self.stdout.write(message % (created_count, total_humanized))
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        try:
            fake_cats_to_create = int(args[0])
        except IndexError:
            fake_cats_to_create = 5
        except ValueError:
            self.stderr.write("\nOptional argument should be integer.")
            sys.exit(1)

        categories = Category.objects.all_categories(True)

        try:
            min_level = int(args[1])
        except (IndexError):
            min_level = 0
        except ValueError:
            self.stderr.write("\nSecond optional argument should be integer.")
            sys.exit(1)

        copy_acl_from = list(Category.objects.all_categories())[0]

        categories = categories.filter(level__gte=min_level)
        fake = Factory.create()

        message = 'Creating %s fake categories...\n'
        self.stdout.write(message % fake_cats_to_create)

        message = '\n\nSuccessfully created %s fake categories in %s'

        created_count = 0
        start_time = time.time()
        show_progress(self, created_count, fake_cats_to_create)
        for i in xrange(fake_cats_to_create):
            parent = random.choice(categories)

            new_category = Category()
            if random.randint(1, 100) > 75:
                new_category.set_name(fake.catch_phrase().title())
            else:
                new_category.set_name(fake.street_name())

            if random.randint(1, 100) > 50:
                if random.randint(1, 100) > 80:
                    new_category.description = '\r\n'.join(fake.paragraphs())
                else:
                    new_category.description = fake.paragraph()

            new_category.insert_at(
                parent,
                position='last-child',
                save=True,
            )

            copied_acls = []
            for acl in copy_acl_from.category_role_set.all():
                copied_acls.append(
                    RoleCategoryACL(
                        role_id=acl.role_id,
                        category=new_category,
                        category_role_id=acl.category_role_id,
                    ))

            if copied_acls:
                RoleCategoryACL.objects.bulk_create(copied_acls)

            created_count += 1
            show_progress(self, created_count, fake_cats_to_create, start_time)

        acl_version.invalidate()

        total_time = time.time() - start_time
        total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
        self.stdout.write(message % (created_count, total_humanized))