def handle(self, *args, **options):
        _file = options["manifest_path"]
        if os.path.isfile(_file) and _file[-5:] == ".json":
            with open(_file) as json_file:
                data = json_handler.load(json_file)
            _type = "TUTORIAL"
            if data["type"].lower() == "article":
                _type = "ARTICLE"
            versioned = VersionedContent("", _type, data["title"], slugify(data["title"]))
            versioned.description = data["description"]
            if "introduction" in data:
                versioned.introduction = data["introduction"]
            if "conclusion" in data:
                versioned.conclusion = data["conclusion"]
            versioned.licence = Licence.objects.filter(code=data["licence"]).first()
            versioned.version = "2.0"
            versioned.slug = slugify(data["title"])
            if "parts" in data:
                # if it is a big tutorial
                for part in data["parts"]:
                    current_part = Container(part["title"], str(part["pk"]) + "_" + slugify(part["title"]))
                    if "introduction" in part:
                        current_part.introduction = part["introduction"]
                    if "conclusion" in part:
                        current_part.conclusion = part["conclusion"]
                    versioned.add_container(current_part)
                    for chapter in part["chapters"]:
                        current_chapter = Container(
                            chapter["title"], str(chapter["pk"]) + "_" + slugify(chapter["title"])
                        )
                        if "introduction" in chapter:
                            current_chapter.introduction = chapter["introduction"]
                        if "conclusion" in chapter:
                            current_chapter.conclusion = chapter["conclusion"]
                        current_part.add_container(current_chapter)
                        for extract in chapter["extracts"]:
                            current_extract = Extract(
                                extract["title"], str(extract["pk"]) + "_" + slugify(extract["title"])
                            )
                            current_chapter.add_extract(current_extract)
                            current_extract.text = current_extract.get_path(True)

            elif "chapter" in data:
                # if it is a mini tutorial
                for extract in data["chapter"]["extracts"]:
                    current_extract = Extract(extract["title"], str(extract["pk"]) + "_" + slugify(extract["title"]))
                    versioned.add_extract(current_extract)
                    current_extract.text = current_extract.get_path(True)

            elif versioned.type == "ARTICLE":
                extract = Extract("text", "text")
                versioned.add_extract(extract)
                extract.text = extract.get_path(True)

            with open(_file, "w") as json_file:
                json_file.write(versioned.get_json())
    def handle(self, *args, **options):
        _file = options['manifest_path']
        if os.path.isfile(_file) and _file[-5:] == '.json':
            with open(_file, 'r') as json_file:
                data = json_handler.load(json_file)
            _type = 'TUTORIAL'
            if data['type'].lower() == 'article':
                _type = 'ARTICLE'
            versioned = VersionedContent('', _type, data['title'], slugify(data['title']))
            versioned.description = data['description']
            if 'introduction' in data:
                versioned.introduction = data['introduction']
            if 'conclusion' in data:
                versioned.conclusion = data['conclusion']
            versioned.licence = Licence.objects.filter(code=data['licence']).first()
            versioned.version = '2.0'
            versioned.slug = slugify(data['title'])
            if 'parts' in data:
                # if it is a big tutorial
                for part in data['parts']:
                    current_part = Container(part['title'],
                                             str(part['pk']) + '_' + slugify(part['title']))
                    if 'introduction' in part:
                        current_part.introduction = part['introduction']
                    if 'conclusion' in part:
                        current_part.conclusion = part['conclusion']
                    versioned.add_container(current_part)
                    for chapter in part['chapters']:
                        current_chapter = Container(chapter['title'],
                                                    str(chapter['pk']) + '_' + slugify(chapter['title']))
                        if 'introduction' in chapter:
                            current_chapter.introduction = chapter['introduction']
                        if 'conclusion' in chapter:
                            current_chapter.conclusion = chapter['conclusion']
                        current_part.add_container(current_chapter)
                        for extract in chapter['extracts']:
                            current_extract = Extract(extract['title'],
                                                      str(extract['pk']) + '_' + slugify(extract['title']))
                            current_chapter.add_extract(current_extract)
                            current_extract.text = current_extract.get_path(True)

            elif 'chapter' in data:
                # if it is a mini tutorial
                for extract in data['chapter']['extracts']:
                    current_extract = Extract(extract['title'],
                                              str(extract['pk']) + '_' + slugify(extract['title']))
                    versioned.add_extract(current_extract)
                    current_extract.text = current_extract.get_path(True)

            elif versioned.type == 'ARTICLE':
                extract = Extract('text', 'text')
                versioned.add_extract(extract)
                extract.text = extract.get_path(True)

            with open(_file, 'w') as json_file:
                json_file.write(versioned.get_json())
Beispiel #3
0
def init_new_repo(db_object,
                  introduction_text,
                  conclusion_text,
                  commit_message='',
                  do_commit=True):
    """Create a new repository in ``settings.ZDS_APP['contents']['private_repo']``\
     to store the files for a new content. Note that ``db_object.sha_draft`` will be set to the good value

    :param db_object: `PublishableContent` (WARNING: should have a valid ``slug``, so previously saved)
    :param introduction_text: introduction from form
    :param conclusion_text: conclusion from form
    :param commit_message: set a commit message instead of the default one
    :param do_commit: perform commit if ``True``
    :return: ``VersionedContent`` object
    :rtype: zds.tutorialv2.models.versioned.VersionedContent
    """

    from zds.tutorialv2.models.versioned import VersionedContent

    # create directory
    path = db_object.get_repo_path()
    if not os.path.isdir(path):
        os.makedirs(path, mode=0o777)

    # init repo:
    Repo.init(path, bare=False, template='')

    # create object
    versioned_content = VersionedContent(None, db_object.type, db_object.title,
                                         db_object.slug)

    # fill some information that are missing :
    versioned_content.licence = db_object.licence
    versioned_content.description = db_object.description

    # perform changes:
    if not commit_message:
        commit_message = 'Création du contenu'

    sha = versioned_content.repo_update(db_object.title,
                                        introduction_text,
                                        conclusion_text,
                                        commit_message=commit_message,
                                        do_commit=do_commit)

    # update sha:
    if do_commit:
        db_object.sha_draft = sha
        db_object.sha_beta = None
        db_object.sha_public = None
        db_object.sha_validation = None

        db_object.save()

    return versioned_content
Beispiel #4
0
def init_new_repo(db_object, introduction_text, conclusion_text, commit_message='', do_commit=True):
    """Create a new repository in ``settings.ZDS_APP['contents']['private_repo']``\
     to store the files for a new content. Note that ``db_object.sha_draft`` will be set to the good value

    :param db_object: `PublishableContent` (WARNING: should have a valid ``slug``, so previously saved)
    :param introduction_text: introduction from form
    :param conclusion_text: conclusion from form
    :param commit_message: set a commit message instead of the default one
    :param do_commit: perform commit if ``True``
    :return: ``VersionedContent`` object
    :rtype: zds.tutorialv2.models.versioned.VersionedContent
    """

    from zds.tutorialv2.models.versioned import VersionedContent

    # create directory
    path = db_object.get_repo_path()
    if not os.path.isdir(path):
        os.makedirs(path, mode=0o777)

    # init repo:
    Repo.init(path, bare=False, template='')

    # create object
    versioned_content = VersionedContent(None, db_object.type, db_object.title, db_object.slug)

    # fill some information that are missing :
    versioned_content.licence = db_object.licence
    versioned_content.description = db_object.description

    # perform changes:
    if not commit_message:
        commit_message = 'Création du contenu'

    sha = versioned_content.repo_update(
        db_object.title, introduction_text, conclusion_text, commit_message=commit_message, do_commit=do_commit)

    # update sha:
    if do_commit:
        db_object.sha_draft = sha
        db_object.sha_beta = None
        db_object.sha_public = None
        db_object.sha_validation = None

        db_object.save()

    return versioned_content
