Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin post processing of uploaded file

I have a standard Django Admin page that is used to upload multiple files. I wish to do the following:

  1. upload some of the files directly
  2. One file need to be encrypted with AES before save (encryption can be done by python or through http to an encryption server.)
  3. A zip file needed to be unzip, process and re-package.

I only have a very basic admin page now. Can anyone point me to the right direction in where to start with? Please point me exactly which file i need to modified as i am still unfamiliar with django.

Just a brief direction will be appreciated. Thank you.

like image 766
Bill Kary Avatar asked Jun 17 '11 03:06

Bill Kary


1 Answers

I didn't test these code, but I can just direct you where to start. I would suggest you to write the code of unzipping at the model's save function. This is the easiest way but not the best. Django admin can handle multiple form as customizing django admin.

I hope your models are somewhat like these

from django.db import models
from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location="/var/www/yoursite/private/")

class SetOfFiles(models.Model):
    name = models.CharField('set name'), max_length=225, null=False, blank=False)

class File(models.Model):
    set = models.ForeignKey(SetOfFiles, verbose_name=_('set'))
    file = models.FileField(storage=fs)

    def save(self, *args, **kwargs):
        if not self.id:
            ... unzip your file ...
            ... encrypt your file if necessary ...
        super(File, self).save(*args, **kwargs)

Create admin.py in the related app customizing your admin to handle multiple insertion:

from django.contrib import admin
class FileInline(admin.TabularInline):
    model = File
class SetOfFilesAdmin(admin.ModelAdmin):
    list_display = ('name',)
    inlines = [FileInline]
admin.site.register(SetOfFiles, SetOfFilesAdmin)

Here is also Unzip a .zip file uploaded with FileBrowser code but it could be a little bit complicated due to using FileBrowser app. You can unzip file just using the zipfile python module. Also you may use PyCrypto at AES encryption.

like image 188
jargalan Avatar answered Oct 26 '22 22:10

jargalan