Calling transform() with no SRID set is not supported - python

On production server I receive the attached error, not emerging on the development server. Both environments are identical (same Django 1.6 and Python 2.7 versions, using virtualenvs same RDBMS version - a postgresql 9.1 server, running locally with a reasonably similar configuration than on production).
The offending code, I suppose, is in views.py:
from djgeojson.views import GeoJSONLayerView
class FilteredMapLayer(GeoJSONLayerView):
def get_queryset(self):
qs = super(FilteredMapLayer, self).get_queryset()
bbox_data = self.request.GET.get('bbox', None)
if bbox_data is not None:
bbox_data = ( x for x in map(float, bbox_data.split(',')))
bbox = Polygon.from_bbox(bbox_data)
features = qs.filter(geom__bboverlaps=bbox)
else:
features = qs
return features
since this is the urlpattern definition that, when requested, triggers the error
url(r'^data.geojson$', FilteredMapLayer.as_view(model=MyModelName, precision = 2, simplify = 0.5), name='myurl'),
here is the complete traceback, that does not provide me any info on where I did wrong....
Traceback (most recent call last):
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/utils/decorators.py", line 29, in _wrapper
return bound_func(*args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in bound_func
return func(self, *args2, **kwargs2)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/views.py", line 51, in dispatch
return super(GeoJSONLayerView, self).dispatch(*args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/views/generic/list.py", line 153, in get
return self.render_to_response(context)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/views.py", line 41, in render_to_response
**options)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 335, in serialize
self.serialize_queryset(queryset)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 277, in serialize_queryset
self.handle_field(obj, self.geometry_field)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 177, in handle_field
geometry = self._handle_geom(GEOSGeometry(value))
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 159, in _handle_geom
geometry.transform(self.srid)
File "/home/m2user/webapps/enea/local/lib/python2.7/site-packages/django/contrib/gis/geos/geometry.py", line 510, in transform
raise GEOSException("Calling transform() with no SRID set is not supported")
.... since if I check the features stored in the model, they all have a SRID defined (4326) and I'm not trying to call any transformation - I suppose - since I'm rendering the view using the same SRID. And, I repeat myself here, the same request on dev server works seamlessly.
What I've tried so far:
recreated the DB from scratch, reloaded all the data. Using Django admin I can see/edit the features stored
checked all software dependences and updated the code from trunk of https://github.com/makinacorpus/django-geojson
checked model definitions: all have geom = models.MultiPolygonField(srid=4326) fields
searched Google and SO for similar issues without any luck
I have no more ideas. Any suggestion is welcome!

I was able to solve my issue by uninstalling and then re-installing all the libraries (as ubuntu packages), the python interpreter, all the code, by re-generating the database (including reinstalling postgis) and then by completely restart nginx (and thus gunicorn). No libraries were updated, however, so I still do not understand what really happened.

Related

Access cleaned data from django admin for pdf processing

As a newbie, I am currently trying to add a print button to every existing django admin view of a project.
The goal is to make it not depend on a single model I could resolve manually for printing but every existing model which contains foreign keys to other models an so on.
For simplicity I thought it would be the best to just get the cleaned_data of the form and process it for the pdf.
I alread added the print button, the path url and so on and it will create a pdf file for me.
What I am not able to do is to access the forms cleaned_data from my BaseAdmins (extends the ModelAdmin) class like this:
form = BaseForm(request.POST)
if form.is_valid():
data = form.cleaned_data
It will just give me random kinds of errors, like object has no attribute 'is_bound'
So I think that I am generally wrong with the context where I am trying to get the cleaned_data from the form. Every tutorial I found is just showing how to get the data but not fully resolved if it contains foreign keys and not in which context.
Could you please clear up for me where it would make sense to pass any kind of form data maybe as session data or post body to a print view where I can process it.
Thank you very much for reading, hope I was able to describe my problem, feel free to ask.
Edit
This is the BaseForm I changed variable names for internal reasons:
class BaseForm(ModelForm):
def clean_custom(self):
another_model = self.cleaned_data.get('AnotherModel')
custom_models = self.cleaned_data.get('CustomModel')
custom_models_allowed = CustomModel.objects.filter(AnotherModel=another_model)
custom_models_was_list = True
if not custom_models:
return
if not isinstance(custom_models, Iterable):
custom_models_was_list = False
custom_models = [custom_models]
for custom_model in custom_models:
if another_model and custom_model not in custom_models_allowed:
custom_models_allowed = [custom_model.titel for custom_model in custom_models_allowed]
custom_models_allowed = ', '.join(custom_models_allowed)
raise ValidationError(
f'{custom_model} is not part of {another_model}. For selection: {custom_models_allowed}'
)
if custom_models_was_list:
return custom_models
else:
return custom_models[0]
I'm trying to add cleaned_data in my BaseAdmin class where I have access to request and get the following trace:
AttributeError
AttributeError: type object 'CustomForm' has no attribute 'is_bound'
Traceback (most recent call last)
File ".../.env/lib/python3.9/site-packages/django/contrib/staticfiles/handlers.py", line 65, in __call__
return self.application(environ, start_response)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/wsgi.py", line 141, in __call__
response = self.get_response(request)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 75, in get_response
response = self._middleware_chain(request)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 36, in inner
response = response_for_exception(request, exc)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File ".../.env/lib/python3.9/site-packages/django_extensions/management/technical_response.py", line 40, in null_technical_500_response
raise exc_value.with_traceback(tb)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File ".../.env/lib/python3.9/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File ".../.env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 606, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File ".../.env/lib/python3.9/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File ".../.env/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File ".../.env/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File ".../admin/base_admin.py", line 203, in change_view
if self.form.is_valid(self.form):
File ".../.env/lib/python3.9/site-packages/django/forms/forms.py", line 185, in is_valid
return self.is_bound and not self.errors
AttributeError: type object 'CustomForm' has no attribute 'is_bound'
This is not a standard question format, you are supposed to give us the code for the piece of code where the error occurs, (i.e. the base_admin.py file) but what you have provided is just 3 lines of it.
But so far I can see you are not checking whether the request is of the type of post or not. Pay attention to the if condition at the beginning of the method
def edit(request):
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = PurposeForm(request.POST)
# check whether it's valid:
if form.is_valid():
for key, value in form.cleaned_data.items():
# etc

Models and ModelForms: needs to have a value for field "id" before this many-to-many relationship can be used

I'm using a Django Model with some many-to-many fields. I'm also using a ModelForm to generate the associated form. It is my understanding that, provided nothing else is overridden, Django should be able to handle many-to-many fields being saved in the ModelForm?
For me, attempting to do this is causing this error:
Internal Server Error: /cameramodel/create/
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/views/generic/edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/views/generic/edit.py", line 141, in post
if form.is_valid():
File "/usr/local/lib/python3.8/site-packages/django/forms/forms.py", line 185, in is_valid
return self.is_bound and not self.errors
File "/usr/local/lib/python3.8/site-packages/django/forms/forms.py", line 180, in errors
self.full_clean()
File "/usr/local/lib/python3.8/site-packages/django/forms/forms.py", line 383, in full_clean
self._post_clean()
File "/usr/local/lib/python3.8/site-packages/django/forms/models.py", line 403, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 1188, in full_clean
self.clean()
File "./schema/models.py", line 1439, in clean
if self.metering_modes is True:
File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 527, in __get__
return self.related_manager_cls(instance)
File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 838, in __init__
raise ValueError('"%r" needs to have a value for field "%s" before '
ValueError: "<CameraModel: Canon Canonflex R2000>" needs to have a value for field "id" before this many-to-many relationship can be used.
So I checked out the docs and it seems like I need to override the save method in the ModelForm. So I did this:
class CameraModelForm(ModelForm):
class Meta:
model = CameraModel
fields = '__all__'
def save(self, commit=True):
form = super(CameraModelForm, self).save(commit=False)
if commit:
form.save(commit=False)
form.save_m2m()
return form
and instead I'm getting a different error:
TypeError at /cameramodel/create/
save() got an unexpected keyword argument 'commit'
Request Method: POST
Request URL: http://localhost:8000/cameramodel/create/
Django Version: 2.2.12
Exception Type: TypeError
Exception Value:
save() got an unexpected keyword argument 'commit'
Exception Location: /home/jonathan/git/camerahub/schema/models.py in save, line 1397
and models.py:1397 is actually an overridden Model save to add a slug field:
class CameraModel(models.Model):
# other stuff here
def save(self, *args, **kwargs):
if not self.slug:
custom_slugify_unique = UniqueSlugify(
unique_check=cameramodel_check, to_lower=True)
self.slug = custom_slugify_unique("{} {} {}".format(
self.manufacturer.name, self.model, str(self.disambiguation or '')))
super().save(*args, **kwargs)
For what it's worth, I am using many-to-many fields in some other models and I'm not seeing a problem with them. I'm a Python/Django beginner though and I'm pretty stuck on this one because simply following the advice on a million other posts like this one hasn't helped me. Grateful for any advice or code snippets anyone can offer.
If anyone needs more context, the whole project is open source and this branch is available here: https://github.com/djjudas21/camerahub/tree/278c_m2m_error
If you override, the save method, it means you want to make some checks/changes before saving your form. I would instead do:
def save(self):
form = super(CameraModelForm, self).save(commit=False)
if ...: # some condition on form values
form.save() # remove the commit here
form.save_m2m()
return form

unhashable type when redirecting back to the website using python-social-auth in Django

I'm trying to add a social media authentication to a website using Social-auth-app-django.
So I've created different apps for the most popular social media websites (Facebook, Twitter, Google+), and have set the callback url there.
But I'm coming across an error when I'm redirected back to the website from say Facebook:
Internal Server Error: /oauth/complete/facebook/
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_django/utils.py", line 50, in wrapper
return func(request, backend, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_django/views.py", line 32, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/actions.py", line 41, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/base.py", line 40, in complete
return self.auth_complete(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/utils.py", line 252, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/facebook.py", line 110, in auth_complete
return self.do_auth(access_token, response, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/facebook.py", line 152, in do_auth
return self.strategy.authenticate(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_django/strategy.py", line 115, in authenticate
return authenticate(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
user = backend.authenticate(**credentials)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/base.py", line 80, in authenticate
return self.pipeline(pipeline, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/base.py", line 83, in pipeline
out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/social_core/backends/base.py", line 105, in run_pipeline
for idx, name in enumerate(pipeline[pipeline_index:]):
TypeError: unhashable type: 'slice'
Below is a summary of how I've configured social_django:
In settings.py:
INSTALLED_APPS = [
'social_django',
...
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_FACEBOOK_KEY = 'xxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxx'
...
PIPELINE = {
'PIPELINE_ENABLED': True,
'STYLESHEETS': {...},
'JAVASCRIPT': {...},
'JS_COMPRESSOR': 'pipeline.compressors.NoopCompressor',
'COMPILERS': (
'pipeline.compilers.sass.SASSCompiler',
)
}
Afterwards, I've obviously migrated the database to create the new tables.
Please find below the versions of Django and social_django:
Django: 1.10.5
social_django: 1.2.0
Regarding the pipeline used, I'm using django-pipeline but it's just for compiling SASS files to CSS.
What might cause this error?
Adding the pipeline below to settings.py seems to have fixed the problem (source):
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
'social_core.pipeline.social_auth.associate_by_email',
)
This error raises when trying to get a slice from a dict object. So, yes at the point of the last line of the traceback, pipelines is a dict object when it is supposed to be a sequence which default value is sociel_core.pipeline.DEFAULT_AUTH_PIPELINE unless your settings provides a PIPELINE object.
https://github.com/python-social-auth/social-core/blob/ccc50a5932b199a1a5209a08563c8997eb99391d/social_core/strategy.py#L99
https://github.com/python-social-auth/social-core/blob/ccc50a5932b199a1a5209a08563c8997eb99391d/social_core/pipeline/init.py#L1
Thus I suspect something probably in your settings module that messes this PIPELINE that should be a sequence (list, tuple, custom) and not a dict.
Hints: install ipython and play with python manage.py shell and inspect the followings.
>>> from social_core.strategy import BaseStrategy
>>> st = BaseStrategy()
>>> st.get_pipeline()
---> ???
>>> from django.conf import settings
>>> settings.PIPELINE
---> ???
Hope this helped

Keyerror u'_source' in django haystack with Elasticsearch backend

I am getting a Keyerror whenever I specify .values() with SearchQuerySet in django Haystack. For e.g.
required_output = SearchQuerySet()[:10]
works fine. But whenever, I do the following, its throwing KeyError everytime.
required_output = SearchQuerySet().values('title', 'slug')[:10]
Here's the Trackback of the error:
Traceback:
File "/home/project_env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "/home/project_env/myproject/source/apps/Project/views.py" in get
151. context = self.get_context_data(object=self.object)
File "/home/project_env/myproject/source/apps/Project/views.py" in get_context_data
108. context['related_project'] = SearchQuerySet().values('title')[:10]
File "/home/project_env/local/lib/python2.7/site-packages/haystack/query.py" in __getitem__
266. self._fill_cache(start, bound)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/query.py" in _fill_cache
656. return super(ValuesListSearchQuerySet, self)._fill_cache(start, end, **kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/query.py" in _fill_cache
166. results = self.query.get_results(**kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/backends/__init__.py" in get_results
645. self.run(**kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py" in run
918. results = self.backend.search(final_query, **search_kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/backends/__init__.py" in wrapper
35. return func(obj, query_string, *args, **kwargs)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py" in search
504. distance_point=kwargs.get('distance_point'), geo_sort=geo_sort)
File "/home/project_env/local/lib/python2.7/site-packages/haystack/backends/elasticsearch_backend.py" in _process_results
580. source = raw_result['_source']
What version of django-haystack are you using? It looks like there is an open issue for this on the django-haystack GitHub. There is also a patch that should fix it. Looks like you'll have to either wait for the merge, or patch it locally.
Issue #1019
Pull Request #1020

Why do I get "AttributeError: 'unicode' object has no attribute 'user' " on some specify url only?

I'm using the #login_required decorator in my project since day one and it's working fine, but for some reason, I'm starting to get "
AttributeError: 'unicode' object has no attribute 'user' " on some specific urls (and those worked in the past).
Example : I am the website, logged, and then I click on link and I'm getting this error that usually is linked to the fact that there is no SessionMiddleware installed. But in my case, there is one since I am logged on the site and the page I am on also had a #login_required.
Any idea?
The url is definied as : (r'^accept/(?P<token>[a-zA-Z0-9_-]+)?$', 'accept'),
and the method as : #login_required
def accept(request,token): ...
The Traceback:
Traceback (most recent call last):
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 674, in __call__
return self.application(environ, start_response)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 141, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 165, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/macbook/dev/pycharm-projects/proj/match/views.py", line 33, in accept
return __process(token,callback)
File "/Users/macbook/virtualenv/proj/lib/python2.6/site-packages/django/contrib/auth/decorators.py", line 24, in _wrapped_view
if test_func(request.user):
AttributeError: 'unicode' object has no attribute 'user'`
The decorator was on a private method that doesn't have the request as a parameter. I removed that decorator (left there because of a refactoring and lack of test [bad me]).
Problem solved.
This can also happen if you call a decorated method from another method without providing a request parameter.

Categories

Resources