Skip to content

Commit

Permalink
Merge 1ee0d98 into 4c9b3a8
Browse files Browse the repository at this point in the history
  • Loading branch information
dralliw committed Sep 1, 2014
2 parents 4c9b3a8 + 1ee0d98 commit caca71d
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 39 deletions.
9 changes: 7 additions & 2 deletions templates/tutorial/tutorial/import.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ <h2>


{% block content %}
<h3>Envoi d'une archive</h3>
<p>
Vous êtes l'auteur d'un cours sur le SdZ ? Nous l'avons récupéré pour vous ! Il est disponible hors-ligne et n'attend plus que votre accord pour être publié. Il vous suffit d'en faire la demande à un membre du Staff via le forum ou directement par MP.
Vous avez commencé a rédiger votre tutoriel sur l'éditeur en ligne de Zeste de Savoir, vous avez téléchargé l'archive correspondate et vous avez fait des modifications sur les fichiers en hors-ligne, et vous souhaitez maintenant importer ces modifications sur le site ? Faites une archive zip (seul format supporté actuellement) du répertoire dans lequel se trouve les fichiers de votre tutoriel et renseignez les deux champs ci-dessous, puis cliquez sur importer.
</p>

{% crispy form_archive %}

<h3>Envoi de fichiers .tuto</h3>
<p>
Vous êtes l'auteur d'un cours sur le SdZ ? Nous l'avons récupéré pour vous ! Il est disponible en hors-ligne et n'attend plus que votre accord pour être publié. Il vous suffit d'en faire la demande à un membre du Staff via le forum ou directement par MP.
</p>

<p>
<strong>Attention</strong>, le fichier attendu n'est pas un .tuto du Site du Zéro, mais un format intermédiaire qui contient du Markdown et non du zCode. Contactez un membre du Staff pour demander la conversion de votre fichier.
</p>
Expand Down
29 changes: 28 additions & 1 deletion zds/tutorial/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,37 @@ def __init__(self, *args, **kwargs):
self.helper.layout = Layout(
Field('file'),
Field('images'),
Submit('submit', 'Importer'),
Submit('import-tuto', 'Importer le .tuto'),
)
super(ImportForm, self).__init__(*args, **kwargs)

class ImportArchiveForm(forms.Form):

file = forms.FileField(
label='Sélectionnez l\'archive de votre tutoriel',
required=True
)

tutorial = forms.ModelChoiceField(
label="Tutoriel vers lequel vous souhaitez importer votre archive",
queryset=Tutorial.objects.none(),
required=True
)

