示例#1
0
文件: options.py 项目: lapbay/milan
class Options(object):
	def __init__(self, meta, app_label=None):
		self.local_fields, self.local_many_to_many = [], []
		self.virtual_fields = []
		self.module_name, self.verbose_name = None, None
		self.verbose_name_plural = None
		self.db_table = ''
		self.ordering = []
		self.unique_together =  []
		self.permissions =  []
		self.object_name, self.app_label = None, app_label
		self.get_latest_by = None
		self.order_with_respect_to = None
		self.db_tablespace = settings.DEFAULT_TABLESPACE
		self.admin = None
		self.meta = meta
		self.pk = None
		self.has_auto_field, self.auto_field = False, None
		self.abstract = False
		self.managed = True
		self.proxy = False
		self.proxy_for_model = None
		self.parents = SortedDict()
		self.duplicate_targets = {}
		self.auto_created = False

		# To handle various inheritance situations, we need to track where
		# managers came from (concrete or abstract base classes).
		self.abstract_managers = []
		self.concrete_managers = []

		# List of all lookups defined in ForeignKey 'limit_choices_to' options
		# from *other* models. Needed for some admin checks. Internal use only.
		self.related_fkey_lookups = []

	def contribute_to_class(self, cls, name):
		from django.db import connection
		from django.db.backends.util import truncate_name

		cls._meta = self
		self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
		# First, construct the default values for these options.
		self.object_name = cls.__name__
		self.module_name = self.object_name.lower()
		self.verbose_name = get_verbose_name(self.object_name)

		# Next, apply any overridden values from 'class Meta'.
		if self.meta:
			meta_attrs = self.meta.__dict__.copy()
			for name in self.meta.__dict__:
				# Ignore any private attributes that Django doesn't care about.
				# NOTE: We can't modify a dictionary's contents while looping
				# over it, so we loop over the *original* dictionary instead.
				if name.startswith('_'):
					del meta_attrs[name]
			for attr_name in DEFAULT_NAMES:
				if attr_name in meta_attrs:
					setattr(self, attr_name, meta_attrs.pop(attr_name))
				elif hasattr(self.meta, attr_name):
					setattr(self, attr_name, getattr(self.meta, attr_name))

			# unique_together can be either a tuple of tuples, or a single
			# tuple of two strings. Normalize it to a tuple of tuples, so that
			# calling code can uniformly expect that.
			ut = meta_attrs.pop('unique_together', self.unique_together)
			if ut and not isinstance(ut[0], (tuple, list)):
				ut = (ut,)
			self.unique_together = ut

			# verbose_name_plural is a special case because it uses a 's'
			# by default.
			if self.verbose_name_plural is None:
				self.verbose_name_plural = string_concat(self.verbose_name, 's')

			# Any leftover attributes must be invalid.
			if meta_attrs != {}:
				raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
		else:
			self.verbose_name_plural = string_concat(self.verbose_name, 's')
		del self.meta

		# If the db_table wasn't provided, use the app_label + module_name.
		if not self.db_table:
			self.db_table = "%s_%s" % (self.app_label, self.module_name)
			self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

	def _prepare(self, model):
		if self.order_with_respect_to:
			self.order_with_respect_to = self.get_field(self.order_with_respect_to)
			self.ordering = ('_order',)
			model.add_to_class('_order', OrderWrt())
		else:
			self.order_with_respect_to = None

		if self.pk is None:
			if self.parents:
				# Promote the first parent link in lieu of adding yet another
				# field.
				field = self.parents.value_for_index(0)
				# Look for a local field with the same name as the
				# first parent link. If a local field has already been
				# created, use it instead of promoting the parent
				already_created = [fld for fld in self.local_fields if fld.name == field.name]
				if already_created:
					field = already_created[0]
				field.primary_key = True
				self.setup_pk(field)
			else:
				auto = AutoField(verbose_name='ID', primary_key=True,
						auto_created=True)
				model.add_to_class('id', auto)

		# Determine any sets of fields that are pointing to the same targets
		# (e.g. two ForeignKeys to the same remote model). The query
		# construction code needs to know this. At the end of this,
		# self.duplicate_targets will map each duplicate field column to the
		# columns it duplicates.
		collections = {}
		for column, target in self.duplicate_targets.iteritems():
			try:
				collections[target].add(column)
			except KeyError:
				collections[target] = set([column])
		self.duplicate_targets = {}
		for elt in collections.itervalues():
			if len(elt) == 1:
				continue
			for column in elt:
				self.duplicate_targets[column] = elt.difference(set([column]))

	def add_field(self, field):
		# Insert the given field in the order in which it was created, using
		# the "creation_counter" attribute of the field.
		# Move many-to-many related fields from self.fields into
		# self.many_to_many.
		if field.rel and isinstance(field.rel, ManyToManyRel):
			self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field)
			if hasattr(self, '_m2m_cache'):
				del self._m2m_cache
		else:
			self.local_fields.insert(bisect(self.local_fields, field), field)
			self.setup_pk(field)
			if hasattr(self, '_field_cache'):
				del self._field_cache
				del self._field_name_cache

		if hasattr(self, '_name_map'):
			del self._name_map

	def add_virtual_field(self, field):
		self.virtual_fields.append(field)

	def setup_pk(self, field):
		if not self.pk and field.primary_key:
			self.pk = field
			field.serialize = False

	def setup_proxy(self, target):
		"""
		Does the internal setup so that the current model is a proxy for
		"target".
		"""
		self.pk = target._meta.pk
		self.proxy_for_model = target
		self.db_table = target._meta.db_table

	def __repr__(self):
		return '<Options for %s>' % self.object_name

	def __str__(self):
		return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))

	def verbose_name_raw(self):
		"""
		There are a few places where the untranslated verbose name is needed
		(so that we get the same value regardless of currently active
		locale).
		"""
		lang = get_language()
		deactivate_all()
		raw = force_unicode(self.verbose_name)
		activate(lang)
		return raw
	verbose_name_raw = property(verbose_name_raw)

	def _fields(self):
		"""
		The getter for self.fields. This returns the list of field objects
		available to this model (including through parent models).

		Callers are not permitted to modify this list, since it's a reference
		to this instance (not a copy).
		"""
		try:
			self._field_name_cache
		except AttributeError:
			self._fill_fields_cache()
		return self._field_name_cache
	fields = property(_fields)

	def get_fields_with_model(self):
		"""
		Returns a sequence of (field, model) pairs for all fields. The "model"
		element is None for fields on the current model. Mostly of use when
		constructing queries so that we know which model a field belongs to.
		"""
		try:
			self._field_cache
		except AttributeError:
			self._fill_fields_cache()
		return self._field_cache

	def _fill_fields_cache(self):
		cache = []
		for parent in self.parents:
			for field, model in parent._meta.get_fields_with_model():
				if model:
					cache.append((field, model))
				else:
					cache.append((field, parent))
		cache.extend([(f, None) for f in self.local_fields])
		self._field_cache = tuple(cache)
		self._field_name_cache = [x for x, _ in cache]

	def _many_to_many(self):
		try:
			self._m2m_cache
		except AttributeError:
			self._fill_m2m_cache()
		return self._m2m_cache.keys()
	many_to_many = property(_many_to_many)

	def get_m2m_with_model(self):
		"""
		The many-to-many version of get_fields_with_model().
		"""
		try:
			self._m2m_cache
		except AttributeError:
			self._fill_m2m_cache()
		return self._m2m_cache.items()

	def _fill_m2m_cache(self):
		cache = SortedDict()
		for parent in self.parents:
			for field, model in parent._meta.get_m2m_with_model():
				if model:
					cache[field] = model
				else:
					cache[field] = parent
		for field in self.local_many_to_many:
			cache[field] = None
		self._m2m_cache = cache

	def get_field(self, name, many_to_many=True):
		"""
		Returns the requested field by name. Raises FieldDoesNotExist on error.
		"""
		to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
		for f in to_search:
			if f.name == name:
				return f
		raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, name))

	def get_field_by_name(self, name):
		"""
		Returns the (field_object, model, direct, m2m), where field_object is
		the Field instance for the given name, model is the model containing
		this field (None for local fields), direct is True if the field exists
		on this model, and m2m is True for many-to-many relations. When
		'direct' is False, 'field_object' is the corresponding RelatedObject
		for this field (since the field doesn't have an instance associated
		with it).

		Uses a cache internally, so after the first access, this is very fast.
		"""
		try:
			try:
				return self._name_map[name]
			except AttributeError:
				cache = self.init_name_map()
				return cache[name]
		except KeyError:
			raise FieldDoesNotExist('%s has no field named %r'
					% (self.object_name, name))

	def get_all_field_names(self):
		"""
		Returns a list of all field names that are possible for this model
		(including reverse relation names). This is used for pretty printing
		debugging output (a list of choices), so any internal-only field names
		are not included.
		"""
		try:
			cache = self._name_map
		except AttributeError:
			cache = self.init_name_map()
		names = cache.keys()
		names.sort()
		# Internal-only names end with "+" (symmetrical m2m related names being
		# the main example). Trim them.
		return [val for val in names if not val.endswith('+')]

	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 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()

	def get_all_related_objects(self, local_only=False, include_hidden=False):
		return [k for k, v in self.get_all_related_objects_with_model(
				local_only=local_only, include_hidden=include_hidden)]

	def get_all_related_objects_with_model(self, local_only=False,
										   include_hidden=False):
		"""
		Returns a list of (related-object, model) pairs. Similar to
		get_fields_with_model().
		"""
		try:
			self._related_objects_cache
		except AttributeError:
			self._fill_related_objects_cache()
		predicates = []
		if local_only:
			predicates.append(lambda k, v: not v)
		if not include_hidden:
			predicates.append(lambda k, v: not k.field.rel.is_hidden())
		return filter(lambda t: all([p(*t) for p in predicates]),
					  self._related_objects_cache.items())

	def _fill_related_objects_cache(self):
		cache = SortedDict()
		parent_list = self.get_parent_list()
		for parent in self.parents:
			for obj, model in parent._meta.get_all_related_objects_with_model(include_hidden=True):
				if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) and obj.model not in parent_list:
					continue
				if not model:
					cache[obj] = parent
				else:
					cache[obj] = model
		for klass in get_models(include_auto_created=True):
			for f in klass._meta.local_fields:
				if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
					cache[RelatedObject(f.rel.to, klass, f)] = None
		self._related_objects_cache = cache

	def get_all_related_many_to_many_objects(self, local_only=False):
		try:
			cache = self._related_many_to_many_cache
		except AttributeError:
			cache = self._fill_related_many_to_many_cache()
		if local_only:
			return [k for k, v in cache.items() if not v]
		return cache.keys()

	def get_all_related_m2m_objects_with_model(self):
		"""
		Returns a list of (related-m2m-object, model) pairs. Similar to
		get_fields_with_model().
		"""
		try:
			cache = self._related_many_to_many_cache
		except AttributeError:
			cache = self._fill_related_many_to_many_cache()
		return cache.items()

	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 get_base_chain(self, model):
		"""
		Returns a list of parent classes leading to 'model' (order from closet
		to most distant ancestor). This has to handle the case were 'model' is
		a granparent or even more distant relation.
		"""
		if not self.parents:
			return
		if model in self.parents:
			return [model]
		for parent in self.parents:
			res = parent._meta.get_base_chain(model)
			if res:
				res.insert(0, parent)
				return res
		raise TypeError('%r is not an ancestor of this model'
				% model._meta.module_name)

	def get_parent_list(self):
		"""
		Returns a list of all the ancestor of this model as a list. Useful for
		determining if something is an ancestor, regardless of lineage.
		"""
		result = set()
		for parent in self.parents:
			result.add(parent)
			result.update(parent._meta.get_parent_list())
		return result

	def get_ancestor_link(self, ancestor):
		"""
		Returns the field on the current model which points to the given
		"ancestor". This is possible an indirect link (a pointer to a parent
		model, which points, eventually, to the ancestor). Used when
		constructing table joins for model inheritance.

		Returns None if the model isn't an ancestor of this one.
		"""
		if ancestor in self.parents:
			return self.parents[ancestor]
		for parent in self.parents:
			# Tries to get a link field from the immediate parent
			parent_link = parent._meta.get_ancestor_link(ancestor)
			if parent_link:
				# In case of a proxied model, the first link
				# of the chain to the ancestor is that parent
				# links
				return self.parents[parent] or parent_link

	def get_ordered_objects(self):
		"Returns a list of Options objects that are ordered with respect to this object."
		if not hasattr(self, '_ordered_objects'):
			objects = []
			# TODO
			#for klass in get_models(get_app(self.app_label)):
			#	opts = klass._meta
			#	if opts.order_with_respect_to and opts.order_with_respect_to.rel \
			#		and self == opts.order_with_respect_to.rel.to._meta:
			#		objects.append(opts)
			self._ordered_objects = objects
		return self._ordered_objects

	def pk_index(self):
		"""
		Returns the index of the primary key field in the self.fields list.
		"""
		return self.fields.index(self.pk)
