Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Add Django Util With Decorator and Views #332

Merged
merged 1 commit into from
Dec 4, 2015

Conversation

waprin
Copy link
Contributor

@waprin waprin commented Oct 22, 2015

This Django extension adds the views, decorators, and signals to oauth2client.

This is heavily based on the work @jonparrott did for oauth2client/flask_util.

Currently the only storage offered uses Django sessions. Based on issue #319 more storage options are planned along with integration to the existing CredentialsField in django_orm.

This adds an environment variable to the tox script. This might not be strictly necessary, but it did simplify the tests a lot, since Django settings get looked up at import time and cached so it's easiest if they have an environment variable pointing to an existing settings module when the tests are started.

This could have been squeezed into a single file, but I spilt it into separate files based on existing Django extension conventions.

sys.modules['django_settings'] = django_settings = imp.new_module(
'django_settings')
django_settings.SECRET_KEY = 'xyzzy'
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_django_settings'

This comment was marked as spam.

@dhermes
Copy link
Contributor

dhermes commented Oct 23, 2015

All new files added should have 100% coverage (towards #212).

From:
https://coveralls.io/builds/3925551

The following have less than 100%:

  • django_util/apps.py: 0%
  • django_util/storage.py: 89.47%
  • tests/test_django_util.py: 98.05% (every line that isn't run should be removed or pragma: NO COVERed)

@dhermes
Copy link
Contributor

dhermes commented Oct 23, 2015

Running tox -e cover locally it also looks like

oauth2client/django_util/storage.py         19      2      4      0    83%   51-52
oauth2client/django_util/views.py           62      0     10      1    99%   133->135

@dhermes
Copy link
Contributor

dhermes commented Oct 23, 2015

If running the tests create db.sqlite3, you should add it to the .gitignore.

@waprin waprin force-pushed the django_util branch 3 times, most recently from 0ff07d0 to 416a548 Compare October 23, 2015 21:25
@waprin
Copy link
Contributor Author

waprin commented Oct 23, 2015

@dhermes thanks for taking a look, I now I have 100% coverage on all the main files and fixe the gitignore. The test file is still missing 3 lines, I added #pragma: no cover, since they are test views and the point is that the decorator redirects before they are run. I could maybe mock them but I think it's clearer to have them as functions with the decorator...but for some reason coverage isn't respecting the pragma. Investigating why...

@dhermes
Copy link
Contributor

dhermes commented Oct 24, 2015

Check out .coveragerc the NO COVER has to be uppercase. (I just did git grep -i pragma to get an idea.)

@waprin
Copy link
Contributor Author

waprin commented Oct 26, 2015

Wow, thanks for the headsup on the pragma case sensitivity, it was really confusing me, forgot to check all the hidden files! Ok, now I should have 100% test coverage. I also added NO COVER to apps.py as well, it's covered in 2.7+ but it skips the code in 2.6, and since the 2.6 case isn't run by coverage the 'else' branch isn't covered. It's basically just a metadata class for recent Django versions, which dropped support for 2.6 and so the version of Django tox installs for 2.6 doesn't have the imports.

So I think this is ready for further review or merge whenever you or nathaniel find the time. jonparrot already did a review.

@theacodes
Copy link
Contributor

Summoning @anthmgoogle, he should give this a review as well.

@dhermes
Copy link
Contributor

dhermes commented Oct 26, 2015

Sorry, I don't plan on reviewing (not enough cycles). I was just "protecting" my work towards #212 by making sure big changes were fully tested.

I actually took a stab at testing django_orm.py and stopped at the point that I'd need a test DB. Now that you've added one, maybe you could easily cover the rest of django_orm.py (just the Storage class) and get us one step closer to 100% coverage?

@nathanielmanistaatgoogle
Copy link
Contributor

I am able to review this but it would be best if I were last. Poke me after both @jonparrott and @anthmgoogle are satisfied?

@waprin
Copy link
Contributor Author

waprin commented Oct 26, 2015

@dhermes Will do, thanks for the help. I planned to do that in a separate PR as part of Storage improvement efforts but I could add it onto this one. Also curious if it would make sense to move django_orm into this package or if that's not ok since it breaks existing code (I'm guessing it's ok since this library has releases).

@nathanielmanistaatgoogle sounds good to me, @jonparrott already took a look (and it's a lot of extremely similar code to his own) but I'll hold off for @anthmgoogle and ping you after.

@theacodes
Copy link
Contributor

I'm satisfied in terms of overall approach, parity with flask_util, documentation, and style (though I'm not nearly as thorough as Nathaniel). @anthmngoogle to confirm it fits with his expectations to satisfy #223.

@dhermes
Copy link
Contributor

dhermes commented Oct 26, 2015

@waprin You should do it in a separate PR. The smaller a PR is, the easier it is on everyone. As for moving django_orm, it seems like the right thing to do. An alias could be set up and deprecated slowly over time (@jonparrott has ideas to this effect).



@required
def requires_default_scopes(request):

This comment was marked as spam.

@anthmgoogle
Copy link

Finished a review pass. I suggested one follow up feature that could be done at a later time. Some minor suggestions about error messages, but consider this a LGTM from me.

Looking great. Really exciting to see this functionality.

@waprin waprin force-pushed the django_util branch 2 times, most recently from 1f37b2e to 822754c Compare November 4, 2015 18:53
@waprin
Copy link
Contributor Author

waprin commented Nov 4, 2015

@anthmgoogle Thanks for review, sorry for delay in turnaround. Let me know if you can +1 now and I can pass it along to Nathaniel.

I agree that missing the optional oauth was a major oversight so I added it. Now there are two decorators oauth_required and oauth_enabled, with similar semantics to the webapp2 and flask decorators. The oauth_enabled lets you check if the authorization has started and if not generate the authorization URL. The biggest difference from flask/webapp2 is they attach their instance to the Django request object.

Also based on your comments, I also improved the error messages in the oauth2callback, and reference the Github issue for the pluggable storage feature in the TODO. That is next up on our radar along with some other refactoring.

@anthmgoogle
Copy link

LGTM.

@waprin
Copy link
Contributor Author

waprin commented Nov 9, 2015

@nathanielmanistaatgoogle ready for your pass when you have a chance.

@nathanielmanistaatgoogle
Copy link
Contributor

I am oversubscribed for the next few weeks but happy to accommodate if necessary; how time-sensitive is this?

@theacodes
Copy link
Contributor

I suppose that's up to @anthmgoogle.

I don't want to start on #326, #319, and #225 until this is in, but those are rather long-tail issues.

@waprin
Copy link
Contributor Author

waprin commented Nov 30, 2015

@nathanielmanistaatgoogle It's the season of giving, perhaps you are ready to give this another pass. I know you are tied up in storage PR atm, just your weekly friendly ping this is still here.

@nathanielmanistaatgoogle
Copy link
Contributor

Thank you for your patience; I am finally getting to this in the next hour.

I apologize if this is slow to swap back into my working memory and I ask the same questions I asked last month.

settings_instance.GOOGLE_OAUTH2_CLIENT_SECRET
else:
raise exceptions.ImproperlyConfigured(
"Must specify either GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,or both "

This comment was marked as spam.

This comment was marked as spam.

@nathanielmanistaatgoogle
Copy link
Contributor

I'm somewhat bothered by the structure of this - rather than a new package with a non-empty __init__.py and several other modules, I would sort of expect behavior like this to land in a django_util.py module in the oauth2client package with other behavior included in package-private helper modules. How much control do we have over how this code is arranged into modules and packages? How strongly is Django pushing the design of this code into its current shape?

@waprin
Copy link
Contributor Author

waprin commented Nov 30, 2015

Not sure exactly what you mean by package-private helper modules - I am guessing you mean everything that is part of the public API goes in oauth2client/django_util.py and everything else goes into a helper module or two? I originally did this PR as one file, and it wouldn't be too much work to go back. Django doesn't really force you in any technical sense. However, I laid it out the way I did so it mirrors other Django apps/extensions I looked at:

From the Django project::
https://github.com/django/django/tree/master/django/contrib/admin
https://github.com/django/django/tree/master/django/contrib/auth

In the wild:
https://github.com/ui/django-rq/tree/master/django_rq
https://github.com/omab/django-social-auth/tree/master/social_auth

So it's a choice between being closer to Django style or other extensions in this library (like Flask). @jonparrott suggested I make it closer to Django which made sense to me. Also, keep in mind we are probably going to move this to a contrib package anyway, #326, which also tilts my opinion towards keeping closer to Django style.

But, ultimately I'm not super committed to one way or the other, and if you have something else in mind, I'm fine to change it to whatever you'd like.

@nathanielmanistaatgoogle
Copy link
Contributor

Yeah, I'm now persuaded that the way this is laid out is just fine.

What's with the failing test result?

Does this need to wait for anything else to go in before it can be merged?

@dhermes
Copy link
Contributor

dhermes commented Dec 3, 2015

Fix the broken RST in the docstring

Warning, treated as error:
/home/travis/build/google/oauth2client/oauth2client/django_util/decorators.py:docstring
of oauth2client.django_util.decorators.oauth_enabled:27: WARNING: Field list ends
without a blank line; unexpected unindent.

@waprin waprin force-pushed the django_util branch 2 times, most recently from cd65d81 to 3267326 Compare December 4, 2015 07:11
@waprin
Copy link
Contributor Author

waprin commented Dec 4, 2015

@nathanielmanistaatgoogle had to rebase, ready to merge when you are.

@@ -46,12 +47,15 @@ deps = {[testenv]deps}
basepython =
python2.6
commands =
nosetests \
nosetests \

This comment was marked as spam.

@nathanielmanistaatgoogle
Copy link
Contributor

Now that the contrib directory exists, should this code be placed there?

@theacodes
Copy link
Contributor

Yes, if you've rebased, @waprin, might as well go ahead and move it. Otherwise I can just move it when I move everything else.

@waprin
Copy link
Contributor Author

waprin commented Dec 4, 2015

Ok I'll move it now

@waprin waprin force-pushed the django_util branch 2 times, most recently from b78d339 to 6813b78 Compare December 4, 2015 18:49
Adds a submodule that provides views, decorator, and signals to help
a Django web application complete OAuth2 authorization.
@waprin
Copy link
Contributor Author

waprin commented Dec 4, 2015

Ok, moved to contrib and I did some double manual checking so I think it's good to go. @nathanielmanistaatgoogle

nathanielmanistaatgoogle added a commit that referenced this pull request Dec 4, 2015
Django Util With Decorator and Views.
@nathanielmanistaatgoogle nathanielmanistaatgoogle merged commit 2e5c7cb into googleapis:master Dec 4, 2015
@dhermes
Copy link
Contributor

dhermes commented Dec 4, 2015

Yay!

@anthmgoogle
Copy link

Great to see this feature. Thanks all.

@dhermes dhermes mentioned this pull request Jan 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants