Tempfile not found when uploading images with the Add function in a Grid

23 views
Skip to first unread message

Paul Ellis

unread,
Nov 1, 2019, 8:13:12 AM11/1/19
to web2py-users
I am getting this error when users upload an image with the Add function of a grid. As background, it is adding a product which can only be seen by users in a particular group (belong to the same company).

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/tmpGdGmPE'


Error snapshot

    <type 'exceptions.OSError'>([Errno 2] No such file or directory: '/tmp/tmpGdGmPE')
 


I have been banging my head against this one for a while and I am not getting anywhere.

I have another function which uses a simple form to add products which can be seen by everyone (admin form) and this has no issues with images.

Can anyone give me a tip or an idea or a direction to continiue searching for a solution?

I am happy to post code but the grid is reasonably complex at 450 lines.


Paul Ellis

unread,
Nov 4, 2019, 5:45:27 AM11/4/19
to web...@googlegroups.com
Actually I was looking at the wrong controller function. I have a utility function which returns part of the UI. Somehow when the form is submitted and this is run the tempfile for the image is closed. So by doing this at the end, after the form processing is complete, the problem is solved.

@auth.requires_login()
def add_custom():

   
#check if there is an offer selected
   
# if not session.quote or not session.quote.number:
       
# session.flash = T('Please select an offer first')
       
# redirect(URL('default', 'offer_landing'))

   
try:
        offerId
= request.args[0]
   
except IndexError:
        offerId
= 0
    offerDetails
= offer.authorise(offerId)

    pagetitle
= H3(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
    status
= roo.status(offerDetails) # <- Moved this line from the top to down here
    response
.view = 'core.html'
   
return dict(
        pagetitle
= pagetitle,
        pagecontent
= form,
        buttongroup
= buttongroup,
        status
= status,
       
)



Reply all
Reply to author
Forward
0 new messages