0

I have a problem with an application developed in python using the django framework, it uses the FPDF library to export a file which is then used by the application to attach it to an automated email.

When this app exports the PDF and saves it to the media directory, the file doesn't inherit permissions from its parent directory and only has read/write permissions, this doesn't allow Django to find the file so it can be attached to the mail.

I was searching on the internet and found people with the same problem, they were recommended to use the ACL configuration to manage default permissions, I tried many times with different methods but it did not work. I don't know what I could have done wrong (I kept having the same error). Dfter having made the ACL configuration the files continued to be exported with the same permissions and when applying the command chmod 777 -R * these did not change their permissions, I had to disable the ACL configuration for it to allow me to apply that command.

This is the error that appears:

Internal Server Error: /treasury/sendMailsSupplierView/SBOJOZF
Traceback (most recent call last):
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/views.py", line 33, in sendMailsSupplierView
    sendEmailSupplier('narvaezsebas8@gmail.com', report) #sendEmailSupplier(emailSupplier, report)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/mails/mailsFunctions.py", line 50, in sendEmailSupplier
    email_traslado.attach_file(report)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/mail/message.py", line 330, in attach_file
    with path.open('rb') as file:
  File "/usr/lib/python3.6/pathlib.py", line 1183, in open
    opener=self._opener)
  File "/usr/lib/python3.6/pathlib.py", line 1037, in _opener
    return self._accessor.open(self, flags, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: 'media/PaySuppiler--27022022142925.pdf'

This is what the latest unapplied files look like sudo chmod 777 *:

administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$ ls -l
total 396
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:17 PaySuppiler--27022022141734.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:28 PaySuppiler--27022022142833.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:29 PaySuppiler--27022022142925.pdf
administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$

These are the permissions of the media directory:

drwxrwxrwx  2 administrador administrador  16384 feb 27 09:29 media

1 Answers1

0

Can you check if the path which is being accessed is correct. As per the permissions of the files inside media folder, every user has read permission, and you are trying to read a file (in binary form).

This error (FileNotFoundError: [Errno 2] No such file or directory: 'media/PaySuppiler--27022022142925.pdf') shows that path is not correct. Try using relative path based on the file e.g os.path.join(os.path.realpath(file), "../media/PaySuppiler--27022022142925.pdf")

Why am I getting a FileNotFoundError?

ruakn
  • 188
  • 1
  • 9
  • It is the route that I use, and the route is correct, this configuration works for me locally but in production it fails due to the file permissions since if I access the route after changing the permissions it allows it to run, but since it is an automatic process I need the permissions to be granted at the time of exporting the file. – Sebastian Narvaez Feb 27 '22 at 15:33
  • okay, try using os.chmod() for changing the permissions and then check. But I would still ask you to print the current directory to see the issue with this – ruakn Feb 27 '22 at 17:42