Bem vindo!

ao blog Willem Allan

Utilizando django-admin-multiupload

Instalação e Configuração

Instalar em seu env

pip install git+git://github.com/gkuhn1/django-admin-multiupload.git

Add ‘multiupload’ to your INSTALLED_APPS

 
INSTALLED_APPS = (
    'multiupload',
)

Exemplo

model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from django.db import models
from sorl.thumbnail import ImageField
from django.template.defaultfilters import slugify
 
def rename_file_and_upload_to(objeto, arquivo):
	"""
	Essa função irá normalizar como um slug, o nome do arquivo que está sendo gravado e, irá gravá-lo
	em /media/uploads/APPNAME_CLASSNAME/nome_do_arquivo_normalizado.extensao
	"""
	import os
	caminho = str( '%s/%s' % ( objeto._meta.app_label, objeto.__class__.__name__ ) ).lower()
	nome, ext = os.path.splitext( arquivo )
	url = slugify( nome[:15] )
	return os.path.join( 'uploads', caminho, url+ext )
 
class Imagem(models.Model):
 
    class Meta:
        ordering = ('nome', )
        verbose_name = 'Imagem'
        verbose_name_plural = 'Imagens'
 
    nome = models.CharField(max_length=255)
    img = ImageField(
        u'Imagem', upload_to=rename_file_and_upload_to, max_length=255
    )
 
    def imagemAdmin(self):
        from sorl.thumbnail import get_thumbnail
        if self.img:
            try:
                im = get_thumbnail(self.img, '100x70', quality=80)
                return '<img src="{0}" />'.format(im.url)
            except:
                return ''
        return ''
    imagemAdmin.is_safe = True
    imagemAdmin.allow_tags = True
    imagemAdmin.short_description = u'Imagem'
 
    def imagem(self):
        from django.conf import settings
        from sorl.thumbnail import get_thumbnail
 
        if self.img:
            try:
                im = get_thumbnail(self.img, '80x45', quality=80)
                return im.url
            except:
                return ''
 
        return ''
 
    def __unicode__(self):
        return self.nome
 
    def url(self):
        return reverse('imagem', args=(self.slug,))

admin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
from django.contrib import admin
from multiupload.admin import MultiUploadAdmin
from sorl.thumbnail.admin import AdminImageMixin
from models import Imagem
 
 
class ImagemAdmin(AdminImageMixin, MultiUploadAdmin):
    list_display = ('nome', 'imagemAdmin', )
 
    # default value of all parameters:
    change_form_template = 'multiupload/change_form.html'
    change_list_template = 'multiupload/change_list.html'
    multiupload_template = 'multiupload/upload.html'
    # if true, enable multiupload on list screen
    # generaly used when the model is the uploaded element
    multiupload_list = True
    # if true enable multiupload on edit screen
    # generaly used when the model is a container for uploaded files
    # eg: gallery
    # can upload files direct inside a gallery.
    multiupload_form = True
    # max allowed filesize for uploads in bytes
    # 3 Mb
    multiupload_maxfilesize = 3 * 2 ** 20
    # min allowed filesize for uploads in bytes
    multiupload_minfilesize = 0
    # tuple with mimetype accepted
    multiupload_acceptedformats = (
        "image/jpeg",
        "image/pjpeg",
        "image/png",
    )
 
    def process_uploaded_file(self, uploaded, object, request):
 
        # example:
        title = request.POST.get('title', '') or uploaded.name
        f = Imagem(nome=title, img=uploaded)
        f.save()
        return {
            'url': f.imagem(),
            'thumbnail_url': f.imagem(),
            'id': f.id,
            'name': f.nome,
        }
 
    def delete_file(self, pk, request):
        '''
        Function to delete a file.
        '''
        # This is the default implementation.
        obj = get_object_or_404(self.queryset(request), pk=pk)
        obj.delete()
 
admin.site.register(Imagem, ImagemAdmin)