Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Pathlib based paths #49

Closed
alirezashamsaei opened this issue Nov 17, 2020 · 11 comments
Closed

Problem with Pathlib based paths #49

alirezashamsaei opened this issue Nov 17, 2020 · 11 comments
Milestone

Comments

@alirezashamsaei
Copy link

Hello and thanks a lot for this awesome app. it makes lives so much easier!
I recently started a new django project (Django 3.1) and as you know, django has moved from os.path to Pathlib. unfortunately, this breaks this app's functionality.
It throws an error in :
django_drf_filepond/views.py, line 107
where it contains the following:
if ((not (storage.location).startswith(local_settings.BASE_DIR)) and (local_settings.BASE_DIR != os.path.dirname(django_drf_filepond.__file__))):

The error reads TypeError: startswith first arg must be str or a tuple of str, not WindowsPath, and everything works when I wrap storage.location in an str function (obviously, because startswhth expects strings.)

anyways, please forgive any mistakes I made, I am a newbie both in django and around github.

@jcohen02
Copy link
Collaborator

Hi @alirezashamsaei, sorry for the delay in getting to this issue but thanks for raising it. Great to hear that you find the app useful too 🙂.

I've not seen any issues so far resulting from the switch to pathlib but it certainly looks like there's a problem here.

Can you confirm what OS version and Python version you're running with? I think this could be a Windows-specifc issue and I presume from the message that you're running on Windows?

The solution here is likely to be to update the default app settings in drf_filepond_settings.py to use pathlib. There's a discussion here about making similar updates in the core Django project template.

However, this will cause issues for people running with older versions of Python that don't support Django 3 so I'll need ensure that this is resolved while retaining support for previous Python/Django versions.

It's likely that this is going to take me a couple of weeks to get round to but I'll provide an update here as soon as possible.

@jcohen02 jcohen02 added this to the v0.3.1 milestone Feb 19, 2021
@nashgc
Copy link

nashgc commented Jun 22, 2021

I got the same trouble. I have Ubuntu 20 and Django 3.2.1. Please make a patch and fix the issue. =)

UPD: btw, I've solved it like that^ BASE_DIR = str(Path(__file__).resolve().parent.parent) - woks fine ;)

@jcohen02
Copy link
Collaborator

jcohen02 commented Jun 22, 2021

Hi @nashgc, I hadn't managed to reproduce this issue and needed to investigate further - I'm keen to get this fixed - are you able to offer any further information or a simple example that can reproduce the error? I was wondering if this could perhaps be something to do with the way that BASE_DIR is specified in the top-level application settings?

I'm currently using django-drf-filepond in an application based on Django 3.2.3 without this issue occurring.

Would it be possible for you to provide some simple demo code that reproduces the problem? I'll take another look

EDIT: Just seen your update - thanks for the info 🙂 - I'll see if I can add something that detects path objects that will cause an issue and converts them - how were you specifying BASE_DIR previously when the issue occurred?

@nashgc
Copy link

nashgc commented Jun 22, 2021

Hi @nashgc, I hadn't managed to reproduce this issue and needed to investigate further - I'm keen to get this fixed - are you able to offer any further information or a simple example that can reproduce the error? I was wondering if this could perhaps be something to do with the way that BASE_DIR is specified in the top-level application settings?

I'm currently using django-drf-filepond in an application based on Django 3.2.3 without this issue occurring.

Would it be possible for you to provide some simple demo code that reproduces the problem? I'll take another look

If I don't wrap PosixPath with str() method it gets me next traceback:

Internal Server Error: /fp/process/ Traceback (most recent call last): File "home/py/project/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "home/py/project/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "home/py/project/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "home/py/project/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "home/py/project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc) File "home/py/project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "home/py/project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc File "home/py/project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs) File "home/py/project/venv/lib/python3.8/site-packages/django_drf_filepond/views.py", line 107, in post if ((not (storage.location).startswith(local_settings.BASE_DIR)) and TypeError: startswith first arg must be str or a tuple of str, not PosixPath

If you need real simple example, I can establish new repo with this issue. Let me know if you need to.
btw, thanks for cool library! It save my time a lot!

@jcohen02
Copy link
Collaborator

Thanks for the details - this is helpful, I'll see if I can reproduce the problem and sort out a fix based on this info - don't worry about creating a new repo for this issue for now, I'll see how I get on and will update here if I need any further information.

Great to hear that you're finding the library useful 👍

@jcohen02
Copy link
Collaborator

I see the issue here now and can reproduce the problem. I'll get a fix in place for this and create a new maintenance release which will hopefully be on PyPi sometime tomorrow.

If wrapping BASE_DIR with str() works for your app, that's great, however, note that if there are other apps expecting to use settings.BASE_DIR and expecting that it's a Path object, this could cause issues. Once I've resolved this, you should be able to remove the str().

@jcohen02 jcohen02 reopened this Jun 24, 2021
@jcohen02
Copy link
Collaborator

@alirezashamsaei, @nashgc, many thanks for reporting this issue. A fix has now been implemented and merged into master.

The fix should address the differences across different Python versions in terms of whether or not pathlib is present and whether functions such as os.path.join are pathlib-aware. This means that the fix maintains support for Python 2.7 and 3.5 since I'm keen to support any legacy apps using this library for now.

Tests are passing locally but I'd like to ensure the CI is passing before releasing an updated version. There's currently an issue that I need to resolve in relation to the CI and also some dependency version updates to address so once these issues are resolved, I'll create a new release and deploy this.

@jcohen02
Copy link
Collaborator

Release v0.3.1 is now available on PyPi with the fix for this issue so I'm going to close this issue but do let me know if you experience any issues with the fix.

@Moiez789
Copy link

TypeError: Invalid path type: tuple

i am facing this problem can anyone help me please ?

@jcohen02
Copy link
Collaborator

The above comment has been moved to a new issue (#83) since this issue is closed.

@Deepu-Infy
Copy link

This works for me
BASE_DIR = str(Path(file).resolve().parent.parent) - woks fine ;)
In addition to this I did change in DATABASE as below
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': str('BASE_DIR' + 'db.sqlite3'),
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants