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 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 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 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())