def get_content_models(cls): """ Return all subclasses of the concrete model. """ concrete_model = base_concrete_model(ContentTyped, cls) return [ m for m in apps.get_models() if m is not concrete_model and issubclass(m, concrete_model) ]
def set_content_model(self): """ Set content_model to the child class's related name, or None if this is the base class. """ if not self.content_model: is_base_class = (base_concrete_model(ContentTyped, self) == self.__class__) self.content_model = (None if is_base_class else self.get_content_model_name())
def delete(self, *args, **kwargs): """ Update the ordering values for siblings. """ lookup = self.with_respect_to() lookup["_order__gte"] = self._order concrete_model = base_concrete_model(Orderable, self) after = concrete_model.objects.filter(**lookup) after.update(_order=models.F("_order") - 1) super(Orderable, self).delete(*args, **kwargs)
def save(self, *args, **kwargs): """ Set the initial ordering value. """ if self._order is None: lookup = self.with_respect_to() lookup["_order__isnull"] = False concrete_model = base_concrete_model(Orderable, self) self._order = concrete_model.objects.filter(**lookup).count() super(Orderable, self).save(*args, **kwargs)
def generate_unique_slug(self): """ Create a unique slug by passing the result of get_slug() to utils.urls.unique_slug, which appends an index if necessary. """ # For custom content types, use the ``Page`` instance for # slug lookup. concrete_model = base_concrete_model(Slugged, self) slug_qs = concrete_model.objects.exclude(id=self.id) return unique_slug(slug_qs, "slug", self.get_slug())
def _get_next_or_previous_by_order(self, is_next, **kwargs): """ Retrieves next or previous object by order. We implement our own version instead of Django's so we can hook into the published manager, concrete subclasses and our custom ``with_respect_to`` method. """ lookup = self.with_respect_to() lookup["_order"] = self._order + (1 if is_next else -1) concrete_model = base_concrete_model(Orderable, self) try: queryset = concrete_model.objects.published except AttributeError: queryset = concrete_model.objects.filter try: return queryset(**kwargs).get(**lookup) except concrete_model.DoesNotExist: pass
def _get_next_or_previous_by_publish_date(self, is_next, **kwargs): """ Retrieves next or previous object by publish date. We implement our own version instead of Django's so we can hook into the published manager and concrete subclasses. """ arg = "publish_date__gt" if is_next else "publish_date__lt" order = "publish_date" if is_next else "-publish_date" lookup = {arg: self.publish_date} concrete_model = base_concrete_model(Displayable, self) try: queryset = concrete_model.objects.published except AttributeError: queryset = concrete_model.objects.all try: return queryset(**kwargs).filter(**lookup).order_by(order)[0] except IndexError: pass
def __init__(self, *args, **kwargs): """ For subclasses that are registered with an Admin class that doesn't implement fieldsets, add any extra model fields to this instance's fieldsets. This mimics Django's behaviour of adding all model fields when no fieldsets are defined on the Admin class. """ super(ContentTypedAdmin, self).__init__(*args, **kwargs) self.concrete_model = base_concrete_model(ContentTyped, self.model) # Test that the fieldsets don't differ from the concrete admin's. if (self.model is not self.concrete_model and self.fieldsets == self.base_concrete_modeladmin.fieldsets): # Make a copy so that we aren't modifying other Admin # classes' fieldsets. self.fieldsets = deepcopy(self.fieldsets) # Insert each field between the publishing fields and nav # fields. Do so in reverse order to retain the order of # the model's fields. model_fields = self.concrete_model._meta.get_fields() concrete_field = '{concrete_model}_ptr'.format( concrete_model=self.concrete_model.get_content_model_name()) exclude_fields = [f.name for f in model_fields] + [concrete_field] try: exclude_fields.extend(self.exclude) except (AttributeError, TypeError): pass try: exclude_fields.extend(self.form.Meta.exclude) except (AttributeError, TypeError): pass fields = (self.model._meta.get_fields() + self.model._meta.many_to_many) for field in reversed(fields): if field.name not in exclude_fields and field.editable: if not hasattr(field, "translated_field"): self.fieldsets[0][1]["fields"].insert(3, field.name)