1
0
Fork 0

- moved all database specific code to the module, cli is just a frontend

git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@219 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
Jan Dittberner 2007-02-11 20:09:07 +00:00
parent 1846c9bcd8
commit 6b1c80899d
4 changed files with 64 additions and 42 deletions

View File

@ -20,8 +20,7 @@
#
# Version: $Id$
import getopt, sys, sqlalchemy
import getopt, sys
from gnuviechadmin import client, exceptions
def usage():
@ -104,17 +103,10 @@ def main():
if verbose:
print "parsed client data is ", clientdata
try:
myclient = client.Client(**clientdata)
except exceptions.MissingFieldsError, mfe:
print mfe
myclient = client.create(**clientdata)
except exceptions.CreationFailedError, cfe:
usage()
sys.exit(2)
try:
sess = sqlalchemy.create_session()
sess.save(myclient)
sess.flush()
except sqlalchemy.exceptions.SQLError, e:
print "saving client failed: ", e
print cfe
sys.exit(2)
if verbose:
print myclient

View File

@ -18,15 +18,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import sqlalchemy
from gnuviechadmin import client, exceptions
from gnuviechadmin import client
def main():
session = sqlalchemy.create_session()
query = session.query(client.Client)
for row in query.select():
for row in client.fetchall():
print row
if __name__ == "__main__":

View File

@ -20,7 +20,8 @@
# Version: $Id$
from sqlalchemy import *
from gnuviechadmin import gva_cfg, exceptions
from gnuviechadmin import gva_cfg
from gnuviechadmin.exceptions import *
meta = BoundMetaData(gva_cfg.GVA_DBE)
client_table = Table('clients', meta,
@ -62,29 +63,50 @@ class Client(object):
if self.__getattribute__(key) is None:
missingfields.append(key)
if missingfields:
raise exceptions.MissingFieldsError(missingfields)
raise MissingFieldsError(missingfields)
# def __repr__(self):
# return """Client (Id %(clientid)d):
# Name: %(title)s %(firstname)s %(lastname)s
# Address: %(address1)s
# %(address2)s
# %(zip)s %(city)s (%(country)s)
# Phone: %(phone)s
# Mobile: %(mobile)s
# Fax: %(fax)s
# Email: %(email)s""" % ({'clientid' : self.clientid,
# 'title' : self.title,
# 'firstname' : self.firstname,
# 'lastname' : self.lastname,
# 'address1' : self.address1,
# 'address2' : self.address2,
# 'zip' : self.zip,
# 'city' : self.city,
# 'country' : self.country,
# 'phone' : self.phone,
# 'mobile' : self.mobile,
# 'fax' : self.fax,
# 'email' : self.email})
def __repr__(self):
return """Client (Id %(clientid)d):
Name: %(title)s %(firstname)s %(lastname)s
Address: %(address1)s
%(address2)s
%(zip)s %(city)s (%(country)s)
Phone: %(phone)s
Mobile: %(mobile)s
Fax: %(fax)s
Email: %(email)s""" % ({'clientid' : self.clientid,
'title' : self.title,
'firstname' : self.firstname,
'lastname' : self.lastname,
'address1' : self.address1,
'address2' : self.address2,
'zip' : self.zip,
'city' : self.city,
'country' : self.country,
'phone' : self.phone,
'mobile' : self.mobile,
'fax' : self.fax,
'email' : self.email})
return "%(clientid)d,%(firstname)s,%(lastname)s,%(email)s" % (
{'clientid' : self.clientid, 'firstname' : self.firstname,
'lastname' : self.lastname, 'email' : self.email})
clientmapper = mapper(Client, client_table)
def create(**kwargs):
try:
myclient = Client(**kwargs)
sess = create_session()
sess.save(myclient)
sess.flush()
except MissingFieldsError, mfe:
raise CreationFailedError(Client.__name__, mfe)
except exceptions.SQLError, sqle:
raise CreationFailedError(Client.__name__, sqle)
return myclient
def fetchall():
session = create_session()
query = session.query(Client)
return query.select()

View File

@ -28,3 +28,16 @@ class is missing."""
def __str__(self):
return "the fields %s are missing." % (repr(self.missing))
class CreationFailedError(Exception):
"""This exception should be raised if a business object could not
be created."""
def __init__(self, classname, cause = None):
self.classname = classname
self.cause = cause
def __str__(self):
msg = "Creating an instance of class %s failed." % (self.classname)
if self.cause:
msg += " The reason is %s." % (str(self.cause))
return msg