You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.2 KiB
36 lines
1.2 KiB
import os, sys, django, csv
|
|
|
|
from django.db import IntegrityError
|
|
|
|
sys.path.append("../")
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lms.settings")
|
|
django.setup()
|
|
|
|
from library.models import Tags, ArticleSection, Article
|
|
|
|
if __name__ == '__main__':
|
|
with open('./article/tags.csv') as article_csv:
|
|
article_reader = csv.DictReader(article_csv)
|
|
for row in article_reader:
|
|
Tags.objects.get_or_create(**row)
|
|
|
|
with open('./article/section.csv') as article_csv:
|
|
article_reader = csv.DictReader(article_csv)
|
|
for row in article_reader:
|
|
ArticleSection.objects.get_or_create(**row)
|
|
|
|
with open('./article/articles.csv') as article_csv:
|
|
article_reader = csv.DictReader(article_csv)
|
|
for row in article_reader:
|
|
row = dict(row)
|
|
tags = row.pop('tags', None).split("[")[1].split("]")[0].split(", ")
|
|
section = ArticleSection.objects.get(id=row.pop('section__id'))
|
|
|
|
try:
|
|
article, _is_create = Article.objects.get_or_create(section=section, **row)
|
|
for tag in tags:
|
|
if tags:
|
|
article.tags.add(Tags.objects.get(id=tag))
|
|
|
|
except IntegrityError:
|
|
pass
|
|
|