Hi guys,
I'm trying to build a simple web app where you can upload a file into
MongoDB (using GridFS) and then get the file with one-click.

This is my model:

Model: mongo.py
---------------
from pymongo import Connection
from gridfs import GridFS

class MongoInterface:

    """Initialize MongoDB connection."""

    def __init__(self):
        global fs
        connection = Connection('localhost', 27017)
        db = connection.mydb
        db.authenticate("myuser","mypasswd")
        fs = GridFS(db)


    def uploadFile(self,filename,data):
        b = fs.put(data, filename=filename)
        out = fs.get(b)
        return out

    def getFile(self,filename):
        b = fs.get_last_version(filename)
        return b

    def getList(self):
        out = fs.list()
        return out

and in default controller, i wrote my methods:

Controller: default.py
----------------------
...

def upload():
    """global mongoId"""
    mongoId = MongoInterface()
    session.myid = mongoId
    """Get file name"""
    c = request.vars.uploadFile.filename
    """Get binary file"""
    file=request.vars.uploadFile.file
    """Upload file into GridFS"""
    file_id = mongoId.uploadFile(c,file)
    file_name = file_id.filename
    file_name = mongoId.getList()
    return dict(message=file_name)

def getmongofile():
    """global mongoId"""
    c = request.args
    mongoId = session.myid
    file = mongoId.getMongoFile(c)

...

Now what happen? Upload files works as well as getting a list of all
uploaded files but when I try to get one of the files, I got this
error:

  file = mongoId.getMongoFile(c)
AttributeError: 'NoneType' object has no attribute 'getMongoFile'

I see that here I have some problems with "global" variable because it
seems that it doesn't recognize mongoId descriptor. What I paste here
is only the last of a long list of tests that I've done (using global
doesn't work I read somewhere in the Web...). How can I maintain my
connection id through different http request in order to instantiate
my MongoDB just one time?

Many thanks in advance for every suggestions,

marcovic

Reply via email to