-
Notifications
You must be signed in to change notification settings - Fork 45
Make InertiaRequest inherit from HttpRequest (#78) #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from json import dumps as json_encode | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from django.http import HttpResponse | ||
from django.http import HttpRequest, HttpResponse | ||
from django.template.loader import render_to_string | ||
|
||
from .helpers import deep_transform_callables, validate_type | ||
|
@@ -25,20 +25,17 @@ | |
INERTIA_SSR_TEMPLATE = "inertia_ssr.html" | ||
|
||
|
||
class InertiaRequest: | ||
class InertiaRequest(HttpRequest): | ||
def __init__(self, request): | ||
self.request = request | ||
|
||
def __getattr__(self, name): | ||
return getattr(self.request, name) | ||
|
||
@property | ||
def headers(self): | ||
return self.request.headers | ||
super().__init__() | ||
self.__dict__.update(request.__dict__) | ||
BrandonShar marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, this is a clean way of handling this; I thought it might be more complex. |
||
|
||
@property | ||
def inertia(self): | ||
return self.request.inertia.all() if hasattr(self.request, "inertia") else {} | ||
inertia_attr = self.__dict__.get("inertia") | ||
BrandonShar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
inertia_attr.all() if inertia_attr and hasattr(inertia_attr, "all") else {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the refactor equivalent should be something like return self.inertia.all() if hasattr(self, "inertia") else {} does that look right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I originally tried something like that, but it ends up in a The current code is a bit of a workaround as it seems the more idiomatic way is to rename the attribute to def share(request, **kwargs):
if not hasattr(request, "_inertia"):
request._inertia = InertiaShare()
request._inertia.set(**kwargs) Then it could be called using return self._inertia.all() if hasattr(self, "_inertia") else {} Do you want me to make these updates or stick to the current code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahhh yeah I get that. Ok, I think this makes sense and maybe we'll come back to it later. Thanks for double checking! |
||
) | ||
|
||
def is_a_partial_render(self, component): | ||
return ( | ||
|
@@ -58,7 +55,7 @@ def is_inertia(self): | |
def should_encrypt_history(self): | ||
return validate_type( | ||
getattr( | ||
self.request, | ||
self, | ||
INERTIA_REQUEST_ENCRYPT_HISTORY, | ||
settings.INERTIA_ENCRYPT_HISTORY, | ||
), | ||
|
Uh oh!
There was an error while loading. Please reload this page.