From 6b1c80899d358c07ff7d3d8a5a3b1b75d3251e54 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sun, 11 Feb 2007 20:09:07 +0000 Subject: [PATCH] - 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 --- bin/createclient | 16 +++------ bin/listclients | 9 ++--- gnuviechadmin/client.py | 68 ++++++++++++++++++++++++------------- gnuviechadmin/exceptions.py | 13 +++++++ 4 files changed, 64 insertions(+), 42 deletions(-) diff --git a/bin/createclient b/bin/createclient index d6811b3..807e40b 100755 --- a/bin/createclient +++ b/bin/createclient @@ -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 diff --git a/bin/listclients b/bin/listclients index 0a8683a..c9c2998 100755 --- a/bin/listclients +++ b/bin/listclients @@ -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__": diff --git a/gnuviechadmin/client.py b/gnuviechadmin/client.py index 06c0ced..153a14b 100644 --- a/gnuviechadmin/client.py +++ b/gnuviechadmin/client.py @@ -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() diff --git a/gnuviechadmin/exceptions.py b/gnuviechadmin/exceptions.py index 961bade..34e2002 100644 --- a/gnuviechadmin/exceptions.py +++ b/gnuviechadmin/exceptions.py @@ -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