Make request object available to JSExtension instances
Review Request #8595 — Created Jan. 5, 2017 and submitted
By making the request object available, the extension subclass can e.g. get hold of the current user which can be useful.
Tested that getting a user's groups as discussed in this mail thread works.
Unit tests pass.
Description | From | Last Updated |
---|---|---|
This will ultimately break any existing extensions that don't define this parameter. We'll need to retain compatibility here. I think … |
chipx86 | |
Col: 5 E301 expected 1 blank line, found 0 |
reviewbot | |
Col: 5 E301 expected 1 blank line, found 0 |
reviewbot | |
Let's just call this js_extension_items. That's specific enough to give info on what we're adding, and generic enough for future … |
chipx86 | |
Blank line between these. |
chipx86 | |
Blank line between these. |
chipx86 | |
Just one blank line between these. |
chipx86 | |
This is probably going to get hard to maintain over time, and is a bit wordy. What we can do … |
chipx86 |
-
Thanks for the contribution! I have some notes on the necessary design for this. Let me know if you don't have time to do this, and we'll finish it up on our end. I'd like to get this into 2.5.8.
-
This will ultimately break any existing extensions that don't define this parameter. We'll need to retain compatibility here.
I think JSExtension itself should not expect to be re-instantiated on every page. We may want to cache instances down the road. So I think this parameter should live in
get_model_data()
instead. However, the same compatibility problem applies.What I'd suggest is to update
init_js_extensions
to callget_context_data
itself. Instead of settingjs_extensions
in the context, we'd want to instead pass a list of dictionaries containing two items:1)
js_extension
2)context_data
This way, we can pre-compute this data without the template having to do it, which allows us to pass the request.
Now, about that compatibility issue. We need to conditionally determine whether
get_context_data
can take a request (the new version should actually take arequest
as a keyword argument, and**kwargs
for future expansion).You can check before calling by doing:
arg_spec = inspect.getargspec(js_extension.get_context_data) if arg_spec.keywords is None: warnings.warn('%s.get_context_data will need to take keyword arguments. The old ' 'function signature is deprecated.' % js_extension_cls.__name__) context_data = js_extension.get_context_data() else: context_data = js_extension.get_context_data(request=request)
Something like that.
We'll also need unit tests to ensure both calls work.
- Change Summary:
-
Attempt to address Christian's comment. No unit test though as I don't really know how to add that.
- Commit:
-
7fbc11537f79183fb449f325a78a09c2d1be00c419707cccda9a0487552fbd724bd2443054af4a3f
-
Tool: Pyflakes Processed Files: djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html Tool: PEP8 Style Checker Processed Files: djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html
- Change Summary:
-
Add unit test.
- Testing Done:
-
Tested that getting a user's groups as discussed in this mail thread works.
+ + Unit tests pass.
- Commit:
-
19707cccda9a0487552fbd724bd2443054af4a3f324edbad9ccded5c3adde12a3a03c04f7863fbd6
-
Tool: Pyflakes Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html Tool: PEP8 Style Checker Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html
-
-
-
Tool: Pyflakes Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html Tool: PEP8 Style Checker Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html
-
Thanks for taking on the unit test! Much appreciated :)
Got a few more notes on the new code, but this is pretty close to what I'd like to see for the change. Just a few things for how we style code, and a couple suggestions on how we can represent state for the template and check against it in the test.
-
Let's just call this
js_extension_items
. That's specific enough to give info on what we're adding, and generic enough for future expansion purposes.It should also be a list, so that the ordering is reliable and consistent (which is important). Each entry can be a dictionary with
js_extension
andmodel_data
keys, which the template can access. -
-
-
-
This is probably going to get hard to maintain over time, and is a bit wordy. What we can do instead is check just for the bits that we're looking for. A regex would be a good option for this.
content = t.render(Context({...})) self.assertIsNotNone(re.search( r'new FooNew\({\s+"test": "new",', content)) self.assertIsNotNone(re.search( r'new FooOld\({\s+"test": "old",', content))
etc. That helps us ensure that we have the content we expect in the relative location we expect, without worrying about changes in whitespace or having to match against a huge string.
- Change Summary:
-
Fix comments.
- Commit:
-
c073cc259150ca6c1e7c29cc9f4009e13adfaf6cc20f60782f59fd0e2e3c95b89cde83a6775a95c3
-
Tool: Pyflakes Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html Tool: PEP8 Style Checker Processed Files: djblets/extensions/templatetags/tests.py djblets/extensions/templatetags/djblets_extensions.py djblets/extensions/extension.py Ignored Files: djblets/extensions/templates/extensions/init_js_extensions.html