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))
def setUp(self): super(ThreadPostSplitApiTestCase, self).setUp() self.category = Category.objects.get(slug='first-category') self.thread = testutils.post_thread(category=self.category) self.posts = [ testutils.reply_thread(self.thread).pk, testutils.reply_thread(self.thread).pk ] self.api_link = reverse('misago:api:thread-post-split', kwargs={ 'thread_pk': self.thread.pk, }) Category( name='Category B', slug='category-b', ).insert_at( self.category, position='last-child', save=True, ) self.category_b = Category.objects.get(slug='category-b') self.override_acl() self.override_other_acl()
def setUp(self): super(ThreadMoveApiTests, self).setUp() Category( name='Category B', slug='category-b', ).insert_at(self.category, position='last-child', save=True) self.category_b = Category.objects.get(slug='category-b')
def setUp(self): super(ThreadsMergeApiTests, self).setUp() self.api_link = reverse('misago:api:thread-merge') Category( name='Category B', slug='category-b', ).insert_at(self.category, position='last-child', save=True) self.category_b = Category.objects.get(slug='category-b')
def get_target(self, kwargs): target = super(CategoryAdmin, self).get_target(kwargs) target_is_special = bool(target.special_role) target_not_in_categories_tree = target.tree_id != CATEGORIES_TREE_ID if target.pk and (target_is_special or target_not_in_categories_tree): raise Category.DoesNotExist() else: return target
def get_target(self, kwargs): target = super(CategoryAdmin, self).get_target(kwargs) threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT) target_is_special = bool(target.special_role) target_not_in_categories_tree = target.tree_id != threads_tree_id if target.pk and (target_is_special or target_not_in_categories_tree): raise Category.DoesNotExist() else: return target
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 setUp(self): super(CategoriesUtilsTests, self).setUp() threadstore.clear() self.root = Category.objects.root_category() self.first_category = Category.objects.get(slug='first-category') """ Create categories tree for test cases: First category (created by migration) Category A + Category B + Subcategory C + Subcategory D Category E + Subcategory F """ Category( name='Category A', slug='category-a', ).insert_at(self.root, position='last-child', save=True) Category( name='Category E', slug='category-e', ).insert_at(self.root, position='last-child', save=True) self.category_a = Category.objects.get(slug='category-a') Category( name='Category B', slug='category-b', ).insert_at(self.category_a, position='last-child', save=True) self.category_b = Category.objects.get(slug='category-b') Category( name='Subcategory C', slug='subcategory-c', ).insert_at(self.category_b, position='last-child', save=True) Category( name='Subcategory D', slug='subcategory-d', ).insert_at(self.category_b, position='last-child', save=True) self.category_e = Category.objects.get(slug='category-e') Category( name='Subcategory F', slug='subcategory-f', ).insert_at(self.category_e, position='last-child', save=True) categories_acl = {'categories': {}, 'visible_categories': []} for category in Category.objects.all_categories(): categories_acl['visible_categories'].append(category.pk) categories_acl['categories'][category.pk] = { 'can_see': 1, 'can_browse': 1 } override_acl(self.user, categories_acl)
def move_categories(stdout, style): query = ''' SELECT * FROM misago_forum WHERE tree_id = %s AND level > 0 ORDER BY lft ''' root = Category.objects.root_category() for forum in fetch_assoc(query, [get_root_tree()]): if forum['type'] == 'redirect': stdout.write(style.ERROR('Skipping redirect: %s' % forum['name'])) continue if forum['level'] == 1: parent = root else: new_parent_id = movedids.get('category', forum['parent_id']) parent = Category.objects.get(pk=new_parent_id) category = Category.objects.insert_node( Category( name=forum['name'], slug=forum['slug'], description=forum['description'], is_closed=forum['closed'], prune_started_after=forum['prune_start'], prune_replied_after=forum['prune_last'], ), parent, save=True, ) movedids.set('category', forum['id'], category.pk) # second pass: move prune_archive_id for forum in fetch_assoc(query, [get_root_tree()]): if not forum['pruned_archive_id']: continue new_category_pk = movedids.get('category', forum['id']) new_archive_pk = movedids.get('category', forum['pruned_archive_id']) Category.objects.filter(pk=new_category_pk).update( archive_pruned_in=Category.objects.get(pk=new_archive_pk), )
def test_move(self): """move(new_category) moves thread to other category""" # pick category instead of category (so we don't have to create one) root_category = Category.objects.root_category() Category( name='New Category', slug='new-category', ).insert_at(root_category, position='last-child', save=True) new_category = Category.objects.get(slug='new-category') self.thread.move(new_category) self.assertEqual(self.thread.category, new_category) for post in self.thread.post_set.all(): self.assertEqual(post.category_id, new_category.id)
def setUp(self): super().setUp() Category( name='Category B', slug='category-b', ).insert_at( self.category, position='last-child', save=True, ) self.category_b = Category.objects.get(slug='category-b') self.api_link = reverse('misago:api:thread-merge', kwargs={ 'pk': self.thread.pk, })
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_move_thread(self): """moves_thread moves unapproved thread to other category""" root_category = Category.objects.root_category() Category( name='New Category', slug='new-category', ).insert_at(root_category, position='last-child', save=True) new_category = Category.objects.get(slug='new-category') self.assertEqual(self.thread.category, self.category) self.assertTrue( moderation.move_thread(self.user, self.thread, new_category)) self.reload_thread() self.assertEqual(self.thread.category, new_category) self.assertTrue(self.thread.has_events) event = self.thread.event_set.last() self.assertIn("moved thread", event.message) self.assertEqual(event.icon, "arrow-right")
def test_move_thread(self): """moves_thread moves unapproved thread to other category""" root_category = Category.objects.root_category() Category( name='New Category', slug='new-category', ).insert_at(root_category, position='last-child', save=True) new_category = Category.objects.get(slug='new-category') self.assertEqual(self.thread.category, self.category) self.assertTrue( moderation.move_thread(self.request, self.thread, new_category)) self.reload_thread() self.assertEqual(self.thread.category, new_category) event = self.thread.last_post self.assertTrue(event.is_event) self.assertEqual(event.event_type, 'moved')
def move_labels(): labels = [] for label in fetch_assoc( 'SELECT * FROM misago_threadprefix ORDER BY slug'): labels.append(label) for label in labels: query = 'SELECT * FROM misago_threadprefix_forums WHERE threadprefix_id= %s' for parent_row in fetch_assoc(query, [label['id']]): parent_id = movedids.get('category', parent_row['forum_id']) parent = Category.objects.get(pk=parent_id) category = Category.objects.insert_node(Category( name=label['name'], slug=label['slug'], ), parent, save=True) label_id = '%s-%s' % (label['id'], parent_row['forum_id']) movedids.set('label', label_id, category.pk)
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))
def setUp(self): super(AddCategoriesToThreadsTests, self).setUp() self.root = Category.objects.root_category() """ Create categories tree for test cases: First category (created by migration) Category A + Category B + Subcategory C + Subcategory D Category E + Subcategory F """ Category( name='Category A', slug='category-a', css_class='showing-category-a', ).insert_at(self.root, position='last-child', save=True) Category( name='Category E', slug='category-e', css_class='showing-category-e', ).insert_at(self.root, position='last-child', save=True) self.root = Category.objects.root_category() self.category_a = Category.objects.get(slug='category-a') Category( name='Category B', slug='category-b', css_class='showing-category-b', ).insert_at(self.category_a, position='last-child', save=True) self.category_b = Category.objects.get(slug='category-b') Category( name='Category C', slug='category-c', css_class='showing-category-c', ).insert_at(self.category_b, position='last-child', save=True) Category( name='Category D', slug='category-d', css_class='showing-category-d', ).insert_at(self.category_b, position='last-child', save=True) self.category_c = Category.objects.get(slug='category-c') self.category_d = Category.objects.get(slug='category-d') self.category_e = Category.objects.get(slug='category-e') Category( name='Category F', slug='category-f', css_class='showing-category-f', ).insert_at(self.category_e, position='last-child', save=True) self.clear_state() Category.objects.partial_rebuild(self.root.tree_id) self.root = Category.objects.root_category() self.category_a = Category.objects.get(slug='category-a') self.category_b = Category.objects.get(slug='category-b') self.category_c = Category.objects.get(slug='category-c') self.category_d = Category.objects.get(slug='category-d') self.category_e = Category.objects.get(slug='category-e') self.category_f = Category.objects.get(slug='category-f') self.categories = list( Category.objects.all_categories(include_root=True))
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))