Django settings outside of project - python

I have a Django project that uses SQLAlchemy to use some legacy ORM objects. This application also hits up an ldap server for user authentication. I was getting sick of moving from development to production servers for both ldap and the database. I was hoping to create a IS_DEVELOPMENT variable in the settings.py. One problem I am running into is that the ORM modules are not in the Django project.
I know django.conf does a great job of parsing that settings file and making those settings available to you throughout your application. What I can not figure out is how to make those same settings available to myself outside of that app.
Anyone done this one before?

"Standalone Django Scripts"

Related

Can we manage the deployment of each django app within a single Django project independently?

Lets say we have one django project that has two apps - FooApp & BarApp.
Each app talks to its own database. Meaning they both manage their own set of models. Is it possible to manage the deployments of these apps independently? It's okay for them to be deployed on same server within the same nginx process as long as I am able to make changes to the apps without bringing down the other as well.
I understand that these can very well be separate projects and they can communicate with each other using RESTful APIs. For my needs, I want to avoid that REST interaction for the time being.
The django documentation [here][1] describes a django project and django app as follows
Django project:
The term project describes a Django web application.
Django App:
The term application describes a Python package that provides some set
of features.
So if the app is simply a python package, and the django project is really the one that defines how these apps are managed via the settings module, then I suppose there is no straight forward way of accomplishing what I want. Or is there anything I can do?
TIA
[1]: https://docs.djangoproject.com/en/dev/ref/applications/

I need to setup an app that uses Postgres and Python Social Auth?

I am starting to develop an app using Django as a framework. I will be using Python Social Auth https://github.com/omab/python-social-auth to login using Facebook.
The app will use Postgres as a database, therefore I am looking for guidance in the sequence to use to install the initial setup. The application will be deployed in Heroku and I found a references https://devcenter.heroku.com/articles/heroku-postgresql in how to install Postgres in my machine and in the hosting.
I need to find the right sequence of installation to be able to have a fully operational setup, before start to add my application.
Does anybody has experience in such configuration?
Your question is borderline close as off topic, asking for tutorial, but, the main answer to your question is the tutorial from heroku on heroku on django This covers getting a simple hello world app up and running and using postgres (including being able to run it locally via heroku local). The one thing I would add is that I prefer to modify my settings.py to look like this:
import dj_database_url
DATABASE = { } # standard postgres for local configuration
if dj_database_url.config(): #override local for heroku set value
DATABASES['default'] = dj_database_url.config()
At the conclusion of this tutorial, you'll have a working app that can use postgres. After that, installing Facebook social auth mainly consists of registering your app to get some secret values and putting those in settings.py as described in the documentation. (Truth in lending, haven't used Facebook auth but have done Google and Twitter Auth. The main thing with Heroku is that I would strongly recommend adding 127.0.0.1:5000 as well as https://.herokuapps.com so you can debug stuff from your local instance first (and also make sure you leave the default django auth so you can still log in via username/password while you're troubleshooting)
(One last tip: you should be able to do heroku run python manage.py shell and then from there do something like:
from django.contrib.auth.models import User
u = User.objects.get(username="Your Facebook username")
u.is_staff = True
u.is_superuser = True
u.save()
To promote your facebook account to be admin, as I forget if social auth sets it up so first user account is admin automatically or not)

How can I use djcelery.schedulers without Django project?

I want to use dynamic scheduler management for celery. I know djcelery have that functionality with database support.
But currently I do not use Django, but Flask. I couldn't find out Flask project or implementation with djcelery.schedulers.
Is it possible to use djcelery and implement dynamic scheduler management system without Django?
Short answer: No, but...
You have to use django. The scheduler's entries are instances of django models so you would have to setup djcelery app somehow (see this code: https://github.com/celery/django-celery/blob/master/djcelery/schedulers.py) Also you won't have the admin interface to add scheduler's entries.
This is just a guess, but you can try setting django's ORM standalone and syncing djcelery's models. (see: Use Django ORM as standalone)
You can also implement your own scheduler following the structure of djcelery/schedulers.py
Also see: Can celery celerybeat use a Database Scheduler without Django?
You can check out this flask-djcelery. It configures djcelery with flask, allows using django admin and also provide a browseable rest api for managing tasks.

Django admin project distribution and management

I have core django admin project that when my customers purchase needs to be modified both in configuration and function. Each customer will have their own instance of the project installed on a different server. Currently I am using django apps to separate out the difference in clients and using settings.py to load the correct app for the correct customer.
So my questions:
Is there a industry standard/best practice/framework to customize configuration and functionality in django admin projects and distribute them?
What would be best to do is write your core project on one side, and store it in a repository (version control). Apart from that, write your sub-applications as requirements and store/track them on separate version control.
Then, to keep everything working together and keep it all integrated, what you should do is that for different functionality, write it all in the same app, but add special settings that change the app's behavior. These settings can be stored in an independent settings_local.py that is imported at the end of your settings file, making them installation-independent, and you keep in your settings.py those that are general to all installations.

Django custom auth backend not recognized on Apache

I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on:
myproject
|- backends/
directory.Everything seems to be working fine, my pages load and I can make read/write operations properly. But whenever I try to login with my Twitter account, application fires an exception telling me:
Error importing authentication backend backends.twitteroauth: "No module named backends.twitteroauth"
In my settings.py, I'm registering my backend as
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'myproject.backends.twitteroauth.TwitterBackend',
)
What is the problem?
Removing database solved my problem. As far as I can guess, if a user is logged, his corresponding login backend is kept as a session variable on the database. My settings.py file was
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'backends.twitteroauth.TwitterBackend',
)
before I made the correction. Changing settings.py and restarting the application was simply not enough. You have to remove session related records from db too.
The problem is that python cannot find the module twitteroauth. What is the name of the file TwitterBackend is in? Also make sure that there is a __init__.py file in backends to mark it as a package.
edit:
What happens if you run the shell
python manage.py shell
and try to import it there?
from myproject.backends.twitteroauth import TwitterBackend
As anything else works fine, I guess myproject is in your python path.
Make sure that backends is on the python path and has a init.py file in the folder.

Categories

Resources