Skip to content

Commit

Permalink
correction des bugs de download pendant la validation
Browse files Browse the repository at this point in the history
  • Loading branch information
firm1 committed Aug 11, 2014
1 parent 355cbeb commit 12ad16f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
Binary file added fixtures/noir_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 20 additions & 10 deletions zds/tutorial/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -289,4 +299,4 @@ class SubCategoryFactory(factory.DjangoModelFactory):


class VaidationFactory(factory.DjangoModelFactory):
FACTORY_FOR = Validation
FACTORY_FOR = Validation
49 changes: 29 additions & 20 deletions zds/tutorial/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -2786,43 +2787,51 @@ 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)
if not os.path.isdir(os.path.join(pt, "images")):
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]
dstdir = os.path.dirname(dstroot)
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):
Expand Down

0 comments on commit 12ad16f

Please sign in to comment.