forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusers.py
More file actions
311 lines (258 loc) · 9.97 KB
/
Copy pathusers.py
File metadata and controls
311 lines (258 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
github3.users
=============
This module contains everything relating to Users.
"""
from json import dumps
from github3.events import Event
from github3.models import GitHubObject, GitHubCore, BaseAccount
from github3.decorators import requires_auth
import warnings
class Key(GitHubCore):
"""The :class:`Key <Key>` object."""
def __init__(self, key, session=None):
super(Key, self).__init__(key, session)
self._api = key.get('url', '')
#: The text of the actual key
self.key = key.get('key')
#: The unique id of the key at GitHub
self.id = key.get('id')
#: The title the user gave to the key
self.title = key.get('title')
def __repr__(self):
return '<User Key [{0}]>'.format(self.title)
def _update_(self, key):
self.__init__(key, self._session)
@requires_auth
def delete(self):
"""Delete this Key"""
return self._boolean(self._delete(self._api), 204, 404)
@requires_auth
def update(self, title, key):
"""Update this key.
:param title: (required), title of the key
:type title: str
:param key: (required), text of the key file
:type key: str
:returns: bool
"""
if not title:
title = self.title
if not key:
key = self.key
data = dumps({'title': title, 'key': key})
json = self._json(self._patch(self._api, data=data), 200)
if json:
self._update_(json)
return True
return False
class Plan(GitHubObject):
"""The :class:`Plan <Plan>` object. This makes interacting with the plan
information about a user easier.
"""
def __init__(self, plan):
super(Plan, self).__init__(plan)
#: Number of collaborators
self.collaborators = plan.get('collaborators')
#: Name of the plan
self.name = plan.get('name')
#: Number of private repos
self.private_repos = plan.get('private_repos')
#: Space allowed
self.space = plan.get('space')
def __repr__(self):
return '<Plan [{0}]>'.format(self.name)
def is_free(self):
"""Checks if this is a free plan.
:returns: bool
"""
return self.name == 'free'
_large = Plan({'name': 'large', 'private_repos': 50,
'collaborators': 25, 'space': 0})
_medium = Plan({'name': 'medium', 'private_repos': 20,
'collaborators': 10, 'space': 0})
_small = Plan({'name': 'small', 'private_repos': 10,
'collaborators': 5, 'space': 0})
_micro = Plan({'name': 'micro', 'private_repos': 5,
'collaborators': 1, 'space': 0})
_free = Plan({'name': 'free', 'private_repos': 0,
'collaborators': 0, 'space': 0})
plans = {'large': _large, 'medium': _medium, 'small': _small,
'micro': _micro, 'free': _free}
class User(BaseAccount):
"""The :class:`User <User>` object. This handles and structures information
in the `User section <http://developer.github.com/v3/users/>`_.
"""
def __init__(self, user, session=None):
super(User, self).__init__(user, session)
if not self.type:
self.type = 'User'
#: ID of the user's image on Gravatar
self.gravatar_id = user.get('gravatar_id', '')
#: True -- for hire, False -- not for hire
self.hireable = user.get('hireable', False)
## The number of public_gists
#: Number of public gists
self.public_gists = user.get('public_gists', 0)
# Private information
#: How much disk consumed by the user
self.disk_usage = user.get('disk_usage', 0)
#: Number of private repos owned by this user
self.owned_private_repos = user.get('owned_private_repos', 0)
#: Number of private gists owned by this user
self.total_private_gists = user.get('total_private_gists', 0)
#: Total number of private repos
self.total_private_repos = user.get('total_private_repos', 0)
#: Which plan this user is on
self.plan = None
if user.get('plan'):
self.plan = plans[user['plan']['name'].lower()]
self.plan.space = user['plan']['space']
def __repr__(self):
return '<User [{0}:{1}]>'.format(self.login, self.name)
def _update_(self, user):
self.__init__(user, self._session)
@requires_auth
def add_email_address(self, address):
"""Add the single email address to the authenticated user's
account.
:param str address: (required), email address to add
:returns: list of email addresses
"""
return self.add_email_addresses([address])
@requires_auth
def add_email_addresses(self, addresses=[]):
"""Add the email addresses in ``addresses`` to the authenticated
user's account.
:param addresses: (optional), email addresses to be added
:type addresses: list
:returns: list of email addresses
"""
json = []
if addresses:
url = self._build_url('user', 'emails')
json = self._json(self._post(url, dumps(addresses)), 201)
return json
@requires_auth
def delete_email_address(self, address):
"""Delete the email address from the user's account.
:param str address: (required), email address to delete
:returns: bool
"""
return self.delete_email_addresses([address])
@requires_auth
def delete_email_addresses(self, addresses=[]):
"""Delete the email addresses in ``addresses`` from the
authenticated user's account.
:param addresses: (optional), email addresses to be removed
:type addresses: list
:returns: bool
"""
url = self._build_url('user', 'emails')
return self._boolean(self._delete(url, data=dumps(addresses)),
204, 404)
@property
def for_hire(self):
"""DEPRECATED: Use hireable instead"""
warnings.warn('Use hireable instead', DeprecationWarning)
def is_assignee_on(self, login, repository):
"""Checks if this user can be assigned to issues on login/repository.
:returns: :class:`bool`
"""
url = self._build_url('repos', login, repository, 'assignees',
self.login)
return self._boolean(self._get(url), 204, 404)
def list_events(self, public=False):
"""Events performed by this user.
:param public: (optional), only list public events for the
authenticated user
:type public: bool
:returns: list of :class:`Event <github3.events.Event>`\ s
"""
# Paginate
path = ['events']
if public:
path.append('public')
url = self._build_url(*path, base_url=self._api)
json = self._json(self._get(url), 200)
return [Event(e, self) for e in json]
def list_followers(self):
"""List followers of this user.
:returns: list of :class:`User <User>`\ s
"""
# Paginate
url = self._build_url('followers', base_url=self._api)
json = self._json(self._get(url), 200)
return [User(u, self) for u in json]
def list_following(self):
"""List users being followed by this user.
:returns: list of :class:`User <User>`\ s
"""
# Paginate
url = self._build_url('following', base_url=self._api)
json = self._json(self._get(url), 200)
return [User(u, self) for u in json]
def list_org_events(self, org):
"""List events as they appear on the user's organization dashboard.
You must be authenticated to view this.
:param org: (required), name of the organization
:type org: str
:returns: list of :class:`Event <github3.events.Event>`\ s
"""
# Paginate
json = []
if org:
url = self._build_url('events', 'orgs', org, base_url=self._api)
json = self._json(self._get(url), 200)
return [Event(e, self) for e in json]
def list_received_events(self, public=False):
"""List events that the user has received. If the user is the
authenticated user, you will see private and public events, otherwise
you will only see public events.
:param public: (optional), determines if the authenticated user sees
both private and public or just public
:type public: bool
:returns: list of :class:`Event <github3.events.Event>`\ s
"""
# Paginate
path = ['received_events']
if public:
path.append('public')
url = self._build_url(*path, base_url=self._api)
json = self._json(self._get(url), 200)
return [Event(e, self) for e in json]
return self._owned_private_repos
@property
def private_gists(self):
"""DEPRECATED: Use total_private_gists"""
warnings.warn('Use total_private_gists', DeprecationWarning)
@requires_auth
def update(self, name=None, email=None, blog=None, company=None,
location=None, hireable=False, bio=None):
"""If authenticated as this user, update the information with
the information provided in the parameters.
:param name: e.g., 'John Smith', not login name
:type name: str
:param email: e.g., '[email protected]'
:type email: str
:param blog: e.g., 'http://www.example.com/jsmith/blog'
:type blog: str
:param company:
:type company: str
:param location:
:type location: str
:param hireable: defaults to False
:type hireable: bool
:param bio: GitHub flavored markdown
:type bio: str
:returns: bool
"""
user = dumps({'name': name, 'email': email, 'blog': blog,
'company': company, 'location': location,
'hireable': hireable, 'bio': bio})
url = self._build_url('user')
json = self._json(self._patch(url, data=user), 200)
if json:
self._update_(json)
return True
return False