Update export helper
This commit is contained in:
parent
e0641fa786
commit
2f45a7b5a8
|
|
@ -1,4 +1,6 @@
|
|||
import csv
|
||||
|
||||
from wagtail.blocks import StreamValue
|
||||
from books.models.contentblock import ContentBlock
|
||||
from books.models.custom_document import CustomDocument
|
||||
|
||||
|
|
@ -10,32 +12,33 @@ columns = [
|
|||
"content_block_id",
|
||||
"document_id",
|
||||
"old",
|
||||
"old_s3",
|
||||
"new",
|
||||
"module",
|
||||
"content_block",
|
||||
]
|
||||
|
||||
|
||||
def format_link(document):
|
||||
def format_link_s3(document):
|
||||
return (
|
||||
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}"
|
||||
|
||||
|
||||
def handle_all_content_blocks():
|
||||
with open("source.csv", mode="w", encoding="utf-8") as file:
|
||||
writer = csv.writer(file)
|
||||
writer.writerow(columns)
|
||||
writer = csv.DictWriter(file, fieldnames=columns)
|
||||
writer.writeheader()
|
||||
# print("id,old link,new link,module,content block link")
|
||||
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("output.csv", mode="w", encoding="utf-8") as destination:
|
||||
reader = csv.DictReader(file)
|
||||
|
|
@ -45,32 +48,12 @@ def read_csv():
|
|||
for row in reader:
|
||||
try:
|
||||
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(row["new"])
|
||||
row["old"] = old_url
|
||||
except ValueError:
|
||||
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
|
||||
row["old"] = old_django_url
|
||||
row["old_s3"] = old_s3_url
|
||||
except ValueError:
|
||||
pass
|
||||
except CustomDocument.DoesNotExist:
|
||||
|
|
@ -83,7 +66,7 @@ def print_document(document, content_block, writer):
|
|||
content_block_id = content_block.id
|
||||
try:
|
||||
# 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
|
||||
except AttributeError:
|
||||
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/{node_id}"
|
||||
# print(f"{id},,{new_link},{module_url}")
|
||||
writer.writerow(
|
||||
[content_block_id, document_id, "", new_link, module_url, content_block_url]
|
||||
row = {
|
||||
"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, contents: StreamValue, writer):
|
||||
# contents: StreamValue
|
||||
document_blocks = contents.blocks_by_name("cms_document_block")
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
def handle_content_block(content_block: ContentBlock, writer):
|
||||
document_blocks = content_block.contents.blocks_by_name("cms_document_block")
|
||||
instruction_block = content_block.contents.blocks_by_name("instruction")
|
||||
solution_block = content_block.contents.blocks_by_name("solution")
|
||||
for db in document_blocks:
|
||||
document = db.value
|
||||
print_document(document, content_block, writer)
|
||||
|
|
|
|||
Loading…
Reference in New Issue