Beispiel #5
0
def get_content_from_json(json,
                          sha,
                          slug_last_draft,
                          public=False,
                          max_title_len=80,
                          hint_licence=None):
    """Transform the JSON formated data into ``VersionedContent``

    :param json: JSON data from a `manifest.json` file
    :param sha: version
    :param slug_last_draft: the slug for draft marked version
    :param max_title_len: max str length for title
    :param public: the function will fill a PublicContent instead of a VersionedContent if `True`
    :param hint_licence: avoid loading the licence if it is already the same as the one loaded
    :return: a Public/VersionedContent with all the information retrieved from JSON
    :rtype: zds.tutorialv2.models.versioned.VersionedContent|zds.tutorialv2.models.database.PublishedContent
    """

    from zds.tutorialv2.models.versioned import Container, Extract, VersionedContent, PublicContent

    if 'version' in json and json['version'] == 2:
        json['version'] = '2'
        if not all_is_string_appart_from_children(json):
            json['version'] = 2
            raise BadManifestError(
                _("Le fichier manifest n'est pas bien formaté."))
        json['version'] = 2
        # create and fill the container
        if len(json['title']) > max_title_len:
            raise BadManifestError(
                _('Le titre doit être une chaîne de caractères de moins de {} caractères.'
                  ).format(max_title_len))

        # check that title gives correct slug
        slugify_raise_on_invalid(json['title'])

        if not check_slug(json['slug']):
            raise InvalidSlugError(json['slug'])
        else:
            json_slug = json['slug']

        if not public:
            versioned = VersionedContent(sha, 'TUTORIAL', json['title'],
                                         json_slug, slug_last_draft)
        else:
            versioned = PublicContent(sha, 'TUTORIAL', json['title'],
                                      json_slug)

        # fill metadata :
        if 'description' in json:
            versioned.description = json['description']

        if 'type' in json:
            if json['type'] in CONTENT_TYPE_LIST:
                versioned.type = json['type']

        if 'licence' in json:
            if hint_licence is not None and hint_licence.code == json[
                    'licence']:
                versioned.licence = hint_licence
            else:
                versioned.licence = Licence.objects.filter(
                    code=json['licence']).first()

        if 'licence' not in json or not versioned.licence:
            versioned.licence = Licence.objects.filter(
                pk=settings.ZDS_APP['content']['default_licence_pk']).first()

        if 'introduction' in json:
            versioned.introduction = json['introduction']
        if 'conclusion' in json:
            versioned.conclusion = json['conclusion']

        # then, fill container with children
        fill_containers_from_json(json, versioned)
    else:
        # minimal support for deprecated manifest version 1
        # supported content types are exclusively ARTICLE and TUTORIAL

        if 'type' in json:
            if json['type'] == 'article':
                _type = 'ARTICLE'
            else:
                _type = 'TUTORIAL'
        else:
            _type = 'ARTICLE'

        if not public:
            versioned = VersionedContent(sha, _type, json['title'],
                                         slug_last_draft)
        else:
            versioned = PublicContent(sha, _type, json['title'],
                                      slug_last_draft)

        if 'description' in json:
            versioned.description = json['description']
        if 'introduction' in json:
            versioned.introduction = json['introduction']
        if 'conclusion' in json:
            versioned.conclusion = json['conclusion']
        if 'licence' in json:
            versioned.licence = Licence.objects.filter(
                code=json['licence']).first()

        if 'licence' not in json or not versioned.licence:
            versioned.licence = Licence.objects.filter(
                pk=settings.ZDS_APP['content']['default_licence_pk']).first()

        if _type == 'ARTICLE':
            extract = Extract('text', '')
            if 'text' in json:
                extract.text = json['text']  # probably 'text.md' !
            versioned.add_extract(extract, generate_slug=True)

        else:  # it's a tutorial
            if json['type'] == 'MINI' and 'chapter' in json and 'extracts' in json[
                    'chapter']:
                for extract in json['chapter']['extracts']:
                    new_extract = Extract(
                        extract['title'], '{}_{}'.format(
                            extract['pk'],
                            slugify_raise_on_invalid(extract['title'], True)))
                    if 'text' in extract:
                        new_extract.text = extract['text']
                    versioned.add_extract(new_extract, generate_slug=False)

            elif json['type'] == 'BIG' and 'parts' in json:
                for part in json['parts']:
                    new_part = Container(
                        part['title'], '{}_{}'.format(
                            part['pk'],
                            slugify_raise_on_invalid(part['title'], True)))

                    if 'introduction' in part:
                        new_part.introduction = part['introduction']
                    if 'conclusion' in part:
                        new_part.conclusion = part['conclusion']
                    versioned.add_container(new_part, generate_slug=False)

                    if 'chapters' in part:
                        for chapter in part['chapters']:
                            new_chapter = Container(
                                chapter['title'], '{}_{}'.format(
                                    chapter['pk'],
                                    slugify_raise_on_invalid(
                                        chapter['title'], True)))

                            if 'introduction' in chapter:
                                new_chapter.introduction = chapter[
                                    'introduction']
                            if 'conclusion' in chapter:
                                new_chapter.conclusion = chapter['conclusion']
                            new_part.add_container(new_chapter,
                                                   generate_slug=False)

                            if 'extracts' in chapter:
                                for extract in chapter['extracts']:
                                    new_extract = Extract(
                                        extract['title'], '{}_{}'.format(
                                            extract['pk'],
                                            slugify_raise_on_invalid(
                                                extract['title'], True)))

                                    if 'text' in extract:
                                        new_extract.text = extract['text']
                                    new_chapter.add_extract(
                                        new_extract, generate_slug=False)

    return versioned
