Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import d'une archive d'images dans une galerie #1410

Merged
merged 5 commits into from Sep 7, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added fixtures/archive-gallery.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions templates/gallery/gallery/details.html
Expand Up @@ -33,6 +33,9 @@
<a href="{% url "zds.gallery.views.new_image" gallery.pk %}" class="new-btn ico-after more blue">
Ajouter une image
</a>
<a href="{% url "zds.gallery.views.import_image" gallery.pk %}" class="new-btn ico-after more blue">
Importer une archive
</a>
{% endblock %}


Expand Down
22 changes: 22 additions & 0 deletions zds/gallery/forms.py
Expand Up @@ -151,6 +151,28 @@ def clean(self):
return cleaned_data


class ArchiveImageForm(forms.Form):
file = forms.FileField(
label='Sélectionnez l\'archive contenant les images à charger',
required=True
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il y aurait moyen d'imposer un .zip direct dans l'uploader ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non, car il faudrait un minimum avoir déjà téléchargé le fichier pour vérifier que c'est bien un zip. On pourrait vérifier juste l'extension dans le formulaire, mais ça ne serait pas efficace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est déjà un minimum de vérifier l'extension.

2014-09-01 23:02 GMT+02:00 firm1 notifications@github.com:

In zds/gallery/forms.py:

@@ -151,6 +151,28 @@ def clean(self):
return cleaned_data

+class ArchiveImageForm(forms.Form):

  • file = forms.FileField(
  •    label='Sélectionnez l\'archive contenant les images à charger',
    
  •    required=True
    
  • )

non, car il faudrait un minimum avoir déjà téléchargé le fichier pour
vérifier que c'est bien un zip. On pourrait vérifier juste l'extension dans
le formulaire, mais ça ne serait pas efficace


Reply to this email directly or view it on GitHub
https://github.com/zestedesavoir/zds-site/pull/1410/files#r16964462.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est a ca que je pensais, avoir un filtre ".zip" comme on en trouve tant (histoire au moins de limiter les chargements par erreurs) afin que l'utilisateur sache quoi charger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est a ca que je pensais, avoir un filtre ".zip" comme on en trouve tant (histoire au moins de limiter les chargements par erreurs) afin que l'utilisateur sache quoi charger

pourquoi pas. C'est fait

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi ne pas prendre en charge les .tar.gz qui sont tout aussi répandus à ce moment là ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi ne pas prendre en charge les .tar.gz qui sont tout aussi répandus à ce moment là ?

parce que le zip est à la portée de tout le monde.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Et que techniquement c'est très facile de lire un ZIP, alors que le .tar.gz
pose des problèmes (à commencer par le fait qu'il faut le décompresser
intégralement pour pouvoir en faire quoi que ce soit).

2014-09-02 15:34 GMT+02:00 firm1 notifications@github.com:

In zds/gallery/forms.py:

@@ -151,6 +151,28 @@ def clean(self):
return cleaned_data

+class ArchiveImageForm(forms.Form):

  • file = forms.FileField(
  •    label='Sélectionnez l\'archive contenant les images à charger',
    
  •    required=True
    
  • )

Pourquoi ne pas prendre en charge les .tar.gz qui sont tout aussi
répandus à ce moment là ?

parce que le zip est à la portée de tout le monde.


Reply to this email directly or view it on GitHub
https://github.com/zestedesavoir/zds-site/pull/1410/files#r16986504.


def __init__(self, *args, **kwargs):
super(ArchiveImageForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'clearfix'
self.helper.form_method = 'post'

self.helper.layout = Layout(
Field('file'),
ButtonHolder(
StrictButton('Importer', type='submit'),
HTML('<a class="btn btn-cancel" '
u'href="{{ gallery.get_absolute_url }}">Annuler</a>'),
),
)


class UpdateImageForm(ImageForm):
def __init__(self, *args, **kwargs):
super(ImageForm, self).__init__(*args, **kwargs)
Expand Down
14 changes: 13 additions & 1 deletion zds/gallery/tests/tests_forms.py
Expand Up @@ -5,7 +5,7 @@
from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile

from zds.gallery.forms import GalleryForm, UserGalleryForm, ImageForm, ImageAsAvatarForm
from zds.gallery.forms import GalleryForm, UserGalleryForm, ImageForm, ImageAsAvatarForm, ArchiveImageForm
from zds.member.factories import ProfileFactory
from zds import settings

Expand Down Expand Up @@ -81,6 +81,18 @@ def test_valid_image_form(self):

self.assertTrue(form.is_valid())
upload_file.close()

def test_valid_archive_image_form(self):
upload_file = open(os.path.join(settings.SITE_ROOT, 'fixtures', 'archive-gallery.zip'), 'r')

data = {}
files = {
'file': SimpleUploadedFile(upload_file.name, upload_file.read())
}
form = ArchiveImageForm(data, files)

self.assertTrue(form.is_valid())
upload_file.close()

def test_empty_title_image_form(self):
upload_file = open(os.path.join(settings.SITE_ROOT, 'fixtures', 'logo.png'), 'r')
Expand Down
36 changes: 36 additions & 0 deletions zds/gallery/tests/tests_views.py
Expand Up @@ -616,3 +616,39 @@ def test_fail_gallery_not_exist(self):
)

self.assertEqual(404, response.status_code)

def test_import_images_in_gallery(self):
login_check = self.client.login(username=self.profile1.user.username, password='hostel77')
self.assertTrue(login_check)

with open(os.path.join(settings.SITE_ROOT, 'fixtures', 'archive-gallery.zip'), 'r') as fp:
response = self.client.post(
reverse(
'zds.gallery.views.import_image',
args=[self.gallery.pk]
),
{
'file': fp
},
follow=False
)
self.assertEqual(302, response.status_code)
self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 1)

