Пример #1
0
def unpublish_content(db_object, moderator=None):
    """
    Remove the given content from the public view.

    .. note::
        This will send content_unpublished event.

    :param db_object: Database representation of the content
    :type db_object: PublishableContent
    :param moderator: the staff user who triggered the unpublish action.
    :type moderator: django.contrib.auth.models.User
    :return: ``True`` if unpublished, ``False`` otherwise
    :rtype: bool
    """

    from zds.tutorialv2.models.database import PublishedContent

    with contextlib.suppress(ObjectDoesNotExist, OSError):
        public_version = PublishedContent.objects.get(
            pk=db_object.public_version.pk)

        results = [
            content_unpublished.send(sender=reaction.__class__,
                                     instance=db_object,
                                     target=ContentReaction,
                                     moderator=moderator,
                                     user=None) for reaction in
            [ContentReaction.objects.filter(related_content=db_object).all()]
        ]
        logging.debug('Nb_messages=%d, messages=%s', len(results), results)
        # remove public_version:
        public_version.delete()
        update_params = {'public_version': None}

        if db_object.is_opinion:
            update_params['sha_public'] = None
            update_params['sha_picked'] = None
            update_params['pubdate'] = None

        db_object.update(**update_params)
        content_unpublished.send(sender=db_object.__class__,
                                 instance=db_object,
                                 target=db_object.__class__,
                                 moderator=moderator)
        # clean files
        old_path = public_version.get_prod_path()
        public_version.content.update(public_version=None, sha_public=None)
        if path.exists(old_path):
            shutil.rmtree(old_path)
        return True

    return False
Пример #2
0
def unpublish_content(db_object, moderator=None):
    """
    Remove the given content from the public view.

    .. note::
        This will send content_unpublished event.

    :param db_object: Database representation of the content
    :type db_object: PublishableContent
    :return: ``True`` if unpublished, ``False`` otherwise
    :rtype: bool
    """

    from zds.tutorialv2.models.database import PublishedContent

    try:
        public_version = PublishedContent.objects.get(
            pk=db_object.public_version.pk)

        # clean files
        old_path = public_version.get_prod_path()

        if os.path.exists(old_path):
            shutil.rmtree(old_path)

        list([
            content_unpublished.send(sender=reaction.__class__,
                                     instance=reaction) for reaction in
            [ContentReaction.objects.filter(related_content=db_object).all()]
        ])

        # remove public_version:
        public_version.delete()
        update_params = {}
        update_params['public_version'] = None

        if db_object.is_opinion:
            update_params['sha_public'] = None
            update_params['sha_picked'] = None
            update_params['pubdate'] = None

        db_object.update(**update_params)
        content_unpublished.send(sender=db_object.__class__,
                                 instance=db_object)

        return True

    except (ObjectDoesNotExist, OSError):
        pass

    return False
Пример #3
0
def unpublish_content(db_object, moderator=None):
    """
    Remove the given content from the public view.

    .. note::
        This will send content_unpublished event.

    :param db_object: Database representation of the content
    :type db_object: PublishableContent
    :param moderator: the staff user who triggered the unpublish action.
    :type moderator: django.contrib.auth.models.User
    :return: ``True`` if unpublished, ``False`` otherwise
    :rtype: bool
    """

    from zds.tutorialv2.models.database import PublishedContent

    with contextlib.suppress(ObjectDoesNotExist, OSError):
        public_version = PublishedContent.objects.get(pk=db_object.public_version.pk)

        results = [
            content_unpublished.send(sender=reaction.__class__, instance=db_object, target=ContentReaction,
                                     moderator=moderator, user=None)
            for reaction in [ContentReaction.objects.filter(related_content=db_object).all()]
        ]
        logging.debug('Nb_messages=%d, messages=%s', len(results), results)
        # remove public_version:
        public_version.delete()
        update_params = {'public_version': None}

        if db_object.is_opinion:
            update_params['sha_public'] = None
            update_params['sha_picked'] = None
            update_params['pubdate'] = None

        db_object.update(**update_params)
        content_unpublished.send(sender=db_object.__class__, instance=db_object, target=db_object.__class__,
                                 moderator=moderator)
        # clean files
        old_path = public_version.get_prod_path()
        public_version.content.update(public_version=None, sha_public=None)
        if path.exists(old_path):
            shutil.rmtree(old_path)
        return True

    return False
Пример #4
0
def unpublish_content(db_object):
    """
    Remove the given content from the public view.

    .. note::
        This will send content_unpublished event.

    :param db_object: Database representation of the content
    :type db_object: PublishableContent
    :return: ``True`` if unpublished, ``False`` otherwise
    :rtype: bool
    """

    from zds.tutorialv2.models.models_database import PublishedContent

    try:
        public_version = PublishedContent.objects.get(
            pk=db_object.public_version.pk)

        # clean files
        old_path = public_version.get_prod_path()

        if os.path.exists(old_path):
            shutil.rmtree(old_path)

        # remove public_version:
        public_version.delete()

        db_object.public_version = None
        db_object.save()
        content_unpublished.send(sender=db_object.__class__,
                                 instance=db_object)
        return True

    except (ObjectDoesNotExist, IOError):
        pass

    return False