diff --git a/fixtures/noir_black.png b/fixtures/noir_black.png new file mode 100644 index 0000000000..118bf04693 Binary files /dev/null and b/fixtures/noir_black.png differ diff --git a/zds/tutorial/factories.py b/zds/tutorial/factories.py index 5bbf344fa4..1ec34ca23d 100644 --- a/zds/tutorial/factories.py +++ b/zds/tutorial/factories.py @@ -20,7 +20,17 @@ from zds.utils.models import SubCategory from zds.utils.tutorials import export_tutorial - +contenu = ( +u'Ceci est un contenu de tutoriel utile et à tester un peu partout' +u'Ce contenu ira aussi bien dans les introductions, que dans les conclusions et les extraits ' +u'le gros intéret étant qu\'il renferme des images pour tester l\'execution coté pandoc ' +u'Exemple d\'image ![Ma pepite souris](http://blog.science-infuse.fr/public/souris.jpg)' +u'\nExemple d\'image ![Image inexistante](http://blog.science-infuse.fr/public/inv_souris.jpg)' +u'\nExemple de gif ![](http://corigif.free.fr/oiseau/img/oiseau_004.gif)' +u'\nExemple de gif inexistant ![](http://corigif.free.fr/oiseau/img/ironman.gif)' +u'\n Attention les tests ne doivent pas crasher ' +u'qu\'un sujet abandonné !') + class BigTutorialFactory(factory.DjangoModelFactory): FACTORY_FOR = Tutorial @@ -47,10 +57,10 @@ def _prepare(cls, create, **kwargs): f.write(json_writer.dumps(man, indent=4, ensure_ascii=False).encode('utf-8')) f.close() f = open(os.path.join(path, tuto.introduction), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() f = open(os.path.join(path, tuto.conclusion), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() repo.index.add(['manifest.json', tuto.introduction, tuto.conclusion]) cm = repo.index.commit("Init Tuto") @@ -90,10 +100,10 @@ def _prepare(cls, create, **kwargs): ensure_ascii=False).encode('utf-8')) file.close() file = open(os.path.join(path, tuto.introduction), "w") - file.write(u'Test') + file.write(contenu.encode('utf-8')) file.close() file = open(os.path.join(path, tuto.conclusion), "w") - file.write(u'Test') + file.write(contenu.encode('utf-8')) file.close() repo.index.add(['manifest.json', tuto.introduction, tuto.conclusion]) @@ -124,11 +134,11 @@ def _prepare(cls, create, **kwargs): part.save() f = open(os.path.join(tutorial.get_path(), part.introduction), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() repo.index.add([part.introduction]) f = open(os.path.join(tutorial.get_path(), part.conclusion), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() repo.index.add([part.conclusion]) @@ -202,14 +212,14 @@ def _prepare(cls, create, **kwargs): part.tutorial.get_path(), chapter.introduction), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() f = open( os.path.join( part.tutorial.get_path(), chapter.conclusion), "w") - f.write(u'Test') + f.write(contenu.encode('utf-8')) f.close() part.tutorial.save() repo = Repo(part.tutorial.get_path()) @@ -289,4 +299,4 @@ class SubCategoryFactory(factory.DjangoModelFactory): class VaidationFactory(factory.DjangoModelFactory): - FACTORY_FOR = Validation + FACTORY_FOR = Validation \ No newline at end of file diff --git a/zds/tutorial/views.py b/zds/tutorial/views.py index db783eb451..e513ab9456 100644 --- a/zds/tutorial/views.py +++ b/zds/tutorial/views.py @@ -2774,6 +2774,7 @@ def get_url_images(md_text, pt): """find images urls in markdown text and download this.""" regex = ur"(!\[.*?\]\()(.+?)(\))" + unknow_path = os.path.join(settings.SITE_ROOT, "fixtures", "noir_black.png") # if text is empty don't download @@ -2786,8 +2787,7 @@ def get_url_images(md_text, pt): parse_object = urlparse(img[1]) # if link is http type - - if parse_object.scheme in ("http", "https", "ftp") or \ + if parse_object.scheme in ["http", "https", "ftp"] or \ parse_object.netloc[:3]=="www" or \ parse_object.path[:3]=="www": (filepath, filename) = os.path.split(parse_object.path) @@ -2795,20 +2795,23 @@ def get_url_images(md_text, pt): os.makedirs(os.path.join(pt, "images")) # download image - - urlretrieve(img[1], os.path.abspath(os.path.join(pt, "images", - filename))) - ext = filename.split(".")[-1] - - # if image is gif, convert to png - - if ext == "gif": - im = ImagePIL.open(os.path.join(pt, img[1])) - im.save(os.path.join(pt, filename.split(".")[0] + ".png")) + down_path=os.path.abspath(os.path.join(pt, "images", filename)) + urlretrieve(img[1], down_path) + try: + ext = filename.split(".")[-1] + im = ImagePIL.open(down_path) + # if image is gif, convert to png + if ext == "gif": + im.save(os.path.join(pt, "images", filename.split(".")[0] + ".png")) + except IOError: + ext = filename.split(".")[-1] + im = ImagePIL.open(unknow_path) + if ext == "gif": + im.save(os.path.join(pt, "images", filename.split(".")[0] + ".png")) + else: + im.save(os.path.join(pt, "images", filename)) else: - # relative link - srcfile = settings.SITE_ROOT + img[1] if os.path.isfile(srcfile): dstroot = pt + img[1] @@ -2816,13 +2819,19 @@ def get_url_images(md_text, pt): if not os.path.exists(dstdir): os.makedirs(dstdir) shutil.copy(srcfile, dstroot) - ext = dstroot.split(".")[-1] - - # if image is gif, convert to png - - if ext == "gif": + try: + ext = dstroot.split(".")[-1] im = ImagePIL.open(dstroot) - im.save(os.path.join(dstroot.split(".")[0] + ".png")) + # if image is gif, convert to png + if ext == "gif": + im.save(os.path.join(dstroot.split(".")[0] + ".png")) + except IOError: + ext = dstroot.split(".")[-1] + im = ImagePIL.open(unknow_path) + if ext == "gif": + im.save(os.path.join(dstroot.split(".")[0] + ".png")) + else: + im.save(os.path.join(dstroot)) def sub_urlimg(g):