employee.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:love 作者: Yelp 项目源码 文件源码
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.')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号