Why has this approach been taken in this class? - python

In the base class inside generic views django creates a method view at runtime , attaches it to the generic view class and then calls the dispatch method on that class
I did not understand the purpose of this approach , why did not the dispatch method was called directly from the as_view method ?
class View(object):
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace']
def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in kwargs.iteritems():
setattr(self, key, value)
#classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError(u"You tried to pass in the %s method name as a "
u"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError(u"%s() received an invalid keyword %r" % (
cls.__name__, key))
def view(request, *args, **kwargs):
self = cls(**initkwargs)
return self.dispatch(request, *args, **kwargs)
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
self.request = request
self.args = args
self.kwargs = kwargs
return handler(request, *args, **kwargs)
def http_method_not_allowed(self, request, *args, **kwargs):
allowed_methods = [m for m in self.http_method_names if hasattr(self, m)]
logger.warning('Method Not Allowed (%s): %s' % (request.method, request.path),
extra={
'status_code': 405,
'request': self.request
}
)
return http.HttpResponseNotAllowed(allowed_methods)

In class based generic views, any request should have it's own instance of the MyView class.
Here is our urls.py:
from foo.views import AboutView
....
(r'^about/', AboutView.as_view()),
urls.py is imported once per django thread. This means that calling as_view does not create an instance of AboutView.
When a request is processed by the urlconf, the view() method is called and only then an AboutView instance is created, passing and populating it with all the relevant data needed for this particular request.
Using :
(r'^about/', AboutView().dispatch), #WRONG!!!!
will cause all requests to share the same instance of the view, including possible properties that should not be re used by different requests.

Related

Django: Unable to Apply Function View Decorator to Class Based View

I'm migrating from regular function based views, to class based views. One of the things that I couldn't migrate were the decorators I used. The decorator in question checks if the credentials of the current user are valid and then executes the decorated function:
def custom_auth(function):
#wraps(function)
def wrap(request, *args, **kwargs):
# Logic for validating if user has correct credentials
# Fetches the user that accessed the function
user_object = User.objects.get(username=request_username)
# Try to execute the decorated function. If it fails, redirect
# to previous page and show an error popup
try:
return function(request, user=user_object, *args, **kwargs)
except:
# Logic for displaying the popup
Previously I could just decorate my function by doing
#custom_auth
def view(request, *args, **kwargs):
# View logic
However, when I try to apply it to my class based view in the same way, I get an error saying __init__() takes 1 positional argument but 2 were given: user='username', view='cbvview'
#custom_auth
class CBV(View):
def get(self, request, *args, **kwargs):
# Get request logic
I know that this is not the way you are supposed to apply the decorator, so I tried with different approaches. Either adding the decorator to urls.py, adding the #method_decorator(custom_auth, name="dispatch") or simply overriding the dispatch method, but none of them work. All of them give me the same error.
What could be the issue? Maybe I should use a custom mixin instead?
Use #method_decorator like following:
from django.utils.decorators import method_decorator
#method_decorator(custom_auth,name="dispatch")
class CBV(View):
def get(self, request, *args, **kwargs):
...
Edit:
Try to make your own mixin as class and inherit it to be used in CBV class so:
class CustomAuthMixin:
def dispatch(self, request, *args, **kwargs):
# Logic for validating if user has correct credentials
# Fetches the user that accessed the function
user_instance = User.objects.get(username=request_username)
# Try to execute the decorated function. If it fails, redirect
# to previous page and show an error popup
try:
return super().dispatch(request, user=user_instance, *args, **kwargs)
except:
# Logic for displaying the popup
class CBV(CustomAuthMixin, View):
def get(self, request, *args, **kwargs):
# Get request logic
from django docs https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/
#method_decorator(login_required, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'
and there is no need to add the user to the function call in the decorator. It is already in the request

Unable to access request headers in decorator - django 4.0.4

