Beispiel #1
0
def upsert_device(login_id,
                  platform_id,
                  device_token,
                  application_version,
                  unregistered_ts=None):
    '''
    Add or update a device entity. Returns new or updated device with relation to login preloaded.
    '''
    session = get_session()
    login = session.query(model.Login).filter(model.Login.id == login_id).one()
    device = session.query(model.Device).\
        filter(model.Device.login == login).\
        filter(model.Device.platform_id == platform_id).\
        filter(func.coalesce(model.Device.device_token_new, model.Device.device_token) == device_token).\
        one_or_none()
    if device is not None:
        device.application_version = application_version
        device.unregistered_ts = unregistered_ts
    else:
        device = model.Device(login=login,
                              platform_id=platform_id,
                              device_token=device_token,
                              application_version=application_version,
                              unregistered_ts=unregistered_ts)
        session.add(device)
    session.commit()
    session.refresh(device)
    session.refresh(device.login)
    session.close()
    return device
Beispiel #2
0
def upsert_device(login_id,
                  platform_id,
                  device_id,
                  device_token,
                  application_version,
                  unregistered_ts=None):
    '''
    Add or update a device entity.
    '''
    session = get_session()
    login = session.query(model.Login).filter(model.Login.id == login_id).one()
    device = session.query(model.Device).\
        filter(model.Device.login == login).\
        filter(model.Device.platform_id == platform_id).\
        filter(model.Device.device_id == device_id).\
        one_or_none()
    if device is not None:
        device.device_token = device_token
        device.application_version = application_version
        device.unregistered_ts = unregistered_ts
        device.device_token_new = None
    else:
        device = model.Device(login=login,
                              platform_id=platform_id,
                              device_id=device_id,
                              device_token=device_token,
                              application_version=application_version,
                              unregistered_ts=unregistered_ts)
        session.add(device)
    session.commit()
    return device