예제 #1
0
    def GET(self, user, id=None):
        data = PrefixDict(self.data, '%s/%s/' % (user, self.typename))
        if not id:
            self.app.status = '200 OK'
            keys = data.keys()
            if not keys:
                self.app.status = '404 Not Found'
                return
            keys.sort()
            if self.max_index > 0 and len(keys) > self.max_index:
                keys = keys[self.max_index:]
            objects = dict([(k, data[k]) for k in keys])
            del objects['']
            for key in objects:
                try:
                    obj = json.loads(objects[key])
                    objects[key] = obj
                except: pass
            yield json.dumps(objects, indent=4, sort_keys=True)
            return
        
        try:
            obj = data[id]
            self.app.status = '200 OK'
            #self.app.header('Content-type', 'application/json')
            yield obj
        except KeyError:
            self.app.status = '404 Not Found'
            yield self.app.status

        f = getattr(self, 'after_GET', None)
        if f:
            f(user, id)
        return
예제 #2
0
    def PUT(self, user, id=None):
        data = PrefixDict(self.data, '%s/%s/' % (user, self.typename))
        if not id:
            self.app.status = '400 Bad Request'
            yield 'ID is required when updating an object'
            return
        
        if id in data.keys():
            postdata = self.app.get_content().getfirst('value')

            try:
                obj = postdata['data'].value
            except TypeError:
                obj = postdata.value
            try:
                obj = json.loads(obj)
            except ValueError:
                self.app.status = '400 Bad Request'
                yield 'Unable to parse JSON data'
                return

            ts = int(time())
            obj['ts'] = ts
            data[id] = json.dumps(obj)
            self.app.status = '200 OK'
            yield id
        else:
            self.app.status = '404 Not Found'
            yield self.app.status

        f = getattr(self, 'after_PUT', None)
        if f:
            f(user, id)
        return
예제 #3
0
    def DELETE(self, user, id=None):
        data = PrefixDict(self.data, '%s/%s/' % (user, self.typename))
        if not id:
            self.app.status = '400 Bad Request'
            yield 'ID is required when deleting an object'
            return

        if id in data.keys():
            del data[id]
            self.app.status = '200 OK'
        else:
            self.app.status = '404 Not Found'
            yield self.app.status
        f = getattr(self, 'after_DELETE', None)
        if f:
            f(user, id)
        return