Add script to export documents
This commit is contained in:
parent
9cf28afdb1
commit
cfbf688946
|
|
@ -0,0 +1,116 @@
|
|||
import csv
|
||||
from books.models.contentblock import ContentBlock
|
||||
from books.models.custom_document import CustomDocument
|
||||
|
||||
# from books.schema.nodes.content import ContentBlockNode
|
||||
from graphql_relay import to_global_id
|
||||
|
||||
|
||||
columns = [
|
||||
"content_block_id",
|
||||
"document_id",
|
||||
"old",
|
||||
"new",
|
||||
"module",
|
||||
"content_block",
|
||||
]
|
||||
|
||||
|
||||
def format_link(document):
|
||||
return (
|
||||
f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
|
||||
)
|
||||
|
||||
|
||||
def format_link2(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)
|
||||
# print("id,old link,new link,module,content block link")
|
||||
for cb in ContentBlock.objects.all():
|
||||
handle_content_block(cb, writer)
|
||||
|
||||
|
||||
def read_csv():
|
||||
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)
|
||||
writer = csv.DictWriter(destination, fieldnames=columns)
|
||||
writer.writeheader()
|
||||
|
||||
for row in reader:
|
||||
try:
|
||||
document = CustomDocument.objects.get(id=row["document_id"])
|
||||
old_url = format_link(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
|
||||
except ValueError:
|
||||
pass
|
||||
except CustomDocument.DoesNotExist:
|
||||
print(f"Document with id {row['document_id']} does not exist")
|
||||
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
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)
|
||||
document_id = document.id
|
||||
except AttributeError:
|
||||
new_link = "missing document"
|
||||
document_id = ""
|
||||
module_slug = content_block.get_parent().get_parent().slug
|
||||
module_url = f"https://app.mykv.ch/module/{module_slug}"
|
||||
# content_block_node = ContentBlockNode(content_block)
|
||||
# content_block_path = content_block_node.resolve_path(content_block, None)
|
||||
node_id = to_global_id("ContentBlockNode", content_block.id)
|
||||
# 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]
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
for ib in instruction_block:
|
||||
document = ib.value["document"]
|
||||
print_document(document, content_block, writer)
|
||||
for sb in solution_block:
|
||||
document = sb.value["document"]
|
||||
print_document(document, content_block, writer)
|
||||
Loading…
Reference in New Issue