BigBoy1337
BigBoy1337

Reputation: 4963

Pyramid: How can I making a static view to some absolute path, and then let users upload files to that path?

In my view callable, I want users to be able to create a new file called filename like so:

@view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
    if 'form.submitted' in request.params:
        name= request.params['name']

        input_file=request.POST['stl'].filename
        vertices, normals = [],[]
        for line in input_file:
            parts = line.split()
            if parts[0] == 'vertex':
                vertices.append(map(float, parts[1:4]))
            elif parts[0] == 'facet':
                normals.append(map(float, parts[2:5]))

        ordering=[]
        N=len(normals)

        ...parsing data...

        data=[vertices,ordering]
        jsdata=json.dumps(data)
        renderer_dict = dict(name=name,data=jsdata)
        app_dir = request.registry.settings['upload_dir']
        filename =  "%s/%s" % ( app_dir , name )
        html_string = render('tutorial:templates/view.pt', renderer_dict, request=request)
        with open(filename,'w') as file:
                file.write(new_comment)
        return HTTPFound(location=request.static_url('tutorial:pages/%(pagename)s.html' % {'pagename': name}))

    return {}   

right now, when I attempt to upload a file, I am getting this error message: IOError: [Errno 2] No such file or directory: u'/path/pages/one' (one is the name variable) I believe this is because I am incorrectly defining the app_dir variable. I want filename to be the url of the new file that is being created with the name variable that is defined above (so that it can be accessed at www.domain.com/pages/name). Here is the file structure of my app:

 env
    tutorial
        tutorial
            templates
                home.pt
            static
                pages
                    (name1)
                    (name2)
                    (name3)
                     ....
            views.py
            __init__.py

In my init.py I have:

config.add_static_view(name='path/pages/', path=config.registry.settings['upload_dir'])

In my development.ini file I have

[app:main]
use = egg:tutorial

upload_dir = /path/pages

Edit: If anyone has an idea on why this question isn't getting much attention, I would love to hear it.

Upvotes: 2

Views: 857

Answers (1)

Michael Merickel
Michael Merickel

Reputation: 23331

While I feel like you probably have a misunderstanding of how to serve up user-generated content, I will show you a way to do what you're asking. Generally user-generated content would not be uploaded into your source, you'll provide some configurable spot outside to place it, as I show below.

Make the path configurable via your INI file:

[app:main]
use = egg:tutorial

upload_dir = /path/to/writable/upload/directory

Add a static view that can serve up files under that directory.

config.add_static_view(name='/url/to/user_uploads', path=config.registry.settings['upload_dir'])

In your upload view you can get your app_dir via

app_dir = request.registry.settings['upload_dir']

Copy the data there, and from then on it'll be available at /url/to/user_uploads/filename.

Upvotes: 4

Related Questions