1
0
Fork 0
gnuviechadmin-historic/testdb/addclient.py

75 lines
2.4 KiB
Python

#!/usr/bin/env python
#
# Copyright (c) 2007 Jan Dittberner
# $Id$
#
import getopt, sys
from gnuviechadmin.dblayer import *
def usage():
print """Usage information:
=====================
%(process)s -h|--help
- prints this help text
%(process)s --firstname=<firstname> --lastname=<lastname> \
--address1=<address1> --town=<town> --zipcode=<zipcode> \
[--address2=<address2>] [--country=<country>] [--state=<state>] \
[--active=true|false] [--phone=<phone>] [--mobile=<mobile>]
- adds a new client
""" % {'process': sys.argv[0]}
if __name__ == "__main__":
try:
(options, args) = getopt.getopt(sys.argv[1:], "h",
['help',
'firstname=', 'lastname=', 'address1=',
'town=', 'zipcode=', 'address2=',
'country=', 'state=', 'active=',
'phone=', 'mobile='])
except getopt.GetoptError:
usage()
sys.exit(1)
if (not options or
dict(options).has_key('-h') or
dict(options).has_key('--help') or
not dict(options).has_key('--firstname') or
not dict(options).has_key('--lastname') or
not dict(options).has_key('--address1') or
not dict(options).has_key('--town') or
not dict(options).has_key('--zipcode') or
not dict(options)['--firstname'].strip() or
not dict(options)['--lastname'].strip() or
not dict(options)['--address1'].strip() or
not dict(options)['--town'].strip() or
not dict(options)['--zipcode'].strip()):
usage()
sys.exit(1)
po = dict(options)
for key in po.keys():
po[key] = po[key].strip()
client = Client()
client.firstname = po['--firstname']
client.lastname = po['--lastname']
client.address1 = po['--address1']
client.town = po['--town']
client.zipcode = po['--zipcode']
if po.has_key('--active'):
client.active = (po['--active'] == 'true')
else:
client.active = True
if po.has_key('--address2') and po['--address2']:
client.address2 = po['--address2']
if po.has_key('--country') and po['--country']:
client.country = po['--country']
if po.has_key('--state') and po['--state']:
client.state = po['--state']
if po.has_key('--phone') and po['--phone']:
client.phone = po['--phone']
if po.has_key('--mobile') and po['--mobile']:
client.mobile = po['--mobile']
session.save(client)
session.flush()