Skip to content

Commit

Permalink
Merge pull request #1820 from dpwrussell/decorator_fix
Browse files Browse the repository at this point in the history
Worked around method_decorator problem with custom getattr
  • Loading branch information
joshmoore committed Dec 4, 2013
2 parents b686b5c + 680e690 commit 67598f5
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions components/tools/OmeroWeb/omeroweb/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from django.conf import settings
from django.utils.http import urlencode
from django.utils.functional import wraps
from functools import update_wrapper
from django.utils import simplejson
from django.core.urlresolvers import reverse
from django.template import loader as template_loader
Expand All @@ -54,6 +54,7 @@ def close(self):
except:
logger.error('Failed to clean up connection.', exc_info=True)


class login_required(object):
"""
OMERO.web specific extension of the Django login_required() decorator,
Expand All @@ -75,6 +76,15 @@ def __init__(self, useragent='OMERO.web', isAdmin=False,
self.omero_group = omero_group
self.allowPublic = allowPublic

# To make django's method_decorator work, this is required until
# python/django sort out how argumented decorator wrapping should work
# https://github.com/openmicroscopy/openmicroscopy/pull/1820
def __getattr__(self, name):
if name == '__name__':
return self.__class__.__name__
else:
return super(login_required, self).getattr(name)

def get_login_url(self):
"""The URL that should be redirected to if not logged in."""
return reverse(settings.LOGIN_VIEW)
Expand Down Expand Up @@ -386,7 +396,8 @@ def wrapped(request, *args, **kwargs):
except:
logger.warn('Failed to clean up connection.', exc_info=True)
return retval
return wraps(f)(wrapped)
return update_wrapper(wrapped, f)


class render_response(object):
"""
Expand All @@ -399,6 +410,15 @@ class render_response(object):
or from the request.
"""

# To make django's method_decorator work, this is required until
# python/django sort out how argumented decorator wrapping should work
# https://github.com/openmicroscopy/openmicroscopy/pull/1820
def __getattr__(self, name):
if name == '__name__':
return self.__class__.__name__
else:
return super(render_response, self).getattr(name)

def prepare_context(self, request, context, *args, **kwargs):
""" Hook for adding additional data to the context dict """
pass
Expand Down Expand Up @@ -432,4 +452,5 @@ def wrapper(request, *args, **kwargs):
t = template_loader.get_template(template)
c = RequestContext(request, context)
return HttpResponse(t.render(c))
return wraps(f)(wrapper)
return update_wrapper(wrapper, f)

0 comments on commit 67598f5

Please sign in to comment.