-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_base.py
74 lines (57 loc) · 1.97 KB
/
populate_base.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
import os
from datetime import datetime
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings")
django.setup()
import data # noqa: E402
from vacancies.models import Company, Specialty, Vacancy # noqa: E402
def fill_specialties(specialties):
"""Populate speciality database
Args:
specialties (dict): 'specialties' dict from moc-file
Returns:
bool: True at the end
"""
for specialty in data.specialties:
Specialty.objects.create(**specialty)
return True
def fill_companies(companies):
"""Populate company database
Args:
companies (dict): 'companies' dict from moc-file
Returns:
dict: mapping dictionary for saving job->company relations
"""
mapping = {}
for company in companies:
moc_id = company.pop("id")
company["name"] = company["title"]
_ = company.pop("title")
new_company = Company(**company)
new_company.save()
mapping.setdefault(moc_id, new_company.pk)
return mapping
def fill_vacancies(vacancies, mapping):
"""Populate vacancy database
Args:
vacancies (dict): 'job' dict from moc-file
mapping (dict): dict which is saved relations between moc-company-id and company in database
Returns:
[type]: [description]
"""
for vacancy in vacancies:
Vacancy.objects.create(
title=vacancy["title"],
salary_min=vacancy["salary_from"],
salary_max=vacancy["salary_to"],
skills=vacancy["skills"],
description=vacancy["description"],
published_at=datetime.strptime(vacancy["posted"], "%Y-%m-%d").date(),
specialty=Specialty.objects.get(code=vacancy["specialty"]),
company=Company.objects.get(pk=mapping[vacancy["company"]]),
)
return True
if __name__ == "__main__":
fill_specialties(data.specialties)
mapping = fill_companies(data.companies)
fill_vacancies(data.jobs, mapping)