Django Celery IntegrityError - python

I want to create a progress bar for my project. I have a class and this class has some functions. Especially, one of them takes a long time (def download_all) and this is my main reason for wanting to create a progress bar.
I successfully set up celery, celery-progress, etc. and they work all fine. My problem is this: I want to integrate the progress bar to download_all function. I
It gives an error: IntegrityError at /o.../k...
NOT NULL constraint failed: django_celery_results_taskresult.task_id
How can I solve it?
functions.py
class myClass():
def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
...
self.download_all(n_user, n_password, n_url, n_port, db_password)
...
#shared_task(bind=True, name="my_add")
def download_all(n_user, n_password, n_url, n_port, db_password)
...
len_scans = len(scans)
progress_recorder = ProgressRecorder(self)
for s in scans:
i = 0
progress_recorder.set_progress(i + 1, len_scans)
i += 1
views.py
def setup_wizard(request):
...
functions.Zafiyet(setup.n_username, setup.n_password,
setup.n_url, setup.n_port, setup.db_password,
username=request.user.username)
traceback
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/operasyonmerkezi/konfigurasyon
Django Version: 3.2.7
Python Version: 3.9.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dashboard',
'accounts',
'logs',
'crispy_forms',
'django_apscheduler',
'easy_timezones',
'django_celery_results',
'celery_progress']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\query.py", line 581, in get_or_create
return self.get(**kwargs), False
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\query.py", line 435, in get
raise self.model.DoesNotExist(
During handling of the above exception (TaskResult matching query does not exist.), another exception occurred:
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
The above exception (NOT NULL constraint failed: django_celery_results_taskresult.task_id) was the direct cause of the following exception:
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\views.py", line 128, in setup_wizard
task = (functions.myClass(setup.n_username, setup.n_password,
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\functions.py", line 44, in __init__
self.download_all(n_user, n_password, n_url, n_port, db_password)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\local.py", line 188, in __call__
return self._get_current_object()(*a, **kw)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\app\task.py", line 389, in __call__
return self.run(*args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\dashboard\functions.py", line 153, in download_all
progress_recorder.set_progress(i + 1, len_scans)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery_progress\backend.py", line 46, in set_progress
self.task.update_state(
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\app\task.py", line 971, in update_state
self.backend.store_result(
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\celery\backends\base.py", line 482, in store_result
self._store_result(task_id, result, state, traceback,
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django_celery_results\backends\database.py", line 66, in _store_result
self.TaskModel._default_manager.store_result(
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django_celery_results\managers.py", line 46, in _inner
return fun(*args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django_celery_results\managers.py", line 168, in store_result
obj, created = self.using(using).get_or_create(task_id=task_id,
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\query.py", line 588, in get_or_create
return self.create(**params), True
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\edeni\Desktop\hawkdragon\myvenv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /operasyonmerkezi/konfigurasyon
Exception Value: NOT NULL constraint failed: django_celery_results_taskresult.task_id

If you want to check progress of task you have 3 options
store current progress as model field in db
store as key value in redis db
count at runtime by filtering and count element in db with changed status
Frontend can get this result by common REST API or websocket.
example minimum configs for REST API
model.py
class MyLongProcess(models.Model):
active_uuid = models.UUIDField('Active process', null=True, blank=True)
name = models.CharField('Name', max_length=255)
current_step = models.IntegerField('Current step', default=0)
total = models.IntegerField('Total', default=0)
#property
def percentage_sending(self):
# or it can be computed by filtering elements processed in celery with complete status
return int((current_step / total) * 100)
views.py
def initiate_execute_long_task(request, name):
process = MyLongProcess.objects.create(active_uuid=uuid.uuid4(), name=name, total=BIG_COUNTED_VALUE)
async_execute_long_task.delay(process)
return HttpResponse()
from app.celery import app
#app.task
def async_execute_long_task(process)
for i in range process.total:
do_some_staff()
process.current_step += 1
process.save()
detail.html
<div class="progress-bar" role="progressbar"
style="width: {{ my_model_object.percentage_sending }}%;"
aria-valuenow="{{ my_model_object.percentage_sending }}"
aria-valuemin="0" aria-valuemax="100">{{ my_model_object.percentage_sending }}%
</div>

do not use self.download_all(n_user, n_password, n_url, n_port, db_password)
but use it self.download_all.delay(n_user, n_password, n_url, n_port, db_password) to run function in celery task
you can use websocket and send data for progress to html and update progress bar with javascript
each time progress updated progress bar will move

First, you probably do not want to use the #shared_task decorator on a method like that.
Your method signature is kind of mangled because of this. The bind=True parameter will cause celery to pass the task object into the call, which may produce unexpected results based on your signature.
Demonstration of what I mean by this:
#shared_task(bind=True, name='mytask')
def my_task(the_task, a, b):
print(the_task) # a celery Task object
return a + b
# OR
#shared_task(name='mytask')
def my_task(a, b):
return a + b
Note that these are ordinary functions, not methods in a class.
Now, compound this with the fact that methods on classes implicitly receive self, the instance of the class, as the first argument. This is also missing from your signature. It's honestly surprising it executes at all as-written... I would have expected it to fail with a TypeError based on the code you provided.
Instead, you should factor out the task to its own function or add the #staticmethod decorator over the method first:
class Foo:
#shared_task(name='mytask')
#staticmethod
def bar(a, b):
...
Winding back your stacktrace, your error comes from an integrity error in the task result table:
Exception Value: NOT NULL constraint failed: django_celery_results_taskresult.task_id
This happens because your app is, for one reason or another, trying to re-use a task ID and trying to store the result that has already been executed and stored in the DB. This is potentially caused by the issues described above, a bad concurrency model in your app, or an out-of-band change to the database/queue that caused unexpected duplication. You need to either flush the offending task out of your message queue or remove the offending result from the database, but there may not be guarantees this won't happen again unless you fix the underlying problem with your application code.

This seems like came from Django model: "self.model.DoesNotExist".
It is not come from celery, from Model.objects.
You'd better check your ORMs.

Related

Django: Overriding Postgres Database wrapper to set search path throws error

I want to establish a connection with a postgres database in django. As it doesn't provide support for postgres schemas I am trying to set the search_path immediately after establishing a connection. To achieve this I have subclassed the DatabaseWrapper class and overridden the _cursor method as below:
from django.db.backends.postgresql.base import DatabaseWrapper
class DatabaseWrapper(DatabaseWrapper):
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
def _cursor(self, name=None):
cursor = super(DatabaseWrapper, self)._cursor(name)
cursor.execute('SET search_path = schema_name')
return cursor
Now the above code works fine for the application code that we have written but when I try access the detail screen of any object in the admin panel of django, I get the below error trace:
File "/Users/azharuddin.syed/Desktop/application/custom_db_engine/base.py", line 13, in \_cursor
cursor.execute('SET search_path = schema_name')
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self.\_execute_with_wrappers(sql, params, many=False, executor=self.\_execute)
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in \_execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in \_execute
return self.cursor.execute(sql, params)
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/azharuddin.syed/Desktop/application/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in \_execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: syntax error at or near "SET"
LINE 1: ...6171701248_sync_1" NO SCROLL CURSOR WITH HOLD FOR SET search...
If I understand correctly then two queries are getting mixed together and getting executed at the same time. Why is this the case?
P.S: I am aware that we can pass options parameter with the search path while defining the connections in django but this is not working in my case as the DB is behind a proxy. Any other solutions will be welcomed.

Django Test "database table is locked"

In my Django project, I have a view that when a user posts a zip file, it will respond immediately back and then process the data in the background with the help of threading. The view works fine in normal test, but when I run the Django's test it fails with a database table is locked error. Currently, I'm using default SQLite database and I know if I switch to another database this problem may be solved but I'm seeking an answer for the current setup. I trimmed the code for simplicity.
It seems that the problem is with writing in DeviceReportModel table. But I'm not sure why TestDeviceReport accessing it.
Model.py:
class DeviceReportModel(models.Model):
device_id = models.PositiveIntegerField(primary_key=True)
ip = models.GenericIPAddressField()
created_time = models.DateTimeField(default=timezone.now)
report_file = models.FileField(upload_to="DeviceReport")
device_datas = models.ManyToManyField(DeviceDataReportModel)
def __str__(self):
return str(self.id)
Serializers.py:
class DeviceReportSerializer(serializers.ModelSerializer):
class Meta:
model = DeviceReportModel
fields = '__all__'
read_only_fields = ('created_time', 'ip', 'device_datas')
views.py:
from django.utils import timezone
from django.core.files.base import ContentFile
from rest_framework.response import Response
from rest_framework import status, generics
import time
import threading
from queue import Queue
class DeviceReportHandler:
ReportQueue = Queue()
#staticmethod
def save_datas(device_object, request_ip, b64datas):
device_data_models = []
# ...
# process device_data_models
# this will take some time
time.sleep(10)
return device_data_models
#classmethod
def Check(cls):
while(True):
if not cls.ReportQueue.empty():
report = cls.ReportQueue.get()
# ...
report_model = DeviceReportModel(
device_id=report['device_object'], ip=report['request_ip'])
# THIS LINE GIVES ERROR
report_model.report_file.save(
"Report_{}.txt.gz".format(timezone.now()), ContentFile(report['report_data']))
device_data_models = cls.save_datas(
report['device_object'], report['request_ip'], 'SomeData')
report_model.device_datas.set(device_data_models)
report_model.save()
print("Report Handle Done")
time.sleep(.1)
#classmethod
def run(cls):
thr = threading.Thread(target=cls.Check)
thr.daemon = True
thr.start()
class DeviceReportView(generics.ListCreateAPIView):
queryset = DeviceReportModel.objects.all()
serializer_class = DeviceReportSerializer
DeviceReportHandler.run()
def post(self, request):
# ...
report = {
'device_object': 1,
'request_ip': '0.0.0.0',
'report_data': b'Some report plain data',
}
# add request to ReportQueue
DeviceReportHandler.ReportQueue.put(report)
return Response("OK", status.HTTP_201_CREATED)
tests.py:
from rest_framework.test import APITestCase
import gzip
from io import BytesIO
import base64
import time
class TestDeviceReport(APITestCase):
#classmethod
def setUpTestData(cls):
# add a new test device for other tests
pass
def generate_device_data(self):
# generate fake device data
return ""
def test_Report(self):
# generate device data
device_data = ''
for i in range(10):
device_data += self.generate_device_data() + '\n'
buf = BytesIO()
compressed = gzip.GzipFile(fileobj=buf, mode="wb")
compressed.write(device_data.encode())
compressed.close()
b64data = base64.b64encode(buf.getvalue()).decode()
data = {
"device_id": 1,
"report_data": b64data
}
response = self.client.post(
'/device/reports/', data=data, format='json')
print(response.status_code, response.content)
def tearDown(self):
# put some sleep to check whether the data has been processed
# see "Report Handle Done"
time.sleep(10)
And here is error log:
(myDjangoEnv) python manage.py test deviceApp.tests.tests.TestDeviceReport
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
201 b'"OK"'
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: database table is locked
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "<project_path>\deviceApp\views.py", line 303, in Check
"Report_{}.txt.gz".format(timezone.now()), ContentFile(report['report_data']))
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\files.py", line 93, in save
self.instance.save()
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\base.py", line 779, in save_base
force_update, using, update_fields,
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\base.py", line 908, in _do_insert
using=using, raw=raw)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\sql\compiler.py", line 1335, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Masoud\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: database table is locked
.
----------------------------------------------------------------------
Ran 1 test in 10.023s
OK
Destroying test database for alias 'default'...
Database is locked errors
SQLite is meant to be a lightweight database, and thus can’t support a high level of concurrency. OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.
Python’s SQLite wrapper has a default timeout value that determines how long the second thread is allowed to wait on the lock before it times out and raises the OperationalError: database is locked error.
If you’re getting this error, you can solve it by:
Switching to another database backend. At a certain point SQLite becomes too “lite” for real-world applications, and these sorts of concurrency errors indicate you’ve reached that point.
Rewriting your code to reduce concurrency and ensure that database transactions are short-lived.
Increase the default timeout value by setting the timeout database option:
'OPTIONS': {
# ...
'timeout': 20,
# ...
}
This will make SQLite wait a bit longer before throwing “database is locked” errors; it won’t really do anything to solve them.
https://docs.djangoproject.com/en/3.0/ref/databases/#database-is-locked-errorsoption
Try using django.test.TransactionTestCase instead of TestCase

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

PyPugJs with Pyramid - Basic

I am trying to use PyPugJs with Pyramid.
Inside my __init.py, I have this
config.include('pypugjs.ext.pyramid')
Inside views.py,
#view_defaults(renderer='json')
class St2Views:
"""docstring for St2Views"""
def __init__(self, request):
super(St2Views, self).__init__()
self.request = request
#view_config(route_name='hello')
def hello(self):
session = self.request.session
return Response('<body><h1>Hello</h1></body>')
#view_config(route_name='home')
def home(self):
return {
'a': 'b'
}
#view_config(route_name='index', renderer='index.pug')
def index(self):
return {}
And I get this error when trying to go to the index route
Traceback (most recent call last):
File "z:\eels\dev\st2\env\lib\site-packages\pyramid_mako\__init__.py", line 148, in __call__
result = template.render_unicode(**system)
File "z:\eels\dev\st2\env\lib\site-packages\mako\template.py", line 454, in render_unicode
as_unicode=True)
File "z:\eels\dev\st2\env\lib\site-packages\mako\runtime.py", line 829, in _render
**_kwargs_for_callable(callable_, data))
File "z:\eels\dev\st2\env\lib\site-packages\mako\runtime.py", line 864, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "z:\eels\dev\st2\env\lib\site-packages\mako\runtime.py", line 890, in _exec_template
callable_(context, *args, **kwargs)
File "z:\eels\dev\st2\st2\index.pug", line 6, in render_body
body
File "z:\eels\dev\st2\env\lib\site-packages\markupsafe\_native.py", line 22, in escape
return Markup(text_type(s)
File "z:\eels\dev\st2\env\lib\site-packages\mako\runtime.py", line 226, in __str__
raise NameError("Undefined")
NameError: Undefined
It seems that the default mako renderer is being called rather than pug. Tried using PyJade as well with .jade extension, but with the same result.
What am I doing wrong ?
The issue was with the pug/jade template, where an undefined(unpassed) variable was being used.

TemplateSyntaxError: 'settings_tags' is not a valid tag library

i got this error when i try to run this test case: WHICH IS written in tests.py of my django application:
def test_accounts_register( self ):
self.url = 'http://royalflag.com.pk/accounts/register/'
self.c = Client()
self.values = {
'email': 'bilal#gmail.com',
'first_name': 'bilal',
'last_name': 'bash',
'password1': 'bilal',
'password2': 'bilal',
}
self.response = self.c.post( self.url, self.values )
my django version is 1.2.1 and python 2.6 and satchmo version is 0.9.2-pre hg-unknown
the complete error log is:
.E....
======================================================================
ERROR: test_accounts_register (administration.tests.AccountsRegisterTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\pytho\satchmo\administration\tests.py", line 53, in test_accounts_reg
ister
self.response = self.c.get( self.url )
File "C:\django\django\test\client.py", line 290, in get
response = self.request(**r)
File "C:\django\django\test\client.py", line 230, in request
response = self.handler(environ)
File "C:\django\django\test\client.py", line 74, in __call__
response = self.get_response(request)
File "C:\django\django\core\handlers\base.py", line 141, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\django\django\core\handlers\base.py", line 180, in handle_uncaught_ex
ception
return callback(request, **param_dict)
File "C:\django\django\views\defaults.py", line 23, in server_error
t = loader.get_template(template_name) # You need to create a 500.html templ
ate.
File "C:\django\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\django\django\template\loader.py", line 134, in find_template
source, display_name = loader(name, dirs)
File "C:\django\django\template\loader.py", line 42, in __call__
return self.load_template(template_name, template_dirs)
File "C:\django\django\template\loader.py", line 48, in load_template
template = get_template_from_string(source, origin, template_name)
File "C:\django\django\template\loader.py", line 168, in get_template_from_str
ing
return Template(source, origin, name)
File "C:\django\django\template\__init__.py", line 158, in __init__
self.nodelist = compile_string(template_string, origin)
File "C:\django\django\template\__init__.py", line 186, in compile_string
return parser.parse()
File "C:\django\django\template\__init__.py", line 282, in parse
compiled_result = compile_func(self, token)
File "C:\django\django\template\defaulttags.py", line 921, in load
(taglib, e))
TemplateSyntaxError: 'settings_tags' is not a valid tag library: Template librar
y settings_tags not found, tried django.templatetags.settings_tags,satchmo_store
.shop.templatetags.settings_tags,django.contrib.admin.templatetags.settings_tags
,django.contrib.comments.templatetags.settings_tags,django.contrib.humanize.temp
latetags.settings_tags,livesettings.templatetags.settings_tags,sorl.thumbnail.te
mplatetags.settings_tags,satchmo_store.contact.templatetags.settings_tags,tax.te
mplatetags.settings_tags,pagination.templatetags.settings_tags,product.templatet
ags.settings_tags,payment.templatetags.settings_tags,payment.modules.giftcertifi
cate.templatetags.settings_tags,satchmo_utils.templatetags.settings_tags,app_plu
gins.templatetags.settings_tags,tinymce.templatetags.settings_tags
----------------------------------------------------------------------
Ran 6 tests in 47.468s
FAILED (errors=1)
Destroying test database 'default'...
It seems to me you probably have a code like {% load settings_tags %} somewhere in your template. Django looks for templatetags/settings_tags.py file in your installed apps' directories. This is the result of not finding a file like this. Maybe the app, which contains it is not in your INSTALLED_APPS or maybe it's a typo. You should be getting the same error when you put this url in your browser.
Sometimes this happens when you forgot to put an __ init __.py in the package.
Like #AJJ said, you may have to restart the server to get the new tags loaded
This is a common issue for this package. When you get it from pypi, it does not contains the template tag: settings_tag.py and that will cause the error 'settings_tags' is not a valid tag library: Template library settings_tags not found.
The current solution is to install it from the github zip.

Categories

Resources