示例#2
0
 def test_value_for_index(self):
     d = SortedDict({"a": 3})
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         self.assertEqual(d.value_for_index(0), 3)
     assert w[0].category is DeprecationWarning
示例#3
0
class Options(object):
    def __init__(self, meta, app_label=None):
        self.local_fields, self.local_many_to_many = [], []
        self.virtual_fields = []
        self.module_name, self.verbose_name = None, None
        self.verbose_name_plural = None
        self.db_table = ''
        self.ordering = []
        self.unique_together =  []
        self.permissions =  []
        self.object_name, self.app_label = None, app_label
        self.get_latest_by = None
        self.order_with_respect_to = None
        self.db_tablespace = settings.DEFAULT_TABLESPACE
        self.admin = None
        self.meta = meta
        self.pk = None
        self.has_auto_field, self.auto_field = False, None
        self.one_to_one_field = None
        self.abstract = False
        self.parents = SortedDict()
        self.duplicate_targets = {}
        # Managers that have been inherited from abstract base classes. These
        # are passed onto any children.
        self.abstract_managers = []

    def contribute_to_class(self, cls, name):
        from django.db import connection
        from django.db.backends.util import truncate_name

        cls._meta = self
        self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
        # First, construct the default values for these options.
        self.object_name = cls.__name__
        self.module_name = self.object_name.lower()
        self.verbose_name = get_verbose_name(self.object_name)

        # Next, apply any overridden values from 'class Meta'.
        if self.meta:
            meta_attrs = self.meta.__dict__.copy()
            for name in self.meta.__dict__:
                # Ignore any private attributes that Django doesn't care about.
                # NOTE: We can't modify a dictionary's contents while looping
                # over it, so we loop over the *original* dictionary instead.
                if name.startswith('_'):
                    del meta_attrs[name]
            for attr_name in DEFAULT_NAMES:
                if attr_name in meta_attrs:
                    setattr(self, attr_name, meta_attrs.pop(attr_name))
                elif hasattr(self.meta, attr_name):
                    setattr(self, attr_name, getattr(self.meta, attr_name))

            # unique_together can be either a tuple of tuples, or a single
            # tuple of two strings. Normalize it to a tuple of tuples, so that
            # calling code can uniformly expect that.
            ut = meta_attrs.pop('unique_together', getattr(self, 'unique_together'))
            if ut and not isinstance(ut[0], (tuple, list)):
                ut = (ut,)
            setattr(self, 'unique_together', ut)

            # verbose_name_plural is a special case because it uses a 's'
            # by default.
            setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))

            # Any leftover attributes must be invalid.
            if meta_attrs != {}:
                raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())
        else:
            self.verbose_name_plural = string_concat(self.verbose_name, 's')
        del self.meta

        # If the db_table wasn't provided, use the app_label + module_name.
        if not self.db_table:
            self.db_table = "%s_%s" % (self.app_label, self.module_name)
            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())


    def _prepare(self, model):
        if self.order_with_respect_to:
            self.order_with_respect_to = self.get_field(self.order_with_respect_to)
            self.ordering = ('_order',)
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote the first parent link in lieu of adding yet another
                # field.
                field = self.parents.value_for_index(0)
                field.primary_key = True
                self.setup_pk(field)
            else:
                auto = AutoField(verbose_name='ID', primary_key=True,
                        auto_created=True)
                model.add_to_class('id', auto)

        # Determine any sets of fields that are pointing to the same targets
        # (e.g. two ForeignKeys to the same remote model). The query
        # construction code needs to know this. At the end of this,
        # self.duplicate_targets will map each duplicate field column to the
        # columns it duplicates.
        collections = {}
        for column, target in self.duplicate_targets.iteritems():
            try:
                collections[target].add(column)
            except KeyError:
                collections[target] = set([column])
        self.duplicate_targets = {}
        for elt in collections.itervalues():
            if len(elt) == 1:
                continue
            for column in elt:
                self.duplicate_targets[column] = elt.difference(set([column]))

    def add_field(self, field):
        # Insert the given field in the order in which it was created, using
        # the "creation_counter" attribute of the field.
        # Move many-to-many related fields from self.fields into
        # self.many_to_many.
        if field.rel and isinstance(field.rel, ManyToManyRel):
            self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field)
            if hasattr(self, '_m2m_cache'):
                del self._m2m_cache
        else:
            self.local_fields.insert(bisect(self.local_fields, field), field)
            self.setup_pk(field)
            if hasattr(self, '_field_cache'):
                del self._field_cache
                del self._field_name_cache

        if hasattr(self, '_name_map'):
            del self._name_map

    def add_virtual_field(self, field):
        self.virtual_fields.append(field)

    def setup_pk(self, field):
        if not self.pk and field.primary_key:
            self.pk = field
            field.serialize = False

    def __repr__(self):
        return '<Options for %s>' % self.object_name

    def __str__(self):
        return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))

    def verbose_name_raw(self):
        """
        There are a few places where the untranslated verbose name is needed
        (so that we get the same value regardless of currently active
        locale).
        """
        lang = get_language()
        deactivate_all()
        raw = force_unicode(self.verbose_name)
        activate(lang)
        return raw
    verbose_name_raw = property(verbose_name_raw)

    def _fields(self):
        """
        The getter for self.fields. This returns the list of field objects
        available to this model (including through parent models).

        Callers are not permitted to modify this list, since it's a reference
        to this instance (not a copy).
        """
        try:
            self._field_name_cache
        except AttributeError:
            self._fill_fields_cache()
        return self._field_name_cache
    fields = property(_fields)

    def get_fields_with_model(self):
        """
        Returns a sequence of (field, model) pairs for all fields. The "model"
        element is None for fields on the current model. Mostly of use when
        constructing queries so that we know which model a field belongs to.
        """
        try:
            self._field_cache
        except AttributeError:
            self._fill_fields_cache()
        return self._field_cache

    def _fill_fields_cache(self):
        cache = []
        for parent in self.parents:
            for field, model in parent._meta.get_fields_with_model():
                if model:
                    cache.append((field, model))
                else:
                    cache.append((field, parent))
        cache.extend([(f, None) for f in self.local_fields])
        self._field_cache = tuple(cache)
        self._field_name_cache = [x for x, _ in cache]

    def _many_to_many(self):
        try:
            self._m2m_cache
        except AttributeError:
            self._fill_m2m_cache()
        return self._m2m_cache.keys()
    many_to_many = property(_many_to_many)

    def get_m2m_with_model(self):
        """
        The many-to-many version of get_fields_with_model().
        """
        try:
            self._m2m_cache
        except AttributeError:
            self._fill_m2m_cache()
        return self._m2m_cache.items()

    def _fill_m2m_cache(self):
        cache = SortedDict()
        for parent in self.parents:
            for field, model in parent._meta.get_m2m_with_model():
                if model:
                    cache[field] = model
                else:
                    cache[field] = parent
        for field in self.local_many_to_many:
            cache[field] = None
        self._m2m_cache = cache

    def get_field(self, name, many_to_many=True):
        """
        Returns the requested field by name. Raises FieldDoesNotExist on error.
        """
        to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
        for f in to_search:
            if f.name == name:
                return f
        raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name)

    def get_field_by_name(self, name):
        """
        Returns the (field_object, model, direct, m2m), where field_object is
        the Field instance for the given name, model is the model containing
        this field (None for local fields), direct is True if the field exists
        on this model, and m2m is True for many-to-many relations. When
        'direct' is False, 'field_object' is the corresponding RelatedObject
        for this field (since the field doesn't have an instance associated
        with it).

        Uses a cache internally, so after the first access, this is very fast.
        """
        try:
            try:
                return self._name_map[name]
            except AttributeError:
                cache = self.init_name_map()
                return cache[name]
        except KeyError:
            raise FieldDoesNotExist('%s has no field named %r'
                    % (self.object_name, name))

    def get_all_field_names(self):
        """
        Returns a list of all field names that are possible for this model
        (including reverse relation names). This is used for pretty printing
        debugging output (a list of choices), so any internal-only field names
        are not included.
        """
        try:
            cache = self._name_map
        except AttributeError:
            cache = self.init_name_map()
        names = cache.keys()
        names.sort()
        # Internal-only names end with "+" (symmetrical m2m related names being
        # the main example). Trim them.
        return [val for val in names if not val.endswith('+')]

    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 self.order_with_respect_to:
            cache['_order'] = OrderWrt(), None, 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()

    def get_all_related_objects(self, local_only=False):
        try:
            self._related_objects_cache
        except AttributeError:
            self._fill_related_objects_cache()
        if local_only:
            return [k for k, v in self._related_objects_cache.items() if not v]
        return self._related_objects_cache.keys()

    def get_all_related_objects_with_model(self):
        """
        Returns a list of (related-object, model) pairs. Similar to
        get_fields_with_model().
        """
        try:
            self._related_objects_cache
        except AttributeError:
            self._fill_related_objects_cache()
        return self._related_objects_cache.items()

    def _fill_related_objects_cache(self):
        cache = SortedDict()
        parent_list = self.get_parent_list()
        for parent in self.parents:
            for obj, model in parent._meta.get_all_related_objects_with_model():
                if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) 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_fields:
                if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
                    cache[RelatedObject(f.rel.to, klass, f)] = None
        self._related_objects_cache = cache

    def get_all_related_many_to_many_objects(self, local_only=False):
        try:
            cache = self._related_many_to_many_cache
        except AttributeError:
            cache = self._fill_related_many_to_many_cache()
        if local_only:
            return [k for k, v in cache.items() if not v]
        return cache.keys()

    def get_all_related_m2m_objects_with_model(self):
        """
        Returns a list of (related-m2m-object, model) pairs. Similar to
        get_fields_with_model().
        """
        try:
            cache = self._related_many_to_many_cache
        except AttributeError:
            cache = self._fill_related_many_to_many_cache()
        return cache.items()

    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 get_base_chain(self, model):
        """
        Returns a list of parent classes leading to 'model' (order from closet
        to most distant ancestor). This has to handle the case were 'model' is
        a granparent or even more distant relation.
        """
        if not self.parents:
            return
        if model in self.parents:
            return [model]
        for parent in self.parents:
            res = parent._meta.get_base_chain(model)
            if res:
                res.insert(0, parent)
                return res
        raise TypeError('%r is not an ancestor of this model'
                % model._meta.module_name)

    def get_parent_list(self):
        """
        Returns a list of all the ancestor of this model as a list. Useful for
        determining if something is an ancestor, regardless of lineage.
        """
        result = set()
        for parent in self.parents:
            result.add(parent)
            result.update(parent._meta.get_parent_list())
        return result

    def get_ancestor_link(self, ancestor):
        """
        Returns the field on the current model which points to the given
        "ancestor". This is possible an indirect link (a pointer to a parent
        model, which points, eventually, to the ancestor). Used when
        constructing table joins for model inheritance.

        Returns None if the model isn't an ancestor of this one.
        """
        if ancestor in self.parents:
            return self.parents[ancestor]
        for parent in self.parents:
            if parent._meta.get_ancestor_link(ancestor):
                return self.parents[parent]

    def get_ordered_objects(self):
        "Returns a list of Options objects that are ordered with respect to this object."
        if not hasattr(self, '_ordered_objects'):
            objects = []
            # TODO
            #for klass in get_models(get_app(self.app_label)):
            #    opts = klass._meta
            #    if opts.order_with_respect_to and opts.order_with_respect_to.rel \
            #        and self == opts.order_with_respect_to.rel.to._meta:
            #        objects.append(opts)
            self._ordered_objects = objects
        return self._ordered_objects
示例#4
0
文件: options.py 项目: oloed/talk.org
class Options(object):
    def __init__(self, meta):
        self.local_fields, self.local_many_to_many = [], []
        self.module_name, self.verbose_name = None, None
        self.verbose_name_plural = None
        self.db_table = ''
        self.ordering = []
        self.unique_together =  []
        self.permissions =  []
        self.object_name, self.app_label = None, None
        self.get_latest_by = None
        self.order_with_respect_to = None
        self.db_tablespace = settings.DEFAULT_TABLESPACE
        self.admin = None
        self.meta = meta
        self.pk = None
        self.has_auto_field, self.auto_field = False, None
        self.one_to_one_field = None
        self.abstract = False
        self.parents = SortedDict()

    def contribute_to_class(self, cls, name):
        cls._meta = self
        self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
        # First, construct the default values for these options.
        self.object_name = cls.__name__
        self.module_name = self.object_name.lower()
        self.verbose_name = get_verbose_name(self.object_name)

        # Next, apply any overridden values from 'class Meta'.
        if self.meta:
            meta_attrs = self.meta.__dict__.copy()
            del meta_attrs['__module__']
            del meta_attrs['__doc__']
            for attr_name in DEFAULT_NAMES:
                if attr_name in meta_attrs:
                    setattr(self, attr_name, meta_attrs.pop(attr_name))
                elif hasattr(self.meta, attr_name):
                    setattr(self, attr_name, getattr(self.meta, attr_name))

            # unique_together can be either a tuple of tuples, or a single
            # tuple of two strings. Normalize it to a tuple of tuples, so that
            # calling code can uniformly expect that.
            ut = meta_attrs.pop('unique_together', getattr(self, 'unique_together'))
            if ut and not isinstance(ut[0], (tuple, list)):
                ut = (ut,)
            setattr(self, 'unique_together', ut)

            # verbose_name_plural is a special case because it uses a 's'
            # by default.
            setattr(self, 'verbose_name_plural', meta_attrs.pop('verbose_name_plural', string_concat(self.verbose_name, 's')))

            # Any leftover attributes must be invalid.
            if meta_attrs != {}:
                raise TypeError, "'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())
        else:
            self.verbose_name_plural = string_concat(self.verbose_name, 's')
        del self.meta

    def _prepare(self, model):
        from django.db import connection
        from django.db.backends.util import truncate_name
        if self.order_with_respect_to:
            self.order_with_respect_to = self.get_field(self.order_with_respect_to)
            self.ordering = ('_order',)
        else:
            self.order_with_respect_to = None

        if self.pk is None:
            if self.parents:
                # Promote the first parent link in lieu of adding yet another
                # field.
                field = self.parents.value_for_index(0)
                field.primary_key = True
                self.pk = field
            else:
                auto = AutoField(verbose_name='ID', primary_key=True,
                        auto_created=True)
                model.add_to_class('id', auto)

        # If the db_table wasn't provided, use the app_label + module_name.
        if not self.db_table:
            self.db_table = "%s_%s" % (self.app_label, self.module_name)
            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

    def add_field(self, field):
        # Insert the given field in the order in which it was created, using
        # the "creation_counter" attribute of the field.
        # Move many-to-many related fields from self.fields into
        # self.many_to_many.
        if field.rel and isinstance(field.rel, ManyToManyRel):
            self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field)
            if hasattr(self, '_m2m_cache'):
                del self._m2m_cache
        else:
            self.local_fields.insert(bisect(self.local_fields, field), field)
            self.setup_pk(field)
            if hasattr(self, '_field_cache'):
                del self._field_cache
                del self._field_name_cache

        if hasattr(self, '_name_map'):
            del self._name_map

    def setup_pk(self, field):
        if not self.pk and field.primary_key:
            self.pk = field
            field.serialize = False

    def __repr__(self):
        return '<Options for %s>' % self.object_name

    def __str__(self):
        return "%s.%s" % (smart_str(self.app_label), smart_str(self.module_name))

    def verbose_name_raw(self):
        """
        There are a few places where the untranslated verbose name is needed
        (so that we get the same value regardless of currently active
        locale).
        """
        lang = get_language()
        deactivate_all()
        raw = force_unicode(self.verbose_name)
        activate(lang)
        return raw
    verbose_name_raw = property(verbose_name_raw)

    def _fields(self):
        """
        The getter for self.fields. This returns the list of field objects
        available to this model (including through parent models).

        Callers are not permitted to modify this list, since it's a reference
        to this instance (not a copy).
        """
        try:
            self._field_name_cache
        except AttributeError:
            self._fill_fields_cache()
        return self._field_name_cache
    fields = property(_fields)

    def get_fields_with_model(self):
        """
        Returns a sequence of (field, model) pairs for all fields. The "model"
        element is None for fields on the current model. Mostly of use when
        constructing queries so that we know which model a field belongs to.
        """
        try:
            self._field_cache
        except AttributeError:
            self._fill_fields_cache()
        return self._field_cache

    def _fill_fields_cache(self):
        cache = []
        for parent in self.parents:
            for field, model in parent._meta.get_fields_with_model():
                if model:
                    cache.append((field, model))
                else:
                    cache.append((field, parent))
        cache.extend([(f, None) for f in self.local_fields])
        self._field_cache = tuple(cache)
        self._field_name_cache = [x for x, _ in cache]

    def _many_to_many(self):
        try:
            self._m2m_cache
        except AttributeError:
            self._fill_m2m_cache()
        return self._m2m_cache.keys()
    many_to_many = property(_many_to_many)

    def get_m2m_with_model(self):
        """
        The many-to-many version of get_fields_with_model().
        """
        try:
            self._m2m_cache
        except AttributeError:
            self._fill_m2m_cache()
        return self._m2m_cache.items()

    def _fill_m2m_cache(self):
        cache = SortedDict()
        for parent in self.parents:
            for field, model in parent._meta.get_m2m_with_model():
                if model:
                    cache[field] = model
                else:
                    cache[field] = parent
        for field in self.local_many_to_many:
            cache[field] = None
        self._m2m_cache = cache

    def get_field(self, name, many_to_many=True):
        """
        Returns the requested field by name. Raises FieldDoesNotExist on error.
        """
        to_search = many_to_many and (self.fields + self.many_to_many) or self.fields
        for f in to_search:
            if f.name == name:
                return f
        raise FieldDoesNotExist, '%s has no field named %r' % (self.object_name, name)

    def get_field_by_name(self, name):
        """
        Returns the (field_object, model, direct, m2m), where field_object is
        the Field instance for the given name, model is the model containing
        this field (None for local fields), direct is True if the field exists
        on this model, and m2m is True for many-to-many relations. When
        'direct' is False, 'field_object' is the corresponding RelatedObject
        for this field (since the field doesn't have an instance associated
        with it).

        Uses a cache internally, so after the first access, this is very fast.
        """
        try:
            try:
                return self._name_map[name]
            except AttributeError:
                cache = self.init_name_map()
                return cache[name]
        except KeyError:
            raise FieldDoesNotExist('%s has no field named %r'
                    % (self.object_name, name))

    def get_all_field_names(self):
        """
        Returns a list of all field names that are possible for this model
        (including reverse relation names).
        """
        try:
            cache = self._name_map
        except AttributeError:
            cache = self.init_name_map()
        names = cache.keys()
        names.sort()
        return names

    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 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()

    def get_all_related_objects(self, local_only=False):
        try:
            self._related_objects_cache
        except AttributeError:
            self._fill_related_objects_cache()
        if local_only:
            return [k for k, v in self._related_objects_cache.items() if not v]
        return self._related_objects_cache.keys()

    def get_all_related_objects_with_model(self):
        """
        Returns a list of (related-object, model) pairs. Similar to
        get_fields_with_model().
        """
        try:
            self._related_objects_cache
        except AttributeError:
            self._fill_related_objects_cache()
        return self._related_objects_cache.items()

    def _fill_related_objects_cache(self):
        cache = SortedDict()
        parent_list = self.get_parent_list()
        for parent in self.parents:
            for obj, model in parent._meta.get_all_related_objects_with_model():
                if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) 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_fields:
                if f.rel and not isinstance(f.rel.to, str) and self == f.rel.to._meta:
                    cache[RelatedObject(f.rel.to, klass, f)] = None
        self._related_objects_cache = cache

    def get_all_related_many_to_many_objects(self, local_only=False):
        try:
            cache = self._related_many_to_many_cache
        except AttributeError:
            cache = self._fill_related_many_to_many_cache()
        if local_only:
            return [k for k, v in cache.items() if not v]
        return cache.keys()

    def get_all_related_m2m_objects_with_model(self):
        """
        Returns a list of (related-m2m-object, model) pairs. Similar to
        get_fields_with_model().
        """
        try:
            cache = self._related_many_to_many_cache
        except AttributeError:
            cache = self._fill_related_many_to_many_cache()
        return cache.items()

    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 get_followed_related_objects(self, follow=None):
        if follow == None:
            follow = self.get_follow()
        return [f for f in self.get_all_related_objects() if follow.get(f.name, None)]

    def get_data_holders(self, follow=None):
        if follow == None:
            follow = self.get_follow()
        return [f for f in self.fields + self.many_to_many + self.get_all_related_objects() if follow.get(f.name, None)]

    def get_follow(self, override=None):
        follow = {}
        for f in self.fields + self.many_to_many + self.get_all_related_objects():
            if override and f.name in override:
                child_override = override[f.name]
            else:
                child_override = None
            fol = f.get_follow(child_override)
            if fol != None:
                follow[f.name] = fol
        return follow

    def get_base_chain(self, model):
        """
        Returns a list of parent classes leading to 'model' (order from closet
        to most distant ancestor). This has to handle the case were 'model' is
        a granparent or even more distant relation.
        """
        if not self.parents:
            return
        if model in self.parents:
            return [model]
        for parent in self.parents:
            res = parent._meta.get_base_chain(model)
            if res:
                res.insert(0, parent)
                return res
        raise TypeError('%r is not an ancestor of this model'
                % model._meta.module_name)

    def get_parent_list(self):
        """
        Returns a list of all the ancestor of this model as a list. Useful for
        determining if something is an ancestor, regardless of lineage.
        """
        result = set()
        for parent in self.parents:
            result.add(parent)
            result.update(parent._meta.get_parent_list())
        return result

    def get_ordered_objects(self):
        "Returns a list of Options objects that are ordered with respect to this object."
        if not hasattr(self, '_ordered_objects'):
            objects = []
            # TODO
            #for klass in get_models(get_app(self.app_label)):
            #    opts = klass._meta
            #    if opts.order_with_respect_to and opts.order_with_respect_to.rel \
            #        and self == opts.order_with_respect_to.rel.to._meta:
            #        objects.append(opts)
            self._ordered_objects = objects
        return self._ordered_objects

    def has_field_type(self, field_type, follow=None):
        """
        Returns True if this object's admin form has at least one of the given
        field_type (e.g. FileField).
        """
        # TODO: follow
        if not hasattr(self, '_field_types'):
            self._field_types = {}
        if field_type not in self._field_types:
            try:
                # First check self.fields.
                for f in self.fields:
                    if isinstance(f, field_type):
                        raise StopIteration
                # Failing that, check related fields.
                for related in self.get_followed_related_objects(follow):
                    for f in related.opts.fields:
                        if isinstance(f, field_type):
                            raise StopIteration
            except StopIteration:
                self._field_types[field_type] = True
            else:
                self._field_types[field_type] = False
        return self._field_types[field_type]
