Multiple PostgreSQL schemas and users on Django with subdomains - django

I have seen various questions on multi-site and multi-host Django, including subdomains and specific schemas per subdomain. What I have not seen is a solution (or tips so I can code one) to this problem.
I am using Django + PostgreSQL on a site, let's say myapp.com
The main site myapp.com is used for registration of companies
A registered company gets its own subdomain, company.myapp.com, and logs in and works from there.
My idea of doing this is by making 2 initial schemas in PostgreSQL.
Schema "auth" for companies and users
Schema "empty_company_template" with the basic tables for a company, all empty but hooked up to the right sequences etc.
When a new company registers, I want this to happen:
Create a new schema for the company, derives from empty_company_template
Create a new DB user for the company, named company (the company name)
Set the search path for this new user to company, auth (no access to empty_company_template, no access to other users schema's)
To me this seems better than the existing solutions that all seem to depend on one single database user for the entire application (with access to all schemes). However, I struggle to get this to work. Is this indeed a viable approach? Can anyone point me in the right direction? It's Django, so perhaps it's been done and I just haven't found it?

I have a working solution that does everything except separate users.
It's a small piece of middleware (just process_request) that determines the subdomain, and executes a SET search_path query on the database. It's good enough for me for now.
Anyone interested in the code, just contact me. I'll publish it somewhere when it's final.
EDIT Dec 22, 2010:
I published the code on my blog at
http://blog.dyve.net/django-subdomains-and-postgresql-schemas

If you want to have separate DB users, you'll probably want separate Django instances, otherwise there will be no security gain. This model will require much more complicated process management. I don't think you'll find a ready-made solution for such an application, so you will most probably have to roll your own. Otherwise, if you're unwilling to invest much time, stick with the one user for the entire app approach.

Related

Using Django to Create Child Sites

I am creating a series of small sites, I'm using the django framework. The theory goes a user comes to a master site, signs up, then he gets his own child site.
Example:
navigate to example.com
user creates an account "mysite"
user then gets his own site: mysite.example.com and he can configure this all he wants
My question: * would it be better to have a "gold" version of the site that gets created for each site?
for instance: cp ~/goldsite ~/mysite and change the database pointers appropriately ** the downside is if I ever have to do maintenance on a file, I would have to change all subsites.
...or * have one host and configure the database to support multiple sites. The DB might get messy.
Any feedback would be great.
Use the same code/database for all the users, otherwise it would be a nightmare to maintain the code. Think about database migrations, backups, update the code with git...
...or * have one host and configure the database to support multiple sites. The DB might get messy.
It shouldn't be really complex, associate each website object with an user and use the permissions properly.

How should I set up and deploy my django project that spans over multiple subdomains and multiple databases?

Let's say I'm building a Django driven site that hosts web shops, let's call it webshop.com. I want it to work like this:
A shop named Foo would like a webshop. They register at webshop.com and the subdomain foo.webshop.com is created, as well as the database foo_DB. The owners of Foo enter their products using an admin interface at foo.webshop.com/admin. Customers can then buy the products from foo.webshop.com.
The the shop Bar has the webshop bar.webshop.com - completely unrelated to the other webshops hosted at webshop.com. And so on. webshop.com should be able to host 100's of stores.
The question is: how should I go about doing this with Django?
At first I was thinking that I should have one settings.py file, one virtual server and one database for each webshop. However I fear that it can be generated too much overhead from running all those instances.
Any pointers or thoughts is greatly appreciated!
Perhaps you could usedjango-sudomains https://github.com/tkaemming/django-subdomains/
It is a middleware which creates a subdomain property for each request.
Since the functionality for each site is the same you could then just query for products based on sudomain requested. You could also assign users a subdomain.
All views would be the same except they would take into account this subdomain. This approach could work easy with 1 shared database where each model has a field that points to the subdomain or a shared database with subdomains being the database naem. https://docs.djangoproject.com/en/dev/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset
I've built half a dozen applications that work like this (though none in python, but it's really irrelevant in this case).
Serve everything from the same virtual host. Get the server name with urlparse Once you have the string you want to match, do a lookup in a master database for the database details which match your customer database (ie the hostname, username, password, etc.). Make sure you sanitize that value before querying on it.
I do think your best bet is separating your customers databases. Not only does this ensure that one database problem doesn't bring down all your customer sites, it also allows you to put them on different servers.

white labeling and Django application

I've just been approached by my boss about the possibility of white labeling our Django-based site for a new, large customer. Basically, it sounds like it would mainly be changing the graphics/logos on the site. But, I'm sure that at some point they will want to start tweaking the business logic as well. I'm still unclear whether the desire is for it to be a subdomain (e.g. customer.mydomain.com) or a completely new custom domain. Ultimately, it will need to share the database with the rest of the system (for multiple reasons). Is this a good use of the Sites Framework in Django? Or is there a better way of doing this?
More info: Django 1.3.1; PostgreSQL 9.1; Hosted on Heroku
Use a combination of middleware (to set the active site) and context processors (to act on that). You could try to hook into contrib's site framework, but I've always outgrown it rather quickly.

How to setup groups (sub-sites) in Django

I'm new to Django and I come from Drupal family. There we have Organic Groups with which we can create groups of content and subsites; how do I do something like that with Django?
Say I'm making this site for my company using Django and every department in my company needs a private section on the site. For example, the design people have their own part of the website into which the back-end developers can not come in. And the back-end developers will have the same thing too.
I want to build the site in such a way that I just login into Django admin and add a new category or subsite or group (whatever the Django term is) with the same settings from other groups or with similar settings.
It depends on what you mean by "private section". You should probably try looking at it from a different angle:
Django splits a site's functionality by means of "apps". Each app does its specific thing, and gets a set of tables in the database. Apps can access each others' tables. For example, it's common for other apps to access the Auth app's user, group, and permissions tables. Is this what you mean by "sub sites"?
As for access control, users can be assigned to groups and they can have various administrative permissions assigned to them. Add, change, and delete permissions are automatically generated for each model (i.e. database table). You can also add your own permissions.
I don't think you'll be able to separate the designers from the back-end developers at the Django level. You'll need to do something else, such as maintain separate source repositories for each and merge them to create the usable site (each group would have read-only access to the other). It really depends on your teams' discipline, because these elements can get intertwined.
Django recommends that static files be served by something else, say directly from your web server, or from another machine with a simple HTTP server (no CGI/WSGI/whatever). This is because Django can only slow down static files compared to direct service. However, for testing, ther is a static page server you can enable.
Given all that, static files usually amount to CSS, images, media, and JavaScript. Of these, the back-end people might want to mess with the JS, but that's it, so this could be in the designers' repo.
The Django tree itself has the code for the site and the apps. It's almost all back end stuff. The exception is the HTML template files, located in the "templates" directory in each app. These are the files that are filled in with the context data supplied by the back-end view code. I have no idea if this is front or back end for you guys; it could be mostly back end if there's a lot of CSS discipline, but I think that's unlikely.
There are a lot of things that you can do in Django that make life easier for one side or the other. For example, template tags allow custom Python code to generate HTML to insert into the page. I use these to generate tab bars and panes, for example.
I really can't help much more without getting a better picture of what your needs are. The question is still vague. You're probably best off taking a day or two going through the tutorial, seeing what the Django perspective is, and then working out how (or if!) it fits into your needs.

How to host 50 domains/sites with common Django code base

I have 50 different websites that use the same layout and code base, but mostly non-overlapping data (regional support sites, not link farm). Is there a way to have a single installation of the code and run all 50 at the same time?
When I have a bug to fix (or deploy new feature), I want to deploy ONE time + 1 restart and be done with it.
Also:
Code needs to know what domain the request is coming to so the appropriate data is displayed.
The Sites framework comes to mind.
Apart from that we have Django running for multiple sites by symlinking Django to various docroots. Works like a charm, too.
I can see two quite distinct ways to do this:
Use one database and the sites framework. Every post/picture/whatever model is connected to a Site and you always filter on Site. This requires a separate settings file for every database.
Use one database for each and every site. This allows different users for every site, but requires duplication of everything that is stored in the database. It also requires a separate settings file pointing to the correct database.
Either way, you do not duplicate any code, only data.
--
If you need to do site-specific, or post-specific changes to ie. a template, you should read up on how Django loads templates. It allows you to specify a list, ie ["story_%d.html", "story_site_%d.html", "story.html"] and django will look for the templates in that order.
I just ran into this and ended up using a custom middleware class that:
Fetch the HTTP_HOST
Clean the HTTP_HOST (remove www, ports, etc.)
Look up domain in a Website table that's tied to each account.
Set the account instance on the HTTPRequest object.
The throughout my view code I do lookups based on the account stored in the HTTPRequest objects.
Hope that helps someone in the future.