Skip to content

Instantly share code, notes, and snippets.

@MainaKamau92
Created August 3, 2020 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MainaKamau92/4ff1f40bfc940a6968d6df740296acbc to your computer and use it in GitHub Desktop.
Save MainaKamau92/4ff1f40bfc940a6968d6df740296acbc to your computer and use it in GitHub Desktop.
from datetime import date
from dateutil.relativedelta import relativedelta
from django.core.exceptions import ObjectDoesNotExist
from tenant_schemas.middleware import BaseTenantMiddleware
from tenant_schemas.utils import get_public_schema_name
class RequestIDTenantMiddleware(BaseTenantMiddleware):
def get_tenant(self, model, hostname, request):
try:
public_schema = model.objects.get(schema_name=get_public_schema_name())
except ObjectDoesNotExist:
public_schema = model.objects.create(
domain_url=hostname,
schema_name=get_public_schema_name(),
tenant_name=get_public_schema_name().capitalize(),
paid_until=date.today() + relativedelta(months=+1),
on_trial=True)
public_schema.save()
x_request_id = request.META.get('HTTP_X_REQUEST_ID', public_schema.tenant_uuid)
tenant_model = model.objects.get(tenant_uuid=x_request_id)
print(tenant_model, public_schema)
return tenant_model if not None else public_schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment