Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

bug 1401246: convert to new-style middleware #4841

Merged
merged 2 commits into from Jun 11, 2018
Merged

bug 1401246: convert to new-style middleware #4841

merged 2 commits into from Jun 11, 2018

Conversation

escattone
Copy link
Contributor

@escattone escattone commented Jun 7, 2018

I did some work on our Kuma middleware a short while ago, but when I realized it needed Django 1.11 in place, I shelved it. I thought it was useful, as it mostly embraced the new, functionalclass-style of writing middleware (except ReadOnlyMiddleware), but it's real goal was to remove the dependency on django.utils.deprecation.MiddlewareMixin and go all-in with the new-style. I didn't want to lose the work, which wasn't totally finished, so I added what was missing and decided it was now or never.

@escattone escattone requested a review from jwhitlock June 7, 2018 00:40
@jwhitlock
Copy link
Contributor

I think it would be more straightforward to use the class style for the converted middleware - https://docs.djangoproject.com/en/2.0/topics/http/middleware/#writing-your-own-middleware

@escattone
Copy link
Contributor Author

escattone commented Jun 7, 2018

No problem. I personally prefer the functional style (when there's no chance that the middleware might need to be sub-classed), but the class-style is great too. I'll update this with the class-style.

@escattone escattone changed the title bug 1401246: convert to new-style middleware (WIP) bug 1401246: convert to new-style middleware Jun 7, 2018
@jwhitlock
Copy link
Contributor

I don't have a strong preference for the class-style, I think you can pick and chose as appropriate. I was mostly thinking about a reduced diff, to figure out why tests are failing.

@escattone
Copy link
Contributor Author

I was surprised that the tests failed, as they worked for me locally last night. Sorry, I'll get that resolved.

@escattone
Copy link
Contributor Author

escattone commented Jun 7, 2018

The tests were failing because I had inadvertently removed lines involving adding hostnames to settings.ALLOWED_HOSTS from two tests. That's been fixed and I also updated to use the new class-style, which makes the diff simpler to review as well. Once the tests pass, this is ready for your review again @jwhitlock!

@escattone
Copy link
Contributor Author

FYI. The headless tests passed against my local dev (pytest --base-url http://localhost:8000 tests/headless).

Copy link
Contributor

@jwhitlock jwhitlock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class-based middlewares made this a lot easier to review. All my comments are nits or future work.

translation.activate(language)
request.LANGUAGE_CODE = language
def __call__(self, request):
# Activate the language, based on the request.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've made this comment redundant with the function name 🙂. However, it's not 100% clear from the name that request gains a LANGUAGE_CODE parameter, maybe that's worth including in the comment.


def __init__(self, get_response):
self.get_response = get_response
super(MiddlewareBase, self).__init__()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the super````__init__ is unneeded because the parent is object

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 added it, thinking it would make it bullet-proof for the hypothetical case of this base class being used along with others in a multiple-inheritance case (to keep the chain of MRO calls going)...but I can't imagine a real case where that would be needed!

class MiddlewareBase(object):

def __init__(self, get_response):
self.get_response = get_response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like having this defined once.

"""
Restricts the accessible endpoints based on the host.
"""
def __call__(self, request):
if settings.ENABLE_RESTRICTIONS_BY_HOST and is_untrusted(request):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may make sense to move this to the __init__:

def __init__(self, get_response):
    if not settings.ENABLE_RESTRICTIONS_BY_HOST:
        raise MiddlewareNotUsed

Or, I may be trying too hard to use every piece of the buffalo, and it would have a negative impact on tests...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL ("every piece of the buffalo") Good point! I remember reading about that new feature, and this may just be the case! I like it because if it's not ever going to be used (settings.ENABLE_RESTRICTIONS_BY_HOST = False), it doesn't remain embedded in the middleware stack.

@codecov-io
Copy link

Codecov Report

Merging #4841 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4841      +/-   ##
==========================================
+ Coverage   95.92%   95.92%   +<.01%     
==========================================
  Files         273      273              
  Lines       24538    24549      +11     
  Branches     1763     1763              
==========================================
+ Hits        23537    23548      +11     
  Misses        791      791              
  Partials      210      210
Impacted Files Coverage Δ
kuma/search/tests/__init__.py 85.71% <100%> (ø) ⬆️
kuma/wiki/middleware.py 100% <100%> (ø) ⬆️
kuma/core/i18n.py 100% <100%> (ø) ⬆️
kuma/core/middleware.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0a58382...f72b830. Read the comment docs.

@escattone
Copy link
Contributor Author

@jwhitlock Thanks for the review! I added some changes that you may want to review one more time.

@jwhitlock
Copy link
Contributor

Changes look good, thanks @escattone!

@jwhitlock jwhitlock merged commit cfa4c11 into mdn:master Jun 11, 2018
@escattone escattone deleted the update-middleware-1401246 branch July 16, 2018 15:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants