Last active
January 31, 2019 19:37
-
-
Save carlosribas/c55a2c456ea346262d668dda4e018d87 to your computer and use it in GitHub Desktop.
Migração do Drupal para o Mezzanine CMS
This file contains hidden or 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
import csv | |
import os | |
from mezzanine.blog.models import BlogPost | |
path = "/path/to/csv/file" | |
os.chdir(path) | |
with open('result.csv') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
publish_date = datetime.fromtimestamp(int(row['changed'])) | |
if row['category'] is not '': | |
post = BlogPost(title=row['title'], created=row['created'], updated=row['changed'], publish_date=publish_date, content=row['content'], user_id=1) | |
post.save() | |
post.categories = row['category'] | |
post.save() |
This file contains hidden or 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
import csv | |
from collections import OrderedDict | |
filenames = "post_title.csv", "post_category.csv", "post_content.csv" | |
data = OrderedDict() | |
fieldnames = [] | |
for filename in filenames: | |
with open(filename, "rb") as fp: | |
reader = csv.DictReader(fp) | |
fieldnames.extend(reader.fieldnames) | |
for row in reader: | |
data.setdefault(row["id"], {}).update(row) | |
fieldnames = list(OrderedDict.fromkeys(fieldnames)) | |
with open("result.csv", "wb") as fp: | |
writer = csv.writer(fp) | |
writer.writerow(fieldnames) | |
for row in data.itervalues(): | |
writer.writerow([row.get(field, '') for field in fieldnames]) |
This file contains hidden or 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
# Backup dos posts: | |
COPY (select nid, title, created, changed from node order by nid) TO '/tmp/post_title.csv' WITH CSV DELIMITER ','; | |
# Backup do conteúdo dos posts: | |
COPY (select entity_id, body_value from field_data_body order by entity_id) TO '/tmp/post_content.csv' WITH CSV DELIMITER ','; | |
# Backup das categorias de cada post: | |
COPY (select entity_id, field_category_tid from field_data_field_category order by entity_id) TO '/tmp/post_category.csv' WITH CSV DELIMITER ','; | |
Incluir linha no início de cada arquivo e adicionar um nome para as colunas. Os seguintes nomes foram criados: | |
- post_title.csv: id,title,created,changed | |
- post_category.csv: id,category | |
- post_content.csv: id,content | |
O arquivo merge_csv.py foi utilizado para unir os três arquivos. | |
Antes de importar os posts, foi criado manualmente as categorias dos posts (respeitando os IDs). O arquivo import_data.py foi utilizado para importar os dados para o Mezzanine CMS. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment