bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.46k stars 424 forks source link

Url Routing and an example application #48

Closed infyloop closed 11 years ago

infyloop commented 11 years ago

Could you please elaborate on the tenant url routing portion as well?

If you go through https://github.com/tkaemming/django-subdomains/blob/master/docs/index.rst they have given ample instructions as how to set their urls.

I am finding it difficult to set up the subdomains following the docs as is. If you could supplement with an example, it will be great.

Thanks!

bernardopires commented 11 years ago

Hi,

All links using reverse on this app always point to the same schema (subdomain) it uses. So if you're at tenant1.domain.com, you can't call reverse to make a link for tenant2.domain.com. In this case you'll have to write the link manually. Was this your doubt, do you have any specific doubts? I'll gladly help you, but I don't know what you didn't understand. There are examples on the docs.

Bernardo

2013/6/22 bitaVersion notifications@github.com

Could you please elaborate on the tenant url routing portion as well?

If you go through https://github.com/tkaemming/django-subdomains/blob/master/docs/index.rstthey have given ample instructions as how to set their urls.

I am finding it difficult to set up the subdomains following the docs as is. If you could supplement with an example, it will be great.

Thanks!

— Reply to this email directly or view it on GitHubhttps://github.com/bcarneiro/django-tenant-schemas/issues/48 .

Bernardo Pires Carneiro

infyloop commented 11 years ago
  1. by example I meant a sample application, with views and urls.
  2. For subdomain url management, will I have to use https://github.com/ennio/django-hosts or https://github.com/tkaemming/django-subdomains

I started the app, put everything in place(middleware, installed_apps, tenant_model, tenant_urls.py and public_urls.py) had this(https://github.com/pennersr/django-allauth) installed.

when I run python manage.py sync_schemas --shared

=== Running syncdb for schema public Syncing... Creating tables ... Creating table south_migrationhistory Creating table account_emailaddress Creating table account_emailconfirmation DatabaseError: relation "django_content_type" does not exist LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...`

Apart from that, the tests also fail. Here is my settings file

SHARED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.humanize', 'django.contrib.sitemaps',

Uncomment the next line to enable the admin:

'south',
'static_sitemaps',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.twitter',
'django_extensions',

)

these are apps that are specific to each tenants TENANT_DJANGO_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', )

TENANT_THIRD_PARTY_APPS = ( 'tenant_schemas', )

TENANT_LOCAL_APPS = ( 'customer, )

TENANT_MODEL = "customer.Org" PUBLIC_SCHEMA_URL_TOKEN = '/main'

MIDDLEWARE_CLASSES = ( 'django.middleware.gzip.GZipMiddleware', 'htmlmin.middleware.HtmlMinifyMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'tenant_schemas.middleware.TenantMiddleware',

Uncomment the next line for simple clickjacking protection:

# 'django.middleware.clickjacking.XFrameOptionsMiddleware',

)

TENANT_APPS = TENANT_DJANGO_APPS + TENANT_LOCAL_APPS + TENANT_THIRD_PARTY_APPS INSTALLED_APPS = SHARED_APPS + TENANT_APPS

when I run the tests, 1 test fails with 8 error

FAIL: test_non_auto_sync_tenant (tenant_schemas.tests.tenants.TenantTestCase)

Traceback (most recent call last): File "/home/betaversion/.virtualenvs/sosio/local/lib/python2.7/site-packages/tenant_schemas/tests/tenants.py", line 53, in test_non_auto_sync_tenant self.assertFalse(schema_exists(tenant.schema_name)) AssertionError: True is not false


Ran 6 tests in 2.396s

FAILED (failures=1, errors=8) Destroying test database for alias 'default'...

The entire stacktrace is https://dpaste.de/OZ9Wc/

The tenant model looks like the following

class BaseModel(TenantMixin):

    """
    An abstract base class model that provides self-managed "created" and
    "modified" multi tenant fields.
    """
    auto_create_schema = True
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Class Org(BaseModel):

    username = models.OneToOneField(User)
    name = models.CharField(max_length=500, null=True, blank=True)
    slug = models.SlugField(null=True, blank=True)
    website = models.URLField(max_length=500, null=True, blank=True)
    established_in = models.DateField(help_text = "MM/DD/YYYY")
    description = models.TextField()

    paid_until =  models.DateField()
    on_trial = models.BooleanField()

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name[0:50])
        super(Org, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name
bernardopires commented 11 years ago

there's an issue right now on this app that requires contenttypes, auth and admin to exist on both tenant and public. you should then be able to use it without errors. ill look into this issue as soon as possible

2013/6/22 bitaVersion notifications@github.com

1.

by example I meant a sample application, with views and urls. 2.

For subdomain url management, will I have to use https://github.com/ennio/django-hosts or https://github.com/tkaemming/django-subdomains

I started the app, put everything in place(middleware, installed_apps, tenant_model, tenant_urls.py and public_urls.py) had this( https://github.com/pennersr/django-allauth) installed. when I run python manage.py sync_schemas --shared

=== Running syncdb for schema public Syncing... Creating tables ... Creating table south_migrationhistory Creating table account_emailaddress Creating table account_emailconfirmation DatabaseError: relation "django_content_type" does not exist LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...` Apart from that, the tests also fail. Here is my settings file

SHARED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.humanize', 'django.contrib.sitemaps',

Uncomment the next line to enable the admin:

'south', 'static_sitemaps', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.facebook', 'allauth.socialaccount.providers.google', 'allauth.socialaccount.providers.twitter', 'django_extensions', )

these are apps that are specific to each tenants TENANT_DJANGO_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', )

TENANT_THIRD_PARTY_APPS = ( 'tenant_schemas', )

TENANT_LOCAL_APPS = ( 'cbase', 'ebase', )

TENANT_MODEL = "customer.Org" PUBLIC_SCHEMA_URL_TOKEN = '/main'

MIDDLEWARE_CLASSES = ( 'django.middleware.gzip.GZipMiddleware', 'htmlmin.middleware.HtmlMinifyMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'tenant_schemas.middleware.TenantMiddleware',

Uncomment the next line for simple clickjacking protection:

'django.middleware.clickjacking.XFrameOptionsMiddleware',

)

TENANT_APPS = TENANT_DJANGO_APPS + TENANT_LOCAL_APPS + TENANT_THIRD_PARTY_APPS INSTALLED_APPS = SHARED_APPS + TENANT_APPS when I run the tests, 1 test fails with 8 error FAIL: test_non_auto_sync_tenant (tenant_schemas.tests.tenants.TenantTestCase)

Traceback (most recent call last): File "/home/betaversion/.virtualenvs/sosio/local/lib/python2.7/site-packages/tenant_schemas/tests/tenants.py", line 53, in test_non_auto_sync_tenant self.assertFalse(schema_exists(tenant.schema_name))

AssertionError: True is not false

Ran 6 tests in 2.396s

FAILED (failures=1, errors=8) Destroying test database for alias 'default'... The entire stacktrace is https://dpaste.de/OZ9Wc/ The tenant model looks like the following

class BaseModel(TenantMixin):

"""
An abstract base class model that provides self-managed "created" and
"modified" multi tenant fields.
"""
auto_create_schema = True
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

class Meta:
    abstract = True

Class Org(BaseModel):

username = models.OneToOneField(User)
name = models.CharField(max_length=500, null=True, blank=True)
slug = models.SlugField(null=True, blank=True)
website = models.URLField(max_length=500, null=True, blank=True)
primary_focus = models.CharField(max_length=500, null=True, blank=True, help_text='Very briefly, what is your prime focus?')
established_in = models.DateField(help_text = "MM/DD/YYYY")
description = models.TextField(help_text = "Brief Description of what your NGO is into.")

paid_until = models.DateField() on_trial = models.BooleanField()

def save(self, _args, _kwargs): self.slug = slugify(self.name[0:50]) super(Org, self).save(_args, _kwargs)

def unicode(self): return self.name

— Reply to this email directly or view it on GitHubhttps://github.com/bcarneiro/django-tenant-schemas/issues/48#issuecomment-19856568 .

Bernardo Pires Carneiro

bernardopires commented 11 years ago

Same as #43