Dealing with a user’s assets.

With everyone building social sites I find it funny that no python framework seems handle user profiles very well.

Normally the code would written as followed:

__init__.py

config.add_route('profile_view', '/:id')
config.add_route('profile_photos', '/:id/photos')

views/profile.py

@view_config(route_name='profile_view', renderer='profile/view.jinja2')
def view(request):
    profile = AuthUser.get_by_id(request.matchdict.get('id'))
    return {
        'profile': profile,
    }

@view_config(route_name='profile_photos', renderer='profile/photos.jinja2')
def photos(request):
    profile = AuthUser.get_by_id(request.matchdict.get('id'))
    return {
        'profile': profile,
    }

Now this code isn’t very DRY, so this is where pyramid_handlers comes in handy.

Same code, using pyramid_handlers.

__init__.py

config.add_handler('profile_view', '/:id', handler='project.views.profile.Profile', action='view')
config.add_handler('profile_photos', '/:id/photos', handler='project.views.profile.Profile', action='photos')

Adding multiple add_handler isn’t very DRY either, so condense the code more.

__init__.py

config.add_handler('profile', '/:id/:action', handler='project.views.profile.Profile')

views/profile.py

class Profile(object):
    def __init__(self, request):
        self.request = request
        self.extra = {}
        self.extra['profile'] = AuthUser.get_by_id(request.matchdict.get('id'))
    
    @action(renderer='profile/view.jinja2')
    def view(self):
        return dict({}, **self.extra)

    @action(renderer='profile/photos.jinja2')
    def photos(self):
        return dict({}, **self.extra)

In response to Wayne’s comment:

This way does use more add_route calls, but it also removes a dependency.

__init__.py

config.add_route('profile_view', '/:id')
config.add_route('profile_photos', '/:id/photos')

views/profile.py

class Profile(object):
    def __init__(self, request):
        self.request = request
        self.extra = {}
        self.extra['user'] = AuthUser.get_by_id(request.matchdict.get('id'))
        
    @view_config(route_name='profile_view', renderer='profile/view.jinja2')
    def view(self):
        return dict({}, **self.extra)        

    @view_config(route_name='profile_photos', renderer='profile/photos.jinja2')
    def photos(self):
        return dict({}, **self.extra)  

Tags: ,

One Response to “Dealing with a user’s assets.”

  1. Wayne Says:

    Good post, though it is a little misleading. You can create class-based views and have your extra dict without using pyramid_handlers. You still end up with multiple add_route callls but it isn’t as bad as you make it out to be in the initial example.

Leave a Reply