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
files]

        #save filename to database - THIS IS WORKING
        filename = new_file.filename
        newfile = File(filename=filename)
        DBSession.add(newfile)

        #write text file to corpus directory - THIS IS WHERE MY
PROBLEM IS
        text_path = os.path.join(corpus_path, filename)
        f = file(text_path, 'w')
        f.write(new_file.value)
        f.close()

        #add new text to corpus database - THIS IS WORKING
        text = CorpusText(title=title, authors=authors, year=year,
source=source,\
                          genre_id=genre_id, description=description,
status_id=status_id,\
                          files=files)
        DBSession.add(text)
        flash("Text was successfully added.")
        redirect('./corpus')

-------------------------------------------------------------------------------------
HTML FORM
-------------------------------------------------------------------------------------
        <form action="/add" method="POST" id="add_text"
enctype="multipart/form-data">
            <fieldset>
            <label for="title">Title</label>
            <input class="formInputText" type="text" name="title"
id="title"
value="العنوان" tabindex="1" onfocus="clearField(this);" />
            <label for="authors">Authors</label>
            <select multiple="true" class="formMultiSelect"
name="authors"
id="authors" size="1" tabindex="2" onfocus="clearField(this);">
                <option selected="selected">Select...</option>
                <option py:for="author in authors"
value="$author.author_id">$author.name</option>
            </select>
            <label for="year">Year</label>
            <input class="formInputText" type="text" name="year"
id="year"
value="YYYY" tabindex="3" onfocus="clearField(this);" />
            <label for="source">Source</label>
            <input class="formInputText" type="text" name="source"
id="source" value="station, publisher, website, etc..." tabindex="4"
onfocus="clearField(this);" />
            <label for="genre">Genre</label>
            <select class="formSelect" name="genre_id" id="genre_id"
size="1" tabindex="5" onfocus="clearField(this);">
                <option selected="selected">Select...</option>
                <option py:for="genre in genres"
value="$genre.genre_id">$
genre.name</option>
            </select>
            <label for="description">Description</label>
            <textarea class="formInputText" type="text"
name="description"
id="description" tabindex="6" onfocus="clearField(this);" />
            <label for="status_id">Status</label>
            <select class="formSelect" name="status_id" id="status_id"
size="1" tabindex="7" onfocus="clearField(this);">
                <option selected="selected">Select...</option>
                <option py:for="status in statuses"
value="$status.status_id">$status.status</option>
            </select>
            <label for="file_id">Files</label>
            <select multiple="true" class="formMultiSelect"
name="file_id"
id="file_id" size="1" tabindex="8" onfocus="clearField(this);">
                <option selected="selected">Select...</option>
                <option py:for="file in files"
value="$file.file_id">$file.filename</option>
            </select>
            <label for="new_file">Upload New File</label>
        <input class="formInputText" type="file" name="new_file"
id="new_file" />
            <br />
            <div style="clear:both;">
                <label>&nbsp;</label>
                <input class="formInputButton" type="submit"
value="Add"
tabindex="9"/>
            </div>
            </fieldset>
        </form>

-- 
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.

Reply via email to