Last active
March 12, 2017 21:26
-
-
Save alex-pat/87546920576b88e61d0199643e729dfc to your computer and use it in GitHub Desktop.
bsuir employees
This file contains 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
#!/usr/bin/env python3 | |
from xml.dom import minidom | |
from employees.models import Employee | |
def add_attr(employee, dom, attr_name): | |
attr = dom.getElementsByTagName(attr_name) | |
employee[attr_name] = attr[0].childNodes[0].data if attr and attr[0].childNodes else '' | |
def parse(file): | |
xmldom = minidom.parse(file) | |
empls = xmldom.getElementsByTagName('employee') | |
prepods = [] | |
for empl_dom in empls: | |
empl = {} | |
add_attr(empl, empl_dom, 'academicDepartment') | |
add_attr(empl, empl_dom, 'calendarId') | |
add_attr(empl, empl_dom, 'firstName') | |
add_attr(empl, empl_dom, 'id') | |
add_attr(empl, empl_dom, 'lastName') | |
add_attr(empl, empl_dom, 'middleName') | |
add_attr(empl, empl_dom, 'photoLink') | |
add_attr(empl, empl_dom, 'rank') | |
prepods.append(empl) | |
return prepods | |
def create_employees(empls): | |
for empl in empls: | |
new_empl = Employee( | |
academicDepartment=empl['academicDepartment'], | |
calendarId=empl['calendarId'], | |
firstName=empl['firstName'], | |
bsuir_id=int(empl['id']), | |
lastName=empl['lastName'], | |
middleName=empl['middleName'], | |
photoLink=empl['photoLink'], | |
description=empl['rank'] | |
) | |
new_empl.save() | |
if __name__ == '__main__': | |
prepods = parse('empl.xml') | |
create_employees(prepods) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment