-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add datas migration for Category and Document
- Loading branch information
1 parent
7a1ecd3
commit 4f84325
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
lacommunaute/documentation/management/commands/migrate_documentation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import sys | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from lacommunaute.documentation.models import Category, Document | ||
from lacommunaute.forum.models import Forum | ||
|
||
|
||
def create_categories_from_catforums(): | ||
# TODO next : | ||
# add redirections when get_absolute_url is setup | ||
transpo_dict = {} | ||
|
||
for forum in Forum.objects.filter(type=1, level=0): | ||
category = Category.objects.create( | ||
name=forum.name, | ||
short_description=forum.short_description, | ||
description=forum.description, | ||
image=forum.image, | ||
) | ||
transpo_dict[forum] = category | ||
|
||
return transpo_dict | ||
|
||
|
||
def create_document_from_forums(category_transpo_dict): | ||
# TODO next : | ||
# add redirections when get_absolute_url is setup | ||
# migrate UpVotes and TaggedItems | ||
|
||
transpo_dict = {} | ||
|
||
for forum in Forum.objects.filter(parent__type=1): | ||
document = Document.objects.create( | ||
name=forum.name, | ||
short_description=forum.short_description, | ||
description=forum.description, | ||
image=forum.image, | ||
category=category_transpo_dict[forum.parent], | ||
partner=forum.partner, | ||
certified=forum.certified, | ||
) | ||
transpo_dict[forum] = document | ||
|
||
return transpo_dict | ||
|
||
|
||
def del_forums(category_transpo_dict, document_transpo_dict): | ||
forums_to_delete = list(category_transpo_dict.keys()) + list(document_transpo_dict.keys()) | ||
return Forum.objects.filter(pk__in=[forum.pk for forum in forums_to_delete]).delete() | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "migration des forums de fiches pratiques vers la documentation" | ||
|
||
def handle(self, *args, **options): | ||
sys.stdout.write("let's go!\n") | ||
|
||
category_transpo_dict = create_categories_from_catforums() | ||
sys.stdout.write("Categories created\n") | ||
|
||
document_transpo_dict = create_document_from_forums(category_transpo_dict) | ||
sys.stdout.write("Documents created\n") | ||
|
||
## TODO next : migrate Ratings, Topics and Stats | ||
|
||
deleted_forums = del_forums(category_transpo_dict, document_transpo_dict) | ||
sys.stdout.write(f"{deleted_forums} forums deleted\n") | ||
|
||
sys.stdout.write("that's all folks!") | ||
sys.stdout.flush() |