Import issues with Python and celery - python

There is a pretty strange issue happening on my production server that doesn't come up in UAT or my development environment. Stack setup is Ubuntu 14LTS, Apache2, Python 2.7,Celery, RabbitMQ, Django 1.6.
How it works is that there is an async task that is called whenever an excel report needs to be generated. Depending on the type of company, a different report is created and emailed to the logged in user. The functions to create the different reports are all placed in a file called report_lib.py and they are not importing from any other app. The code goes like this:
if policies.carrier == "class_one":
report_lib.create_remittance_class_one_report(company_id)
else:
report_lib.create_generic_remittance_report(company_id)
Get this, the first conditional works no problem. The second conditional works when I don't have a company type set(works as expected), but for any other class it fails with the following error:
[2014-10-27 17:56:55,271: WARNING/Worker-1] There was an error: 'module' object has no attribute 'create_generic_remittance_report'
To make things more interesting I removed the conditional and defaulted to the code just calling the "create_generic_remittance_report" function for all cases, which works without complaining about that module.
I'm 99% sure that there are no-circular references. Oh, and the library being referenced is under the only app being used in the django project. Could this be a compiler caching issue?
Has anyone come across a similar issue using the same setup?
Please help!

Found the issue, the function being referenced was indented an extra tab. I guess since celery only calls it, it came back with that error.

Related

Django Template loader doesn't have "get_contents()" function implemented. Is there a substitute?

I am working on upgrading some older Django code however I am running into issues with the loader function "get_contents()" as it has apparently been deprecated. I'm not sure how to proceed. Should I create a new child claqss from that loader and implement the function myself or is there another way?
I have basically tried looking this problem up on google and the few answers I've come across have been somewhat vague.
I can't show much code, as this is a private project, but I can show one line with the names changed.
templateVariable = loader.get_template('appName/filename.txt')
This is the error I'm getting when I try and run the function:
'Loader' object has no attribute 'get_contents'
So it turns out that there was a custom loader being used in the project and the get_contents() function hadn't been implemented yet. Another mystery solved.

Django 'CSRFCheck' object has no attribute 'process_request'

It was all ok till suddenly I got the above error while implementing DjangoObjectPermissions on my APIs.
Before this It was working ok even my production environment it is working fine. I am seeing this error on my local environment only.
according to this answer, the error would go away, but I need to know why?
Please let me know what information should I add to this post.
Following are the related packages installed.
Django==1.10
django-allauth==0.29.0
django-angular==0.8.3
django-debug-toolbar==1.6
django-debug-toolbar-request-history==0.0.3
django-debug-toolbar-template-profiler==1.0.1
django-debug-toolbar-template-timings==0.7
djangorestframework==3.5.3
Add try/except statement to enforce_csrf()
Note that this error originates from rest_framework/authentication.py, within class SessionAuthentication, method enforce_csrf(). The enforce_csrf() method iinitiates variable, check = CSRFCheck(), and the next line says "check.process_request(request).
If using an IDE, you'll quickly notice that CSRFCheck() doesn't have this attribute/method thus the error. Many developers will quickly tell you to upgrade to django >= 1.11.6 but you are bound to run into the same error.
For that matter, am using django 1.11.6 and rest_framework 3.9.1.
So try this remedy, it worked for me. Use the try/except statement.
Go within the rest_framework/authentication.py, below, check = CSRFCheck(),
add this...
def enforce_csrf(self, request):
...
try:
check.process_request(request)
except:
pass
What the lines say is this:
After declaring the "check" variable, call (try) this method "process_request()" and if it does not work (except), just pass. You achieve two things with this: One, you literally leave the rest_framework source code compatible (given that upgrading to further versions of django "might" solve this), and two, you get working code (nice!!, especially if working Agile)

Firebase Python firestore.DELETE_FIELD appears as an error in VS Code

So I'm using the delete field method on documents in my Firestore database, and it seems to be working fine, but for some reason in VS Code it shows up as an error. I have it set up nearly identical to how they do it in their reference documents, except I'm deleting fields dynamically from a variable. Here's how my code looks:
db.collection("collection1").document(doc1).update({fieldValue: firestore.DELETE_FIELD})
Not sure why, but VS Code says firebase_admin.firestore has no "DELETE_FIELD" member, but the code still works as expected? Not a huge deal, but it's just annoying that my editor shows I have an error but it still works. Let me know if any of you have ran into this problem.
In case anyone else arrives here, DELETE_FIELD is an attribute of google.cloud.firestore_v1.transforms
So I solved it with:
from google.cloud.firestore_v1.transforms import DELETE_FIELD
db.collection("collection1").document(doc1).update({fieldValue: DELETE_FIELD})

Forcing ArrayFire to switch backends in Python

I have been attempting to force ArrayFire to use its CPU backend, rather than the default CUDA backend. According to this documentation page, you only need to call arrayfire.set_backend('cpu'). However, when I attempt to do this, an error is thrown with the message global name 'backend' is not defined. If you take a look at the source code, you will see that a global variable backend is defined within the module directly before the set_backend function is implemented. The following functions set and get various attributes of this backend object. My question is: is an internal implementation error on their part causing this error, or is there something I'm doing wrong (or that can be done on my part to fix this)? I haven't worked much with Python modules before, and would greatly appreciate any help!

Correct place to put extra startup code in django?

I would like to run some environment checks when my django process starts and die noisily in the case of an error. I'm thinking things like the database has an incorrect encoding or the machine has a python version we don't support.
I'd rather our team be faced with a fatal error that they have to fix, rather than be able to ignore it.
I'm Ok with writing these checks but I'm curious about where the best place to put them is. How do I get them to execute as part of django's startup process? I thought there might be a signal I could listen too, but I can't find a relevant one in the docs.
If you don't want to use settings module, then try project's __init__.py.
If you want to check that the system is correctly installed, I think that you should write your own admin command and run it as post-installation check.
I think that it doesn't worth to check if the python version is correctly installed too often especially if you are installing the django app on shared-host. My app is hosted at alwaysdata and they restart the FastCgi process every hour. These checks can have an impact on the application response time.
We use the top-level urls.py for this.
I would put them in settings.py. In the past, I have put system checks like this:
try:
from local_settings import *
except ImportError:
print "Missing %s" % os.path.join(PROJECT_ROOT, "local_settings.py")
if DEBUG:
for p in [PROJECT_ROOT, MEDIA_ROOT, THEME_DIR, ADMIN_MEDIA_ROOT] + list(TEMPLATE_DIRS):
p = os.path.normpath(p)
if not os.path.exists(p):
print "Missing path: %s" % p
I have tested all three __init__.py Settings.py and urls.py methods and this is what I have found.
When code is run from either __init__.py or Settings.py, the start up functions are run twice upon web server start up; when the start up functions are run from urls.py the code is run once, however it is run only upon the first request made to the we server leading to a potentially long wait for the first user to visit your site.
It is standard practise to call a 'warming' page when bringing large web applications back online so I don't see that calling a start up function from a CLEARLY identified location in the urls.py should be a problem.
Most of the answers here are extremely old, so I assume that's why Django's checks framework isn't mentioned.
It's been a part of django since at least v2 (django==3.1 at the time of writing).
That's probably the right place for most of the checks you're requiring.
I'd still consider using settings, as mentioned by other answers, for checks where settings contents are required but which also need to be run prior to readying the apps.
You can put it in settings.py as mentioned by others, but having code in the settings is not ideal. There is also the option of adding a handler for django.db.models.signals.class_prepared that does the desired start up checks after a specific model class is prepared.

Categories