Ejemplo n.º 1
0
    def _publish(self, published=True, **kwargs):
        with xact():
            filter_args = {
                'state': self.PUBLISHED,
                'object_id': self.object_id,
            }

            klass = self.get_version_class()
            klass.normal.filter(**filter_args).update(state=self.ARCHIVED)

            now = timezone.now()

            if published:
                self.state = self.PUBLISHED
                self.date_published = now
            else:
                self.last_scheduled = None
                self.date_published = None

            self.save()

            # Update the base model as published and cache the scheduled
            # date for comparisons.
            self._meta._base_model.objects.filter(pk=self.object_id).update(
                is_published=published, v_last_save=self.last_scheduled)
            self.is_published = published
            self.v_last_save = self.last_scheduled

        # Send published signal
        klass = self.__class__
        if hasattr(self._meta, '_view_model'):
            klass = self.__class__

        published_signal.send(sender=klass, instance=self)
Ejemplo n.º 2
0
    def _clone(self, **attrs):
        """
        Makes a copy of an model instance.

        for every key in **attrs value will
        be set on the new instance.
        """

        with xact():
            # Gather objs we'll need save after
            old_m2ms = self._gather_m2ms()
            old_reverses = self._gather_reverses()

            for k, v in attrs.items():
                setattr(self, k, v)

            # Do the clone
            self.prep_for_clone()
            self.validate_unique()
            # Prevent last save from changing
            self.save(last_save=self.last_save)

            # save m2ms
            self._set_m2ms(old_m2ms)
            # Prevent last save from changing
            self.save(last_save=self.last_save)

            # save reverses
            self._clone_reverses(old_reverses)
Ejemplo n.º 3
0
    def make_draft(self):
        """
        Make this version the draft
        """
        assert self.__class__ == self.get_version_class()

        # If this is draft do nothing
        if self.state == self.DRAFT:
            return

        with xact():
            # Delete whatever is currently this draft
            try:
                klass = self.get_version_class()
                old_draft = klass.normal.get(object_id=self.object_id,
                                             state=self.DRAFT)
                old_draft.delete()
            except klass.DoesNotExist:
                pass

            # Set this to draft and save
            self.state = self.DRAFT
            # Make last_scheduled and last save match on draft
            self.last_save = self.last_scheduled
            self._clone()
Ejemplo n.º 4
0
    def make_draft(self):
        """
        Make this version the draft
        """
        assert self.__class__ == self.get_version_class()

        # If this is draft do nothing
        if self.state == self.DRAFT:
            return

        with xact():
            # Delete whatever is currently this draft
            try:
                klass = self.get_version_class()
                old_draft = klass.normal.get(object_id=self.object_id,
                                             state=self.DRAFT)
                old_draft.delete()
            except klass.DoesNotExist:
                pass

            # Set this to draft and save
            self.state = self.DRAFT
            # Make last_scheduled and last save match on draft
            self.last_save = self.last_scheduled
            self._clone()
Ejemplo n.º 5
0
    def _clone(self, **attrs):
        """
        Makes a copy of an model instance.

        for every key in **attrs value will
        be set on the new instance.
        """

        with xact():
            # Gather objs we'll need save after
            old_m2ms = self._gather_m2ms()
            old_reverses = self._gather_reverses()

            for k, v in attrs.items():
                setattr(self, k, v)

            # Do the clone
            self.prep_for_clone()
            # Prevent last save from changing
            self.save(last_save=self.last_save)

            # save m2ms
            self._set_m2ms(old_m2ms)
            # Prevent last save from changing
            self.save(last_save=self.last_save)

            # save reverses
            self._clone_reverses(old_reverses)
