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 module for dynamic ALLOWED_HOSTS #34

Open
wants to merge 8 commits into
base: develop-multisite
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
41 changes: 41 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,44 @@ 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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this as we are using django-allowedsites app

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

__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)
1 change: 1 addition & 0 deletions ecommerce/extensions/edly_ecommerce_app/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def process_request(self, request):
setattr(settings, config_key, current_value)
else:
setattr(settings, config_key, config_value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove this extra line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

else:
logger.warning('Site configuration for site (%s) has no django settings overrides.', current_site)

Expand Down
2 changes: 2 additions & 0 deletions ecommerce/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from oscar import OSCAR_MAIN_TEMPLATE_DIR

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

# PATH CONFIGURATION
# Absolute filesystem path to the Django project directory
Expand Down Expand Up @@ -377,6 +378,7 @@
'rest_framework_swagger',
'django_sites_extensions',
'corsheaders',
'allowedsites',
]

# Apps specific to this project go here.
Expand Down
9 changes: 9 additions & 0 deletions ecommerce/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# into your settings, but ImproperlyConfigured is an exception.
from django.core.exceptions import ImproperlyConfigured

from allowedsites import AllowedSites
from ecommerce.settings.base import *

# Protocol used for construcing absolute callback URLs
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 = AllowedSites(defaults=(
'panel.edly.io',
'panel.backend.edly.io',
'.edly.io',
'ecommerce.healthcheck.local'
)
)
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ unidecode==0.4.21 # via django-oscar
uritemplate==3.0.0 # via coreapi
urllib3==1.25.6 # via requests
zeep==2.1.1
django-allowedsites==0.1.0