Permalink
Browse files

Merge pull request #680 from sigmavirus24/split-user-obj

Split User object into more accurate objects
  • Loading branch information...
2 parents 3e251f2 + 30be3c6 commit b58ff53ce9607f71aeb06f46eefe991f83c5e83e @sigmavirus24 committed on GitHub Feb 3, 2017
View
@@ -13,6 +13,25 @@
from .models import GitHubCore
+class EventUser(GitHubCore):
+ """The class that represents the user information returned in Events."""
+
+ def _update_attributes(self, user):
+ self.avatar_url = user['avatar_url']
+ self.display_login = user.get('display_login')
+ self.gravatar_id = user['id']
+ self.id = user['id']
+ self.login = user['login']
+ self._api = self.url = user['url']
+
+ def to_user(self):
+ """Retrieve a full User object for this EventUser."""
+ from . import users
+ url = self._build_url('users', self.login)
+ json = self._json(self._get(url), 200)
+ return self._instance_or_null(users.User, json)
+
+
class Event(GitHubCore):
"""The :class:`Event <Event>` object. It structures and handles the data
@@ -37,10 +56,9 @@ def _update_attributes(self, event):
# not want to do:
event = copy.deepcopy(event)
- from .users import User
from .orgs import Organization
#: :class:`User <github3.users.User>` object representing the actor.
- self.actor = self._class_attribute(event, 'actor', User)
+ self.actor = self._class_attribute(event, 'actor', EventUser)
#: datetime object representing when the event was created.
self.created_at = self._strptime_attribute(event, 'created_at')
@@ -83,9 +101,8 @@ def _commitcomment(payload, session):
def _follow(payload, session):
- from .users import User
if payload.get('target'):
- payload['target'] = User(payload['target'], session)
+ payload['target'] = EventUser(payload['target'], session)
return payload
@@ -121,9 +138,8 @@ def _issueevent(payload, session):
def _member(payload, session):
- from .users import User
if payload.get('member'):
- payload['member'] = User(payload['member'], session)
+ payload['member'] = EventUser(payload['member'], session)
return payload
@@ -160,13 +176,12 @@ def _release(payload, session):
def _team(payload, session):
from .orgs import Team
from .repos import Repository
- from .users import User
if payload.get('team'):
payload['team'] = Team(payload['team'], session)
if payload.get('repo'):
payload['repo'] = Repository(payload['repo'], session)
if payload.get('sender'):
- payload['sender'] = User(payload['sender'], session)
+ payload['sender'] = EventUser(payload['sender'], session)
return payload
@@ -8,8 +8,8 @@
"""
from __future__ import unicode_literals
+from .. import users
from ..models import BaseComment
-from ..users import User
class GistComment(BaseComment):
@@ -34,7 +34,9 @@ def _update_attributes(self, comment):
self._api = self._get_attribute(comment, 'url')
#: :class:`User <github3.users.User>` who made the comment
#: Unless it is not associated with an account
- self.user = self._class_attribute(comment, 'user', User, self)
+ self.user = self._class_attribute(
+ comment, 'user', users.ShortUser, self,
+ )
def _repr(self):
return '<Gist Comment [{0}]>'.format(self.user.login)
@@ -9,12 +9,14 @@
from __future__ import unicode_literals
from json import dumps
+
+from .. import users
+
from ..models import GitHubCore
from ..decorators import requires_auth
from .comment import GistComment
from .file import GistFile
from .history import GistHistory
-from ..users import User
class Gist(GitHubCore):
@@ -75,7 +77,9 @@ def _update_attributes(self, data):
#: :class:`User <github3.users.User>` object representing the owner of
#: the gist.
- self.owner = self._class_attribute(data, 'owner', User, self)
+ self.owner = self._class_attribute(
+ data, 'owner', users.ShortUser, self,
+ )
self._files = self._get_attribute(data, 'files', [])
if self._files:
@@ -8,8 +8,8 @@
"""
from __future__ import unicode_literals
+from .. import users
from ..models import GitHubCore
-from ..users import User
class GistHistory(GitHubCore):
@@ -35,7 +35,9 @@ def _update_attributes(self, history):
self.version = self._get_attribute(history, 'version')
#: user who made these changes
- self.user = self._class_attribute(history, 'user', User, self)
+ self.user = self._class_attribute(
+ history, 'user', users.ShortUser, self,
+ )
#: dict containing the change status; see also: deletions, additions,
#: total
View
@@ -12,7 +12,6 @@
from json import dumps
from base64 import b64decode
from .models import GitHubCore, BaseCommit
-from .users import User
from .decorators import requires_auth
@@ -92,22 +91,6 @@ def _update_attributes(self, commit):
def _repr(self):
return '<Commit [{0}:{1}]>'.format(self._author_name, self.sha)
- def author_as_User(self):
- """Attempt to return the author attribute as a
- :class:`User <github3.users.User>`. No guarantees are made about the
- validity of this object, i.e., having a login or created_at object.
-
- """
- return User(self.author, self)
-
- def committer_as_User(self):
- """Attempt to return the committer attribute as a
- :class:`User <github3.users.User>` object. No guarantees are made
- about the validity of this object.
-
- """
- return User(self.committer, self)
-
class Reference(GitHubCore):
@@ -195,14 +178,6 @@ def _update_attributes(self, tag):
def _repr(self):
return '<Tag [{0}]>'.format(self.tag)
- def tagger_as_User(self):
- """Attempt to return the tagger attribute as a
- :class:`User <github3.users.User>`. No guarantees are made about the
- validity of this object, i.e., having a login or created_at object.
-
- """
- return User(self.tagger, self)
-
class Tree(GitData):
View
@@ -145,10 +145,10 @@ def all_users(self, number=-1, etag=None, per_page=None, since=None):
:param str etag: (optional), ETag from a previous request to the same
endpoint
:param int per_page: (optional), number of users to list per request
- :returns: generator of :class:`User <github3.users.User>`
+ :returns: generator of :class:`~github3.users.ShortUser`
"""
url = self._build_url('users')
- return self._iter(int(number), url, users.User, etag=etag,
+ return self._iter(int(number), url, users.ShortUser, etag=etag,
params={'per_page': per_page, 'since': since})
@requires_basic_auth
@@ -447,7 +447,7 @@ def follow(self, username):
return resp
def followed_by(self, username, number=-1, etag=None):
- """Iterate over users being followed by ``username``.
+ r"""Iterate over users being followed by ``username``.
.. versionadded:: 1.0.0
@@ -458,14 +458,14 @@ def followed_by(self, username, number=-1, etag=None):
returns all people you follow
:param str etag: (optional), ETag from a previous request to the same
endpoint
- :returns: generator of :class:`User <github3.users.User>`\ s
+ :returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('users', username, 'following')
- return self._iter(int(number), url, users.User, etag=etag)
+ return self._iter(int(number), url, users.ShortUser, etag=etag)
@requires_auth
def followers(self, number=-1, etag=None):
- """Iterate over followers of the authenticated user.
+ r"""Iterate over followers of the authenticated user.
.. versionadded:: 1.0.0
@@ -475,13 +475,13 @@ def followers(self, number=-1, etag=None):
-1 returns all followers
:param str etag: (optional), ETag from a previous request to the same
endpoint
- :returns: generator of :class:`User <github3.users.User>`\ s
+ :returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('user', 'followers')
- return self._iter(int(number), url, users.User, etag=etag)
+ return self._iter(int(number), url, users.ShortUser, etag=etag)
def followers_of(self, username, number=-1, etag=None):
- """Iterate over followers of ``username``.
+ r"""Iterate over followers of ``username``.
.. versionadded:: 1.0.0
@@ -492,14 +492,14 @@ def followers_of(self, username, number=-1, etag=None):
-1 returns all followers
:param str etag: (optional), ETag from a previous request to the same
endpoint
- :returns: generator of :class:`User <github3.users.User>`\ s
+ :returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('users', username, 'followers')
- return self._iter(int(number), url, users.User, etag=etag)
+ return self._iter(int(number), url, users.ShortUser, etag=etag)
@requires_auth
def following(self, number=-1, etag=None):
- """Iterate over users the authenticated user is following.
+ r"""Iterate over users the authenticated user is following.
.. versionadded:: 1.0.0
@@ -509,10 +509,10 @@ def following(self, number=-1, etag=None):
returns all people you follow
:param str etag: (optional), ETag from a previous request to the same
endpoint
- :returns: generator of :class:`User <github3.users.User>`\ s
+ :returns: generator of :class:`~github3.users.ShortUser`\ s
"""
url = self._build_url('user', 'following')
- return self._iter(int(number), url, users.User, etag=etag)
+ return self._iter(int(number), url, users.ShortUser, etag=etag)
def gist(self, id_num):
"""Retrieve the gist using the specified id number.
@@ -800,18 +800,18 @@ def markdown(self, text, mode='', context='', raw=False):
@requires_auth
def me(self):
- """Retrieves the info for the authenticated user.
+ """Retrieve the info for the authenticated user.
.. versionadded:: 1.0
This was separated from the ``user`` method.
:returns: The representation of the authenticated user.
- :rtype: :class:`User <github3.users.User>`
+ :rtype: :class:`~github3.users.AuthenticatedUser`
"""
url = self._build_url('user')
json = self._json(self._get(url), 200)
- return self._instance_or_null(users.User, json)
+ return self._instance_or_null(users.AuthenticatedUser, json)
@requires_auth
def membership_in(self, organization):
@@ -1588,10 +1588,10 @@ def update_me(self, name=None, email=None, blog=None, company=None,
return False
def user(self, username):
- """Returns a User object for the specified user name.
+ """Retrieve a User object for the specified user name.
:param str username: name of the user
- :returns: :class:`User <github3.users.User>`
+ :returns: :class:`~github3.users.User`
"""
url = self._build_url('users', username)
json = self._json(self._get(url), 200)
@@ -1657,7 +1657,7 @@ def user_with_id(self, number):
"""Get the user's information with id ``number``.
:param int number: the user's id number
- :returns: :class:`User <github3.users.User>`
+ :returns: :class:`~github3.users.User`
"""
number = int(number)
json = None
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+from .. import users
from ..utils import timestamp_parameter
from ..models import BaseComment
-from ..users import User
class IssueComment(BaseComment):
@@ -26,7 +26,9 @@ def _update_attributes(self, comment):
super(IssueComment, self)._update_attributes(comment)
#: :class:`User <github3.users.User>` who made the comment
- self.user = self._class_attribute(comment, 'user', User, self)
+ self.user = self._class_attribute(
+ comment, 'user', users.ShortUser, self,
+ )
#: Issue url (not a template)
self.issue_url = self._get_attribute(comment, 'issue_url')
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+from .. import users
from ..models import GitHubCore
-from ..users import User
class IssueEvent(GitHubCore):
@@ -37,7 +37,9 @@ def _update_attributes(self, event):
self.issue = self._class_attribute(event, 'issue', Issue, self)
#: :class:`User <github3.users.User>` who caused this event.
- self.actor = self._class_attribute(event, 'actor', User, self)
+ self.actor = self._class_attribute(
+ event, 'actor', users.ShortUser, self,
+ )
#: Number of comments
self.comments = self._get_attribute(event, 'comments')
@@ -55,7 +57,9 @@ def _update_attributes(self, event):
self.id = self._get_attribute(event, 'id')
#: :class:`User <github3.users.User>` that is assigned
- self.assignee = self._class_attribute(event, 'assignee', User, self)
+ self.assignee = self._class_attribute(
+ event, 'assignee', users.ShortUser, self,
+ )
#: Dictionary containing milestone details
self.milestone = self._get_attribute(event, 'milestone', {})
Oops, something went wrong.

0 comments on commit b58ff53

Please sign in to comment.