Example #1
0
 def save(self, update_site=False, *args, **kwargs):
     """
     Create a unique slug by appending an index. Set the site to
     the current site when the record is first created, unless the
     ``update_site`` argument is explicitly set to ``True``.
     """
     if not self.slug:
         # For custom content types, use the ``Page`` instance for
         # slug lookup.
         concrete_model = base_concrete_model(Slugged, self)
         self.slug = self.get_slug()
         i = 0
         while True:
             if i > 0:
                 if i > 1:
                     self.slug = self.slug.rsplit("-", 1)[0]
                 self.slug = "%s-%s" % (self.slug, i)
             qs = concrete_model.objects.all()
             if self.id is not None:
                 qs = qs.exclude(id=self.id)
             try:
                 qs.get(slug=self.slug)
             except ObjectDoesNotExist:
                 break
             i += 1
     if update_site or not self.id:
         self.site = Site.objects.get_current()
     super(Slugged, self).save(*args, **kwargs)
Example #2
0
 def save(self, update_site=False, *args, **kwargs):
     """
     Create a unique slug by appending an index. Set the site to
     the current site when the record is first created, unless the
     ``update_site`` argument is explicitly set to ``True``.
     """
     if not self.slug:
         # For custom content types, use the ``Page`` instance for
         # slug lookup.
         concrete_model = base_concrete_model(Slugged, self)
         self.slug = self.get_slug()
         i = 0
         while True:
             if i > 0:
                 if i > 1:
                     self.slug = self.slug.rsplit("-", 1)[0]
                 self.slug = "%s-%s" % (self.slug, i)
             qs = concrete_model.objects.all()
             if self.id is not None:
                 qs = qs.exclude(id=self.id)
             try:
                 qs.get(slug=self.slug)
             except ObjectDoesNotExist:
                 break
             i += 1
     if update_site or not self.id:
         self.site = Site.objects.get_current()
     super(Slugged, self).save(*args, **kwargs)
Example #3
0
 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)
     ]
Example #4
0
 def save(self, *args, **kwargs):
     """
     Set the initial ordering value.
     """
     if self._order is None:
         lookup = self.with_respect_to()
         concrete_model = base_concrete_model(Orderable, self)
         self._order = concrete_model.objects.filter(**lookup).count()
     super(Orderable, self).save(*args, **kwargs)
Example #5
0
 def set_content_model(self):
     """
     Set content_model to the child class's related name, or None if this is
     the base class.
     """
     is_base_class = (base_concrete_model(ContentTyped,
                                          self) == self.__class__)
     self.content_model = (None if is_base_class else
                           self.get_content_model_name())
Example #6
0
 def save(self, *args, **kwargs):
     """
     Set the initial ordering value.
     """
     if self._order is None:
         lookup = self.with_respect_to()
         concrete_model = base_concrete_model(Orderable, self)
         self._order = concrete_model.objects.filter(**lookup).count()
     super(Orderable, self).save(*args, **kwargs)
Example #7
0
 def set_content_model(self):
     """
     Set content_model to the child class's related name, or None if this is
     the base class.
     """
     is_base_class = (
         base_concrete_model(ContentTyped, self) == self.__class__)
     self.content_model = (
         None if is_base_class else self.get_content_model_name())
Example #8
0
 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())
Example #9
0
 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)
Example #10
0
 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)
Example #11
0
 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())
Example #12
0
 def adjacent_by_order(self, direction):
     """
     Retrieves next object by order in the given direction.
     """
     lookup = self.with_respect_to()
     lookup["_order"] = self._order + direction
     concrete_model = base_concrete_model(Orderable, self)
     try:
         return concrete_model.objects.get(**lookup)
     except concrete_model.DoesNotExist:
         pass
Example #13
0
 def adjacent_by_order(self, direction):
     """
     Retrieves next object by order in the given direction.
     """
     lookup = self.with_respect_to()
     lookup["_order"] = self._order + direction
     concrete_model = base_concrete_model(Orderable, self)
     try:
         return concrete_model.objects.get(**lookup)
     except concrete_model.DoesNotExist:
         pass
Example #14
0
 def save(self, *args, **kwargs):
     """
     Create a unique slug by appending an index.
     """
     if not self.slug:
         self.slug = self.get_slug()
     # 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)
     self.slug = unique_slug(slug_qs, "slug", self.slug)
     super(Slugged, self).save(*args, **kwargs)
Example #15
0
 def save(self, *args, **kwargs):
     """
     Create a unique slug by appending an index.
     """
     if not self.slug:
         self.slug = self.get_slug()
     # 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)
     self.slug = unique_slug(slug_qs, "slug", self.slug)
     super(Slugged, self).save(*args, **kwargs)
Example #16
0
    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") and field.name \
                             not in self.fieldsets[0][1]["fields"]:
                        self.fieldsets[0][1]["fields"].insert(3, field.name)
Example #17
0
 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
Example #18
0
 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
Example #19
0
 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
Example #20
0
 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
Example #21
0
    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.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)
Example #22
0
 def save(self, *args, **kwargs):
     """
     Create a unique slug by appending an index.
     """
     if not self.slug:
         self.slug = self.get_slug()
     # For custom content types, use the ``Page`` instance for
     # slug lookup.
     concrete_model = base_concrete_model(Slugged, self)
     i = 0
     while True:
         if i > 0:
             if i > 1:
                 self.slug = self.slug.rsplit("-", 1)[0]
             self.slug = "%s-%s" % (self.slug, i)
         qs = concrete_model.objects.all()
         if self.id is not None:
             qs = qs.exclude(id=self.id)
         try:
             qs.get(slug=self.slug)
         except ObjectDoesNotExist:
             break
         i += 1
     super(Slugged, self).save(*args, **kwargs)
Example #23
0
 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)]