def __init__(self, user, *args, **kwargs):
super(ImportArchiveForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'content-wrapper'
self.helper.form_method = 'post'
self.fields['tutorial'].queryset=Tutorial.objects.filter(authors__in=[user])

self.helper.layout = Layout(
Field('file'),
Field('tutorial'),
Submit('import-archive', 'Importer l\'archive'),
)


# Notes


Expand Down
144 changes: 137 additions & 7 deletions zds/tutorial/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import os
import shutil
import tarfile
import zipfile
from urllib import urlretrieve
import HTMLParser
from django.conf import settings
from django.core import mail
Expand All @@ -20,6 +23,9 @@
from zds.tutorial.models import Note, Tutorial, Validation, Extract, Part, Chapter
from zds.utils.models import SubCategory, Licence, Alert
from zds.utils.misc import compute_hash

from django.conf.global_settings import MEDIA_ROOT

@override_settings(MEDIA_ROOT=os.path.join(SITE_ROOT, 'media-test'))
@override_settings(REPO_PATH=os.path.join(SITE_ROOT, 'tutoriels-private-test'))
@override_settings(
Expand Down Expand Up @@ -123,6 +129,75 @@ def setUp(self):

mail.outbox = []

def test_import_archive(self):
#create temporary data directory
temp = os.path.join(SITE_ROOT, "temp")

# download tar
if not os.path.isdir(temp):
os.makedirs(temp, mode=0777)
tarpath = os.path.join(MEDIA_ROOT, "temp", self.bigtuto.get_phy_slug()+".tar")
result = self.client.get(
reverse('zds.tutorial.views.download')+"?tutoriel={}".format(str(self.bigtuto.pk)),
follow=False)

save_path=os.path.join(settings.REPO_PATH, self.bigtuto.get_phy_slug()+".tar")
shutil.copy(save_path, tarpath)
tar = tarfile.open(tarpath)
tar.extractall(path=os.path.join(temp, self.bigtuto.get_phy_slug()))
tar.close()

self.assertTrue(os.path.isdir(os.path.join(temp, self.bigtuto.get_phy_slug())))

# update markdown files
up_intro_tfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), self.bigtuto.introduction), "a")
up_intro_tfile.write(u"preuve de modification de l'introduction")
up_intro_tfile.close()
up_conclu_tfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), self.bigtuto.conclusion), "a")
up_conclu_tfile.write(u"preuve de modification de la conclusion")
up_conclu_tfile.close()
parts = Part.objects.filter(tutorial__pk=self.bigtuto.pk)
for part in parts:
up_intro_pfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), part.introduction), "a")
up_intro_pfile.write(u"preuve de modification de l'introduction")
up_intro_pfile.close()
up_conclu_pfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), part.conclusion), "a")
up_conclu_pfile.write(u"preuve de modification de la conclusion")
up_conclu_pfile.close()
chapters = Chapter.objects.filter(part__pk=part.pk)
for chapter in chapters:
up_intro_cfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), chapter.introduction), "a")
up_intro_cfile.write(u"preuve de modification de l'introduction")
up_intro_cfile.close()
up_conclu_cfile = open(os.path.join(temp, self.bigtuto.get_phy_slug(), chapter.conclusion), "a")
up_conclu_cfile.write(u"preuve de modification de la conclusion")
up_conclu_cfile.close()

# zip directory
shutil.make_archive(os.path.join(temp, self.bigtuto.get_phy_slug()),
"zip",
os.path.join(temp, self.bigtuto.get_phy_slug()))

self.assertTrue(os.path.isfile(os.path.join(temp, self.bigtuto.get_phy_slug()+".zip")))

# import zip archive
result = self.client.post(
reverse('zds.tutorial.views.import_tuto'),
{
'file': open(
os.path.join(
temp,
os.path.join(temp, self.bigtuto.get_phy_slug()+".zip")),
'r'),
'tutorial': self.bigtuto.pk,
'import-archive': "importer"},
follow=False)
self.assertEqual(result.status_code, 302)
self.assertEqual(Tutorial.objects.all().count(), 1)

#delete temporary data directory
shutil.rmtree(temp)

def test_add_note(self):
"""To test add note for tutorial."""
user1 = ProfileFactory().user
Expand Down Expand Up @@ -402,7 +477,8 @@ def test_import_tuto(self):
'tuto',
'temps-reel-avec-irrlicht',
'images.zip'),
'r')},
'r'),
'import-tuto': "importer"},
follow=False)
self.assertEqual(result.status_code, 302)

Expand Down Expand Up @@ -477,7 +553,7 @@ def test_url_for_guest(self):
self.chapter2_1.slug]),
follow=False)
self.assertEqual(result.status_code, 302)

def test_workflow_tuto(self):
"""Test workflow of tutorial."""

Expand Down Expand Up @@ -2164,6 +2240,59 @@ def setUp(self):

mail.outbox = []

def test_import_archive(self):
#create temporary data directory
temp = os.path.join(SITE_ROOT, "temp")

# download tar
if not os.path.isdir(temp):
os.makedirs(temp, mode=0777)
tarpath = os.path.join(MEDIA_ROOT, "temp", self.minituto.get_phy_slug()+".tar")
result = self.client.get(
reverse('zds.tutorial.views.download')+"?tutoriel={}".format(str(self.minituto.pk)),
follow=False)

