Ejemplo n.º 1
0
    def __init__(self, title, slug='', parent=None, position_in_parent=1):
        """Initialize the data model that will handle the dialog with raw versionned data at level container

        :param title: container title (str)
        :param slug: container slug (basicaly slugify(title))
        :param parent: container parent (None if this container is the root container)
        :param position_in_parent: the display order
        :return:
        """
        self.title = title
        self.slug = slug
        self.parent = parent
        self.position_in_parent = position_in_parent

        self.children = []  # even if you want, do NOT remove this line
        self.children_dict = {}

        self.slug_pool = default_slug_pool()
Ejemplo n.º 2
0
    def __init__(self, title, slug='', parent=None, position_in_parent=1):
        """Initialize the data model that will handle the dialog with raw versionned data at level container.

        :param title: container title (str)
        :param slug: container slug (basicaly slugify(title))
        :param parent: container parent (None if this container is the root container)
        :param position_in_parent: the display order
        :return:
        """
        self.title = title
        self.slug = slug
        self.parent = parent
        self.position_in_parent = position_in_parent

        self.children = []  # even if you want, do NOT remove this line
        self.children_dict = {}

        self.slug_pool = default_slug_pool()
Ejemplo n.º 3
0
    def form_valid(self, form):
        versioned = self.versioned_object

        if self.request.FILES["archive"]:
            try:
                zfile = zipfile.ZipFile(self.request.FILES["archive"], "r")
            except zipfile.BadZipfile:
                messages.error(self.request,
                               _("Cette archive n'est pas au format ZIP."))
                return super(UpdateContentWithArchive, self).form_invalid(form)

            try:
                new_version = UpdateContentWithArchive.extract_content_from_zip(
                    zfile)
            except BadArchiveError as e:
                messages.error(self.request, e.message)
                return super(UpdateContentWithArchive, self).form_invalid(form)
            else:

                # Warn the user if the license has been changed
                manifest = json_handler.loads(
                    str(zfile.read("manifest.json"), "utf-8"))
                if new_version.licence and "licence" in manifest and manifest[
                        "licence"] != new_version.licence.code:
                    messages.info(
                        self.request,
                        _("la licence « {} » a été appliquée.").format(
                            new_version.licence.code))

                # first, update DB object (in order to get a new slug if needed)
                title_is_changed = self.object.title != new_version.title
                self.object.title = new_version.title
                self.object.description = new_version.description
                self.object.licence = new_version.licence
                self.object.type = new_version.type  # change of type is then allowed !!
                self.object.save(force_slug_update=title_is_changed)

                new_version.slug = self.object.slug  # new slug if any !!

                # ok, then, let's do the import. First, remove everything in the repository
                while True:
                    if versioned.children:
                        versioned.children[0].repo_delete(do_commit=False)
                    else:
                        break  # this weird construction ensure that everything is removed

                versioned.slug_pool = default_slug_pool(
                )  # slug pool to its initial value (to avoid weird stuffs)

                # start by copying extra information
                self.object.insert_data_in_versioned(
                    versioned)  # better have a clean version of those one
                versioned.description = new_version.description
                versioned.type = new_version.type
                versioned.licence = new_version.licence

                # update container (and repo)
                introduction = ""
                conclusion = ""

                if new_version.introduction:
                    introduction = str(zfile.read(new_version.introduction),
                                       "utf-8")
                if new_version.conclusion:
                    conclusion = str(zfile.read(new_version.conclusion),
                                     "utf-8")

                versioned.repo_update_top_container(new_version.title,
                                                    new_version.slug,
                                                    introduction,
                                                    conclusion,
                                                    do_commit=False)

                # then do the dirty job:
                try:
                    UpdateContentWithArchive.update_from_new_version_in_zip(
                        versioned, new_version, zfile)
                except BadArchiveError as e:
                    versioned.repository.index.reset()
                    messages.error(self.request, e.message)
                    return super(UpdateContentWithArchive,
                                 self).form_invalid(form)

                # and end up by a commit !!
                commit_message = form.cleaned_data["msg_commit"]

                if not commit_message:
                    commit_message = _(
                        "Importation d'une archive contenant « {} ».").format(
                            new_version.title)

                sha = versioned.commit_changes(commit_message)

                # now, use the images from the archive if provided. To work, this HAVE TO happen after commiting files !
                if "image_archive" in self.request.FILES:
                    try:
                        zfile = zipfile.ZipFile(
                            self.request.FILES["image_archive"], "r")
                    except zipfile.BadZipfile:
                        messages.error(
                            self.request,
                            _("L'archive contenant les images n'est pas au format ZIP."
                              ))
                        return self.form_invalid(form)

                    UpdateContentWithArchive.use_images_from_archive(
                        self.request, zfile, versioned, self.object.gallery)

                    commit_message = _(
                        "Utilisation des images de l'archive pour « {} »"
                    ).format(new_version.title)
                    sha = versioned.commit_changes(
                        commit_message)  # another commit

                # of course, need to update sha
                self.object.sha_draft = sha
                self.object.update_date = datetime.now()
                self.object.save()

                self.success_url = reverse("content:view",
                                           args=[versioned.pk, versioned.slug])

        return super(UpdateContentWithArchive, self).form_valid(form)