12345678910111213141516171819202122 |
- def add_categories_to_threads(root_category, categories, threads):
- categories_dict = {}
- for category in categories:
- categories_dict[category.pk] = category
- top_categories_map = {}
- for thread in threads:
- thread.category = categories_dict[thread.category_id]
- if thread.category == root_category:
- thread.top_category = None
- elif thread.category.parent_id == root_category.pk:
- thread.top_category = thread.category
- elif thread.category_id in top_categories_map:
- thread.top_category = top_categories_map[thread.category_id]
- else:
- for category in categories:
- if (category.parent_id == categories[0].pk and
- category.has_child(thread.category)):
- top_categories_map[thread.category_id] = category
- thread.top_category = category
|