def dump_categories_data(apps, schema_editor, **context) -> dict: for category in OldCategory.get_root_nodes(): if _is_wrong_blog_related_to_categories_tree(category): raise Exception( f'One or several of descendants of this category are linked to articles from different blogs: {category.safe_translation_getter("name", language_code=settings.LANGUAGE_CODE, any_language=True)}' ) categories_data_list = prepare_category_data_list( OldCategory.get_root_nodes()) upload_categories_data(categories_data_list) assign_blog_configs()
def assign_blog_configs(): for root_category in OldCategory.get_root_nodes(): tree_related_app_configs = list([ article.app_config for article in root_category.article_set.all() ]) for category in root_category.get_descendants(): tree_related_app_configs.extend( [article.app_config for article in category.article_set.all()]) if tree_related_app_configs: newsblog_config = list(set(tree_related_app_configs))[0] else: newsblog_config = None new_category = NewCategory.objects.get( translations__slug=root_category.slug) if newsblog_config: new_category.newsblog_config = newsblog_config new_category.save() for category in root_category.get_descendants(): new_category = NewCategory.objects.get( translations__slug=category.slug) if newsblog_config: new_category.newsblog_config = newsblog_config new_category.save()