Thanks Dave,
below is a version of the code as is, inches a variant of what you
suggested I think...
#
-------------------------------------------------------------------------------
@auth.requires(auth.has_membership(role='super'))
def del_op_audios():
try:
# Before we delete the db references, find the uploaded file
name/path
#
upload_path_name = db.audios.the_file.uploadfolder
#try to get the name of the uploaded file (not original file)
# - what does this need to be???
# print db.audios.the_file and you get a sensible filename, e.g.
"audios.the_file.87f39e4b2eac56a2.7331343035323830332e706466.mp3"
# but I don't seem to be able to get that name as a string to
pass to os.path.join????
upload_file_name = db.audios.the_file.??????? or ??????
print(f'upload_file = "{upload_file_name}"')
print(f'upload_path = "{upload_path_name}"')
# since we've got record_versioning turned on, we can't truly
delete the record
# with a "del db.audios[request.vars.record_id]"
# so go directly to db and delete the record
# - Relying on the SQL CASCADE being enabled
#
db.executesql(f'DELETE FROM audios WHERE
id={request.vars.record_id};')
# Commit the changes
db.commit()
# Now that the record is gone, delete the associated uploaded file
# something like this ...
#
os.remove(os.path.join(request.folder, upload_path_name,
upload_file_name))
session.op_result = 'Success - DELETE successful'
except Exception as why:
session.op_result = f'Failed - {why}'
redirect(URL('default', 'list_all_rows'))
What am I missing?
On Tuesday, October 8, 2019 at 11:30:59 AM UTC-7, jim kaubisch wrote:
>
>
> 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
>
--
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/8af25c61-5b88-4dd8-a2a5-dd02d6108c75%40googlegroups.com.