示例#5
0
class BoundColumns(object):
    """
    Container for spawning :class:`.BoundColumn` objects.

    This is bound to a table and provides its :attr:`.Table.columns` property.
    It provides access to those columns in different ways (iterator,
    item-based, filtered and unfiltered etc), stuff that would not be possible
    with a simple iterator in the table class.

    A ``BoundColumns`` object is a container for holding
    ``BoundColumn`` objects. It provides methods that make accessing
    columns easier than if they were stored in a ``list`` or
    ``dict``. ``Columns`` has a similar API to a ``dict`` (it
    actually uses a ``SortedDict`` interally).

    At the moment you'll only come across this class when you access a
    :attr:`.Table.columns` property.

    :type table: :class:`.Table` object
    :param table: the table containing the columns
    """
    def __init__(self, table):
        self.table = table
        # ``self._columns`` attribute stores the bound columns (columns that
        # have a real name, )
        self._columns = SortedDict()

    def _spawn_columns(self):
        """
        (re)build the "_bound_columns" cache of :class:`.BoundColumn` objects
        (note that :attr:`.base_columns` might have changed since last time);
        creating :class:`.BoundColumn` instances can be costly, so we reuse
        existing ones.
        """
        columns = SortedDict()
        for name, column in self.table.base_columns.items():
            if name in self._columns:
                columns[name] = self._columns[name]
            else:
                columns[name] = BoundColumn(self.table, column, name)
        self._columns = columns

    def iternames(self):
        return (name for name, column in self.iteritems())

    def names(self):
        return list(self.iternames())

    def iterall(self):
        """
        Return an iterator that exposes all :class:`.BoundColumn` objects,
        regardless of visiblity or sortability.
        """
        return (column for name, column in self.iteritems())

    def all(self):
        return list(self.iterall())

    def iteritems(self):
        """
        Return an iterator of ``(name, column)`` pairs (where ``column`` is a
        :class:`.BoundColumn` object).
        """
        self._spawn_columns()
        if self.table.sequence:
            return ((x, self._columns[x]) for x in self.table.sequence)
        else:
            return self._columns.iteritems()

    def items(self):
        return list(self.iteritems())

    def itersortable(self):
        """
        Same as :meth:`.BoundColumns.all` but only returns sortable
        :class:`.BoundColumn` objects.

        This is useful in templates, where iterating over the full
        set and checking ``{% if column.sortable %}`` can be problematic in
        conjunction with e.g. ``{{ forloop.last }}`` (the last column might not
        be the actual last that is rendered).
        """
        return ifilter(lambda x: x.sortable, self.all())

    def sortable(self):
        return list(self.itersortable())

    def itervisible(self):
        """
        Same as :meth:`.sortable` but only returns visible
        :class:`.BoundColumn` objects.

        This is geared towards table rendering.
        """
        return ifilter(lambda x: x.visible, self.all())

    def visible(self):
        return list(self.itervisible())

    def __iter__(self):
        """
        Convenience API with identical functionality to :meth:`visible`.
        """
        return self.itervisible()

    def __contains__(self, item):
        """
        Check if a column is contained within a :class:`.Columns` object.

        *item* can either be a :class:`.BoundColumn` object, or the name of a
        column.
        """
        if isinstance(item, basestring):
            return item in self.iternames()
        else:
            # let's assume we were given a column
            return item in self.iterall()

    def __len__(self):
        """
        Return how many :class:`BoundColumn` objects are contained (and
        visible).
        """
        self._spawn_columns()
        return len(self.visible())

    def __getitem__(self, index):
        """
        Retrieve a specific :class:`BoundColumn` object.

        *index* can either be 0-indexed or the name of a column

        .. code-block:: python

            columns['speed']  # returns a bound column with name 'speed'
            columns[0]        # returns the first column
        """
        self._spawn_columns()
        if isinstance(index, int):
            return self._columns.value_for_index(index)
        elif isinstance(index, basestring):
            return self._columns[index]
        else:
            raise TypeError(u'row indices must be integers or str, not %s' %
                            index.__class__.__name__)
示例#6
0
 def test_value_for_index(self):
     d = SortedDict({"a": 3})
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         self.assertEqual(d.value_for_index(0), 3)
     assert w[0].category is DeprecationWarning