Update export helper
This commit is contained in:
parent
e0641fa786
commit
2f45a7b5a8
|
|
@ -1,4 +1,6 @@
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
|
from wagtail.blocks import StreamValue
|
||||||
from books.models.contentblock import ContentBlock
|
from books.models.contentblock import ContentBlock
|
||||||
from books.models.custom_document import CustomDocument
|
from books.models.custom_document import CustomDocument
|
||||||
|
|
||||||
|
|
@ -10,32 +12,33 @@ columns = [
|
||||||
"content_block_id",
|
"content_block_id",
|
||||||
"document_id",
|
"document_id",
|
||||||
"old",
|
"old",
|
||||||
|
"old_s3",
|
||||||
"new",
|
"new",
|
||||||
"module",
|
"module",
|
||||||
"content_block",
|
"content_block",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def format_link(document):
|
def format_link_s3(document):
|
||||||
return (
|
return (
|
||||||
f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
|
f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def format_link2(document):
|
def format_link_django(document):
|
||||||
return f"https://app.mykv.ch{document.url}"
|
return f"https://app.mykv.ch{document.url}"
|
||||||
|
|
||||||
|
|
||||||
def handle_all_content_blocks():
|
def handle_all_content_blocks():
|
||||||
with open("source.csv", mode="w", encoding="utf-8") as file:
|
with open("source.csv", mode="w", encoding="utf-8") as file:
|
||||||
writer = csv.writer(file)
|
writer = csv.DictWriter(file, fieldnames=columns)
|
||||||
writer.writerow(columns)
|
writer.writeheader()
|
||||||
# print("id,old link,new link,module,content block link")
|
# print("id,old link,new link,module,content block link")
|
||||||
for cb in ContentBlock.objects.all():
|
for cb in ContentBlock.objects.all():
|
||||||
handle_content_block(cb, writer)
|
handle_content_block(content_block=cb, contents=cb.contents, writer=writer)
|
||||||
|
|
||||||
|
|
||||||
def read_csv():
|
def fill_csv_with_old_data():
|
||||||
with open("source.csv", mode="r", encoding="utf-8") as file:
|
with open("source.csv", mode="r", encoding="utf-8") as file:
|
||||||
with open("output.csv", mode="w", encoding="utf-8") as destination:
|
with open("output.csv", mode="w", encoding="utf-8") as destination:
|
||||||
reader = csv.DictReader(file)
|
reader = csv.DictReader(file)
|
||||||
|
|
@ -45,32 +48,12 @@ def read_csv():
|
||||||
for row in reader:
|
for row in reader:
|
||||||
try:
|
try:
|
||||||
document = CustomDocument.objects.get(id=row["document_id"])
|
document = CustomDocument.objects.get(id=row["document_id"])
|
||||||
old_url = format_link(document)
|
old_django_url = format_link_django(document)
|
||||||
|
old_s3_url = format_link_s3(document)
|
||||||
# print(document.file.url)
|
# print(document.file.url)
|
||||||
# print(row["new"])
|
# print(row["new"])
|
||||||
row["old"] = old_url
|
row["old"] = old_django_url
|
||||||
except ValueError:
|
row["old_s3"] = old_s3_url
|
||||||
pass
|
|
||||||
except CustomDocument.DoesNotExist:
|
|
||||||
print(f"Document with id {row['document_id']} does not exist")
|
|
||||||
|
|
||||||
writer.writerow(row)
|
|
||||||
|
|
||||||
|
|
||||||
def read_csv_2():
|
|
||||||
with open("output.csv", mode="r", encoding="utf-8") as file:
|
|
||||||
with open("output2.csv", mode="w", encoding="utf-8") as destination:
|
|
||||||
reader = csv.DictReader(file)
|
|
||||||
writer = csv.DictWriter(destination, fieldnames=columns)
|
|
||||||
writer.writeheader()
|
|
||||||
|
|
||||||
for row in reader:
|
|
||||||
try:
|
|
||||||
document = CustomDocument.objects.get(id=row["document_id"])
|
|
||||||
new_url = format_link2(document)
|
|
||||||
# print(document.file.url)
|
|
||||||
# print(row["new"])
|
|
||||||
row["new"] = new_url
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
except CustomDocument.DoesNotExist:
|
except CustomDocument.DoesNotExist:
|
||||||
|
|
@ -83,7 +66,7 @@ def print_document(document, content_block, writer):
|
||||||
content_block_id = content_block.id
|
content_block_id = content_block.id
|
||||||
try:
|
try:
|
||||||
# new_link = f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
|
# new_link = f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
|
||||||
new_link = format_link(document)
|
new_link = format_link_django(document)
|
||||||
document_id = document.id
|
document_id = document.id
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
new_link = "missing document"
|
new_link = "missing document"
|
||||||
|
|
@ -96,15 +79,31 @@ def print_document(document, content_block, writer):
|
||||||
# content_block_url = f"https://app.mykv.ch/{content_block_path}"
|
# content_block_url = f"https://app.mykv.ch/{content_block_path}"
|
||||||
content_block_url = f"https://app.mykv.ch/content/{node_id}"
|
content_block_url = f"https://app.mykv.ch/content/{node_id}"
|
||||||
# print(f"{id},,{new_link},{module_url}")
|
# print(f"{id},,{new_link},{module_url}")
|
||||||
writer.writerow(
|
row = {
|
||||||
[content_block_id, document_id, "", new_link, module_url, content_block_url]
|
"content_block_id": content_block_id,
|
||||||
)
|
"document_id": document_id,
|
||||||
|
"old": "",
|
||||||
|
"old_s3": "",
|
||||||
|
"new": new_link,
|
||||||
|
"module": module_url,
|
||||||
|
"content_block": content_block_url,
|
||||||
|
}
|
||||||
|
writer.writerow(row)
|
||||||
|
# writer.writerow(
|
||||||
|
# [content_block_id, document_id, "", new_link, module_url, content_block_url]
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
def handle_content_block(content_block: ContentBlock, writer):
|
def handle_content_block(content_block: ContentBlock, contents: StreamValue, writer):
|
||||||
document_blocks = content_block.contents.blocks_by_name("cms_document_block")
|
# contents: StreamValue
|
||||||
instruction_block = content_block.contents.blocks_by_name("instruction")
|
document_blocks = contents.blocks_by_name("cms_document_block")
|
||||||
solution_block = content_block.contents.blocks_by_name("solution")
|
instruction_block = contents.blocks_by_name("instruction")
|
||||||
|
solution_block = contents.blocks_by_name("solution")
|
||||||
|
list_item_blocks = contents.blocks_by_name("content_list_item")
|
||||||
|
for li in list_item_blocks:
|
||||||
|
handle_content_block(
|
||||||
|
contents=li.value, content_block=content_block, writer=writer
|
||||||
|
)
|
||||||
for db in document_blocks:
|
for db in document_blocks:
|
||||||
document = db.value
|
document = db.value
|
||||||
print_document(document, content_block, writer)
|
print_document(document, content_block, writer)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue