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.apps.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 apps.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] = cache[f.attname] = (f, model, True, True) for f, model in self.get_fields_with_model(): cache[f.name] = cache[f.attname] = (f, model, True, False) for f in self.virtual_fields: if hasattr(f, 'related'): cache[f.name] = cache[f.attname] = ( f.related, None if f.model == self.model else f.model, True, False) if apps.ready(): self._name_map = cache return cache
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 apps.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