Ejemplo n.º 6
0
    def save(self, *args, **kwargs):
        models.signals.pre_save.send(sender=self.__class__,
                                     instance=self,
                                     raw=kwargs.get('raw'),
                                     using=kwargs.get('using'))

        last_save = kwargs.pop('last_save', timezone.now())
        with xact():
            # Make sure we have a state
            if not self.vid:
                self.state = self.DRAFT

            # Get a version instance
            version = self._meta._version_model()
            base = self._meta._base_model()

            # Build out the version
            for f in self._meta.local_fields:
                try:
                    attname = f.name
                    version._meta.get_field(attname)
                    if isinstance(f, models.ForeignKey):
                        attname = "%s_id" % f.name

                    setattr(version, attname, getattr(self, attname))

                # version doesn't have this field
                except FieldDoesNotExist:
                    if self.should_save_base:
                        setattr(base, f.name, getattr(self, attname))

            # Make sure we have a base
            if not self.object_id or self.should_save_base:
                base.is_published = self.is_published
                base.pk = self.pk

                base.save(*args, **kwargs)
                self.pk = base.pk

                # set base object
                version.object = base
            else:
                version.object_id = self.object_id

            # Save the version and copy the db state
            version.save(last_save=last_save)

            self.vid = version.vid
            self._state = version._state
            self.state = version.state
            self.last_save = version.last_save
        models.signals.post_save.send(sender=self.__class__,
                                      instance=self,
                                      raw=kwargs.get('raw'),
                                      using=kwargs.get('using'))
Ejemplo n.º 7
0
    def publish(self, user=None, when=None):
        """
        Publishes a item and any sub items.
        A new transaction will be started if
        we aren't already in a transaction.

        Should only be run on draft items
        """

        assert self.state == self.DRAFT

        user_published = 'code'
        if user:
            user_published = user.username

        now = timezone.now()

        with xact():
            # If this item hasn't got live yet and no new date was specified
            # delete the old scheduled items and schedule this one on that date
            published = False
            if getattr(self._meta, '_is_view', False):
                published = self.is_published
            else:
                published = self.object.is_published

            if not when and not published and self.last_scheduled:
                klass = self.get_version_class()
                for obj in klass.normal.filter(
                        object_id=self.object_id,
                        last_scheduled=self.last_scheduled,
                        state=self.SCHEDULED):
                    when = self.date_published
                    obj.delete()

            when = when or now

            # Drafts get preserved so save the
            # time we last cloned this
            if self.state == self.DRAFT:
                self.last_scheduled = now
                self.date_published = when
                self.save(last_save=now)

            self._clone()

            self.user_published = user_published
            self.state = self.SCHEDULED
            self.save()

            self.schedule(when=when)
Ejemplo n.º 8
0
    def save(self, *args, **kwargs):
        models.signals.pre_save.send(sender=self.__class__, instance=self,
            raw=kwargs.get('raw'), using=kwargs.get('using'))

        last_save = kwargs.pop('last_save', timezone.now())
        with xact():
            # Make sure we have a state
            if not self.vid:
                self.state = self.DRAFT

            # Get a version instance
            version = self._meta._version_model()
            base = self._meta._base_model()

            # Build out the version
            for f in self._meta.local_fields:
                try:
                    attname = f.name
                    version._meta.get_field(attname)
                    if isinstance(f, models.ForeignKey):
                        attname = "%s_id" % f.name

                    setattr(version, attname, getattr(self, attname))

                # version doesn't have this field
                except FieldDoesNotExist:
                    if self.should_save_base:
                        setattr(base, f.name, getattr(self, attname))

            # Make sure we have a base
            if not self.object_id or self.should_save_base:
                base.is_published = self.is_published
                base.pk = self.pk

                base.save(*args, **kwargs)
                self.pk = base.pk

                # set base object
                version.object = base
            else:
                version.object_id = self.object_id

            # Save the version and copy the db state
            version.save(last_save=last_save)

            self.vid = version.vid
            self._state = version._state
            self.state = version.state
            self.last_save = version.last_save
        models.signals.post_save.send(sender=self.__class__, instance=self,
            raw=kwargs.get('raw'), using=kwargs.get('using'))
Ejemplo n.º 9
0
    def publish(self, user=None, when=None):
        """
        Publishes a item and any sub items.
        A new transaction will be started if
        we aren't already in a transaction.

        Should only be run on draft items
        """

        assert self.state == self.DRAFT

        user_published = 'code'
        if user:
            user_published = user.username

        now = timezone.now()

        with xact():
            # If this item hasn't got live yet and no new date was specified
            # delete the old scheduled items and schedule this one on that date
            published = False
            if getattr(self._meta, '_is_view', False):
                published = self.is_published
            else:
                published = self.object.is_published

            if not when and not published and self.last_scheduled:
                klass = self.get_version_class()
                for obj in klass.normal.filter(object_id=self.object_id,
                                        last_scheduled=self.last_scheduled,
                                        state=self.SCHEDULED):
                    when = self.date_published
                    obj.delete()

            when = when or now

            # Drafts get preserved so save the
            # time we last cloned this
            if self.state == self.DRAFT:
                self.last_scheduled = now
                self.date_published = when
                self.save(last_save=now)

            self._clone()

            self.user_published = user_published
            self.state = self.SCHEDULED
            self.save()

            self.schedule(when=when)
