I have 2 similar controller functions using SQLFORM() with an upload field.
One works and one is giving an error online (not when testing locally). I
suspect a file system permission problem but I am not sure.
The error only occurs if an image is uploaded.
The extra processing after the form is submitted is not the cause. I
removed it as a test and the error remains.
The error:
Traceback
1.
2.
3.
4.
5.
6.
7.
Traceback (most recent call last):
File "/home/act/web2py/gluon/main.py", line 505, in wsgibase
request.body.close()
File "/usr/local/lib/python2.7/tempfile.py", line 431, in close
self.unlink(self.name)
OSError: [Errno 2] No such file or directory: '/tmp/tmp2PXnS0'
The Function:
@auth.requires_login()
def add_custom():
try:
offerId = request.args[0]
except IndexError:
offerId = 0
offerDetails = offer.authorise(offerId)
status = roo.status(offerDetails)
pagetitle = H4(T('Custom Product'))
buttongroup = [btn.back()]
try:
record = db.custom_product[request.args[1]] or None
except [IndexError, KeyError, TypeError]:
record = None
if session.custom_copy:
db.custom_product.productname.default = session.custom_copy.
productname
db.custom_product.description.default = session.custom_copy.
description
db.custom_product.purchasecost.default = session.custom_copy.
purchasecost
db.custom_product.monthlycost.default = session.custom_copy.
monthlycost
#db.custom_product.smallimage.default =
session.custom_copy.smallimage
session.custom_copy = None
db.custom_product.created_on.readable = False
db.custom_product.updated_on.readable = False
form = SQLFORM(db.custom_product, record, showid = False)
if form.process().accepted:
#do this for insert but not update
if not record:
# need to get the max priorites from the offer table
pp = mp = None
# offerDetails = db.offer[session.quote.number]
# increment whichever needs to be used and save it to the offer
table
if form.vars.purchasecost:
if offerDetails.offer.maxpurchasepriority:
offerDetails.offer.maxpurchasepriority += 1
else:
offerDetails.offer.maxpurchasepriority = 1
pp = offerDetails.offer.maxpurchasepriority
if form.vars.monthlycost or (not form.vars.purchasecost and not
form.vars.monthlycost):
if offerDetails.offer.maxmonthlypriority:
offerDetails.offer.maxmonthlypriority += 1
else:
offerDetails.offer.maxmonthlypriority = 1
mp = offerDetails.offer.maxmonthlypriority
offerDetails.offer.update_record()
# insert record into product_offer_item
db.product_offer_item.insert(
offer_id = offerDetails.offer.id,
custom_pid = form.vars.id,
quantity = 1,
purchasepriority = pp,
monthlypriority = mp,
)
else:
# only do this for updates
# check if a price change also needs a priority change
poiRecord = record.product_offer_item.select().first()
pp = copy.copy(poiRecord.purchasepriority)
mp = copy.copy(poiRecord.monthlypriority)
#poi_set = db(db.product_offer_item.custom_pid == poiRecord.id)
if pp and (not form.vars.purchasecost > 0):
poiRecord.update_record(purchasepriority = 0)
if not pp and (form.vars.purchasecost > 0):
poiRecord.update_record(purchasepriority = 99.5)
if mp and (form.vars.purchasecost > 0 and not form.vars.monthlycost
> 0):
poiRecord.update_record(monthlypriority = 0)
if not mp and\
((not form.vars.purchasecost > 0 and not
form.vars.monthlycost
> 0) or\
not form.vars.purchasecost > 0 and form.vars.monthlycost
> 0):
poiRecord.update_record(monthlypriority = 99.5)
offer.position_check(offerDetails.offer.id)
session.flash = T('Success')
redirect(btn.EditOffer(offerDetails.offer.id,
dict(custId = offerDetails.customer.id)).url)
elif form.errors:
response.flash = T('Errors in form')
else:
pass
response.view = 'core.html'
return dict(
pagetitle = pagetitle,
pagecontent = form,
buttongroup = buttongroup,
status = status,
)
The Table definition:
db.define_table(
'custom_product',
Field('productname', label=T('Product Name')),
Field('description', 'text', label=T('Description')),
Field('purchasecost', 'double', default = 0, label = T('Purchase Cost'
)),
Field('monthlycost', 'double', default = 0, label = T('Monthly Cost')),
Field('created_on', 'datetime', default=request.now, writable=False,
label=T('Created On')),
Field('updated_on', 'datetime', update=request.now, writable=False,
label=T('Updated On')),
Field('smallimage', 'upload', label=T('Image')),
)
This function does not have an issue:
@auth.requires_membership('oversight')
def add():
title = H3(T('Add Product'))
btns = [btn.back(session.baselocation)]
rVars = request.vars
cid = rVars.get('custId', None)
rArgs = request.args
status = roo.status_check(rArgs, rVars)
pid = rVars.get('pid', None)
if pid:
record = db.product[pid]
else:
record = None
form = SQLFORM(db.product,
record,
ignore_rw= True,
fields= [
'productname',
'group_id',
'description',
'purchasecost',
'monthlycost',
'product_code',
'smallimage',
'unavailable',
'reseller_id',
],
)
if form.process().accepted:
response.flash = T('Product Added')
# print form.vars
# gid = form.vars.group_id
# session.flash = T('Product Added')
# redirect(URL(
# 'product',
# 'catalogue',
# vars = dict(custId= cid),
# args = [rArgs[0], gid]))
response.view = 'core.html'
return dict(
pagetitle= title,
buttongroup= btns,
pagecontent= form,
status = status,
)
The Table definition:
db.define_table(
'product',
Field('productname', label=T('Product Name')),
Field('group_id', 'reference productgroup', label = T('Product Group')),
Field('description', 'text', label=T('Description')),
Field('purchasecost', 'double', default = 0.0, label = T('Purchase Cost'
)),
Field('monthlycost', 'double', default = 0.0, label = T('Monthly Cost'
)),
Field('product_code', label = T('Product Code')),
Field('smallimage', 'upload', label=T('Image')),
Field('assist_info', 'text', label=T('Assistant Product Information')),
Field('unavailable', 'boolean', default=False, label = T('Unavailable'
)),
Field('reseller_id', 'reference reseller', writable=False),
Field('created_on', 'datetime', default=request.now, writable=False,
label=T('Created On')),
Field('created_by', 'reference auth_user', writable=False, label=T('Created
By')),
Field('updated_on', 'datetime', update=request.now, writable=False,
label=T('Updated On')),
Field('updated_by', 'reference auth_user', writable=False, label=T('Updated
By')),
format = '%(productname)s'
)
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/277b8fcc-39e1-412b-9d0d-95cb486d81a3%40googlegroups.com.