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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo : galérie (et message bien trop long pour future pep) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
corrigé
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}) |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pourquoi pas. C'est fait
There was a problem hiding this comment.
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à ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parce que le zip est à la portée de tout le monde.
There was a problem hiding this comment.
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: