Jan Dittberner
bd306389f0
- new sysuser class git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@220 a67ec6bc-e5d5-0310-a910-815c51eb3124
98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
# -*- 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.tables import *
|
|
from gnuviechadmin import sysuser
|
|
from gnuviechadmin.exceptions import *
|
|
|
|
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,
|
|
properties = {'sysusers': relation(sysuser.Sysuser)})
|
|
|
|
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()
|