File: collection.py

package info (click to toggle)
python-pypump 0.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 3,153; makefile: 134
file content (120 lines) | stat: -rw-r--r-- 3,498 bytes parent folder | download | duplicates (4)
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
##
# 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 logging

from pypump.models import (PumpObject, Deleteable)
from pypump.models.feed import Feed

_log = logging.getLogger(__name__)


class Collection(PumpObject, Deleteable):
    """ This object represents a pump.io **collection**, collections have
    a members :class:`Feed <pypump.models.feed.Feed>` and methods for adding
    and removing objects to that feed.

    :param id: (optional) Collection id.

    Example:
        >>> friendlist = pump.me.lists['Friends']
        >>> list(friendlist.members)
        [<Person: alice@example.org>]
        >>> friendlist.add(pump.Person('bob@example.org'))
    """

    object_type = 'collection'
    _ignore_attr = ["in_reply_to"]
    _mapping = {
        "_members": "members"
    }

    def __init__(self, id=None, **kwargs):
        super(Collection, self).__init__(**kwargs)

        self.id = id

    @property
    def members(self):
        """ :class:`Feed <pypump.models.feed.Feed>` of collection members.
        """
        if self._members is None:
            self._members = Feed(self.links["members"], pypump=self._pump)
        return self._members

    def add(self, obj):
        """ Adds a member to the collection.

        :param obj: Object to add.

        Example:
            >>> mycollection.add(pump.Person('bob@example.org'))
        """
        activity = {
            "verb": "add",
            "object": {
                "objectType": obj.object_type,
                "id": obj.id
            },
            "target": {
                "objectType": self.object_type,
                "id": self.id
            }
        }

        self._post_activity(activity)

        # Remove the cash so it's re-generated next time it's needed
        self._members = None

    def remove(self, obj):
        """ Removes a member from the collection.

        :param obj: Object to remove.

        Example:
            >>> mycollection.remove(pump.Person('bob@example.org'))
        """
        activity = {
            "verb": "remove",
            "object": {
                "objectType": obj.object_type,
                "id": obj.id
            },
            "target": {
                "objectType": self.object_type,
                "id": self.id
            }
        }

        self._post_activity(activity)

        # Remove the cash so it's re-generated next time it's needed
        self._members = None

    def __unicode__(self):
        return u'{0}'.format(self.display_name or self.id)

    def __repr__(self):
        return "<{type}: {id}>".format(type=self.object_type.capitalize(),
                                       id=self.id)


class Public(PumpObject):

    def __init__(self):
        self.id = "http://activityschema.org/collection/public"
        self.object_type = 'collection'