Exemple #1
0
def lookup_current_user():
    result = None
    g.user = None
    if getattr(current_app, 'connection', None) is None:
        current_app.connection = accounting.db.get_connection()
        current_app.dbname = config.config.get('accounting', 'mongodb_dbname')
    g.database = current_app.connection[current_app.dbname]
    auth = request.headers.get('Authorization')
    userid = session.get('userid')
    with ReadonlyContext(g.database):
        if auth:
            _, key = auth.split()
            try:
                g.user = blm.accounting.APIUser._query(
                    key=key, _attrList={'lastAccess'}).run()[0]
            except IndexError:
                raise werkzeug.exceptions.Forbidden()
        elif userid:
            try:
                g.user = blm.accounting.User._query(
                    id=session['userid'], _attrList={'lastAccess'}
                ).run()[0]
            except IndexError:
                del session['userid']
            else:
                invitation = session.pop('invitation', None)
                if invitation:
                    result = acceptInvitation(g.user, invitation)
        if g.user:
            if g.user.lastAccess[0] + USER_ACCESS_RESOLUTION < time.time():
                op = ChangeToi(g.user, {'lastAccess': [time.time()]})
                with CommitContext.clone() as ctx:
                    ctx.runCommit([op])  # not interested

    return result
Exemple #2
0
def process(database):
    with ReadonlyContext(database):
        for order in blm.accounting.PGOrder._query(sent=[False]).run():
            with CommitContext.clone() as ctx:
                op = CallToi(order.id[0], 'send', [])
                interested = 'sendpg-%s' % ObjectId()
                ctx.runCommit([op], interested)
                result, error = wait_for_commit(database, interested)
                if error:
                    raise error
Exemple #3
0
def acceptInvitation(user, code):
    with ReadonlyContext(g.database):
        q = blm.accounting.Invitation._query(inviteCode=code)
        for invitation in q.run():
            user, = blm.accounting.User._query(id=user.id).run()
            op = CallToi(invitation.id[0], 'accept', [[user]])
            interested = ObjectId()
            with CommitContext.clone() as ctx:
                ctx.runCommit([op], interested=interested)
            result, error = wait_for_commit(g.database, interested)
            if error:
                return redirect(url_for('invitation_expired'))
Exemple #4
0
def createOrders(database):
    with ReadonlyContext(database):
        for org in blm.accounting.identifyOrgsWithDueTransfers():
            supInvList = blm.accounting.identifyDueTransfers(org=org)
            with CommitContext.clone() as ctx:
                op = CallBlm('accounting', 'createSignedBgcOrder',
                             [[org], supInvList])
                interested = 'createSignedBgcOrder_org-%s_%s' % (org.id[0],
                                                                 ObjectId())
                ctx.runCommit([op], interested)
            result, error = wait_for_commit(database, interested)
            if error:
                raise error
Exemple #5
0
def sendOrders(database, out):
    with ReadonlyContext(database):
        for order in blm.accounting.BgcOrder._query(
                order_signed=NotEmpty(), sent=Empty()).run():
            with CommitContext.clone() as ctx:
                op = CallBlm('accounting', 'sendBgcOrder', [[order]])
                interested = 'sendBgcOrder_order-%s_%s' % (order.id[0],
                                                           ObjectId())
                ctx.runCommit([op], interested)
            result, error = wait_for_commit(database, interested)
            if error:
                raise error
            for r in result:
                out.write('Sent {}\n'.format(r))