Beispiel #6
0
def get_content_from_json(json,
                          sha,
                          slug_last_draft,
                          public=False,
                          max_title_len=80,
                          hint_licence=None):
    """Transform the JSON formated data into ``VersionedContent``

    :param json: JSON data from a `manifest.json` file
    :param sha: version
    :param slug_last_draft: the slug for draft marked version
    :param max_title_len: max str length for title
    :param public: the function will fill a PublicContent instead of a VersionedContent if `True`
    :param hint_licence: avoid loading the licence if it is already the same as the one loaded
    :return: a Public/VersionedContent with all the information retrieved from JSON
    :rtype: zds.tutorialv2.models.versioned.VersionedContent|zds.tutorialv2.models.database.PublishedContent
    """

    from zds.tutorialv2.models.versioned import Container, Extract, VersionedContent, PublicContent

    if "version" in json and json["version"] in (
            2, 2.1):  # add newest version of manifest
        if not all_is_string_appart_from_given_keys(
                json, ("children", "ready_to_publish", "version")):
            raise BadManifestError(
                _("Le fichier manifest n'est pas bien formaté."))
        # create and fill the container
        if len(json["title"]) > max_title_len:
            raise BadManifestError(
                _("Le titre doit être une chaîne de caractères de moins de {} caractères."
                  ).format(max_title_len))

        # check that title gives correct slug
        slugify_raise_on_invalid(json["title"])

        if not check_slug(json["slug"]):
            raise InvalidSlugError(json["slug"])
        else:
            json_slug = json["slug"]

        if not public:
            versioned = VersionedContent(sha, "TUTORIAL", json["title"],
                                         json_slug, slug_last_draft)
        else:
            versioned = PublicContent(sha, "TUTORIAL", json["title"],
                                      json_slug)

        # fill metadata :
        if "description" in json:
            versioned.description = json["description"]

        if "type" in json:
            if json["type"] in CONTENT_TYPE_LIST:
                versioned.type = json["type"]

        if "licence" in json:
            if hint_licence is not None and hint_licence.code == json[
                    "licence"]:
                versioned.licence = hint_licence
            else:
                versioned.licence = Licence.objects.filter(
                    code=json["licence"]).first()
        # Note: There is no fallback to a default license in case of problems.
        # The author will have to set it himself prior to publication.

        if "introduction" in json:
            versioned.introduction = json["introduction"]
        if "conclusion" in json:
            versioned.conclusion = json["conclusion"]

        # then, fill container with children
        fill_containers_from_json(json, versioned)
    else:
        # minimal support for deprecated manifest version 1
        # supported content types are exclusively ARTICLE and TUTORIAL

        if "type" in json:
            if json["type"] == "article":
                _type = "ARTICLE"
            else:
                _type = "TUTORIAL"
        else:
            _type = "ARTICLE"

        if not public:
            versioned = VersionedContent(sha, _type, json["title"],
                                         slug_last_draft)
        else:
            versioned = PublicContent(sha, _type, json["title"],
                                      slug_last_draft)

        if "description" in json:
            versioned.description = json["description"]
        if "introduction" in json:
            versioned.introduction = json["introduction"]
        if "conclusion" in json:
            versioned.conclusion = json["conclusion"]
        if "licence" in json:
            versioned.licence = Licence.objects.filter(
                code=json["licence"]).first()
        # Note: There is no fallback to a default license in case of problems.
        # The author will have to set it himself prior to publication.

        versioned.ready_to_publish = True  # the parent is always ready to publish
        if _type == "ARTICLE":
            extract = Extract("text", "")
            if "text" in json:
                extract.text = json["text"]  # probably 'text.md' !
            versioned.add_extract(extract, generate_slug=True)

        else:  # it's a tutorial
            if json["type"] == "MINI" and "chapter" in json and "extracts" in json[
                    "chapter"]:
                for extract in json["chapter"]["extracts"]:
                    new_extract = Extract(
                        extract["title"],
                        "{}_{}".format(
                            extract["pk"],
                            slugify_raise_on_invalid(extract["title"], True)),
                    )
                    if "text" in extract:
                        new_extract.text = extract["text"]
                    versioned.add_extract(new_extract, generate_slug=False)

            elif json["type"] == "BIG" and "parts" in json:
                for part in json["parts"]:
                    new_part = Container(
                        part["title"], "{}_{}".format(
                            part["pk"],
                            slugify_raise_on_invalid(part["title"], True)))

                    if "introduction" in part:
                        new_part.introduction = part["introduction"]
                    if "conclusion" in part:
                        new_part.conclusion = part["conclusion"]
                    versioned.add_container(new_part, generate_slug=False)

                    if "chapters" in part:
                        for chapter in part["chapters"]:
                            new_chapter = Container(
                                chapter["title"],
                                "{}_{}".format(
                                    chapter["pk"],
                                    slugify_raise_on_invalid(
                                        chapter["title"], True)),
                            )

                            if "introduction" in chapter:
                                new_chapter.introduction = chapter[
                                    "introduction"]
                            if "conclusion" in chapter:
                                new_chapter.conclusion = chapter["conclusion"]
                            new_part.add_container(new_chapter,
                                                   generate_slug=False)

                            if "extracts" in chapter:
                                for extract in chapter["extracts"]:
                                    new_extract = Extract(
                                        extract["title"],
                                        "{}_{}".format(
                                            extract["pk"],
                                            slugify_raise_on_invalid(
                                                extract["title"], True)),
                                    )

                                    if "text" in extract:
                                        new_extract.text = extract["text"]
                                    new_chapter.add_extract(
                                        new_extract, generate_slug=False)

    return versioned
