def patched_clear_cache(self):
    keys_id = [_cache_key_for_site_id(site_id) for site_id in SITE_CACHE]
    keys_host = [
        _cache_key_for_site_host(site_host) for site_host in SITE_CACHE
    ]
    cache.delete_many(keys_id + keys_host)
    SITE_CACHE.clear()
示例#2
0
def current_site(request):
    site_pk = request.GET.get(SITE_VAR, None) if request.GET.get(SITE_VAR, None) else request.POST.get(SITE_VAR, None)
    if not site_pk:
        site_pk = request.session.get('cms_admin_site', None)
    if site_pk:
        try:
            site = SITE_CACHE.get(site_pk) or Site.objects.get(pk=site_pk)
            SITE_CACHE[site_pk] = site
            return site
        except Site.DoesNotExist:
            return None
    else:
        return Site.objects.get_current()
示例#3
0
def current_site(request):
    site_pk = request.GET.get(SITE_VAR, None) if request.GET.get(SITE_VAR, None) else request.POST.get(SITE_VAR, None)
    if not site_pk:
        site_pk = request.session.get('cms_admin_site', None)
    if site_pk:
        try:
            site = SITE_CACHE.get(site_pk) or Site.objects.get(pk=site_pk)
            SITE_CACHE[site_pk] = site
            return site
        except Site.DoesNotExist:
            return None
    else:
        return Site.objects.get_current()
示例#4
0
def current_site(request):
    if SITE_VAR in request.REQUEST:
        site_pk = request.REQUEST[SITE_VAR]
    else:
        site_pk = request.session.get('cms_admin_site', None)
    if site_pk:
        try:
            site = SITE_CACHE.get(site_pk) or Site.objects.get(pk=site_pk)
            SITE_CACHE[site_pk] = site
            return site
        except Site.DoesNotExist:
            return None
    else:
        return Site.objects.get_current()
示例#5
0
def current_site(request):
    if SITE_VAR in request.REQUEST:
        site_pk = request.REQUEST[SITE_VAR]
    else:
        site_pk = request.session.get('cms_admin_site', None)
    if site_pk:
        try:
            site = SITE_CACHE.get(site_pk) or Site.objects.get(pk=site_pk)
            SITE_CACHE[site_pk] = site
            return site
        except Site.DoesNotExist:
            return None
    else:
        return Site.objects.get_current()
 def get_site(self, request):
     host = request.get_host().lower()
     if ':' in host:
         host, _ = host.split(':', 1)
     # First check, if there are any cached sites
     site = SITE_CACHE.get(host, None)
     if site is None:
         if Site._meta.installed:
             # Secondly, find the matching Site objects and set the cache
             matches = Site.objects.filter(domain__iexact=host)
             try:
                 site = matches[0]
             except IndexError:
                 site = None
         else:
             site = CaseInsensitiveRequestSite(request)
         SITE_CACHE[host] = site
     return site
示例#7
0
def get_site(request):
    """
    Returns a ``Site`` object for the current host name on request.

    If ``django.contrib.sites`` is not installed, a case insensitive
    ``RequestSite`` will be returned, otherwise the first ``Site`` matching the
    current host name will be returned.

    If no matching ``Site`` object is found, the default behaviour is to return
    ``None``. This can be altered via the ``SITES_FALLBACK`` Django setting:
    
    * Set to ``'default'`` to return the default site as defined by
      ``SITE_ID``, or

    * set to ``'request'`` to return a case insensitive ``RequestSite``.
    """
    host = request.get_host().lower()
    # Try to retrieve the site object from cache.
    site = SITE_CACHE.get(host, None)
    if site is None:
        if Site._meta.installed:
            # Look for a site matching the full host name.
            matches = Site.objects.filter(domain__iexact=host)
            try:
                site = matches[0]
            except IndexError:
                # If no match was found and the host name contained a port, try
                # looking again for a site matching just the domain.
                if ':' in host:
                    domain = host.split(':', 1)[0]
                    matches = Site.objects.filter(domain__iexact=domain)
                    try:
                        site = matches[0]
                    except IndexError:
                        fallback = getattr(settings, 'SITES_FALLBACK', None)
                        if fallback == 'default':
                            site = Site.objects.get_current()
                        elif fallback == 'request':
                            site = models.CaseInsensitiveRequestSite(request)
        else:
            site = models.CaseInsensitiveRequestSite(request)
        # Save the site object in the cache for faster lookup next time.
        SITE_CACHE[host] = site
    return site
示例#8
0
def patched_get_site_by_id(self, site_id):
    """
    Monkey patched version of Django's SiteManager._get_site_by_id() function.

    Adds a configurable timeout to the in-memory SITE_CACHE for each cached Site.
    This allows for the use of an in-memory cache for Site models, avoiding one
    or more DB hits on every request made to the Django application, but also allows
    for changes made to models associated with the Site model and accessed via the
    Site model's relationship accessors to take effect without having to manual
    recycle all Django worker processes active in an application environment.
    """
    now = timezone.now()
    site = SITE_CACHE.get(site_id)
    cache_timeout = SITE_CACHE_TIMEOUTS.get(site_id, now)
    if not site or cache_timeout <= now:
        site = self.get(pk=site_id)
        SITE_CACHE[site_id] = site
        SITE_CACHE_TIMEOUTS[site_id] = now + get_site_cache_ttl()
    return SITE_CACHE[site_id]
示例#9
0
 def save(self, *args, **kwargs):
     SITE_CACHE.pop(self.site.domain, None)
     return super(SiteTheme, self).save(*args, **kwargs)
示例#10
0
 def delete(self, using=None):
     SITE_CACHE.pop(self.site.domain, None)
     return super(SiteTheme, self).delete(using=using)
示例#11
0
 def save(self, *args, **kwargs):
     SITE_CACHE.pop(self.site.domain, None)
     return super(SiteTheme, self).save(*args, **kwargs)
示例#12
0
 def delete(self, using=None):
     SITE_CACHE.pop(self.site.domain, None)
     return super(SiteTheme, self).delete(using=using)