def test_denies_import_images_in_gallery(self):
login_check = self.client.login(username=self.profile2.user.username, password='hostel77')
self.assertTrue(login_check)

with open(os.path.join(settings.SITE_ROOT, 'fixtures', 'archive-gallery.zip'), 'r') as fp:
response = self.client.post(
reverse(
'zds.gallery.views.import_image',
args=[self.gallery.pk]
),
{
'file': fp
},
follow=True
)
self.assertEqual(403, response.status_code)
self.assertEqual(Image.objects.filter(gallery=self.gallery).count(), 0)
2 changes: 2 additions & 0 deletions zds/gallery/urls.py
Expand Up @@ -21,4 +21,6 @@
'zds.gallery.views.delete_image'),
url(r'^image/editer/(?P<gal_pk>\d+)/(?P<img_pk>\d+)/$',
'zds.gallery.views.edit_image'),
url(r'^image/importer/(?P<gal_pk>\d+)/$',
'zds.gallery.views.import_image'),
)
76 changes: 75 additions & 1 deletion zds/gallery/views.py
Expand Up @@ -11,13 +11,19 @@
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.shortcuts import redirect, get_object_or_404
from zds.gallery.forms import ImageForm, UpdateImageForm, GalleryForm, UserGalleryForm, ImageAsAvatarForm
from zds.gallery.forms import ArchiveImageForm, ImageForm, UpdateImageForm, GalleryForm, UserGalleryForm, ImageAsAvatarForm
from zds.gallery.models import UserGallery, Image, Gallery
from zds.tutorial.models import Tutorial
from zds.member.decorator import can_write_and_read_now
from zds.utils import render_template
from zds.utils import slugify

from django.core.files import File
from zds.tutorial.models import Tutorial
import zipfile
import shutil
import os
from django.db import transaction


@login_required
Expand Down Expand Up @@ -297,3 +303,71 @@ def new_image(request, gal_pk):
form = ImageForm(initial={"new_image": True}) # A empty, unbound form
return render_template("gallery/image/new.html", {"form": form,
"gallery": gal})

@can_write_and_read_now
@login_required
@transaction.atomic
def import_image(request, gal_pk):
"""Create images from zip archive."""

gal = get_object_or_404(Gallery, pk=gal_pk)

try:
gal_mode = UserGallery.objects.get(gallery=gal, user=request.user)
if gal_mode.mode != 'W':
raise PermissionDenied
except:
raise PermissionDenied

#if request is POST
if request.method == "POST":
form = ArchiveImageForm(request.POST, request.FILES)
if form.is_valid():
archive = request.FILES["file"]
temp = os.path.join(settings.SITE_ROOT, "temp")
if not os.path.exists(temp):
os.makedirs(temp)
zfile = zipfile.ZipFile(archive, "a")
for i in zfile.namelist():
ph_temp = os.path.abspath(os.path.join(temp, i))
(dirname, filename) = os.path.split(i)
if filename.strip() == "":
if not os.path.exists(ph_temp):
os.makedirs(ph_temp)
continue
data = zfile.read(i)
fp = open(ph_temp, "wb")
fp.write(data)
fp.close()
title = os.path.basename(i)
# if size is too large don't save
if os.stat(ph_temp).st_size > settings.IMAGE_MAX_SIZE:
messages.error(request, u"L'image {} n'a pas pu être importée dans la galérie car elle est beaucoup trop lourde".format(title))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo : galérie (et message bien trop long pour future pep)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

corrigé

et message bien trop long pour future pep

de toute façon faudra repasser une pep générale, et bloquer les autres PR non pep8 donc ça sera fait a ce moment là

continue

f = File(open(ph_temp, "rb"))
f.name = title
# create picture in database
pic = Image()
pic.gallery = gal
pic.title = title
pic.pubdate = datetime.now()
pic.physical = f
pic.save()
f.close()

zfile.close()

if os.path.exists(temp):
shutil.rmtree(temp)

# Redirect to the newly uploaded gallery
return redirect(reverse("zds.gallery.views.gallery_details",
args=[gal.pk, gal.slug]))
else:
return render_template("gallery/image/new.html", {"form": form,
"gallery": gal})
else:
form = ArchiveImageForm(initial={"new_image": True}) # A empty, unbound form
return render_template("gallery/image/new.html", {"form": form,
"gallery": gal})