Skip to content

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions inertia/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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")
return (
inertia_attr.all() if inertia_attr and hasattr(inertia_attr, "all") else {}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 RecursionError as the property repeatedly calls itself.

The current code is a bit of a workaround as it seems the more idiomatic way is to rename the attribute to self._inertia, but that would require updating share.py. It could be done with something similar to this.

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 (
Expand All @@ -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,
),
Expand Down
Loading