Hi,

I am very new in web2py (but I love it from the first met:) ), and
faced a problem
what I could solve with a workaround but I think it is not the right
way.

I have a custom form what I created with SQLFORM:
form=SQLFORM(db.assets,
 
fields=["assetno","subassetno","qty","costuser","name","serialno","activationdate","type_id"],
                col3={'qty':'No. of records will be
created!','serialno':'E.g. S/N of a product'})

I am using this in the table definiton for field activationdate:
Field("activationdate","date",label="Date of
activation",widget=SQLFORM.widgets.date.widget)

If the quantity is greater then 1, I must insert additional entries to
the db.

I made this in the controller in order to achieve this:
    if len(request.vars)>0:
        qty=int(request.vars.qty)
        if qty>1:
            request.vars.qty='1'
        if form.accepts(request.vars, session):
            response.flash = '%s records inserted' % qty
            if qty>1:
                for i in range(1,qty):
 
db.assets.insert(assetno=request.vars.assetno,subassetno=request.vars.subassetno,qty=1,costuser=request.vars.costuser,name=request.vars.name,serialno=request.vars.serialno,activationdate=request.vars.activationdate,type_id=int(request.vars.type_id))
                db.commit()
        elif form.errors:
            response.flash = 'Bad data!'

it fails at db.insert, because I have hungarian locale and the date
from the date widget comes in this format:
YYYY.mm.dd.
and not in this format:
YYY-mm-dd

I used this solution (converting the datestring :/ ):
db.assets.insert(assetno=request.vars.assetno,subassetno=request.vars.subassetno,qty=1,costuser=request.vars.costuser,name=request.vars.name,serialno=request.vars.serialno,activationdate=datetime.datetime.strptime(request.vars.activationdate,"%Y.
%m.%d.").date().isoformat(),type_id=int(request.vars.type_id))

My question is:
Is there any other more simple way to handle such problems?

PS: I've translated the variable names in the code to english to be
more understandable (most of them were hungarian), could be I've some
typo :)

Reply via email to