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}
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from feedduty.models import ( BaseModel, MyModel, ) DBSession.configure(bind=engine) BaseModel.metadata.create_all(engine) with transaction.manager: model = MyModel(name='one', value=55) DBSession.add(model)
def collection_post(self): """ Create new object - Only accepts POST requests on the collection URI """ form = self.form(self.request.POST) obj = self.model() if form.validate(): # extract values from form and populate the feed instance form.populate_obj(obj) # Save the feed to the database DBSession.add(obj) resp = {'success': True, 'result': obj} else: resp = {'success': False, 'errors': {}} return resp
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ # configure SQLAlchemy engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) BaseModel.metadata.bind = engine # Load pyramid configuration config = Configurator(settings=settings) # Configure extensions config.add_jinja2_search_path("feedduty:api/templates") config.add_jinja2_search_path("feedduty:web/templates") # Configure renderers # Configure JSON renderer to understand certain objects json_renderer = JSON() json_renderer.add_adapter(datetime.datetime, _datetime_adapter) config.add_renderer('json', json_renderer) config.add_renderer('.html', jinja2_renderer) # Static assets - this should only work in dev, in prod it should be served by web server (nginx?) config.add_static_view('static', 'feedduty:static', cache_max_age=3600) # API Routing configuration config.add_route('api_main', '/api') # Catch all route - loads the web app config.add_route('app', '/') # scan for view handlers config.scan('feedduty.api.views') config.scan('feedduty.web.views') # return a uwsgi compatible app return config.make_wsgi_app()
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
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
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
def tearDown(self): DBSession.remove() testing.tearDown()