File: __init__.py

package info (click to toggle)
python-pypump 0.3%2Bgit20130823.1.97bffc6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 328 kB
  • ctags: 333
  • sloc: python: 1,630; makefile: 140; sh: 7
file content (231 lines) | stat: -rw-r--r-- 6,294 bytes parent folder | download
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
##
# Copyright (C) 2013 Jessica T. (Tsyesika) <xray7224@googlemail.com>
# 
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##

import json

class AbstractModel(object):

    @property
    def TYPE(self):
        return self.__class__.__name__

    @property
    def objectType(self):
        return self.TYPE.lower()

    _mapping = {
        "objectType":"TYPE",
    }

    _pump = None

    def __init__(self, pypump=None, *args, **kwargs):
        """ Sets up pump instance """
        if pypump:
            self._pump = pypump

    def _post_activity(self, activity):
        """ Posts a activity to feed """
        data = self._pump.request(self.ENDPOINT, method="POST", data=activity)

        if not data:
            return False

        if "error" in data:
            raise PumpException(data["error"])

        self.unserialize(data["object"], obj=self)

        return True

    def serialize(self, *args, **kwargs):
        """ Changes it from obj -> JSON """
        data = {}
        for item in dir(self):
            if item.startswith("_"):
                continue # we don't want
            
            value =  getattr(self, item)
            
            # we need to double check we're not in mapper
            item = self.remap(item)
            data[item] = value

        return json.dumps(data, *args, **kwargs)

    @staticmethod
    def unserialize(self, data, *args, **kwargs):
        """ Changes it from JSON -> obj """
        data = json.loads(data)

        klass = self(pypump=self._pump)

        for key, value in data.items():
            key = self.remap(key)
            if key is None:
                continue

            setattr(klass, key, value)            

        return klass

    def remap(self, data):
        """ Remaps """
        if data in self._mapping.keys():
            return self._mappping[data]
        elif data in self.__mapping.values():
            for k, v in self._mapping.items():
                if data == v:
                    return k

        return data

class Likeable(object):
    """
        Provides the model with the like and unlike methods as well as
        the property likes which will look up who's liked the model instance
        and return you back a list of user objects
    
        must have _links["likes"]
    """

    @property
    def likes(self):
        """ Gets who's liked this object """
        endpoint = self._links["likes"]
        likes = self._pump.request(endpoint, raw=True)
        likes_obj = []
        for l in likes.get("items", likes_obj):
            likes_obj.append(self._pump.Person.unserialize(l))
        
        return likes_obj

    favorites = likes

    def like(self, verb="like"):
        """ Likes the model """
        activity = {
            "verb": verb,
            "object": {
                "id": self.id,
                "objectType": self.objectType,
            }
        }

        self._post_activity(activity)

    def unlike(self, verb="unlike"):
        """ Unlikes the model """
        activity = {
            "verb": verb,
            "object": {
                "id": self.id,
                "objectType": self.objectType,
            }
        }

        self._post_activity(activity)

    def favorite(self):
        """ Favourite model """
        return self.like(verb="favorite")

    def unfavorite(self):
        """ Unfavourite model """
        return self.unlike(verb="unfavorite")

class Commentable(object):
    """
        Provides the model with the comment method allowing you to post
        a comment to on the model. It also provides an ability to read
        comments.

        must have _likes["replies"]
    """

    @property
    def comments(self):
        """ Fetches the comment objects for the models """
        endpoint = self._links["replies"]
        comments = self._pump.request(endpoint, raw=True)
        comments_obj = []
        for c in comments.get("items", comments_obj):
            comments_obj.append(self._pump.Comment.unserialize(c))
        return comments_obj

    def comment(self, comment):
        """ Posts a comment object on model """
        comment.inReplyTo = self
        comment.send()

class Shareable(object):
    """
        Provides the model with the share and unshare methods and shares
        property allowing you to see who's shared the model.

        must have _likes["shares"]
    """

    @property
    def shares(self):
        """ Fetches the people who've shared the model """
        endpoint = self._links["shares"]
        shares = self._pump.request(endpoint, raw=True)
        shares_obj = []
        for p in shares.get("items", shares_obj):
            shares_obj.append(self._pump.Person.unserialize(p))
        return shares_obj

    def share(self):
        """ Shares the model """
        activity = {
            "verb": "share",
            "object": {
                "id": self.id,
                "objectType": self.objectType,
            },
        }

        self._post_activity(activity)

    def unshare(self):
        """ Unshares a previously shared model """
        activity = {
            "verb": "unshare",
            "object": {
                "id": self.id,
                "objectType": self.objectType,
            },
        }

        self._post_activity(activity)

class Deleteable(object):
    """ Provides the model with the ability to be deleted """

    def delete(self):
        """ Delete's a model """
        activity = {
            "verb": "delete",
            "object": {
                "id": self.id,
                "objectType": self.objectType,
            }
        }

        self._post_activity(activity)