Ejemplo n.º 10
0
    def save(self, *args, **kwargs):
        """
        Saves this item.

        Creates a default base if there isn't
        one already.
        """
        with xact():
            if not self.vid:
                self.state = self.DRAFT

                if not self.object_id:
                    base = self._meta._base_model(is_published=False)
                    base.save(*args, **kwargs)
                    self.object = base

            super(VersionModel, self).save(*args, **kwargs)
Ejemplo n.º 11
0
    def save(self, *args, **kwargs):
        """
        Saves this item.

        Creates a default base if there isn't
        one already.
        """
        with xact():
            if not self.vid:
                self.state = self.DRAFT

                if not self.object_id:
                    base = self._meta._base_model(is_published=False)
                    base.save(*args, **kwargs)
                    self.object = base

            super(VersionModel, self).save(*args, **kwargs)
Ejemplo n.º 12
0
    def unpublish(self):
        """
        Unpublish this item.

        This will set and currently published versions to
        the archived state and delete all currently scheduled
        versions.
        """
        assert self.state == self.DRAFT

        with xact():
            self._publish(published=False)

            # Delete all scheduled items
            klass = self.get_version_class()
            for obj in klass.normal.filter(object_id=self.object_id,
                                           state=self.SCHEDULED):
                obj.delete()
Ejemplo n.º 13
0
    def unpublish(self):
        """
        Unpublish this item.

        This will set and currently published versions to
        the archived state and delete all currently scheduled
        versions.
        """
        assert self.state == self.DRAFT

        with xact():
            self._publish(published=False)

            # Delete all scheduled items
            klass = self.get_version_class()
            for obj in klass.normal.filter(object_id=self.object_id,
                                            state=self.SCHEDULED):
                obj.delete()
Ejemplo n.º 14
0
    def _publish(self, published=True, **kwargs):
        with xact():
            filter_args = {
                'state': self.PUBLISHED,
                'object_id': self.object_id,
            }

            klass = self.get_version_class()
            klass.normal.filter(**filter_args).update(
                                  state=self.ARCHIVED)

            now = timezone.now()

            if published:
                self.state = self.PUBLISHED
                self.date_published = now
            else:
                self.last_scheduled = None
                self.date_published = None

            self.save()

            # Update the base model as published and cache the scheduled
            # date for comparisons.
            self._meta._base_model.objects.filter(pk=self.object_id
                                    ).update(is_published=published,
                                             v_last_save=self.last_scheduled)
            self.is_published = published
            self.v_last_save = self.last_scheduled

        # Send published signal
        klass = self.__class__
        if hasattr(self._meta, '_view_model'):
            klass = self.__class__

        published_signal.send(sender=klass, instance=self)
Ejemplo n.º 15
0
 def delete(self, **kwargs):
     with xact():
         # Delete's happen in public so that all
         # versions are found.
         with manager.SwitchSchema('public'):
             super(VersionView, self).delete(**kwargs)
Ejemplo n.º 16
0
 def delete(self, **kwargs):
     with xact():
         # Delete's happen in default schema so that all
         # versions are found.
         with manager.SwitchSchema('public'):
             super(VersionView, self).delete(**kwargs)
Ejemplo n.º 17
0
 def handle_pre_delete_signal(cls, sender, instance, **kwargs):
     with xact():
         klass = cls._meta._version_model
         for obj in klass.normal.filter(object_id=instance.object_id):
             obj._delete_reverses()
Ejemplo n.º 18
0
 def handle_pre_delete_signal(cls, sender, instance, **kwargs):
     with xact():
         klass = cls._meta._version_model
         for obj in klass.normal.filter(object_id=instance.object_id):
             obj._delete_reverses()