acounting
authorSandro Knauß <knauss@netzguerilla.net>
Wed, 03 Nov 2010 01:19:26 +0100
changeset 56 3718abbfffd6
parent 55 ae3669fef3b0
child 57 97ef6ca145e6
acounting
iro/acounting.py
iro/database.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/acounting.py	Wed Nov 03 01:19:26 2010 +0100
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+
+from database import Database
+
+class Acounting(Database):
+    def __init__(self,id, connection):
+        Database.__init__(self,connection)
+        self.id=id
+
+    def setId(self,id):
+        self.id=id
+
+    def getStatus(self):
+        self.connect()
+        self.cursor.execute ("SELECT status,tel FROM %s WHERE id='%s'" % (self.connection['table'], self.id))
+        ret= self.cursor.fetchall()
+        self.disconnect()
+        return ret
+
+    def addGood(self, good,disconnect=True):
+        if type(good) == list:
+            for i in good:
+                self.addGood(i)
+            if disconnect:
+                self.disconnect()
+        else:
+            self.connect()
+            self.cursor.execute("INSERT INTO %s (id,tel,status) VALUES('%s','%s','sended')" % (self.connection['table'], self.id, good))
+            if disconnect:
+                self.disconnect()
+            
+
+    def addFailed(self, failed,disconnect=True):
+        if type(failed) == list:
+            for i in failed:
+                self.addFailed(i,False)
+            if disconnect:
+                self.disconnect()
+        else:
+            self.connect()
+            self.cursor.execute ("INSERT INTO %s (id,tel,status) VALUES('%s','%s','failed')"%(self.connection['table'], self.id, failed))
+            if disconnect:
+                self.disconnect()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/database.py	Wed Nov 03 01:19:26 2010 +0100
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import MySQLdb
+import sqlite3
+class Database(object):
+    def __init__(self,connection):
+        self.conn=None
+        self.cursor=None
+        self.connection=connection
+        self.testConnection()
+
+    def testConnection(self):
+        self.connect()
+        self.disconnect()
+
+    def getConn(self):
+        return self.conn
+
+    def getCursor(self):
+        return self.cursor
+
+    def connect(self):
+        if not self.getConn():
+            if (self.connection['type']=='mysql'):
+                self.conn=MySQLdb.connect(
+                    host   = self.connection["host"],
+                    db     = self.connection["db"],
+                    user   = self.connection["user"],
+                    passwd = self.connection["passwd"],
+                    )
+            if (self.connection['type']=='sqlite'):
+                self.conn=sqlite3.connect(self.connection['path'])
+
+        if not self.getCursor():
+            self.cursor = self.conn.cursor()
+
+    def disconnect(self):
+        if self.getCursor():
+            self.cursor.close()
+        self.cursor=None
+        if self.getConn():
+            self.conn.close()
+        self.conn=None
+
+