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.
102 lines
3.8 KiB
102 lines
3.8 KiB
import csv
|
|
import os
|
|
import random
|
|
import string
|
|
import sys
|
|
|
|
import django
|
|
|
|
sys.path.append("../")
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lms.settings")
|
|
django.setup()
|
|
|
|
from courses.api import InApiTeacher
|
|
from django.contrib.auth import get_user_model
|
|
from courses.models import Course, Lesson, Topic
|
|
from storage.models import File
|
|
|
|
if __name__ == '__main__':
|
|
Course.objects.all().delete()
|
|
|
|
with open('./course/course.csv') as user_csv:
|
|
user_reader = csv.DictReader(user_csv)
|
|
for row in user_reader:
|
|
row = dict(row)
|
|
teachers = row.pop('teachers', None).split("[")[1].split("]")[0].split(", ")
|
|
course, _is_create = Course.objects.get_or_create(**row)
|
|
|
|
try:
|
|
for teacher in teachers:
|
|
teacher = teacher.replace("\'", "")
|
|
if teacher:
|
|
teacher = get_user_model().objects.get(email=teacher).out_key
|
|
InApiTeacher.add_teacher(course.slug, teacher)
|
|
|
|
except get_user_model().DoesNotExist:
|
|
print('Плохо')
|
|
|
|
with open('./course/vertex.csv') as vertex_csv:
|
|
vertex_reader = csv.DictReader(vertex_csv)
|
|
for row in vertex_reader:
|
|
row = dict(row)
|
|
model_type = row.pop('type', None)
|
|
description = row.pop('description', None)
|
|
title = row.pop('title', None)
|
|
pk = row.pop('id', None)
|
|
try:
|
|
m = row.pop('materials', None)
|
|
materials = []
|
|
if m:
|
|
materials = [File.objects.get(id=i).key for i in m.split("[")[1].split("]")[0].split(", ")]
|
|
except ValueError:
|
|
pass
|
|
|
|
if model_type == 'topic':
|
|
course = Course.objects.get(id=row.pop('course', None))
|
|
Topic.objects.create(
|
|
id=pk,
|
|
icon=row.pop('icon', None),
|
|
course=course,
|
|
description=description,
|
|
title=title,
|
|
)
|
|
|
|
try:
|
|
topic_id = row.pop('topic', None)
|
|
last_pivot = PivotVertex.objects.filter(map_course=map_obj).last()
|
|
if model_type == 'tutorial':
|
|
topic = Topic.objects.get(id=topic_id)
|
|
small_vertex = Lesson.objects.create(
|
|
id=pk,
|
|
video=row.pop('video', None),
|
|
materials=materials,
|
|
topic=topic,
|
|
free=row['free'],
|
|
description=description,
|
|
title=title,
|
|
token=''.join(random.choice(string.ascii_letters) for x in range(15))
|
|
)
|
|
PivotVertex.objects.create(
|
|
map_course=map_obj,
|
|
vertex=small_vertex,
|
|
sort=last_pivot.sort+1 if last_pivot else 1,
|
|
)
|
|
|
|
if model_type == 'task':
|
|
topic = Topic.objects.get(id=topic_id)
|
|
small_vertex = Lesson.objects.create(
|
|
id=pk,
|
|
materials=materials,
|
|
topic=topic,
|
|
description=description,
|
|
title=title,
|
|
valid_type=1,
|
|
token=''.join(random.choice(string.ascii_letters) for x in range(15))
|
|
)
|
|
PivotVertex.objects.create(
|
|
map_course=map_obj,
|
|
vertex=small_vertex,
|
|
sort=last_pivot.sort+1 if last_pivot else 1,
|
|
)
|
|
except Topic.DoesNotExist:
|
|
pass |