Files @ f2b9decf2752
Branch filter:

Location: NPO-Accounting/experimental-accounting-api/accounting/storage/sql/__init__.py

Joar Wandborg
SQL, GTK

- Made the storage model slightly more flexible
- Made a small P-o-C GUI application in GTK
- Polished accounting.client
- models.Transaction.id is now a str
- Fixed transaction.id marshalling for storage.ledgercli
import logging
import json

from flask.ext.sqlalchemy import SQLAlchemy

from accounting.exceptions import AccountingException
from accounting.storage import Storage
from accounting.models import Transaction, Posting, Amount

_log = logging.getLogger(__name__)
db = None


class SQLStorage(Storage):
    def __init__(self, app=None):
        global db

        if not app:
            raise Exception('Missing app keyword argument')

        self.app = app
        db = self.db = SQLAlchemy(app)

        from .models import Transaction as SQLTransaction, \
            Posting as SQLPosting, Amount as SQLAmount

        db.create_all()

        self.Transaction = SQLTransaction
        self.Posting = SQLPosting
        self.Amount = SQLAmount

    def get_transactions(self, *args, **kw):
        transactions = []

        for transaction in self.Transaction.query.all():
            dict_transaction = transaction.as_dict()
            dict_postings = dict_transaction.pop('postings')

            postings = []

            for dict_posting in dict_postings:
                dict_amount = dict_posting.pop('amount')
                posting = Posting(**dict_posting)
                posting.amount = Amount(**dict_amount)

                postings.append(posting)

            dict_transaction.update({'postings': postings})

            transactions.append(Transaction(**dict_transaction))

        return transactions

    def update_transaction(self, transaction):
        if transaction.id is None:
            raise AccountingException('The transaction id must be set for'
                                      ' update_transaction calls')

        _log.debug('DUMMY: Update transaction: %s', transaction)

    def add_transaction(self, transaction):
        if transaction.id is None:
            transaction.generate_id()

        _t = self.Transaction()
        _t.uuid = transaction.id
        _t.date = transaction.date
        _t.payee = transaction.payee
        _t.meta = json.dumps(transaction.metadata)

        self.db.session.add(_t)

        for posting in transaction.postings:
            _p = self.Posting()
            _p.transaction_uuid = transaction.id
            _p.account = posting.account
            _p.meta = json.dumps(posting.metadata)
            _p.amount = self.Amount(symbol=posting.amount.symbol,
                                    amount=posting.amount.amount)

            self.db.session.add(_p)

        self.db.session.commit()