-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_model.py
96 lines (86 loc) · 3.7 KB
/
csv_to_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import os
import csv
import django
from django.db.models import Q
from django.utils.dateparse import parse_datetime
from django.utils.timezone import make_aware
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()
from univ.models import Univ, SJ, JH, Major, Schedule
with open('csv/university.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if not Univ.objects.filter(name=row[0]):
Univ.objects.create(name=row[0], logo=row[1], review_url=row[2])
for univ in Univ.objects.all():
if not SJ.objects.filter(univ=univ, sj='수시'):
SJ.objects.create(univ=univ, sj='수시')
if not SJ.objects.filter(univ=univ, sj='정시'):
SJ.objects.create(univ=univ, sj='정시')
jh_file = f'csv/{univ.name}/jh.csv'
major_file = f'csv/{univ.name}/major.csv'
schedule_file = f'csv/{univ.name}/schedule.csv'
if os.path.isfile(jh_file):
with open(jh_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
univ = row[0]
sj = row[1]
jh = row[2]
if not JH.objects.filter(Q(sj__univ__name=univ) & Q(sj__sj=sj) & Q(name=jh)):
JH.objects.create(sj=SJ.objects.get(Q(univ__name=univ) & Q(sj=sj)), name=jh)
if os.path.isfile(major_file):
with open(major_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
univ = row[0]
sj = row[1]
jh = row[2]
major = row[3]
if not Major.objects.filter(Q(jh__sj__univ__name=univ) & Q(jh__sj__sj=sj) & Q(jh__name=jh) & Q(name=major)):
Major.objects.create(jh=JH.objects.get(Q(sj__univ__name=univ) & Q(sj__sj=sj) & Q(name=jh)), name=major)
if os.path.isfile(schedule_file):
with open(schedule_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
univ = row[0]
sj = row[1]
jh = row[2]
major = row[3]
description = row[4]
start_date = make_aware(parse_datetime(row[5]))
end_date = make_aware(parse_datetime(row[6]))
if not Schedule.objects.filter(
Q(major__jh__sj__univ__name=univ) &
Q(major__jh__sj__sj=sj) &
Q(major__jh__name=jh) &
Q(major__name=major) &
Q(description=description)
):
Schedule.objects.create(
major=Major.objects.get(
Q(jh__sj__univ__name=univ) &
Q(jh__sj__sj=sj) &
Q(jh__name=jh) &
Q(name=major)
),
description=description,
start_date=start_date,
end_date=end_date,
)
else:
schedule = Schedule.objects.get(
Q(major__jh__sj__univ__name=univ) &
Q(major__jh__sj__sj=sj) &
Q(major__jh__name=jh) &
Q(major__name=major) &
Q(description=description)
)
schedule.start_date = start_date
schedule.end_date = end_date
schedule.save()