forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvitation.py
More file actions
133 lines (100 loc) · 3.88 KB
/
Copy pathinvitation.py
File metadata and controls
133 lines (100 loc) · 3.88 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
# -*- coding: utf-8 -*-
"""Invitation related logic."""
from __future__ import unicode_literals
from json import dumps
from .. import models
from .. import users
from ..decorators import requires_auth
class Invitation(models.GitHubCore):
"""Representation of an invitation to collaborate on a repository.
.. attribute:: created_at
A :class:`~datetime.datetime` instance representing the time and date
when this invitation was created.
.. attribute:: html_url
The URL to view this invitation in a browser.
.. attribute:: id
The unique identifier for this invitation.
.. attribute:: invitee
A :class:`~github3.users.ShortUser` representing the user who was
invited to collaborate.
.. attribute:: inviter
A :class:`~github3.users.ShortUser` representing the user who invited
the ``invitee``.
.. attribute:: permissions
The permissions that the ``invitee`` will have on the repository. Valid
values are ``read``, ``write``, and ``admin``.
.. attribute:: repository
A :class:`~github3.repos.ShortRepository` representing the repository
on which the ``invitee` was invited to collaborate.
.. attribute:: url
The API URL that the ``invitee`` can use to respond to the invitation.
Note that the ``inviter`` must use a different URL, not returned by
the API, to update or cancel the invitation.
"""
class_name = "Invitation"
allowed_permissions = frozenset(["admin", "read", "write"])
def _update_attributes(self, invitation):
from . import repo
self.created_at = self._strptime(invitation["created_at"])
self.html_url = invitation["html_url"]
self.id = invitation["id"]
self.invitee = users.ShortUser(invitation["invitee"], self)
self.inviter = users.ShortUser(invitation["inviter"], self)
self.permissions = invitation["permissions"]
self.repository = repo.ShortRepository(invitation["repository"], self)
self.url = invitation["url"]
def _repr(self):
return "<Invitation [{0}]>".format(self.repository.full_name)
@requires_auth
def accept(self):
"""Accept this invitation.
:returns:
True if successful, False otherwise
:rtype:
bool
"""
return self._boolean(self._patch(self.url), 204, 404)
@requires_auth
def decline(self):
"""Decline this invitation.
:returns:
True if successful, False otherwise
:rtype:
bool
"""
return self._boolean(self._delete(self.url), 204, 404)
@requires_auth
def delete(self):
"""Delete this invitation.
:returns:
True if successful, False otherwise
:rtype:
bool
"""
url = self._build_url(
"invitations", self.id, base_url=self.repository.url
)
return self._boolean(self._delete(url), 204, 404)
@requires_auth
def update(self, permissions):
"""Update this invitation.
:param str permissions:
(required), the permissions that will be granted by this invitation
once it has been updated. Options: 'admin', 'read', 'write'
:returns:
The updated invitation
:rtype:
:class:`~github3.repos.invitation.Invitation`
"""
if permissions not in self.allowed_permissions:
raise ValueError(
"'permissions' must be one of {0}".format(
", ".join(sorted(self.allowed_permissions))
)
)
url = self._build_url(
"invitations", self.id, base_url=self.repository.url
)
data = {"permissions": permissions}
json = self._json(self._patch(url, data=dumps(data)), 200)
return self._instance_or_null(Invitation, json)