1. Robert Brewer
  2. flowrate

Commits

Robert Brewer  committed e7fed76

Update to new *_mult fields

  • Participants
  • Parent commits c829ec0
  • Branches default

Comments (0)

Files changed (1)

File flowrate/csvutil.py

View file
  • Ignore whitespace
 
 class Importer(object):
 
-    def get_account_id(self, account_name):
-        a = flowrate.db.execute("SELECT id FROM accounts WHERE name = %s",
+    def get_account(self, account_name):
+        a = flowrate.db.execute("SELECT * FROM accounts WHERE name = %s",
                                 (account_name,)).fetchone()
-        if a is None:
-            return None
-        else:
-            return a.id
 
     def add_tx(self, credit, debit, amount, postdate, description):
         exists = flowrate.db.execute(
             "SELECT * FROM transactions "
             "WHERE credit_account = %s AND debit_account = %s "
             "AND amount = %s AND postdate = %s;",
-            (credit, debit, amount, postdate)).fetchone()
+            (credit['id'], debit['id'], amount, postdate)).fetchone()
         if exists is None:
             newrow = flowrate.db.execute("INSERT INTO transactions "
                 "(amount, credit_account, debit_account, postdate,"
-                " description) VALUES (%s, %s, %s, %s, %s) RETURNING *",
-                (amount, credit, debit, postdate, description)).fetchone()
+                " description, credit_mult, debit_mult) "
+                "VALUES (%s, %s, %s, %s, %s) RETURNING *",
+                (amount, credit['id'], debit['id'], postdate, description,
+                 -1 if credit['type'] in ('asset', 'expense') else 1,
+                 1 if debit['type'] in ('asset', 'expense') else -1,
+                 )).fetchone()
             flows.fulfill(newrow)
             return newrow.id
         else:
     def process_tx(self, tx):
         """Process the given transaction row from a CSV DictReader."""
         if tx['Transaction Type'] == 'credit':
-            credit = self.get_account_id(tx['Category'])
-            debit = self.get_account_id(tx['Account Name'])
+            credit = self.get_account(tx['Category'])
+            debit = self.get_account(tx['Account Name'])
         else:
-            credit = self.get_account_id(tx['Account Name'])
-            debit = self.get_account_id(tx['Category'])
-        # Let subclasses override get_account_id if they
+            credit = self.get_account(tx['Account Name'])
+            debit = self.get_account(tx['Category'])
+        # Let subclasses override get_account if they
         # want to add new accountids, otherwise pass
         if credit is None or debit is None:
             return
             exc.tx = tx
             self.errors.append(exc)
 
-    def get_account_id(self, account_name):
-        acctid = Importer.get_account_id(self, account_name)
-        if acctid is None:
+    def get_account(self, account_name):
+        acct = Importer.get_account(self, account_name)
+        if acct is None:
             raise UnknownAccount(account_name)
-        return acctid
+        return acct
 
 
 class CommandLineMintImporter(MintImporter):
 
-    def get_account_id(self, account_name):
-        acctid = Importer.get_account_id(self, account_name)
-        if acctid is None:
-            acctid = self.ask_account_id(account_name)
-        return acctid
+    def get_account(self, account_name):
+        acct = Importer.get_account(self, account_name)
+        if acct is None:
+            acct = self.ask_account(account_name)
+        return acct
 
-    def ask_account_id(self, account_name):
+    def ask_account(self, account_name):
+        """Prompt the user to enter a new account id."""
         while True:
             newid = raw_input("Enter an account number for %s: " %
                               repr(account_name))
                 print "That account number is already in use by %s" % repr(a.name)
                 continue
             else:
-                return newid
+                return {"id": newid, "name": account_name, "type": newtype}
 
     def add_tx(self, credit, debit, amount, postdate, description):
         MintImporter.add_tx(self, credit, debit, amount, postdate, description)
-        print credit, debit, amount, postdate, description
+        print credit['id'], debit['id'], amount, postdate, description
 
 
 if __name__ == '__main__':