|
| 1 | +""" |
| 2 | +github3.notifications |
| 3 | +===================== |
| 4 | +
|
| 5 | +This module contains the classes relating to notifications. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +fromjsonimportdumps |
| 10 | +fromgithub3.modelsimportGitHubCore |
| 11 | + |
| 12 | + |
| 13 | +classThread(GitHubCore): |
| 14 | +def__init__(self,notif,session=None): |
| 15 | +super(Thread,self).__init__(notif,session) |
| 16 | +self._api=notif.get('url') |
| 17 | +#: Comment responsible for the notification |
| 18 | +self.comment=notif.get('comment', {}) |
| 19 | +#: Thread information |
| 20 | +self.thread=notif.get('thread', {}) |
| 21 | + |
| 22 | +fromgithub3.reposimportRepository |
| 23 | +#: Repository the comment was made on |
| 24 | +self.repository=Repository(notif.get('repository', {}),self) |
| 25 | +#: When the thread was last updated |
| 26 | +self.updated_at=self._strptime(notif.get('updated_at')) |
| 27 | +#: Id of the thread |
| 28 | +self.id=notif.get('id') |
| 29 | +#: Dictionary of urls for the thread |
| 30 | +self.urls=notif.get('urls') |
| 31 | +#: datetime object representing the last time the user read the thread |
| 32 | +self.last_read_at=notif.get('last_read_at') |
| 33 | +ifself.last_read_at: |
| 34 | +self.last_read_at=self._strptime(self.last_read_at) |
| 35 | +#: The reason you're receiving the notification |
| 36 | +self.reason=notif.get('reason') |
| 37 | +#: Subject of the Notification, e.g., which issue/pull/diff is this in |
| 38 | +#: relation to. This is a dictionary |
| 39 | +self.subject=notif.get('subject') |
| 40 | +self._unread=notif.get('unread') |
| 41 | + |
| 42 | +def__repr__(self): |
| 43 | +return'<Thread [{0}]>'.format(self.subject.get('title')) |
| 44 | + |
| 45 | +defdelete_subscription(self): |
| 46 | +"""Delete subscription for this thread. |
| 47 | +
|
| 48 | + :returns: bool |
| 49 | + """ |
| 50 | +url=self._build_url('subscription',base_url=self._api) |
| 51 | +returnself._boolean(self._delete(url),204,404) |
| 52 | + |
| 53 | +defis_unread(self): |
| 54 | +"""Tells you if the thread is unread or not.""" |
| 55 | +returnself._unread |
| 56 | + |
| 57 | +defmark(self): |
| 58 | +"""Mark the thread as read. |
| 59 | +
|
| 60 | + :returns: bool |
| 61 | + """ |
| 62 | +mark= {'read':True} |
| 63 | +returnself._boolean(self._patch(self._api,data=dumps(mark)),205, |
| 64 | +404) |
| 65 | + |
| 66 | +defset_subscription(self,subscribed,ignored): |
| 67 | +"""Set the user's subscription for this thread |
| 68 | +
|
| 69 | + :param bool subscribed: (required), determines if notifications should |
| 70 | + be received from this thread. |
| 71 | + :param bool ignored: (required), determines if notifications should be |
| 72 | + ignored from this thread. |
| 73 | + :returns: :class;`Subscription <Subscription>` |
| 74 | + """ |
| 75 | +url=self._build_url('subscription',base_url=self._api) |
| 76 | +sub=dumps({'subscribed':subscribed,'ignored':ignored}) |
| 77 | +json=self._json(self._put(url,data=sub),200) |
| 78 | +returnSubscription(json,self)ifjsonelseNone |
| 79 | + |
| 80 | +defsubscription(self): |
| 81 | +"""Checks the status of the user's subscription to this thread. |
| 82 | +
|
| 83 | + :returns: :class:`Subscription <Subscription>` |
| 84 | + """ |
| 85 | +url=self._build_url('subscription',base_url=self._api) |
| 86 | +json=self._json(self._get(url),200) |
| 87 | +returnSubscription(json,self)ifjsonelseNone |
| 88 | + |
| 89 | + |
| 90 | +classSubscription(GitHubCore): |
| 91 | +def__init__(self,sub,session=None): |
| 92 | +super(Subscription,self).__init__(sub,session) |
| 93 | +self._api=sub.get('url') |
| 94 | +#: reason user is subscribed to this thread/repository |
| 95 | +self.reason=sub.get('reason') |
| 96 | +#: datetime representation of when the subscription was created |
| 97 | +self.created_at=self._strptime(sub.get('created_at')) |
| 98 | +#: API url of the thread if it exists |
| 99 | +self.thread_url=sub.get('thread_url') |
| 100 | +#: API url of the repository if it exists |
| 101 | +self.repository_url=sub.get('repository_url') |
| 102 | +self._ignored=sub.get('ignored',False) |
| 103 | +self._subscribed=sub.get('subscribed',False) |
| 104 | + |
| 105 | +def__repr__(self): |
| 106 | +return'<Subscription [{0}]>'.format(self._subscribed) |
| 107 | + |
| 108 | +defdelete(self): |
| 109 | +returnself._boolean(self._delete(self._api),204,404) |
| 110 | + |
| 111 | +defis_ignored(self): |
| 112 | +returnself._ignored |
| 113 | + |
| 114 | +defis_subscribed(self): |
| 115 | +returnself._subscribed |
| 116 | + |
| 117 | +defset(self,subscribed,ignored): |
| 118 | +"""Set the user's subscription for this subscription |
| 119 | +
|
| 120 | + :param bool subscribed: (required), determines if notifications should |
| 121 | + be received from this thread. |
| 122 | + :param bool ignored: (required), determines if notifications should be |
| 123 | + ignored from this thread. |
| 124 | + """ |
| 125 | +sub=dumps({'subscribed':subscribed,'ignored':ignored}) |
| 126 | +json=self._json(self._put(self._api,data=sub),200) |
| 127 | +self.__init__(json,self._session) |