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())
def fill_containers_from_json(json_sub, parent): """Function which call itself to fill container :param json_sub: dictionary from 'manifest.json' :param parent: the container to fill :raise BadManifestError: if the manifest is not well formed or the content's type is not correct :raise KeyError: if one mandatory key is missing """ from zds.tutorialv2.models.versioned import Container, Extract if "children" in json_sub: for child in json_sub["children"]: if not all_is_string_appart_from_given_keys( child, ("children", "ready_to_publish")): raise BadManifestError( _("Le fichier manifest n'est pas bien formaté dans le conteneur " + str(json_sub["title"]))) if child["object"] == "container": slug = "" try: slug = child["slug"] if not check_slug(slug): raise InvalidSlugError(slug) except KeyError: pass new_container = Container(child["title"], slug) if "introduction" in child: new_container.introduction = child["introduction"] if "conclusion" in child: new_container.conclusion = child["conclusion"] try: parent.add_container(new_container, generate_slug=(slug == "")) except InvalidOperationError as e: raise BadManifestError(e.message) new_container.ready_to_publish = child.get( "ready_to_publish", True) if "children" in child: fill_containers_from_json(child, new_container) elif child["object"] == "extract": slug = "" try: slug = child["slug"] if not check_slug(slug): raise InvalidSlugError(child["slug"]) except KeyError: pass new_extract = Extract(child["title"], slug) if "text" in child: new_extract.text = child["text"] try: parent.add_extract(new_extract, generate_slug=(slug == "")) except InvalidOperationError as e: raise BadManifestError(e.message) else: raise BadManifestError( _("Type d'objet inconnu : « {} »").format(child["object"]))
def testParseBadManifest(self): base_content = PublishableContentFactory(author_list=[self.user_author]) versioned = base_content.load_version() versioned.add_container(Container("un peu plus près de 42")) versioned.dump_json() manifest = os.path.join(versioned.get_path(), "manifest.json") dictionary = json_handler.load(open(manifest)) old_title = dictionary["title"] # first bad title dictionary["title"] = 81 * ["a"] self.assertRaises( BadManifestError, get_content_from_json, dictionary, None, "", max_title_len=PublishableContent._meta.get_field("title").max_length, ) dictionary["title"] = "".join(dictionary["title"]) self.assertRaises( BadManifestError, get_content_from_json, dictionary, None, "", max_title_len=PublishableContent._meta.get_field("title").max_length, ) dictionary["title"] = "..." self.assertRaises( InvalidSlugError, get_content_from_json, dictionary, None, "", max_title_len=PublishableContent._meta.get_field("title").max_length, ) dictionary["title"] = old_title dictionary["children"][0]["title"] = 81 * ["a"] self.assertRaises( BadManifestError, get_content_from_json, dictionary, None, "", max_title_len=PublishableContent._meta.get_field("title").max_length, ) dictionary["children"][0]["title"] = "bla" dictionary["children"][0]["slug"] = "..." self.assertRaises( InvalidSlugError, get_content_from_json, dictionary, None, "", max_title_len=PublishableContent._meta.get_field("title").max_length, )
def fill_containers_from_json(json_sub, parent): """Function which call itself to fill container :param json_sub: dictionary from 'manifest.json' :param parent: the container to fill :raise BadManifestError: if the manifest is not well formed or the content's type is not correct :raise KeyError: if one mandatory key is missing """ from zds.tutorialv2.models.versioned import Container, Extract if 'children' in json_sub: for child in json_sub['children']: if not all_is_string_appart_from_children(child): raise BadManifestError( _("Le fichier manifest n'est pas bien formaté dans le conteneur " + str(json_sub['title']))) if child['object'] == 'container': slug = '' try: slug = child['slug'] if not check_slug(slug): raise InvalidSlugError(slug) except KeyError: pass new_container = Container(child['title'], slug) if 'introduction' in child: new_container.introduction = child['introduction'] if 'conclusion' in child: new_container.conclusion = child['conclusion'] try: parent.add_container(new_container, generate_slug=(slug == '')) except InvalidOperationError as e: raise BadManifestError(e.message) if 'children' in child: fill_containers_from_json(child, new_container) elif child['object'] == 'extract': slug = '' try: slug = child['slug'] if not check_slug(slug): raise InvalidSlugError(child['slug']) except KeyError: pass new_extract = Extract(child['title'], slug) if 'text' in child: new_extract.text = child['text'] try: parent.add_extract(new_extract, generate_slug=(slug == '')) except InvalidOperationError as e: raise BadManifestError(e.message) else: raise BadManifestError( _("Type d'objet inconnu : « {} »").format(child['object']))
def testParseBadManifest(self): base_content = PublishableContentFactory( author_list=[self.user_author]) versioned = base_content.load_version() versioned.add_container(Container('un peu plus près de 42')) versioned.dump_json() manifest = os.path.join(versioned.get_path(), 'manifest.json') dictionary = json_handler.load(open(manifest)) old_title = dictionary['title'] # first bad title dictionary['title'] = 81 * ['a'] self.assertRaises(BadManifestError, get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field( 'title').max_length) dictionary['title'] = ''.join(dictionary['title']) self.assertRaises(BadManifestError, get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field( 'title').max_length) dictionary['title'] = '...' self.assertRaises(InvalidSlugError, get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field( 'title').max_length) dictionary['title'] = old_title dictionary['children'][0]['title'] = 81 * ['a'] self.assertRaises(BadManifestError, get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field( 'title').max_length) dictionary['children'][0]['title'] = 'bla' dictionary['children'][0]['slug'] = '...' self.assertRaises(InvalidSlugError, get_content_from_json, dictionary, None, '', max_title_len=PublishableContent._meta.get_field( 'title').max_length)
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
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