Upload ZIP-File into Database

460 views
Skip to first unread message

Robert

unread,
Feb 8, 2009, 7:24:42 AM2/8/09
to Django users
Dear Community,

I have a ZIP-File which contains five .lst-Files. I want to fill with
these five files a mysql-database. Is there a possibility to handle
this via the admin interface? I tried the following but it doesn't
work:

from django import forms
from django.contrib import admin
from cStringIO import StringIO
import zipfile
from dateiverwaltung.models import UploadZipFile

class UploadZipFileForm(forms.ModelForm):
class Meta:
model = UploadZipFile

zip_file = forms.Field(widget=forms.FileInput())
#zip_file = UploadZipFile.zip_file

def clean_zipfile(zip_file):
msg = 'File must be a valid ZIP-archive'
if zip_file.content_type != 'application/zip':
raise forms.ValidationError( msg )
else:
zipname = zip_file.name
zippfad = zip_file.chunks
zip = zipfile.ZipFile(fname)
assert False
try:
zip = zipfile.ZipFile(StringIO(zip_file.content))
except:
raise forms.ValidationError( "Not able to unpack the
archive" )
bad_file = zip.testzip()
zip.close()
del zip
if bad_file:
raise forms.ValidationError( msg )
return zip_file # Return the clean zip_file

zip_file.clean = clean_zipfile

class UploadZipFileAdmin(admin.ModelAdmin):
form = UploadZipFileForm
list_diplay = ('zip_file', 'uploadtime')

def save_model(self, request, obj, form, change):
pass

admin.site.register(UploadZipFile, UploadZipFileAdmin)

Karen Tracey

unread,
Feb 8, 2009, 9:34:56 AM2/8/09
to django...@googlegroups.com
On Sun, Feb 8, 2009 at 7:24 AM, Robert <meusburg...@aon.at> wrote:

Dear Community,

I have a ZIP-File which contains five .lst-Files. I want to fill with
these five files a mysql-database. Is there a possibility to handle
this via the admin interface? I tried the following but it doesn't
work:

This sort of thing can certainly be done, the django-photologue application (http://code.google.com/p/django-photologue/), for example, has a GalleryUpload model that lets you upload a zip file of images and the zip file is automatically expanded to its constituent images when you save a new GalleryUpload object in the admin.  I have not looked at how exactly it is implemented, but looking at its code might point you in the right direction.

If you want more specific help with the code you've got, giving people a clue what "it doesn't work" looks like would probably help.  I like puzzles, but I don't much like ones that are missing pieces, and leaving out the details of what is going wrong: what you do, what you expect the outcome to be, and what the outcome is instead, including any error messages and tracebacks -- is leaving out a few too many pieces for me to have any interest in looking at your puzzle.

Karen

Robert

unread,
Feb 9, 2009, 4:37:04 AM2/9/09
to Django users
Hi Karen,

I didn't mind to start a quiz...
The problem is that in the code above django seems to control if the
ZIP-File contains Images. I'm not sure but when I take a ZIP-File
which contains Images there is no problem, but when I take my ZIP-File
I always get the error: BadZipfile - File is not a ZIP-File. I've also
looked at the code from django-photologue, but there is the same
problem. I don't figure out where this 'Image-Check' is defined...
As I told before my ZIP-File contains five .lst-Files which contains
the data for my database. How I can I tell django to expand these
files, read them one by one and fill my database with the values?

On Feb 8, 3:34 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Ariel Mauricio Nunez Gomez

unread,
Feb 9, 2009, 9:25:27 AM2/9/09
to django...@googlegroups.com
I did that once and had no problem customizing GalleryUpload for my needs.

Try start writing some code and get back to us if it fails.

http://code.google.com/p/django-photologue/source/browse/trunk/photologue/models.py#194

Ariel.

Karen Tracey

unread,
Feb 9, 2009, 10:55:03 AM2/9/09
to django...@googlegroups.com
On Mon, Feb 9, 2009 at 4:37 AM, Robert <meusburg...@aon.at> wrote:

Hi Karen,

I didn't mind to start a quiz...
The problem is that in the code above django seems to control if the
ZIP-File contains Images. I'm not sure but when I take a ZIP-File
which contains Images there is no problem, but when I take my ZIP-File
I always get the error: BadZipfile - File is not a ZIP-File. I've also
looked at the code from django-photologue, but there is the same
problem. I don't figure out where this 'Image-Check' is defined...
As I told before my ZIP-File contains five .lst-Files which contains
the data for my database. How I can I tell django to expand these
files, read them one by one and fill my database with the values?

This really doesn't clarify much.  "BadZipfile - File is not a ZIP-File" is not a string contained anywhere in the code you posted so doesn't help pinpoint where the code you posted is running into trouble.  Furthermore, from a brief look at your code I don't see where you are doing anything to even try to iterate through the files contained in the zip file and do something with them (your code rather seems to be trying five different ways to 'test' whether it's gotten a valid zipfile?). 

I've not used the python ZipFile support myself, but looking a this:

http://code.google.com/p/django-photologue/source/browse/trunk/photologue/models.py#194

I can see there is a loop 'for filename in zip.namelist():' that then extracts the data for individual files via 'data = zip.read(filename)', and does stuff with the data.  I don't see anything like that in what you have -- Django is not going to do this sort of processing automatically for you, you have to write the code to do it.  And if your problem is you do not know how to manipulate zip files in Python, the right place to ask about that would be someplace like comp.lang.python, not here.  There is nothing Django-specific about manipulating zip files in your application; ZipFile is provided by Python, not Django.

Perhaps you just haven't gotten that far, though, since the message you report sounds like the basic testing of "is this a valid zipfile" is failing.  In which case one thing that would be nice to know is can you test/validate the zip file read from the file system (perhaps using the python shell) using code similar to what you have?  If that too is failing, then you've got a problem with your zipfile being acceptable to Python's zip file support and Django can do nothing about that -- again the right place to look for guidance on that would by a Python forum, not a Django one. 

Karen
 
Reply all
Reply to author
Forward
0 new messages