def _update_employees(employee_dicts):
"""Given a JSON string in the format "[{employee info 1}, {employee info 2}, ...]",
create new employee records and update existing records as necessary.
Then determine whether any employees have been terminated since the last update,
and mark these employees as such.
"""
logging.info('Updating employees...')
all_employees, new_employees = [], []
current_usernames = set()
for d in employee_dicts:
existing_employee = Employee.query(Employee.username == d['username']).get()
if existing_employee is None:
new_employee = Employee.create_from_dict(d, persist=False)
all_employees.append(new_employee)
new_employees.append(new_employee)
else:
existing_employee.update_from_dict(d)
# If the user is in the S3 dump, then the user is no longer
# terminated.
existing_employee.terminated = False
all_employees.append(existing_employee)
current_usernames.add(d['username'])
ndb.put_multi(all_employees)
# Figure out if there are any employees in the DB that aren't in the S3
# dump. These are terminated employees, and we need to mark them as such.
usernames_to_employees = dict(
(employee.username, employee)
for employee
in Employee.query()
)
db_usernames = set(usernames_to_employees.keys())
terminated_usernames = db_usernames - current_usernames
terminated_employees = []
for u in terminated_usernames:
employee = usernames_to_employees[u]
employee.terminated = True
terminated_employees.append(employee)
ndb.put_multi(terminated_employees)
logging.info('Done.')
评论列表
文章目录