[Python-modules-commits] [python-social-auth] 28/32: PEP8 and code simplification

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:49 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.12
in repository python-social-auth.

commit 4f58c8a582906a7acae77ed5558953a55d7ce6b7
Author: Matías Aguirre <matiasaguirre at gmail.com>
Date:   Thu Jul 9 18:03:14 2015 -0300

    PEP8 and code simplification
---
 docs/backends/github.rst             |  8 ++++++
 docs/backends/github_enterprise.rst  |  8 +++---
 docs/backends/index.rst              |  1 +
 social/backends/github.py            | 48 ++++++++++++++++++------------------
 social/backends/github_enterprise.py | 43 +++++++++++++-------------------
 social/utils.py                      | 11 +++++++++
 6 files changed, 66 insertions(+), 53 deletions(-)

diff --git a/docs/backends/github.rst b/docs/backends/github.rst
index 382c742..d964275 100644
--- a/docs/backends/github.rst
+++ b/docs/backends/github.rst
@@ -50,4 +50,12 @@ Be sure to define the ``Team ID`` using the setting::
 This ``id`` will be used to check that the user really belongs to the given
 team and discard it if they're not part of it.
 
+
+Github for Enterprises
+----------------------
+
+Check the docs :ref:`github-enterprise` if planning to use Github
+Enterprises.
+
+
 .. _GitHub Developers: https://github.com/settings/applications/new
diff --git a/docs/backends/github_enterprise.rst b/docs/backends/github_enterprise.rst
index 33b18ec..5d59cc9 100644
--- a/docs/backends/github_enterprise.rst
+++ b/docs/backends/github_enterprise.rst
@@ -1,3 +1,5 @@
+.. _github-enterprise:
+
 GitHub Enterprise
 =================
 
@@ -14,11 +16,11 @@ GitHub Enterprise works similar to regular Github, which is in turn based on Fac
 
 - Also it's possible to define extra permissions with::
 
-      SOCIAL_AUTH_GITHUB_SCOPE = [...]
+      SOCIAL_AUTH_GITHUB_ENTERPRISE_SCOPE = [...]
 
 
 GitHub Enterprise for Organizations
-------------------------
+-----------------------------------
 
 When defining authentication for organizations, use the
 ``GithubEnterpriseOrganizationOAuth2`` backend instead. The settings are the same as
@@ -35,7 +37,7 @@ organization and discard it if they're not part of it.
 
 
 GitHub Enterprise for Teams
-----------------
+---------------------------
 
 Similar to ``GitHub Enterprise for Organizations``, there's a GitHub for Teams backend,
 use the backend ``GithubEnterpriseTeamOAuth2``. The settings are the same as
diff --git a/docs/backends/index.rst b/docs/backends/index.rst
index 8456946..ec66b1d 100644
--- a/docs/backends/index.rst
+++ b/docs/backends/index.rst
@@ -77,6 +77,7 @@ Social backends
    flickr
    foursquare
    github
+   github_enterprise
    google
    instagram
    jawbone
diff --git a/social/backends/github.py b/social/backends/github.py
index 85b05dc..f5d8756 100644
--- a/social/backends/github.py
+++ b/social/backends/github.py
@@ -13,8 +13,9 @@ from social.exceptions import AuthFailed
 class GithubOAuth2(BaseOAuth2):
     """Github OAuth authentication backend"""
     name = 'github'
-    AUTHORIZATION_URL_SUFFIX = 'login/oauth/authorize'
-    ACCESS_TOKEN_URL_SUFFIX = 'login/oauth/access_token'
+    API_URL = 'https://api.github.com/'
+    AUTHORIZATION_URL = 'https://github.com/login/oauth/authorize'
+    ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
     ACCESS_TOKEN_METHOD = 'POST'
     SCOPE_SEPARATOR = ','
     EXTRA_DATA = [
@@ -23,17 +24,8 @@ class GithubOAuth2(BaseOAuth2):
         ('login', 'login')
     ]
 
-    @property
-    def API_URL(self):
-        return 'https://api.github.com/'
-
-    @property
-    def AUTHORIZATION_URL(self):
-        return urljoin('https://github.com/', self.AUTHORIZATION_URL_SUFFIX)
-
-    @property
-    def ACCESS_TOKEN_URL(self):
-        return urljoin('https://github.com/', self.ACCESS_TOKEN_URL_SUFFIX)
+    def api_url(self):
+        return self.API_URL
 
     def get_user_details(self, response):
         """Return user details from Github account"""
@@ -57,10 +49,10 @@ class GithubOAuth2(BaseOAuth2):
 
             if emails:
                 email = emails[0]
-                primary_emails = [e for e in emails
-                                    if not isinstance(e, dict) or
-                                       e.get('primary')]
-
+                primary_emails = [
+                    e for e in emails
+                    if not isinstance(e, dict) or e.get('primary')
+                ]
                 if primary_emails:
                     email = primary_emails[0]
                 if isinstance(email, dict):
@@ -69,7 +61,7 @@ class GithubOAuth2(BaseOAuth2):
         return data
 
     def _user_data(self, access_token, path=None):
