Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/db/routes.py
blob: 221d066a05edfaa6ee01f4a5c0bc5905cf929978 (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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Copyright (C) 2011-2014 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/>.

# pylint: disable-msg=W0611

import logging
from contextlib import contextmanager

from sugar_network import toolkit
from sugar_network.db.metadata import Aggregated, Author
from sugar_network.toolkit.router import ACL, File
from sugar_network.toolkit.router import route, fallbackroute, preroute
from sugar_network.toolkit.coroutine import this
from sugar_network.toolkit import http, ranges, enforce


_logger = logging.getLogger('db.routes')


class Routes(object):

    def __init__(self, volume, find_limit=None):
        this.volume = self.volume = volume
        this.add_property('resource', _get_resource)
        self._find_limit = find_limit

    @preroute
    def __preroute__(self, op):
        this.reset_property('resource')

    @route('POST', [None], acl=ACL.AUTH, mime_type='application/json')
    def create(self):
        with self._post(ACL.CREATE) as doc:
            doc.created()
            if this.principal:
                authors = doc.posts['author'] = {}
                self._useradd(authors, this.principal, Author.ORIGINAL)
            self.volume[this.request.resource].create(doc.posts)
            this.request.guid = doc.guid
            return doc.guid

    @route('PUT', [None, None], acl=ACL.AUTH | ACL.AUTHOR)
    def update(self):
        with self._post(ACL.WRITE) as doc:
            if not doc.posts:
                return
            doc.updated()
            self.volume[this.request.resource].update(doc.guid, doc.posts)

    @route('PUT', [None, None, None], acl=ACL.AUTH | ACL.AUTHOR)
    def update_prop(self):
        request = this.request
        request.content = {request.prop: request.content}
        self.update()

    @route('DELETE', [None, None], acl=ACL.AUTH | ACL.AUTHOR)
    def delete(self):
        # Node data should not be deleted immediately
        # to make master-slave synchronization possible
        directory = self.volume[this.request.resource]
        doc = directory[this.request.guid]
        enforce(doc.available, http.NotFound, 'Resource not found')
        doc.posts['state'] = 'deleted'
        doc.updated()
        directory.update(doc.guid, doc.posts, 'delete')

    @route('GET', [None],
            arguments={'offset': int, 'limit': int, 'reply': ('guid',)},
            mime_type='application/json')
    def find(self, reply, limit):
        self._preget()
        request = this.request
        if not limit:
            request['limit'] = self._find_limit
        elif self._find_limit and limit > self._find_limit:
            _logger.warning('The find limit is restricted to %s',
                    self._find_limit)
            request['limit'] = self._find_limit
        documents, total = self.volume[request.resource].find(
                not_state='deleted', **request)
        result = [self._postget(i, reply) for i in documents]
        return {'total': total, 'result': result}

    @route('GET', [None, None], cmd='exists', mime_type='application/json')
    def exists(self):
        return self.volume[this.request.resource][this.request.guid].available

    @route('GET', [None, None], arguments={'reply': list},
            mime_type='application/json')
    def get(self, reply):
        if not reply:
            reply = []
            for prop in self.volume[this.request.resource].metadata.values():
                if prop.acl & ACL.READ and not isinstance(prop, Aggregated):
                    reply.append(prop.name)
        self._preget()
        doc = self.volume[this.request.resource].get(this.request.guid)
        enforce(doc.available, http.NotFound, 'Resource not found')
        return self._postget(doc, reply)

    @route('GET', [None, None, None], mime_type='application/json')
    def get_prop(self):
        request = this.request
        directory = self.volume[request.resource]
        directory.metadata[request.prop].assert_access(ACL.READ)
        return directory[request.guid].repr(request.prop)

    @route('HEAD', [None, None, None])
    def get_prop_meta(self):
        return self.get_prop()

    @route('POST', [None, None, None],
            acl=ACL.AUTH | ACL.AGG_AUTHOR, mime_type='application/json')
    def insert_to_aggprop(self):
        return self._aggpost(ACL.INSERT)

    @route('PUT', [None, None, None, None],
            acl=ACL.AUTH | ACL.AGG_AUTHOR, mime_type='application/json')
    def update_aggprop(self):
        self._aggpost(ACL.REPLACE)

    @route('DELETE', [None, None, None, None],
            acl=ACL.AUTH | ACL.AGG_AUTHOR, mime_type='application/json')
    def remove_from_aggprop(self):
        self._aggpost(ACL.REMOVE)

    @route('GET', [None, None, None, None], mime_type='application/json')
    def get_aggprop(self):
        doc = self.volume[this.request.resource][this.request.guid]
        prop = doc.metadata[this.request.prop]
        prop.assert_access(ACL.READ)
        enforce(isinstance(prop, Aggregated), http.BadRequest,
                'Property is not aggregated')
        agg_value = doc[prop.name].get(this.request.key)
        enforce(agg_value is not None, http.NotFound,
                'Aggregated item not found')
        return prop.subreprcast(agg_value['value'])

    @route('PUT', [None, None], cmd='useradd',
            arguments={'role': 0}, acl=ACL.AUTH | ACL.AUTHOR)
    def useradd(self, user, role):
        request = this.request
        enforce(user, "Argument 'user' is not specified")
        directory = self.volume[request.resource]
        authors = directory.get(request.guid)['author']
        self._useradd(authors, user, role)
        directory.update(request.guid, {'author': authors})

    @route('PUT', [None, None], cmd='userdel', acl=ACL.AUTH | ACL.AUTHOR)
    def userdel(self, user):
        request = this.request
        enforce(user, "Argument 'user' is not specified")
        enforce(user != this.principal, 'Cannot remove yourself')
        directory = self.volume[request.resource]
        authors = directory.get(request.guid)['author']
        enforce(user in authors, 'No such user')
        del authors[user]
        directory.update(request.guid, {'author': authors})

    @fallbackroute('GET', ['blobs'])
    def blobs(self):
        return self.volume.blobs.get(this.request.path[1:])

    @fallbackroute('GET', ['assets'])
    def assets(self):
        return self.volume.blobs.get(this.request.path)

    @contextmanager
    def _post(self, access):
        content = this.request.content
        enforce(isinstance(content, dict), http.BadRequest, 'Invalid value')

        if access == ACL.CREATE:
            guid = content.get('guid') or toolkit.uuid()
            doc = self.volume[this.request.resource][guid]
            enforce(not doc.exists, 'Resource already exists')
            doc.posts['guid'] = guid
            for name, prop in doc.metadata.items():
                if name not in content and prop.default is not None:
                    doc.posts[name] = prop.default
        else:
            enforce('guid' not in content, http.BadRequest,
                    'GUID in cannot be changed')
            doc = self.volume[this.request.resource][this.request.guid]
            enforce(doc.available, 'Resource not found')

        def teardown(new, old):
            for name, value in new.items():
                if old.get(name) != value:
                    doc.metadata[name].teardown(value)

        try:
            for name, value in content.items():
                prop = doc.metadata[name]
                prop.assert_access(access, doc.orig(name))
                if value is None:
                    doc.posts[name] = prop.default
                    continue
                try:
                    doc.posts[name] = prop.typecast(value)
                except Exception, error:
                    error = 'Value %r for %r property is invalid: %s' % \
                            (value, prop.name, error)
                    _logger.exception(error)
                    raise http.BadRequest(error)
            yield doc
        except Exception:
            teardown(doc.posts, doc.origs)
            raise
        else:
            teardown(doc.origs, doc.posts)

    def _preget(self):
        reply = this.request.get('reply')
        if not reply:
            this.request['reply'] = ('guid',)
        else:
            directory = self.volume[this.request.resource]
            for prop in reply:
                directory.metadata[prop].assert_access(ACL.READ)

    def _postget(self, doc, props):
        result = {}
        for name in props:
            result[name] = doc.repr(name)
        return result

    def _useradd(self, authors, user, role):
        props = {}
        user_doc = self.volume['user'][user]
        if user_doc.available:
            props['name'] = user_doc['name']
            role |= Author.INSYSTEM
        else:
            role &= ~Author.INSYSTEM
        props['role'] = role & (Author.INSYSTEM | Author.ORIGINAL)

        if user in authors:
            authors[user].update(props)
        else:
            authors[user] = props

    def _aggpost(self, acl):
        request = this.request
        doc = self.volume[request.resource][request.guid]
        prop = doc.metadata[request.prop]
        enforce(isinstance(prop, Aggregated), http.BadRequest,
                'Property is not aggregated')
        prop.assert_access(acl)

        aggid = request.key
        if aggid and aggid in doc[request.prop]:
            aggvalue = doc[request.prop][aggid]
            prop.subteardown(aggvalue['value'])
        else:
            enforce(acl != ACL.REMOVE, http.NotFound, 'No aggregated item')

        aggvalue = {}
        if acl != ACL.REMOVE:
            value = prop.subtypecast(request.content)
            if type(value) is tuple:
                aggid_, value = value
                enforce(not aggid or aggid == aggid_, http.BadRequest,
                        'Wrong aggregated id')
                aggid = aggid_
            elif not aggid:
                aggid = toolkit.uuid()
            aggvalue['value'] = value

        if this.principal:
            authors = aggvalue['author'] = {}
            role = Author.ORIGINAL if this.principal in doc['author'] else 0
            self._useradd(authors, this.principal, role)
        doc.posts[request.prop] = {aggid: aggvalue}
        doc.updated()
        self.volume[request.resource].update(request.guid, doc.posts)

        return aggid


def _get_resource():
    request = this.request
    return this.volume[request.resource][request.guid]