Using class based views. I have a decorator that retrieves headers for verification. However I get this error accessing the request headers in the decorator:
Decorator exception 'DetailsView' object has no attribute 'headers'
I must emphasize that accessing request headers in the view function works fine.
View function:
class DetailsView(View):
#check_access
def get(self, request):
res = {
'status': 200,
'response': 'heeaders test.',
'test': 'okay'
}
return HttpResponse(JsonResponse(res, safe=False), content_type='text/json')
Decorator:
def check_access():
def decorator(view_function):
def wrap(request, *args, **kwargs):
try:
print("headers", request.headers)
return view_function(request, *args, **kwargs)
except Exception as e:
return HttpResponse('Unauthorized', status=401)
return wrap
return decorator
Delete 'def check_access():' and Change decorate function to check_access.
And move 'self' argument.
in View Function
class DetailsView(View):
#check_access
def get(request): # Delete Self
....
in decorator
def check_access(view_function):
def wrap(self, request, *args, **kwargs): # add self
...
return wrap
referenced site : Decorator

Spyne, Django change WSDL url

I am using django behind nginx reverse proxy and django sees the server url different than what it actually is hosted on like:
Django: http://webserver.com
Nginx: https://webserver.com
When I try to add the WSDL to SoapUI it automatically defaults to the first http://webserver.com server and then all the requests fail. I have tried the code below, but it did not work:
...
app = Application(
[EXTWS],
tns='soap.views',
in_protocol=Soap11(validator='soft'),
out_protocol=Soap11(),
)
app.transport = "no_transport_at_all"
...
wsdl = Wsdl11(app.interface)
if os.environ.get("DEBUG"):
wsdl.build_interface_document('http://localhost:8000/wsdl/')
else:
url = f'https://{settings.DOMAIN}/wsdl/'
wsdl.build_interface_document(url)
Inspirations: here and here
EDIT:
Looks like the code above achieves some things but the resulting WSDL document when accessed in browser is still the same, maybe it is generated on request; the documentation said "... Spyne will get the URL from the first request, build the wsdl on-the-fly and cache it as a string in memory for later requests." but here it is generated manually, so it should not generate a new one maybe? Or it is generating it by request because it is django, not wsgi.
EDIT:
Looks like building the tree by hand does not make any difference as when you send the first requests, a new instance of Wsdl11 class is generated.
Temporarily, I achieved changing the url to what I want by basically monkey patching the two classes as follows:
from functools import update_wrapper
from spyne.server.http import HttpBase, HttpMethodContext, HttpTransportContext
from spyne.application import get_fault_string_from_exception, Application
from django.http import HttpResponse, HttpResponseNotAllowed, Http404
class MonkeyDjangoServer(DjangoServer):
def handle_wsdl(self, request, *args, **kwargs):
"""Return services WSDL."""
ctx = HttpMethodContext(self, request,
'text/xml; charset=utf-8')
if self.doc.wsdl11 is None:
raise Http404('WSDL is not available')
if self._wsdl is None:
# Interface document building is not thread safe so we don't use
# server interface document shared between threads. Instead we
# create and build interface documents in current thread. This
# section can be safely repeated in another concurrent thread.
self.doc.wsdl11.service_elt_dict = {}
# here you can put whatever you want
self.doc.wsdl11.build_interface_document("http://MONKEY/")
wsdl = self.doc.wsdl11.get_interface_document()
if self._cache_wsdl:
self._wsdl = wsdl
else:
wsdl = self._wsdl
ctx.transport.wsdl = wsdl
response = HttpResponse(ctx.transport.wsdl)
return self.response(response, ctx, ())
class MonkeyDjangoView(DjangoView):
#classmethod
def as_view(cls, **initkwargs):
"""Register application, server and create new view.
:returns: callable view function
"""
# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__,
key))
def get(key):
value = initkwargs.get(key)
return value if value is not None else getattr(cls, key)
def pop(key):
value = initkwargs.pop(key, None)
return value if value is not None else getattr(cls, key)
application = get('application') or Application(
services=get('services'),
tns=get('tns'),
name=get('name'),
in_protocol=get('in_protocol'),
out_protocol=get('out_protocol'),
)
server = pop('server') or MonkeyDjangoServer(application,
chunked=get('chunked'),
cache_wsdl=get('cache_wsdl'))
def view(request, *args, **kwargs):
self = cls(server=server, **initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
urlpatterns = [
url(r'^EXTWS/$', MonkeyDjangoView.as_view(application=app)),
# url(r'^schema/$', get_schema),
]
This is some unacceptable kind of solution so I will be waiting for a logical implementation of this behavior. Until then, I will be using this.

Where does Django request object originated in `class View`?

Here is the code segment I am referring to
#classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
I am looking for the code that the request object is passed in to.
The common place where as_view is used is in url
However I couldn't reference to request object in
def url(regex, view, kwargs=None, name=None, prefix=''):
if isinstance(view, (list, tuple)):
# For include(...) processing.
urlconf_module, app_name, namespace = view
return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
else:
if isinstance(view, six.string_types):
warnings.warn(
'Support for string view arguments to url() is deprecated and '
'will be removed in Django 1.10 (got %s). Pass the callable '
'instead.' % view,
RemovedInDjango110Warning, stacklevel=2
)
if not view:
raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex)
if prefix:
view = prefix + '.' + view
return RegexURLPattern(regex, view, kwargs, name)
Can someone point me a direction?
Note that the request is never passed to as_view().
The as_view() method is called when the url config is loaded, before any requests are handled. It defines a method view, and returns it.
def view(request, *args, **kwargs):
...
return view
This view method takes an argument request, positional and keyword arguments. The view method is then passed to the url instance. Note that url simply requires a callable that takes a request argument. This could be a callable returned by calling as_view() for a class based view, or a regular function based view, it makes no difference to how the request is passed to the view.
def function_view(request, *args, **kwargs):
return HttpResponse("I'm a function based view")
url(r'^cbv/$', MyView.as_view()),
url(r'^fv/$', function_view),
Then, when a request is handled, the url is resolved into this view, and BaseHandler.get_response calls the view with the request, and args and kwargs captured from the url.
The request is processed by BaseHandler.get_response:
wrapped_callback = self.make_view_atomic(callback)
try:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
The request is created from the WSGIHandler class.
James Bennett talks about this in Django In Depth at around 2 hours and 14 minutes. Slides can be found here.

