On Nov 21, 2010, at 4:25 AM, Karen McNeil wrote:
> I have a form to add new rows to my database which is working fine,
> and I'm
> trying to add a file upload field to it. I'm following the Movie
> tutorial,
> with a few changes. (My main table is CorpusText, and the filenames go
> in a
> related table named File, since each text can have multiple associated
> files. The actual file is saved in a folder, not in the database.)
>
> I've got it half working: the form field is working fine, and the
> filename
> is successfully added to the database. The problem comes up when I
> added
> the code to actually save the file to the folder where it should be; I
> get
> the error: *"TypeError: 'unicode' object is not callable."
>
> *The files that I'm working with are all in Arabic, and so are 'utf-8'
> text
> files. (I assume that this is where the problem is, since the tutorial
> is made for an image file.)
>
> I've searched on Google and haven't found any solutions... Any ideas?
>
> Thanks,
> Karen
>
> PS - I started out using Widgets and Sprox and trying it all that
> way, but
> was having too many problems. That's why I'm just doing a plain HTML
> form.
>
> -------------------------------------------------------------------------------
> CONTROLLER CODE
> -------------------------------------------------------------------------------
>
> corpus_path =
> os.path.join(os.path.abspath(resource_filename('tunisian',
> 'corpus')))
>
> @expose()
> def add(self, title, authors, genre_id, description, status_id,
> file_id,
> new_file=None, year=None,source=None):
> if genre_id is not None:
> genre_id = int(genre_id)
> if status_id is not None:
> status_id = int(status_id)
> if year is not None:
> year = int(year)
> if authors is not None:
> if not isinstance(authors, list):
> authors = [authors]
> authors = [DBSession.query(Author).get(author) for author
> in
> authors]
> if file_id is not None:
> if not isinstance(file_id, list):
> files = [file_id]
> files = [DBSession.query(File).get(file) for file in
You should really try & either use tw.forms or at least a validators dictionary
to make the above code much simpler + more declarative. Like this:
@expose()
@validate(dict(genre_id=Int(), status_id=Int(),
authors=ForEach(convert_to_list=True)))
...
> files]
>
> #save filename to database - THIS IS WORKING
> filename = new_file.filename
> newfile = File(filename=filename)
Having the variables "new_file" and "newfile" is really confusing.
> DBSession.add(newfile)
>
> #write text file to corpus directory - THIS IS WHERE MY
> PROBLEM IS
> text_path = os.path.join(corpus_path, filename)
Bad idea. Use a hash or id-based filename here, because otherwise you will end
up with users overwriting their files.
> f = file(text_path, 'w')
> f.write(new_file.value)
> f.close()
I have difficulties believing that this can cause your problem. new_file.value
is a str-object (there is nothing that communicates the file-contents encoding,
so there is no chance for a decoded unicode object)
A small test-controller-action I created works just fine with the above.
So please post the *full*traceback, and make sure the code shown here is really
the one you are testing.
Diez
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en.