some more details:
the problem occurs from the second time I use the controller
functionupload_anagrafica
thanks a lot
Manuele
# <model> #############################################################
db.define_table('site',
Field('name', length=16, required=True, unique=True,
label=T('Nome')), # <- si potrebbe usare una formula che toglie gli spazi
Field('longitude', 'decimal(6,3)',
requires=IS_DECIMAL_IN_RANGE(-180, 180, error_message=T('Not a
valid integeritude value inserted!')),
label=T('Longitudine'), comment='°'),
Field('latitude', 'decimal(6,3)',
requires=IS_DECIMAL_IN_RANGE(-90, 90, error_message=T('Not a
valid Latitude value inserted!')),
label=T('Latitudine'), comment='°'),
Field('height', 'integer',
requires = IS_INT_IN_RANGE(0, 8900, error_message=T('Height
value out of reasonable range [0-8900]!')),
label=T('Quota'), comment='m s.l.m.'),
Field('code', length=15,required=True, unique=True, requires =
IS_UPPER(),
label=T('Codice'), comment=T('Codice univoco di identificazione
sito')),
Field('full_name', length=64, label=T('Nome completo')),
# Field('start_validity', 'datetime', label=T('Aggiornamento'),
comment=T('Data di aggiornamento delle informazioni')),
link_to_company,
auth.signature.created_on,
auth.signature.modified_on,
auth.signature.created_by,
auth.signature.modified_by,
format = '%(full_name)s',
migrate=settings.migrate
)
# <module: meteo.py> ##############################################
class IntegrityError: pass
class superPower(object):
def __init__(self, table, *uniques):
self.table_name = table._tablename
self._table = table
self._db = table._db
self.uniques = uniques
class superPowerSite(superPower):
ok_tabs = ('site', )
def __init__(self, table, unique):
superPower.__init__(self, table, unique)
self.unique = unique
if self.table_name not in self.ok_tabs: raise IOError
if not unique in [field for field in self._table.fields if
self._table[field].unique]: raise IOError
def import_anagrafica(self, path_to_file, owner_company):
f = open(path_to_file, 'r')
header = f.readline()
for line in f:
infos = dict(zip(self._table.fields[1:7], \
[x.strip() for x in line.split(';')[1:]]))
if len(infos) == 6:
infos[self._table.fields[7]] = owner_company
try:
self._table.insert(**infos)
except: #IntegrityError:
self._db(self._table[self.unique]==infos[self.unique]).update(**infos)
# <controller: setup.py> ##############################################
from pytz import timezone
#@auth.requires_membership('admin')
def upload_anagrafica():
uploadPath = os.path.join(request.folder,'uploads/anagrafica')
if not os.path.exists(uploadPath):
os.makedirs(uploadPath)
form = SQLFORM.factory(
Field('anagrafica', 'upload', uploadfolder=uploadPath,
label='File', comment=T('Carica file di anagrafica')),
# Field('start_validity', 'date', default=dt.date.today(),
requires=IS_DATE(format=T('%d/%m/%Y')),
# label=T('Aggiornamento'), comment=T('specificare la
data di inizio validità del file di anagrafica che si sta caricando.')),
link_to_company
)
if form.accepts(request.vars):
response.flash = T('Form compilato correttamente.')
# start_validity =
timezone('Europe/Rome').localize(dt.datetime.combine(form.vars.start_validity,
dt.time(0, 0))).astimezone(timezone('UTC'))
# path_to_file = uploadPath + form.vars.anagrafica
path_to_file = os.path.join(uploadPath, form.vars.anagrafica)
# anagrafica().Import(path_to_file, form.vars.owner_company)
print path_to_file, form.vars.owner_company
site = superPowerSite(db.site, 'code')
site.import_anagrafica(path_to_file, form.vars.owner_company)
return dict(form=form)