Django Class-based mixin view does not return a HttpResponse object

Trying to use a mixin to create a class-based view in Django but keep getting the following error message:
ValueError: The view twitter.views.TwitterExampleView didn't return an HttpResponse object.
As shown in the code below, I extend from View creating a base TwitterView for the app to handle error messages being returned from Twitter. That view is further extended for a TwitterNetworkView where a Twitter network is attached to the view. The other classes allow for some transformation to happen to parameters passed into requests. The final class, the TwitterExampleView, includes both the mixin for parameter transformation and the TwitterNetworkView. It's get method returns as a placeholder a string "blah". Only when it bubbles back into the dispatch is the response rendered so what am I overlooking?
class TwitterView(View):
def dispatch(self, request, *args, **kwargs):
try:
response = super(TwitterView, self).dispatch(request, *args, **kwargs)
return render_response(request, response)
except TwitterApiException, e:
return return_error(request, e, e.status_code)
class TwitterNetworkView(TwitterView):
def dispatch(self, request, *args, **kwargs):
self.network = get_network_or_404(request.user, kwargs['network_id'])
super(TwitterNetworkView, self).dispatch(request, *args, **kwargs)
class DefineParamsMixin(object):
def get_params(self):
return null
class TwitterPagedDefineParams(DefineParamsMixin):
def get_params(self):
return define_params(
Param('page'),
Param('since'),
Param('before'),
Param('limit', transform_func=int)
)
class TwitterExampleView(TwitterPagedDefineParams, TwitterNetworkView):
def get(self, request, *args, **kwargs):
return "blahhhhh"
You're not returning anything from dispatch in your TwitterNetworkView mixin. With no return statement, the method returns None rather than an HttpResponse. One fix would be to have it return the result of super(TwitterNetworkView, self).dispatch(request, *args, **kwargs).
You need to return an HttpResponse object.
from django.http import HttpResponse
...
class TwitterExampleView(TwitterPagedDefineParams, TwitterNetworkView):
def get(self, request, *args, **kwargs):
return HttpResponse("blahhhhh")
Take a look at https://docs.djangoproject.com/en/1.7/intro/tutorial03/#write-your-first-view

Categories

Resources