def perform_create(self, title, physical, legend=""): """Create a new image :param title: title :type title: str :param physical: :type physical: file :param legend: legend (optional) :type legend: str """ if physical.size > settings.ZDS_APP["gallery"]["image_max_size"]: raise ImageTooLarge(title, physical.size) try: ImagePIL.open(physical) except OSError: raise NotAnImage(physical) image = Image() image.gallery = self.gallery image.title = title if legend: image.legend = legend else: image.legend = image.title image.physical = physical image.slug = slugify(title) image.pubdate = datetime.datetime.now() image.save() self.image = image return self.image
def new_image(request, gal_pk): ''' Creates a new image ''' gal = get_object_or_404(Gallery, pk=gal_pk) if request.method == 'POST': form = ImageForm(request.POST, request.FILES) if form.is_valid() \ and request.FILES['physical'].size < settings.IMAGE_MAX_SIZE: img = Image() img.physical = request.FILES['physical'] img.gallery = gal img.title = request.POST['title'] img.slug = slugify(request.FILES['physical']) img.legend = request.POST['legend'] img.pubdate = datetime.now() img.save() # Redirect to the document list after POST return redirect(gal.get_absolute_url()) else: # TODO: add errors to the form and return it raise Http404 else: form = ImageForm() # A empty, unbound form return render_template('gallery/new_image.html', { 'form': form, 'gallery': gal })
def set_avatar_from_file(self, avatar, filename='avatar.png'): """ Updates the avatar of this user from a file, creating a gallery on his account if needed and adding the avatar to the gallery. :param avatar: The avatar file (file-like object). :param filename: The file name, including the type extension. """ user_gallery = UserGallery.objects.filter(gallery__title=ZDS_APP['gallery']['avatars_gallery'], user=self.user)\ .first() if not user_gallery: gallery = Gallery() gallery.title = ZDS_APP['gallery']['avatars_gallery'] gallery.subtitle = '' gallery.slug = slugify(ZDS_APP['gallery']['avatars_gallery']) gallery.pubdate = datetime.now() gallery.save() user_gallery = UserGallery() user_gallery.gallery = gallery user_gallery.mode = GALLERY_WRITE user_gallery.user = self.user user_gallery.save() image = Image() image.title = _('Avatar') image.legend = _('Avatar importé') image.gallery = user_gallery.gallery image.physical = get_thumbnailer(avatar, relative_name=filename) image.pubdate = datetime.now() image.save() self.avatar_url = image.get_absolute_url()
def new_image(request, gal_pk): """Creates a new image.""" gal = get_object_or_404(Gallery, pk=gal_pk) # check if the user can upload new image in this gallery try: gal_mode = UserGallery.objects.get(gallery=gal, user=request.user) if gal_mode.mode != 'W': raise PermissionDenied except: raise PermissionDenied if request.method == "POST": form = ImageForm(request.POST, request.FILES) if form.is_valid() and request.FILES["physical"].size < settings.IMAGE_MAX_SIZE: img = Image() img.physical = request.FILES["physical"] img.gallery = gal img.title = request.POST["title"] img.slug = slugify(request.FILES["physical"]) img.legend = request.POST["legend"] img.pubdate = datetime.now() img.save() # Redirect to the newly uploaded image edit page after POST return redirect(reverse("zds.gallery.views.edit_image", args=[gal.pk, img.pk])) else: return render_template("gallery/image/new.html", {"form": form, "gallery": gal}) else: form = ImageForm(initial={"new_image": True}) # A empty, unbound form return render_template("gallery/image/new.html", {"form": form, "gallery": gal})
def form_valid(self, form): context = self.get_context_data(**self.kwargs) img = Image() img.gallery = context['gallery'] img.title = form.cleaned_data['title'] if form.cleaned_data['legend'] and form.cleaned_data['legend'] != '': img.legend = form.cleaned_data['legend'] else: img.legend = img.title img.physical = self.request.FILES['physical'] img.pubdate = datetime.now() img.save() return redirect(reverse('gallery-image-edit', args=[img.gallery.pk, img.pk]))
def new_image(request, gal_pk): """Creates a new image.""" gal = get_object_or_404(Gallery, pk=gal_pk) # check if the user can upload new image in this gallery try: gal_mode = UserGallery.objects.get(gallery=gal, user=request.user) if gal_mode.mode != 'W': raise PermissionDenied except: raise PermissionDenied if request.method == "POST": form = ImageForm(request.POST, request.FILES) if form.is_valid(): img = Image() img.physical = request.FILES["physical"] img.gallery = gal img.title = request.POST["title"] img.slug = slugify(request.FILES["physical"]) img.legend = request.POST["legend"] img.pubdate = datetime.now() img.save() # Redirect to the newly uploaded image edit page after POST return redirect( reverse("zds.gallery.views.edit_image", args=[gal.pk, img.pk])) else: return render_template("gallery/image/new.html", { "form": form, "gallery": gal }) else: form = ImageForm(initial={"new_image": True}) # A empty, unbound form return render_template("gallery/image/new.html", { "form": form, "gallery": gal })
def form_valid(self, form): context = self.get_context_data() gallery = context['gallery'] archive = self.request.FILES['file'] temp = os.path.join(tempfile.gettempdir(), str(time.time())) if not os.path.exists(temp): os.makedirs(temp) zfile = zipfile.ZipFile(archive, 'a') for i in zfile.namelist(): filename = os.path.split(i)[1] ph_temp = os.path.abspath(os.path.join(temp, os.path.basename(i))) if not filename.strip(): # don't deal with directory continue # create file for image f_im = open(ph_temp, 'wb') f_im.write(zfile.read(i)) f_im.close() (title, ext) = os.path.splitext(os.path.basename(i)) # if size is too large, don't save if os.stat(ph_temp).st_size > settings.ZDS_APP['gallery'][ 'image_max_size']: messages.error( self.request, _(u'Votre image "{}" est beaucoup trop lourde, réduisez sa taille à moins de {:.0f}' u"Kio avant de l'envoyer.").format( title, settings.ZDS_APP['gallery']['image_max_size'] / 1024)) continue # if it's not an image, pass try: ImagePIL.open(ph_temp) except IOError: continue # create picture in database: f_im = File(open(ph_temp, 'rb')) f_im.name = title + ext pic = Image() pic.gallery = gallery pic.title = title pic.legend = '' pic.pubdate = datetime.now() pic.physical = f_im pic.save() f_im.close() if os.path.exists(ph_temp): os.remove(ph_temp) zfile.close() if os.path.exists(temp): shutil.rmtree(temp) return redirect(gallery.get_absolute_url())