Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds dynamic allowed hosts #35

Open
wants to merge 1 commit into
base: develop-multisite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ecommerce/extensions/edly_ecommerce_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,46 @@ def user_is_course_creator(request):

decoded_cookie_data = decode_edly_user_info_cookie(edly_user_info_cookie)
return decoded_cookie_data.get('is_course_creator', False)

class AllowedHosts(object):
"""
Dynamic ALLOWED_HOSTS class that adds all current sites to ALLOWED_HOSTS.
"""
__slots__ = ('defaults', 'sites', 'cache')

def __init__(self, defaults=None, cache=True):
self.defaults = defaults or ()
self.sites = None
self.cache = cache

def get_sites(self):
if self.cache is True and self.sites is not None:
return self.sites + self.defaults

from django.contrib.sites.models import Site, SITE_CACHE
sites = Site.objects.all()
self.sites = tuple(site.domain for site in sites)

# fill Site.objects.get_current()'s cache for the lifetime
# of this process. Probably.
if self.cache is True:
for site_to_cache in sites:
if site_to_cache.pk not in SITE_CACHE:
SITE_CACHE[site_to_cache.pk] = site_to_cache

return self.sites + self.defaults

def __iter__(self):
return iter(self.get_sites())

def __str__(self):
return ', '.join(self.get_sites())

def __contains__(self, other):
return other in self.get_sites()

def __len__(self):
return len(self.get_sites())

def __add__(self, other):
return self.__class__(defaults=self.defaults + other.defaults)
9 changes: 9 additions & 0 deletions ecommerce/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.exceptions import ImproperlyConfigured

from ecommerce.settings.base import *
from ecommerce.extensions.edly_ecommerce_app.helpers import AllowedHosts

# Protocol used for construcing absolute callback URLs
PROTOCOL = 'https'
Expand Down Expand Up @@ -106,3 +107,11 @@ def get_env_setting(setting):

# Edly configuration
EDLY_COOKIE_SECRET_KEY = config_from_yaml.get('EDLY_COOKIE_SECRET_KEY', EDLY_COOKIE_SECRET_KEY)

ALLOWED_HOSTS = AllowedHosts(defaults=(
'panel.edly.io',
'panel.backend.edly.io',
'.edly.io',
'ecommerce.healthcheck.local'
)
)