Example #1
0
def get_device(user, uid, user_agent, undelete=True):
    """
    Loads or creates the device indicated by user, uid.

    If the device has been deleted and undelete=True, it is undeleted.
    """

    store_ua = user.get_wksetting(STORE_UA)

    save = False

    try:
        device = user.get_device_by_uid(uid, only_active=False)

    except DeviceDoesNotExist:
        device = Device(uid=uid)
        user.devices.append(device)
        save = True

    if device.deleted and undelete:
        device.deleted = False
        user.set_device(device)
        save = True

    if store_ua and user_agent and \
            getattr(device, 'user_agent', None) != user_agent:
        device.user_agent = user_agent
        user.set_device(device)
        save = True

    if save:
        user.save()

    return device
Example #2
0
    def _get(user, uid, undelete):

        save = False

        try:
            device = user.get_device_by_uid(uid, only_active=False)

        except DeviceDoesNotExist:
            device = Device(uid=uid)
            user.devices.append(device)
            save = True

        if device.deleted and undelete:
            device.deleted = False
            user.set_device(device)
            save = True

        if store_ua and user_agent and getattr(device, "user_agent", None) != user_agent:
            device.user_agent = user_agent
            user.set_device(device)
            save = True

        if save:
            user.save()

        return device