def find_setting(group, key): """Get a setting or longsetting by group and key, cache and return it.""" setting = None use_db, overrides = (True, {}) ck = cache_key('Setting', group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def new_init_name_map(meta): original_map = original_init_name_map(meta) updated_map = self.update_item_name_map(original_map, meta) if original_map != updated_map and app_cache_ready(): meta._name_map = updated_map return updated_map return original_map
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) if use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def ensure_completely_loaded(force=False): """ This method ensures all models are completely loaded FeinCMS requires Django to be completely initialized before proceeding, because of the extension mechanism and the dynamically created content types. For more informations, have a look at issue #23 on github: http://github.com/feincms/feincms/issues#issue/23 """ global COMPLETELY_LOADED if COMPLETELY_LOADED and not force: return True # Ensure meta information concerning related fields is up-to-date. # Upon accessing the related fields information from Model._meta, # the related fields are cached and never refreshed again (because # models and model relations are defined upon import time, if you # do not fumble around with models like we do in FeinCMS.) # # Here we flush the caches rather than actually _filling them so # that relations defined after all content types registrations # don't miss out. from django.db.models import loading for model in loading.get_models(): for cache_name in ( '_field_cache', '_field_name_cache', '_m2m_cache', '_related_objects_cache', '_related_many_to_many_cache', '_name_map'): try: delattr(model._meta, cache_name) except AttributeError: pass # Randomly call some cache filling methods # http://goo.gl/XNI2qz model._meta._fill_fields_cache() # Calls to get_models(...) are cached by the arguments used in the call. # This cache is normally cleared in loading.register_models(), but we # invalidate the get_models() cache, by calling get_models # above before all apps have loaded. (Django's load_app() doesn't clear the # get_models cache as it perhaps should). So instead we clear the # get_models cache again here. If we don't do this, Django 1.5 chokes on # a model validation error (Django 1.4 doesn't exhibit this problem). # See Issue #323 on github. if hasattr(loading, 'cache'): loading.cache._get_models_cache.clear() if hasattr(loading.app_cache_ready, '__call__'): if loading.app_cache_ready(): COMPLETELY_LOADED = True else: # TODO Django 1.7 offers us better ways of handling this, maybe. if loading.app_cache_ready: COMPLETELY_LOADED = True return True
def get_all_related_many_to_many_objects(self): try: # Try the cache first. return self._all_related_many_to_many_objects except AttributeError: rel_objs = [] for klass in get_models(): for f in klass._meta.many_to_many: if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: rel_objs.append(RelatedObject(f.rel.to, klass, f)) if app_cache_ready(): self._all_related_many_to_many_objects = rel_objs return rel_objs
def _register_document(document_cls): if app_cache_ready(): while document_cls: backend = document_cls._meta.get_backend() backend.register_document(document_cls) register_collection(document_cls) document_registered.send_robust(sender=document_cls._meta.collection, document=document_cls) if not _pending_registered_documents: break document_cls = _pending_registered_documents.pop() else: _pending_registered_documents.append(document_cls)
def by_host(host=None, id_only=False, recursion=False): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. - `recursion`: used to prevent an endless loop of calling this function """ site = None # if the host value wasn't passed in, take a look inside the request for data if not host: request = get_current_request() if request: # if the request object already has the site set, just return it now # and skip the intensive lookup - it's unnecessary! if hasattr(request, 'site'): # if the request.site value isn't of type Site, just return it, as the # developer using this app is doing something funky if not isinstance(request.site, Site): return request.site # otherwise, just return the id or site depending on what was requested return id_only and request.site.id or request.site else: host = request.get_host() if host: if app_cache_ready(): key = 'SITE%s' % (host,) # try to get the Site out of Django's cache site = cache.get(key) if not site: site = lookup(site, host, recursion, id_only) # if we finally have the Site, save it in the cache to prevent # the intensive lookup again! if site: cache.set(key, site) else: site = lookup(site, host, recursion, id_only) # was it an ID-only request? if so return the site ID only! if site and id_only: site = site.id return site
def init_name_map(self): """ Initialises the field name -> field object mapping. """ cache = dict([(f.name, (f, m, True, False)) for f, m in self.get_fields_with_model()]) for f, model in self.get_m2m_with_model(): cache[f.name] = (f, model, True, True) for f, model in self.get_all_related_m2m_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, True) for f, model in self.get_all_related_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, False) if self.order_with_respect_to: cache["_order"] = OrderWrt(), None, True, False if app_cache_ready(): self._name_map = cache return cache
def _fill_related_many_to_many_cache(self): cache = SortedDict() parent_list = self.get_parent_list() for parent in self.parents: for obj, model in parent._meta.get_all_related_m2m_objects_with_model(): if obj.field.creation_counter < 0 and obj.model not in parent_list: continue if not model: cache[obj] = parent else: cache[obj] = model for klass in get_models(): for f in klass._meta.local_many_to_many: if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta: cache[RelatedObject(f.rel.to, klass, f)] = None if app_cache_ready(): self._related_many_to_many_cache = cache return cache
def init_name_map(self): """ Initialises the field name -> field object mapping. """ cache = {} # We intentionally handle related m2m objects first so that symmetrical # m2m accessor names can be overridden, if necessary. for f, model in self.get_all_related_m2m_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, True) for f, model in self.get_all_related_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, False) for f, model in self.get_m2m_with_model(): cache[f.name] = (f, model, True, True) for f, model in self.get_fields_with_model(): cache[f.name] = (f, model, True, False) if app_cache_ready(): self._name_map = cache return cache
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError as nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting) else: grp = overrides.get(group, None) if grp and grp.has_key(key): val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) if not setting: raise SettingNotSet(key, cachekey=ck) return setting
def find(cls, group, key): """Get a setting from cache, if possible""" site_id = settings.SITE_ID ck = cache_key('Setting', site_id, group, key) try: setting = cache_get(ck) except NotCachedError: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=site_id, key__exact=key, group__exact=group) setting.cache_set() except Setting.DoesNotExist: raise SettingNotSet(key) else: raise SettingNotSet("App cache not loaded: %s" % key) return setting
def _fill_related_many_to_many_cache(self): cache = OrderedDict() parent_list = self.get_parent_list() for parent in self.parents: for obj, model in parent._meta.get_all_related_m2m_objects_with_model(): if obj.field.creation_counter < 0 and obj.model not in parent_list: continue if not model: cache[obj] = parent else: cache[obj] = model for klass in self.app_cache.get_models(only_installed=False): if not klass._meta.swapped: for f in klass._meta.local_many_to_many: if f.rel and not isinstance(f.rel.to, six.string_types) and self == f.rel.to._meta: cache[f.related] = None if app_cache_ready(): self._related_many_to_many_cache = cache return cache
def _fill_related_many_to_many_cache(self): cache = OrderedDict() parent_list = self.get_parent_list() for parent in self.parents: for obj, model in parent._meta.get_all_related_m2m_objects_with_model( ): if obj.field.creation_counter < 0 and obj.model not in parent_list: continue if not model: cache[obj] = parent else: cache[obj] = model for klass in self.app_cache.get_models(only_installed=False): if not klass._meta.swapped: for f in klass._meta.local_many_to_many: if (f.rel and not isinstance(f.rel.to, six.string_types) and self == f.rel.to._meta): cache[f.related] = None if app_cache_ready(): self._related_many_to_many_cache = cache return cache
def init_name_map(self): """ Initialises the field name -> field object mapping. """ from django.db.models.loading import app_cache_ready cache = {} # We intentionally handle related m2m objects first so that symmetrical # m2m accessor names can be overridden, if necessary. for f, model in self.get_all_related_m2m_objects_with_model(): try: cache[f.field.collection_name] = (f, model, False, True) except: pass for f, model in self.get_all_related_objects_with_model(): try: cache[f.field.collection_name] = (f, model, False, False) except: pass if app_cache_ready(): self._name_map = cache return cache
def find_setting(group, key, site=None): """Get a setting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) if use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: pass else: setting.cache_set()
def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" siteid = _safe_get_siteid(site) setting = None use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) grp = overrides.get(group, None) if grp and key in grp: val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) elif use_db: try: setting = cache_get(ck) except NotCachedError, nce: if loading.app_cache_ready(): try: setting = Setting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) except Setting.DoesNotExist: # maybe it is a "long setting" try: setting = LongSetting.objects.get( site__id__exact=siteid, key__exact=key, group__exact=group) except LongSetting.DoesNotExist: pass cache_set(ck, value=setting)
def by_host(host=None, id_only=False, recursion=False): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. - `recursion`: used to prevent an endless loop of calling this function """ site = None # if the host value wasn't passed in, take a look inside the request for data if not host: request = get_current_request() if request: # if the request object already has the site set, just return it now # and skip the intensive lookup - it's unnecessary! if hasattr(request, 'site'): # if the request.site value isn't of type Site, just return it, as the # developer using this app is doing something funky if not isinstance(request.site, Site): return request.site # otherwise, just return the id or site depending on what was requested return id_only and request.site.id or request.site else: host = request.get_host() if host: if app_cache_ready(): key = 'SITE%s' % (host,) # try to get the Site out of Django's cache site = cache.get(key) if not site: try: site = Site.objects.get(domain=host) except Site.DoesNotExist: # if the Site couldn't be found, strip the port off if it # exists and try again if host.find(":") > -1: try: # strip the port tmp = host.split(":")[0] site = Site.objects.get(domain=tmp) except Site.DoesNotExist: pass # if the Site still hasn't been found, add or remove the 'www.' # from the host and try with that. if not recursion and not site and getattr(settings, 'MULTIHOST_AUTO_WWW', True): if host.startswith('www.'): site = by_host(host=host[4:], id_only=id_only, recursion=True) else: site = by_host(host = 'www.%s' % host, id_only=id_only, recursion=True) # if we finally have the Site, save it in the cache to prevent # the intensive lookup again! if site: cache.set(key, site) # was it an ID-only request? if so return the site ID only! if site and id_only: site = site.id return site
def by_host(host=None, id_only=False, recursion=False): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. - `recursion`: used to prevent an endless loop of calling this function """ site = None # if the host value wasn't passed in, take a look inside the request for data if not host: request = get_current_request() if request: # if the request object already has the site set, just return it now # and skip the intensive lookup - it's unnecessary! if hasattr(request, 'site'): # if the request.site value isn't of type Site, just return it, as the # developer using this app is doing something funky if not isinstance(request.site, Site): return request.site # otherwise, just return the id or site depending on what was requested return id_only and request.site.id or request.site else: host = request.get_host() if host: if app_cache_ready(): key = 'SITE%s' % (host, ) # try to get the Site out of Django's cache site = cache.get(key) if not site: try: site = Site.objects.get(domain=host) except Site.DoesNotExist: # if the Site couldn't be found, strip the port off if it # exists and try again if host.find(":") > -1: try: # strip the port tmp = host.split(":")[0] site = Site.objects.get(domain=tmp) except Site.DoesNotExist: pass # if the Site still hasn't been found, add or remove the 'www.' # from the host and try with that. if not recursion and not site and getattr( settings, 'MULTIHOST_AUTO_WWW', True): if host.startswith('www.'): site = by_host(host=host[4:], id_only=id_only, recursion=True) else: site = by_host(host='www.%s' % host, id_only=id_only, recursion=True) # if we finally have the Site, save it in the cache to prevent # the intensive lookup again! if site: cache.set(key, site) # was it an ID-only request? if so return the site ID only! if site and id_only: site = site.id return site
def by_host(host=None, id_only=False, called_recursive=None): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. """ global _WARNED if id_only: site = -1 else: site = None debug_domain = None debug_target = None if settings.DEBUG: raw = get_threadlocal_setting('DEBUG_DOMAIN') if raw: parts = raw.split('=') if len(parts) == 2: debug_domain = parts[0] debug_target = parts[1] else: debug_domain = raw debug_target = 'com' if not host: request = threadlocals.get_current_request() if request: host = request.get_host() else: log.debug('No request') site = by_settings(id_only=id_only) if host: if app_cache_ready(): try: site = keyedcache.cache_get('SITE', host=host, id_only=id_only) if id_only: site = site.id except keyedcache.NotCachedError, nce: try: log.debug('looking up site by host: %s', host) site = Site.objects.get(domain=host) except Site.DoesNotExist: if host.find(":") > -1: try: # strip the port host = host.split(":")[0] site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if debug_domain and host.endswith(debug_domain): host = host[:-len(debug_domain)] + debug_target log.debug('Using debug domain: %s', host) try: site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if not site and get_threadlocal_setting('AUTO_WWW') and not called_recursive: if host.startswith('www'): log.debug('trying site lookup without www') site = by_host(host=host[4:], id_only=id_only, called_recursive=True) else: log.debug('trying site lookup with www') site = by_host(host = 'www.%s' % host, id_only=id_only, called_recursive=True) if site: keyedcache.cache_set(nce.key, value=site) if id_only: site = site.id else: if not host in _WARNED: log.warn("Site for '%s' is not configured on this server - add to sites in admin", host) _WARNED[host] = True site = by_settings(id_only=id_only) else: log.debug('app cache not ready') site = by_settings(id_only=id_only)
def by_host(host=None, id_only=False, called_recursive=None): """Get the current site by looking at the request stored in the thread. Returns the best match found in the `django.contrib.sites` app. If not found, then returns the default set as given in `settings.SITE_ID` Params: - `host`: optional, host to look up - `id_only`: if true, then do not retrieve the full site, just the id. """ global _WARNED if id_only: site = -1 else: site = None debug_domain = None debug_target = None if settings.DEBUG: raw = get_threadlocal_setting('DEBUG_DOMAIN') if raw: parts = raw.split('=') if len(parts) == 2: debug_domain = parts[0] debug_target = parts[1] else: debug_domain = raw debug_target = 'com' if not host: request = threadlocals.get_current_request() if request: host = request.get_host() else: log.debug('No request') site = by_settings(id_only=id_only) if host: if app_cache_ready(): try: site = keyedcache.cache_get('SITE', host=host, id_only=id_only) if id_only: site = site.id except keyedcache.NotCachedError, nce: try: log.debug('looking up site by host: %s', host) site = Site.objects.get(domain=host) except Site.DoesNotExist: if host.find(":") > -1: try: # strip the port host = host.split(":")[0] site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if debug_domain and host.endswith(debug_domain): host = host[:-len(debug_domain)] + debug_target log.debug('Using debug domain: %s', host) try: site = Site.objects.get(domain=host) except Site.DoesNotExist: pass if not site and get_threadlocal_setting( 'AUTO_WWW') and not called_recursive: if host.startswith('www'): log.debug('trying site lookup without www') site = by_host(host=host[4:], id_only=id_only, called_recursive=True) else: log.debug('trying site lookup with www') site = by_host(host='www.%s' % host, id_only=id_only, called_recursive=True) if site: keyedcache.cache_set(nce.key, value=site) if id_only: site = site.id else: if not host in _WARNED: log.warn( "Site for '%s' is not configured on this server - add to sites in admin", host) _WARNED[host] = True site = by_settings(id_only=id_only) else: log.debug('app cache not ready') site = by_settings(id_only=id_only)
def ensure_completely_loaded(force=False): """ This method ensures all models are completely loaded FeinCMS requires Django to be completely initialized before proceeding, because of the extension mechanism and the dynamically created content types. For more informations, have a look at issue #23 on github: http://github.com/feincms/feincms/issues#issue/23 """ global COMPLETELY_LOADED if COMPLETELY_LOADED and not force: return True # Ensure meta information concerning related fields is up-to-date. # Upon accessing the related fields information from Model._meta, # the related fields are cached and never refreshed again (because # models and model relations are defined upon import time, if you # do not fumble around with models like we do in FeinCMS.) # # Here we flush the caches rather than actually _filling them so # that relations defined after all content types registrations # don't miss out. from django.db.models import loading for model in loading.get_models(): for cache_name in ('_field_cache', '_field_name_cache', '_m2m_cache', '_related_objects_cache', '_related_many_to_many_cache', '_name_map'): try: delattr(model._meta, cache_name) except AttributeError: pass # Randomly call some cache filling methods # http://goo.gl/XNI2qz model._meta._fill_fields_cache() # Calls to get_models(...) are cached by the arguments used in the call. # This cache is normally cleared in loading.register_models(), but we # invalidate the get_models() cache, by calling get_models # above before all apps have loaded. (Django's load_app() doesn't clear the # get_models cache as it perhaps should). So instead we clear the # get_models cache again here. If we don't do this, Django 1.5 chokes on # a model validation error (Django 1.4 doesn't exhibit this problem). # See Issue #323 on github. if hasattr(loading, 'cache'): try: loading.cache.get_models.cache_clear() # Django 1.7+ except AttributeError: loading.cache._get_models_cache.clear() # Django 1.6- if hasattr(loading.app_cache_ready, '__call__'): if loading.app_cache_ready(): COMPLETELY_LOADED = True else: # TODO Django 1.7 offers us better ways of handling this, maybe. if loading.app_cache_ready: COMPLETELY_LOADED = True return True
# We intentionally handle related m2m objects first so that symmetrical # m2m accessor names can be overridden, if necessary. for f, model in self.get_all_related_m2m_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, True) for f, model in self.get_all_related_objects_with_model(): cache[f.field.related_query_name()] = (f, model, False, False) for f, model in self.get_m2m_with_model(): cache[f.name] = (f, model, True, True) for f, model in self.get_fields_with_model(): cache[f.name] = (f, model, True, False) ??? if app_cache_ready(): self._name_map = cache return cache def get_add_permission(self): return 'add_%s' % self.object_name.lower() def get_change_permission(self): return 'change_%s' % self.object_name.lower() def get_delete_permission(self): return 'delete_%s' % self.object_name.lower() ########################### 下面的六个方法给我的感觉是核心, 但现在还不知道作用在哪里 ???