save_path=os.path.join(settings.REPO_PATH, self.minituto.get_phy_slug()+".tar")
shutil.copy(save_path, tarpath)
tar = tarfile.open(tarpath)
tar.extractall(path=os.path.join(temp, self.minituto.get_phy_slug()))
tar.close()

self.assertTrue(os.path.isdir(os.path.join(temp, self.minituto.get_phy_slug())))

# update markdown files
up_intro_tfile = open(os.path.join(temp, self.minituto.get_phy_slug(), self.minituto.introduction), "a")
up_intro_tfile.write(u"preuve de modification de l'introduction")
up_intro_tfile.close()
up_conclu_tfile = open(os.path.join(temp, self.minituto.get_phy_slug(), self.minituto.conclusion), "a")
up_conclu_tfile.write(u"preuve de modification de la conclusion")
up_conclu_tfile.close()

# zip directory
shutil.make_archive(os.path.join(temp, self.minituto.get_phy_slug()),
"zip",
os.path.join(temp, self.minituto.get_phy_slug()))

self.assertTrue(os.path.isfile(os.path.join(temp, self.minituto.get_phy_slug()+".zip")))

# import zip archive
result = self.client.post(
reverse('zds.tutorial.views.import_tuto'),
{
'file': open(
os.path.join(
temp,
os.path.join(temp, self.minituto.get_phy_slug()+".zip")),
'r'),
'tutorial': self.minituto.pk,
'import-archive': "importer"},
follow=False)
self.assertEqual(result.status_code, 302)
self.assertEqual(Tutorial.objects.all().count(), 1)

#delete temporary data directory
shutil.rmtree(temp)

def add_test_extract_named_introduction(self):
"""test the use of an extract named introduction"""

Expand Down Expand Up @@ -2459,7 +2588,7 @@ def test_dislike_note(self):
self.assertEqual(Note.objects.get(pk=note3.pk).dislike, 0)

def test_import_tuto(self):
"""Test import of big tuto."""
"""Test import of mini tuto."""
result = self.client.post(
reverse('zds.tutorial.views.import_tuto'),
{
Expand All @@ -2468,17 +2597,18 @@ def test_import_tuto(self):
settings.SITE_ROOT,
'fixtures',
'tuto',
'temps-reel-avec-irrlicht',
'temps-reel-avec-irrlicht.tuto'),
'securisez-vos-mots-de-passe-avec-lastpass',
'securisez-vos-mots-de-passe-avec-lastpass.tuto'),
'r'),
'images': open(
os.path.join(
settings.SITE_ROOT,
'fixtures',
'tuto',
'temps-reel-avec-irrlicht',
'securisez-vos-mots-de-passe-avec-lastpass',
'images.zip'),
'r')},
'r'),
'import-tuto': "importer"},
follow=False)
self.assertEqual(result.status_code, 302)

Expand Down
71 changes: 43 additions & 28 deletions zds/tutorial/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from lxml import etree

from forms import TutorialForm, PartForm, ChapterForm, EmbdedChapterForm, \
ExtractForm, ImportForm, NoteForm, AskValidationForm, ValidForm, RejectForm
ExtractForm, ImportForm, ImportArchiveForm, NoteForm, AskValidationForm, ValidForm, RejectForm
from models import Tutorial, Part, Chapter, Extract, Validation, never_read, \
mark_read, Note
from zds.gallery.models import Gallery, UserGallery, Image
Expand All @@ -53,7 +53,7 @@
from zds.utils.mps import send_mp
from zds.utils.paginator import paginator_range
from zds.utils.templatetags.emarkdown import emarkdown
from zds.utils.tutorials import get_blob, export_tutorial_to_md, move
from zds.utils.tutorials import get_blob, export_tutorial_to_md, move, import_archive
from zds.utils.misc import compute_hash, content_has_changed

