# -*- coding: UTF-8 -*- # # Copyright (C) 2007 by Jan Dittberner. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. # # Version: $Id$ from sqlalchemy import * from gnuviechadmin import gva_cfg from gnuviechadmin.exceptions import * meta = BoundMetaData(gva_cfg.GVA_DBE) client_table = Table('clients', meta, Column('clientid', Integer, primary_key=True), Column('title', String(10)), Column('firstname', String(64), nullable=False), Column('lastname', String(64), nullable=False), Column('address1', String(64), nullable=False), Column('address2', String(64)), Column('zip', String(7), nullable=False), Column('city', String(64), nullable=False), Column('country', String(5), nullable=False), Column('phone', String(32), nullable=False), Column('mobile', String(32)), Column('fax', String(32)), Column('email', String(64), unique=True, nullable=False)) client_table.create(checkfirst=True) class Client(object): mandatory = ('firstname', 'lastname', 'address1', 'zip', 'city', 'country', 'phone', 'email') """This class provides a client representation""" def __init__(self, **kwargs): self.clientid = None self.country = 'de' self.title = None self.address2 = None self.mobile = None self.fax = None self.organisation = None for key in kwargs.keys(): self.__setattr__(key, kwargs[key]) self.validate() def validate(self): missingfields = [] for key in self.mandatory: if self.__getattribute__(key) is None: missingfields.append(key) if 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 "%(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()