Beispiel #1
0
    def test_with_correct_context_class(self):
        @context.maybe_with_context()
        def foo():
            return ContextBroker().context

        with context.ReadonlyContext(object()) as ctx:
            assert foo() is ctx

        class MyContext(context.ReadonlyContext):
            pass

        with MyContext(object()) as ctx:
            assert foo() is ctx

        class WeirdContext(object):
            database = object()
            user = FakeUser()

            def __enter__(self):
                ContextBroker().pushContext(self)
                return self

            def __exit__(self, exc_type, exc_val, exc_tb):
                assert ContextBroker().context == self
                ContextBroker().popContext()

        with WeirdContext():
            ctx = foo()
            assert isinstance(ctx, context.ReadonlyContext)
            assert ctx.database is WeirdContext.database
def main(pipe):
    database = accounting.db.connect()

    with context.ReadonlyContext(database) as ctx:
        print 'Fetching accounts and balances.'
        accounts = [
            account for account in blm.accounting.Account._query(
                _attrList=['balance', 'balance_quantity']).run()
        ]
        balances = dict((toi.id[0], (toi.balance[0], toi.balance_quantity[0]))
                        for toi in accounts)
        accounts = [account.id[0] for account in accounts]

    for n, toids in enumerate(iterate.chunks(accounts, 100)):
        interested = 'update-%s' % toids[0]
        print 'Creating commit %d for %s' % (n, interested)
        with commit.CommitContext(database) as ctx:
            blm.accounting.Account._query(id=toids,
                                          _attrList=[
                                              'opening_balance',
                                              'opening_quantity', 'balance',
                                              'quantity'
                                          ]).run()
            ops = []
            for toid in toids:
                op = commit.CallToi(toid, 'updateBalance', [])
                ops.append(op)
            ctx.runCommit(ops, interested=interested)
        pipe.send(interested)
    pipe.send(None)
    pipe.close()

    with context.ReadonlyContext(database) as ctx:
        print 'Fetching accounts and balances for verification.'
        accounts = [
            account for account in blm.accounting.Account._query(
                _attrList=['balance', 'balance_quantity']).run()
        ]
        new_balances = dict(
            (toi.id[0], (toi.balance[0], toi.balance_quantity[0]))
            for toi in accounts)

    for account, old in balances.iteritems():
        new = new_balances.get(account)
        if new != old:
            print 'DIFF: %s %r -> %r' % (account, old, new)
Beispiel #3
0
def main(args):
    database = accounting.db.connect()

    with context.ReadonlyContext(database) as ctx:
        for org in args:
            org = blm.accounting.Org._query(id=ObjectId(org))
            org = org.run()
            if org:
                print ', '.join(org[0].email)
Beispiel #4
0
def invoke_toi(request, database, user, toid, methodName):
    with context.ReadonlyContext(database, user):
        try:
            toi, = blm.TO._query(id=toid).run()
            method = getattr(toi, methodName)
        except (ValueError, AttributeError):
            log.debug('No such toi method: %s.%s', toid, methodName)
            flask.abort(httplib.NOT_FOUND)

    params = _get_params(request, method)
    op = commit.CallToi(toid, methodName, params)
    return _invoke(request, database, user, op)
Beispiel #5
0
    def test_with_custom_context_class(self):
        class MyContext(context.ReadonlyContext):
            pass

        database = object()
        user = FakeUser()

        @context.maybe_with_context(MyContext)
        def foo():
            return ContextBroker().context

        with context.ReadonlyContext(database, user):
            ctx = foo()
            assert isinstance(ctx, MyContext)
            assert ctx.database is database
            assert ctx.user is user
Beispiel #6
0

if __name__ == '__main__':

    class StdOutWrapper(object):
        def __init__(self, fp):
            self.fp = fp

        def write(self, s):
            self.fp.write(s.encode('cp437'))

    from pytransact import context
    import bson, locale, sys
    locale.setlocale(locale.LC_CTYPE, '')
    enc = locale.getpreferredencoding()
    database = db.connect()
    for acctname in sys.argv[1:]:
        acctname = acctname.decode(enc)
        sys.stdout.write('Exporting %s ' % acctname)
        sys.stdout.flush()
        interested = bson.objectid.ObjectId()
        with context.ReadonlyContext(database) as ctx:
            org, acct = acctname.split(':')
            org = accounting.Org._query(name=Q.Like('%s*' % org)).run()
            if org:
                acct = accounting.Accounting._query(org=org,
                                                    start=Q.Like('%s*' %
                                                                 acct)).run()

            sie_export(StdOutWrapper(sys.stdout), acct[0])