def render_chapter_form(chapter):
Expand Down Expand Up @@ -2429,31 +2429,46 @@ def local_import(request):
@login_required
def import_tuto(request):
if request.method == "POST":
form = ImportForm(request.POST, request.FILES)

# check extension

if "file" in request.FILES:
filename = str(request.FILES["file"])
ext = filename.split(".")[-1]
if ext == "tuto":
import_content(request, request.FILES["file"],
request.FILES["images"], "")
# for import tuto
if "import-tuto" in request.POST:
form = ImportForm(request.POST, request.FILES)
form_archive = ImportArchiveForm(user=request.user)
if "file" in request.FILES:
filename = str(request.FILES["file"])
ext = filename.split(".")[-1]
if ext == "tuto":
import_content(request, request.FILES["file"],
request.FILES["images"], "")
else:
raise Http404
return redirect(reverse("zds.member.views.tutorials"))
elif "import-archive" in request.POST:
form = ImportForm()
form_archive = ImportArchiveForm(request.user, request.POST, request.FILES)
(check, reason) = import_archive(request)
if not check:
messages.error(request, reason)
else:
raise Http404
return redirect(reverse("zds.member.views.tutorials"))
messages.success(request, reason)
return redirect(reverse("zds.member.views.tutorials"))


else:
form = ImportForm()
profile = get_object_or_404(Profile, user=request.user)
oldtutos = []
if profile.sdz_tutorial:
olds = profile.sdz_tutorial.strip().split(":")
else:
olds = []
for old in olds:
oldtutos.append(get_info_old_tuto(old))
form_archive = ImportArchiveForm(user=request.user)

profile = get_object_or_404(Profile, user=request.user)
oldtutos = []
if profile.sdz_tutorial:
olds = profile.sdz_tutorial.strip().split(":")
else:
olds = []
for old in olds:
oldtutos.append(get_info_old_tuto(old))
return render_template(
"tutorial/tutorial/import.html", {"form": form, "old_tutos": oldtutos})
"tutorial/tutorial/import.html", {"form": form,
"form_archive":form_archive,
"old_tutos": oldtutos})


# Handling repo
Expand Down Expand Up @@ -2714,7 +2729,7 @@ def download(request):
repo = Repo(ph)
repo.archive(open(ph + ".tar", "w"))
response = HttpResponse(open(ph + ".tar", "rb").read(),
mimetype="application/tar")
content_type="application/tar")
response["Content-Disposition"] = \
"attachment; filename={0}.tar".format(tutorial.slug)
return response
Expand All @@ -2732,7 +2747,7 @@ def download_markdown(request):
".md")
response = HttpResponse(
open(phy_path, "rb").read(),
mimetype="application/txt")
content_type="application/txt")
response["Content-Disposition"] = \
"attachment; filename={0}.md".format(tutorial.slug)
return response
Expand All @@ -2751,7 +2766,7 @@ def download_html(request):
raise Http404
response = HttpResponse(
open(phy_path, "rb").read(),
mimetype="text/html")
content_type="text/html")
response["Content-Disposition"] = \
"attachment; filename={0}.html".format(tutorial.slug)
return response
Expand All @@ -2770,7 +2785,7 @@ def download_pdf(request):
raise Http404
response = HttpResponse(
open(phy_path, "rb").read(),
mimetype="application/pdf")
content_type="application/pdf")
response["Content-Disposition"] = \
"attachment; filename={0}.pdf".format(tutorial.slug)
return response
Expand All @@ -2789,7 +2804,7 @@ def download_epub(request):
raise Http404
response = HttpResponse(
open(phy_path, "rb").read(),
mimetype="application/epub")
content_type="application/epub")
response["Content-Disposition"] = \
"attachment; filename={0}.epub".format(tutorial.slug)
return response
Expand Down

0 comments on commit caca71d

Please sign in to comment.