-        url = urljoin(self.API_URL, 'user{0}'.format(path or ''))
+        url = urljoin(self.api_url(), 'user{0}'.format(path or ''))
         return self.get_json(url, params={'access_token': access_token})
 
 
@@ -103,9 +95,13 @@ class GithubOrganizationOAuth2(GithubMemberOAuth2):
     no_member_string = 'User doesn\'t belong to the organization'
 
     def member_url(self, user_data):
-        return urljoin(self.API_URL, 'orgs/{org}/members/{username}'.format(
-            org=self.setting('NAME'),
-            username=user_data.get('login')))
+        return urljoin(
+            self.api_url(),
+            'orgs/{org}/members/{username}'.format(
+                org=self.setting('NAME'),
+                username=user_data.get('login')
+            )
+        )
 
 
 class GithubTeamOAuth2(GithubMemberOAuth2):
@@ -114,6 +110,10 @@ class GithubTeamOAuth2(GithubMemberOAuth2):
     no_member_string = 'User doesn\'t belong to the team'
 
     def member_url(self, user_data):
-        return urljoin(self.API_URL, 'teams/{team_id}/members/{username}'.format(
-            team_id=self.setting('ID'),
-            username=user_data.get('login')))
+        return urljoin(
+            self.api_url(),
+            'teams/{team_id}/members/{username}'.format(
+                team_id=self.setting('ID'),
+                username=user_data.get('login')
+            )
+        )
diff --git a/social/backends/github_enterprise.py b/social/backends/github_enterprise.py
index 6e7344f..656a92e 100644
--- a/social/backends/github_enterprise.py
+++ b/social/backends/github_enterprise.py
@@ -4,34 +4,23 @@ Github Enterprise OAuth2 backend, docs at:
 """
 from six.moves.urllib.parse import urljoin
 
-from social.backends.github import (
-    GithubOAuth2, GithubOrganizationOAuth2, GithubTeamOAuth2)
-
-
-def append_slash(url):
-    """Make sure we append a slash at the end of the URL otherwise we have issues with urljoin
-    Example:
-    >>> urlparse.urljoin('http://www.example.com/api/v3', 'user/1/')
-    'http://www.example.com/api/user/1/'
-    """
-    if not url:
-        return url
-    return "%s/" % url if not url.endswith('/') else url
+from social.utils import append_slash
+from social.backends.github import GithubOAuth2, GithubOrganizationOAuth2, \
+    GithubTeamOAuth2
 
 
 class GithubEnterpriseMixin(object):
-
-    @property
-    def API_URL(self):
+    def api_url(self):
         return append_slash(self.setting('API_URL'))
 
-    @property
-    def AUTHORIZATION_URL(self):
-        return urljoin(append_slash(self.setting('URL')), GithubOAuth2.AUTHORIZATION_URL_SUFFIX)
+    def authorization_url(self):
+        return self._url('login/oauth/authorize')
+
+    def access_token_url(self):
+        return self._url('login/oauth/access_token')
 
-    @property
-    def ACCESS_TOKEN_URL(self):
-        return urljoin(append_slash(self.setting('URL')), GithubOAuth2.ACCESS_TOKEN_URL_SUFFIX)
+    def _url(self, path):
+        return urljoin(append_slash(self.setting('URL')), path)
 
 
 class GithubEnterpriseOAuth2(GithubEnterpriseMixin, GithubOAuth2):
@@ -39,13 +28,15 @@ class GithubEnterpriseOAuth2(GithubEnterpriseMixin, GithubOAuth2):
     name = 'github-enterprise'
 
 
-class GithubEnterpriseOrganizationOAuth2(GithubEnterpriseMixin, GithubOrganizationOAuth2):
-    """Github Enterprise OAuth2 authentication backend for organizations"""
-    DEFAULT_SCOPE = ['read:org']
+class GithubEnterpriseOrganizationOAuth2(GithubEnterpriseMixin,
+                                         GithubOrganizationOAuth2):
+    """Github Enterprise OAuth2 authentication backend for
+    organizations"""
     name = 'github-enterprise-org'
+    DEFAULT_SCOPE = ['read:org']
 
 
 class GithubEnterpriseTeamOAuth2(GithubEnterpriseMixin, GithubTeamOAuth2):
     """Github Enterprise OAuth2 authentication backend for teams"""
-    DEFAULT_SCOPE = ['read:org']
     name = 'github-enterprise-team'
+    DEFAULT_SCOPE = ['read:org']
diff --git a/social/utils.py b/social/utils.py
index 2cd41df..c2db351 100644
--- a/social/utils.py
+++ b/social/utils.py
@@ -235,3 +235,14 @@ def handle_http_errors(func):
             else:
                 raise
     return wrapper
+
+
+def append_slash(url):
+    """Make sure we append a slash at the end of the URL otherwise we
+    have issues with urljoin Example:
+    >>> urlparse.urljoin('http://www.example.com/api/v3', 'user/1/')
+    'http://www.example.com/api/user/1/'
+    """
+    if url and not url.endswith('/'):
+        url = '{0}/'.format(url)
+    return url

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list