Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Review changes
michald4e1273716ca
diff --git a/saleor/settings.py b/saleor/settings.pyindex 8d23bb2f6e..f19ba198f6 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -380,8 +380,6 @@ GRAPHENE = {PROJECT_ROOT, 'saleor', 'static', 'schema.json')}-# SITE_SETTINGS_ID = 1-AUTHENTICATION_BACKENDS = ['saleor.registration.backends.facebook.CustomFacebookOAuth2','saleor.registration.backends.google.CustomGoogleOAuth2',diff --git a/saleor/site/migrations/0007_auto_20171027_0856.py b/saleor/site/migrations/0007_auto_20171027_0856.pyindex aa36b4c68f..e331fb3e61 100644--- a/saleor/site/migrations/0007_auto_20171027_0856.py+++ b/saleor/site/migrations/0007_auto_20171027_0856.py@@ -9,9 +9,14 @@ def link_to_sites(apps, schema_editor):SiteSettings = apps.get_model('site', 'SiteSettings')Site = apps.get_model('sites', 'Site')- for site in SiteSettings.objects.all():- site.site = Site.objects.get_or_create(domain=site.domain, name=site.name)[0]- site.save()+ for setting in SiteSettings.objects.all():+ setting.site = Site.objects.get_or_create(domain=setting.domain, name=setting.name)[0]+ setting.save()++ for site in Site.objects.all():+ if not site.settings:+ site.settings = SiteSettings.objects.create()+ site.save()class Migration(migrations.Migration):@@ -22,5 +27,5 @@ class Migration(migrations.Migration):]operations = [- migrations.RunPython(link_to_sites),+ migrations.RunPython(link_to_sites, migrations.RunPython.noop),]diff --git a/saleor/site/models.py b/saleor/site/models.pyindex e8bf1876f5..d275b9ed99 100644--- a/saleor/site/models.py+++ b/saleor/site/models.py@@ -1,72 +1,12 @@-import threading--from django.contrib.sites.models import SiteManager, Site-from django.core.exceptions import ImproperlyConfigured+from django.contrib.sites.models import Sitefrom django.db import models-from django.http.request import split_domain_port+from django.utils.encoding import python_2_unicode_compatiblefrom django.utils.translation import pgettext_lazyfrom . import AuthenticationBackends-lock = threading.Lock()-with lock:- THREADED_SITE_CACHE = {}---def new_get_current(self, request=None):- from django.conf import settings- if getattr(settings, 'SITE_ID', ''):- site_id = settings.SITE_ID- if site_id not in THREADED_SITE_CACHE:- with lock:- site = self.prefetch_related('settings').filter(pk=site_id)[0]- THREADED_SITE_CACHE[site_id] = site- return THREADED_SITE_CACHE[site_id]- elif request:- host = request.get_host()- try:- # First attempt to look up the site by host with or without port.- if host not in THREADED_SITE_CACHE:- with lock:- site = self.prefetch_related('settings').filter(- domain__iexact=host)[0]- THREADED_SITE_CACHE[host] = site- return THREADED_SITE_CACHE[host]- except Site.DoesNotExist:- # Fallback to looking up site after stripping port from the host.- domain, port = split_domain_port(host)- if domain not in THREADED_SITE_CACHE:- with lock:- site = self.prefetch_related('settings').filter(- domain__iexact=domain)[0]- THREADED_SITE_CACHE[domain] = site- return THREADED_SITE_CACHE[domain]-- raise ImproperlyConfigured(- "You're using the Django \"sites framework\" without having "- "set the SITE_ID setting. Create a site in your database and "- "set the SITE_ID setting or pass a request to "- "Site.objects.get_current() to fix this error."- )---def new_clear_cache(self):- global THREADED_SITE_CACHE- with lock:- THREADED_SITE_CACHE = {}---def new_get_by_natural_key(self, domain):- return self.prefetch_related('settings').filter(domain__iexact=domain)[0]---SiteManager.get_current = new_get_current-SiteManager.clear_cache = new_clear_cache-SiteManager.get_by_natural_key = new_get_by_natural_key--@python_2_unicode_compatibleclass SiteSettings(models.Model):site = models.OneToOneField(Site, related_name='settings')diff --git a/saleor/site/patch_sites.py b/saleor/site/patch_sites.pynew file mode 100644index 0000000000..0c89f63bc8--- /dev/null+++ b/saleor/site/patch_sites.py@@ -0,0 +1,77 @@+import threading++from django.contrib.sites.models import SiteManager+from django.core.exceptions import ImproperlyConfigured+from django.http.request import split_domain_port+++'''+We are patching django.contrib.sites because it is cached on module level and+can be in some cases thread unsafe. Also django.contrib.sites can be used by+other modules and we don't want to lose its functionality. Saleor has its own+SiteSetting, but having those settings in two places is not what we wanted.+Instead we link django.contrib.sites and saleor Site.Settings with One To One+relationship.+In this patch we are also prefetching our settings for better performance.+'''+++lock = threading.Lock()+with lock:+ THREADED_SITE_CACHE = {}+++def new_get_current(self, request=None):+ from django.conf import settings+ if getattr(settings, 'SITE_ID', ''):+ site_id = settings.SITE_ID+ if site_id not in THREADED_SITE_CACHE:+ with lock:+ site = self.prefetch_related('settings').filter(pk=site_id)[0]+ THREADED_SITE_CACHE[site_id] = site+ return THREADED_SITE_CACHE[site_id]+ elif request:+ host = request.get_host()+ try:+ # First attempt to look up the site by host with or without port.+ if host not in THREADED_SITE_CACHE:+ with lock:+ site = self.prefetch_related('settings').filter(+ domain__iexact=host)[0]+ THREADED_SITE_CACHE[host] = site+ return THREADED_SITE_CACHE[host]+ except Site.DoesNotExist:+ # Fallback to looking up site after stripping port from the host.+ domain, port = split_domain_port(host)+ if domain not in THREADED_SITE_CACHE:+ with lock:+ site = self.prefetch_related('settings').filter(+ domain__iexact=domain)[0]+ THREADED_SITE_CACHE[domain] = site+ return THREADED_SITE_CACHE[domain]++ raise ImproperlyConfigured(+ "You're using the Django \"sites framework\" without having "+ "set the SITE_ID setting. Create a site in your database and "+ "set the SITE_ID setting or pass a request to "+ "Site.objects.get_current() to fix this error."+ )+++def new_clear_cache(self):+ global THREADED_SITE_CACHE+ with lock:+ THREADED_SITE_CACHE = {}+++def new_get_by_natural_key(self, domain):+ return self.prefetch_related('settings').filter(domain__iexact=domain)[0]+++def patch_contrib_sites():+ SiteManager.get_current = new_get_current+ SiteManager.clear_cache = new_clear_cache+ SiteManager.get_by_natural_key = new_get_by_natural_key+++patch_contrib_sites()