I want to delete the file created as a result of an upload field.

87 views
Skip to first unread message

jim kaubisch

unread,
Oct 8, 2019, 2:30:59 PM10/8/19
to web2py-users

In several forms I have fields like to one below

Field('the_file'    , 'upload' , label=T('Audio File *'), 
                                         default = False,
                                         uploadfolder=os.path.join(request.folder,'uploads/resources/audio')
                                         ),


When I delete the db record containing the result of processing the field above, I also want to delete the actual uploaded file.

If I "print"  db.audios.the_file I get a sensible file name, e.g. "audios.the_file.87f39e4b2eac56a2.7331343035323830332e706466.mp3"
but -  if I pass db.audios.the_file to os.path.join, I get an exception - "EXCEPTION = join() argument must be str or bytes, not 'Field'"
and  - if I pass str(db.audios.the_file) to os.path.join I end up with something ending in 'uploads/resources/audio/the_file'

Simple question. How do I get the name of the uploaded file in a form I can use in os.path.join

Thanks

jim kaubisch

unread,
Oct 8, 2019, 5:44:38 PM10/8/19
to web2py-users
Just stumbled on the "autodelete" attribute in field definitions - which serves my needs at the moment, but would still be useful to know how to get at the filename for cases when you don't want auto delete

jim kaubisch

unread,
Oct 9, 2019, 11:30:39 AM10/9/19
to web2py-users


On Tuesday, October 8, 2019 at 11:30:59 AM UTC-7, jim kaubisch wrote:
turns out, I don't think auto delete works for me.

I'm using "record_versioning", hence record delete doesn't actually delete the record and, as far as I can tell, there is no "I really mean delete not archive" option.
As far as I can tell, my only option (which I'm using) is to go directly to the db and delete the record there (DELETE xxx FROM yyy WHERE id=zzz). 
That indeed deletes the record and cascades, but bypasses the web2py "auto delete". After the deletion the uploaded files remain:-(

Seems that without being able to get to the filename of the uploaded file there's no way to delete it, and if you delete the db record you've lost the only reference to the file so its now an orphan

villas

unread,
Oct 9, 2019, 12:06:10 PM10/9/19
to web2py-users
I must confess that I had a sudden feeling of panic on seeing:  DELETE xxx FROM yyy ...
My experience is there is little or no benefit in deleting records.  Record versioning and archiving is the way to go.
That is_active field may have a purpose after all! 
And you can always write a function to cull associated images if you are seriously short on space.
All the best.

jim kaubisch

unread,
Oct 9, 2019, 8:06:13 PM10/9/19
to web2py-users

I agree in principle, villas - that's why I turned on record versioning in the first place. But sometimes, occasionally, there is a genuine need to actually delete something to the point that ALL traces are REMOVED
   -  a confidential item that got added by mistake, an item for which you no longer have a right-to-use license, and MUST NOT be in your database, 
   - the accumulation over time of records that are obsolete, will never be accessed again, but are so numerous that they obscure the useful current info, 
   - etc.

A function to cull? Yes, I guess that's what I'm trying to do. 
But I need the name that web2py created for the uploaded  file - which is what I am trying to figure out how to retrieve, and is the topic of my original question

Dave S

unread,
Oct 9, 2019, 8:43:38 PM10/9/19
to web2py-users
I don't understand the problem. Your table should have the server-side file name in the upload field (the_file), and I think you've seen that. Try doing a select() to get the value. That of course returns a row, so use row("the_file") with square brackets.

That might save your short cut version, too.

I make use of this quite a bit, because I chew on file contents to make reports.

Dave S
/dps

Message has been deleted

jim kaubisch

unread,
Oct 18, 2019, 9:59:47 AM10/18/19
to web2py-users
Thanks, Dave. Your suggestion made the answer click for me. Embarrassingly, for some reason I got it in my head that both the upload directory and the upload file name were located in the db record for a given item :-(  Once it clicked that the directory comes from the schema and only the filename comes from the db record it all worked of course.


On Tuesday, October 8, 2019 at 11:30:59 AM UTC-7, jim kaubisch wrote:
Reply all
Reply to author
Forward
0 new messages