Exemple #1
0
def get_instance_config():
    dbsession = DBSession()
    rows = dbsession.query(InstanceConfig.name, InstanceConfig.value).all()
    data = dict(rows)
    if 'client_id' not in data or 'client_secret' not in data:
        raise ValueError('This instance has not yet been configured.')
    return data
Exemple #2
0
def prepare_user(request, access_token, parent):
    """Create or update a User object using the given access token."""
    instance_config = get_instance_config()
    response = requests.post(instance_config['api_url'] + '/wallet/info', {
        'access_token': access_token,
    })
    if response.status_code != 200:
        if response.status_code == 401:
            root = find_root(parent)
            url = request.resource_url(root, 'login')
            raise HTTPFound(location=url)
        else:
            response.raise_for_status()

    info = json.loads(response.content)
    me = info['profile']
    wingcash_id = me['id']
    dbsession = DBSession()
    user = dbsession.query(User).get(wingcash_id)
    if user is None:
        user = User(wingcash_id=wingcash_id)
        dbsession.add(user)
    user.__parent__ = parent
    user.__name__ = unicode(wingcash_id)
    user.access_token = access_token
    user.display_name = unicode(me['title'])
    user.url = me['url']
    user.image48 = me['image48']
    user.cash_usd = str(sum(
        Decimal(h['amount'])
        for h in me['holdings']
        if h['currency'] == 'USD' and h['loop_id'] == '0'))
    return user
Exemple #3
0
def configure_view(root, request):
    form = Form(
        ConfigureSchema(),
        buttons=(Button(name='submit', value='Save Changes'),))
    message = ''
    dbsession = DBSession()

    if 'submit' in request.POST:
        try:
            data = form.validate(request.POST.items())
        except ValidationFailure, e:
            rform = e.render()
        else:
            for old_obj in dbsession.query(InstanceConfig).filter(
                    InstanceConfig.name.in_(data.keys())).all():
                dbsession.delete(old_obj)
            for name, value in data.items():
                dbsession.add(InstanceConfig(name=name, value=value))
            rform = form.render(data)
            message = 'Changes Saved.'