Django White Labelling Django.How

Author avatar wrote on 30/05/2022

Check if user and page belongs to the same company

views.py

from accounts.utils import get_session_company, is_a_company_user

views.py

on single


    company = get_session_company(request.session['company_id'])
    context['is_a_company_user'] = is_a_company_user(company, request.user)
    if not context['is_a_company_user']:
        return render(request, template_name, context)

on lists

company = get_session_company(request.session['company_id'])

Template


{% if is_a_company_user %}

{% else %}
{% include 'partials/_not_the_same_company.html' %}
{% endif %}

Point other websites or subdoamins to your website

DNS setup (on other domains)

Add record
Type: CNAME
Name: subdomain e.g: portal
content: to the main domain myDomain.com
proxied : DNS Only

Setup

Accounts.utils.py

from company.models import Company

def get_session_company(company_id):
    company = None
    if company_id and int(company_id) > 0:
        try:
            company = Company.objects.get(id=int(company_id))
        except Exception as e:
            print('No company in session, we took first')
            print(e)
            company = Company.objects.all().first()
    else:

means a SU or TR logging in and no company on session

        company = Company.objects.all().first()
    return company


def is_a_company_user(company, user):
    if user.profile.user_type == 'SU' or user.profile.user_type == 'TR':
        return True
    if company:

As long as we return one company

        if company == user.profile.companies():
            return True
    return False

Get domain name

Views

request.META['HTTP_HOST']

Template

{{ request.META.HTTP_HOST }}

module.context_processors.py

https://stackoverflow.com/questions/1451138/how-can-i-get-the-domain-name-of-my-site-within-a-django-template

Quick and simple, but not good for production:

views

request.scheme # http or https
request.META['HTTP_HOST'] # example.com
request.path # /some/content/1/

Template

{{ request.scheme }} :// {{ request.META.HTTP_HOST }} {{ request.path }}

https://stackoverflow.com/questions/4845239/how-can-i-disable-djangos-admin-in-a-deployed-project-but-keep-it-for-local-de