예제 #1
0
    def delete(self):
        """
        Delete an object
        """
        obj = DBSession.query(self.model).get(int(self.request.matchdict['id']))
        if not obj:
            raise HTTPNotFound()

        # Delete the feed
        DBSession.delete(obj)

        return {'success': True}
예제 #2
0
    def collection_get(self):
        """
        List objects - Only accepts GET requests on the collection URI

        """

        collection = DBSession.query(self.model).filter()
        json_response = {'success': True, 'result': [obj.__json__(self.request) for obj in collection]}

        if self.render_json:
            resp = json_response
        else:
            # embed the response for the HTML templates
            resp = {'json_response': json.dumps(json_response, indent=2), 'form': self.form()}

        return resp
예제 #3
0
    def get(self):
        """
        Retrieve an object
        """
        obj = DBSession.query(self.model).get(int(self.request.matchdict['id']))
        if not obj:
            raise HTTPNotFound()

        json_response = {'success': True, 'result': obj.__json__(self.request)}

        if self.render_json:
            resp = json_response
        else:
            # embed the response for the HTML templates
            resp = {'json_response': json.dumps(json_response, indent=2)}
            resp['form'] = self.form(obj=obj)

        return resp
예제 #4
0
    def put(self):
        """
        Update an object
        """
        form = self.form(self.request.POST)
        obj = DBSession.query(self.model).get(int(self.request.matchdict['id']))
        if not obj:
            raise HTTPNotFound()

        if form.validate():
            # extract values from form and populate the feed instance
            # Since the object already exists in the database, the db session will automatically commit the changes
            form.populate_obj(obj)

            resp = {'success': True, 'result': obj.__json__(self.request)}
        else:
            resp = {'success': False, 'errors': {}}

        return resp