Beispiel #7
0
def get_content_from_json(json, sha, slug_last_draft, public=False, max_title_len=80, hint_licence=None):
    """Transform the JSON formated data into ``VersionedContent``

    :param json: JSON data from a `manifest.json` file
    :param sha: version
    :param slug_last_draft: the slug for draft marked version
    :param max_title_len: max str length for title
    :param public: the function will fill a PublicContent instead of a VersionedContent if `True`
    :param hint_licence: avoid loading the licence if it is already the same as the one loaded
    :return: a Public/VersionedContent with all the information retrieved from JSON
    :rtype: zds.tutorialv2.models.versioned.VersionedContent|zds.tutorialv2.models.database.PublishedContent
    """

    from zds.tutorialv2.models.versioned import Container, Extract, VersionedContent, PublicContent

    if 'version' in json and json['version'] in (2, 2.1):  # add newest version of manifest
        if not all_is_string_appart_from_given_keys(json, ('children', 'ready_to_publish', 'version')):
            raise BadManifestError(_("Le fichier manifest n'est pas bien formaté."))
        # create and fill the container
        if len(json['title']) > max_title_len:
            raise BadManifestError(
                _('Le titre doit être une chaîne de caractères de moins de {} caractères.').format(max_title_len))

        # check that title gives correct slug
        slugify_raise_on_invalid(json['title'])

        if not check_slug(json['slug']):
            raise InvalidSlugError(json['slug'])
        else:
            json_slug = json['slug']

        if not public:
            versioned = VersionedContent(sha, 'TUTORIAL', json['title'], json_slug, slug_last_draft)
        else:
            versioned = PublicContent(sha, 'TUTORIAL', json['title'], json_slug)

        # fill metadata :
        if 'description' in json:
            versioned.description = json['description']

        if 'type' in json:
            if json['type'] in CONTENT_TYPE_LIST:
                versioned.type = json['type']

        if 'licence' in json:
            if hint_licence is not None and hint_licence.code == json['licence']:
                versioned.licence = hint_licence
            else:
                versioned.licence = Licence.objects.filter(code=json['licence']).first()

        if 'licence' not in json or not versioned.licence:
            versioned.licence = Licence.objects.filter(pk=settings.ZDS_APP['content']['default_licence_pk']).first()

        if 'introduction' in json:
            versioned.introduction = json['introduction']
        if 'conclusion' in json:
            versioned.conclusion = json['conclusion']

        # then, fill container with children
        fill_containers_from_json(json, versioned)
    else:
        # minimal support for deprecated manifest version 1
        # supported content types are exclusively ARTICLE and TUTORIAL

        if 'type' in json:
            if json['type'] == 'article':
                _type = 'ARTICLE'
            else:
                _type = 'TUTORIAL'
        else:
            _type = 'ARTICLE'

        if not public:
            versioned = VersionedContent(sha, _type, json['title'], slug_last_draft)
        else:
            versioned = PublicContent(sha, _type, json['title'], slug_last_draft)

        if 'description' in json:
            versioned.description = json['description']
        if 'introduction' in json:
            versioned.introduction = json['introduction']
        if 'conclusion' in json:
            versioned.conclusion = json['conclusion']
        if 'licence' in json:
            versioned.licence = Licence.objects.filter(code=json['licence']).first()

        if 'licence' not in json or not versioned.licence:
            versioned.licence = Licence.objects.filter(pk=settings.ZDS_APP['content']['default_licence_pk']).first()
        versioned.ready_to_publish = True  # the parent is always ready to publish
        if _type == 'ARTICLE':
            extract = Extract('text', '')
            if 'text' in json:
                extract.text = json['text']  # probably 'text.md' !
            versioned.add_extract(extract, generate_slug=True)

        else:  # it's a tutorial
            if json['type'] == 'MINI' and 'chapter' in json and 'extracts' in json['chapter']:
                for extract in json['chapter']['extracts']:
                    new_extract = Extract(
                        extract['title'],
                        '{}_{}'.format(extract['pk'], slugify_raise_on_invalid(extract['title'], True)))
                    if 'text' in extract:
                        new_extract.text = extract['text']
                    versioned.add_extract(new_extract, generate_slug=False)

            elif json['type'] == 'BIG' and 'parts' in json:
                for part in json['parts']:
                    new_part = Container(
                        part['title'], '{}_{}'.format(part['pk'], slugify_raise_on_invalid(part['title'], True)))

                    if 'introduction' in part:
                        new_part.introduction = part['introduction']
                    if 'conclusion' in part:
                        new_part.conclusion = part['conclusion']
                    versioned.add_container(new_part, generate_slug=False)

                    if 'chapters' in part:
                        for chapter in part['chapters']:
                            new_chapter = Container(
                                chapter['title'],
                                '{}_{}'.format(chapter['pk'], slugify_raise_on_invalid(chapter['title'], True)))

                            if 'introduction' in chapter:
                                new_chapter.introduction = chapter['introduction']
                            if 'conclusion' in chapter:
                                new_chapter.conclusion = chapter['conclusion']
                            new_part.add_container(new_chapter, generate_slug=False)

                            if 'extracts' in chapter:
                                for extract in chapter['extracts']:
                                    new_extract = Extract(
                                        extract['title'],
                                        '{}_{}'.format(extract['pk'], slugify_raise_on_invalid(extract['title'], True)))

                                    if 'text' in extract:
                                        new_extract.text = extract['text']
                                    new_chapter.add_extract(new_extract, generate_slug=False)

    return versioned