示例#1
0
class MainWebObjectsView(BaseResource):
    def __init__(self, request):
        super(MainWebObjectsView, self).__init__(request)
        settings = request.registry.settings
        self.mgr = WebObjectManager(request.db)

    def serialize_object(self, dbobj):
        data = dbobj.serialize()
        data['content'] = dbobj.content
        #import pdb ; pdb.set_trace()
        #data['thumbnail'] = base64.b64encode(dbobj.thumbnail)
        return data

    def collection_query(self):
        return self.mgr.query()

    def get(self):
        id = int(self.request.matchdict['id'])
        dbobj = self.mgr.get(id)
        if 'content' in self.request.GET:
            return dbobj.content
        return self.serialize_object(dbobj)

    def _get_json_from_request(self):
        data = self.request.json
        name = data['name']
        type = data['type']
        content = data['content']
        return name, type, content
    
    def put(self):
        id = int(self.request.matchdict['id'])
        name, type, content = self._get_json_from_request()
        dbobj = self.mgr.update_object(id, name, type, content)
        return self.serialize_object(dbobj)
    
        
    def collection_post(self):
        name, type, content = self._get_json_from_request()
        wobject = self.mgr.add_object(name, type, content)
        return dict(object=wobject.serialize(), result='success')

    def _handleerrors(self):
        try:
            pass
        except FilenameInDatabaseError:
            raise HTTPConflict("This file already exists.")
        except ImageFileExistsError:
            raise HTTPConflict("This file already exists.")
示例#2
0
 def __init__(self, request):
     super(MainWebObjectsView, self).__init__(request)
     settings = request.registry.settings
     self.mgr = WebObjectManager(request.db)