def __init__(self): init_model(True) application.test = True cherrypy.config.update({ "environment": "embedded" }) self.app = TestApp(application) self.fixture = SQLAlchemyFixture(env = models, engine = meta.engine, style = NamedDataStyle()) self.datasets = ( ds.KategoriaData, ds.PodkategoriaData, ds.ProduktData, ds.SiecData, ds.MiastoData, ds.SklepData, ds.ZakupData)
def GET(self, *args, **kwargs): init_model() s = Session() q = s.query(self.Model) for p in self.params: try: q = q.filter(getattr(self.Model, p) == kwargs[p]) except KeyError: raise cp.HTTPError(400, 'Bad request: %s not specified' % p) items = q.order_by(self.Model.nazwa).all() result = [{'id': i.id, 'nazwa': i.nazwa} for i in items] cp.response.headers['Content-Type'] = 'application/json' return json.dumps(result)
def POST(self, *args, **kwargs): try: inp = json.load(cp.request.body) except ValueError: raise cp.HTTPError('400', 'Malformed JSON') init_model() s = Session() try: m = self.Model.from_data(inp, s) except ValueError as e: raise cp.HTTPError(400, 'Bad input: %s' % e.args[0]) s.add(m) s.commit() return json.dumps({'id': m.id})
def POST(self): if ('application/json' not in cp.request.headers['Content-Type']): raise cp.HTTPError('400', 'JSON needed') try: inp = json.load(cp.request.body) except ValueError: raise cp.HTTPError('400', 'Malformed JSON') init_model() s = Session() data = inp['data'] sklep_id = inp['sklep'] try: sklep = s.query(Sklep).filter(Sklep.id == sklep_id).one() except orm.exc.NoResultFound: raise cp.HTTPError('400', 'Bad shop') zakupy = [] for z in inp['zakupy']: try: produkt = s.query(Produkt).filter( Produkt.id == z['produkt-id'] ).one() except orm.exc.NoResultFound: raise cp.HTTPError('400', 'Bad product') zakupy.append(Zakup( data, sklep, produkt, to_dec(z['cena']), to_dec(z['ilosc']) )) s.commit() for z in inp['zakupy']: z['id'] = zakupy.pop(0).id cp.response.headers['Content-Type'] = 'application/json' return json.dumps({"success": True, 'message': '', 'zakupy': inp['zakupy']})