Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/resources.py
blob: c9dc672ee96976e534deb1201f53edea5541f370 (plain)
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
# Copyright (C) 2012, Aleksey Lim
#
# 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/>.

from gettext import gettext as _

from sugar_network import client
from sugar_network.util import enforce


class Resource(client.Object):
    """Common routines for Sugar Network resources."""

    #: Resource name
    resource = None
    #: List of properties to return from query to avoid
    # additional requests to fetch missed properties
    reply_properties = None

    def __init__(self, guid=None, **filters):
        """Get access to resource object.

        If no arguments given, new object will be assumed.

        :param guid:
            object's GUID to find
        :param filters:
            a dictionary of properties to find object if `guid`
            was not specified

        """
        if guid:
            client.Object.__init__(self, self.resource, {'guid': guid})
        elif not filters:
            client.Object.__init__(self, self.resource)
        else:
            query = self.find(**filters)
            enforce(query.total, KeyError, _('No objects found'))
            enforce(query.total == 1, _('Found more than one object'))
            client.Object.__init__(self, self.resource, query[0])

    @classmethod
    def find(cls, *args, **kwargs):
        """Query resource objects.

        Function accpet the same arguments as `sugar_network.Query()`.

        """
        if 'reply_properties' not in kwargs:
            kwargs['reply_properties'] = cls.reply_properties
        return client.Query(cls.resource, *args, **kwargs)

    @classmethod
    def delete(cls, guid):
        """Delete resource object.

        :param guid:
            resource object's GUID

        """
        client.delete(cls.resource, guid)


class User(Resource):

    resource = 'user'
    reply_properties = ['guid', 'nickname', 'color']

    @classmethod
    def new(cls):
        raise RuntimeError(_('Users cannot be created explicitly'))

    @classmethod
    def delete(cls, guid):
        raise RuntimeError(_('Users cannot be deleted explicitly'))


class Context(Resource):

    resource = 'context'
    reply_properties = ['guid', 'author', 'name', 'title', 'summary']


class Question(Resource):

    resource = 'question'
    reply_properties = ['guid', 'author', 'title']


class Idea(Resource):

    resource = 'idea'
    reply_properties = ['guid', 'author', 'title']


class Problem(Resource):

    resource = 'problem'
    reply_properties = ['guid', 'author', 'title']


class Review(Resource):

    resource = 'review'
    reply_properties = ['guid', 'author', 'title']


class Solution(Resource):

    resource = 'solution'
    reply_properties = ['guid', 'author', 'title']


class Artifact(Resource):

    resource = 'artifact'
    reply_properties = ['guid', 'author', 'title']


class Implementation(Resource):

    resource = 'implementation'
    reply_properties = ['guid', 'author', 'version', 'date', 'stability']


class Report(Resource):

    resource = 'report'
    reply_properties = ['guid', 'author', 'title']


class Notification(Resource):

    resource = 'notification'
    reply_properties = [
            'guid', 'author', 'type', 'object_type', 'object', 'to', 'message']


class Comment(Resource):

    resource = 'comment'
    reply_properties = ['guid', 'author', 'message']