Esempio n. 1
0
class ProgramaHandler(BaseHandler):

    def initialize(self):
        self._model = Programa(self.application.db)

    def get(self, id=None):
        data = self._get_data(inspect.currentframe())
        try:
            query = self._model.get(data)
            self.write(escape.json_encode(query))
            self.set_header("Content-Type", "application/json")
        except ValueError:
            self.send_error(400)

    def post(self, id=None):
        data = self._post_put_data(self._json_args)
        try:
            query = self._model.save(data)
            self.write(escape.json_encode(query))
            self.set_header("Content-Type", "application/json")
        except ValueError:
            self.send_error(400)

    def put(self, id):
        if id == str(self._json_args['id']):
            try:
                data = self._post_put_data(self._json_args)
                query = self._model.update(data)
                self.write(escape.json_encode(query))
                self.set_header("Content-Type", "application/json")
            except ValueError:
                self.send_error(400)
        else:
            self.send_error(400)

    def delete(self, id):
        try:
            data = self._get_data(inspect.currentframe())
            self._model.fake_delete(data)
        except ValueError:
            self.send_error(400)
Esempio n. 2
0
class EstadoActualHandler(RequestHandler):

    def initialize(self):
        self.hist = HistorialPrograma(self.application.db)
        self.progs = Programa(self.application.db)

    def get(self):
        last_hist = self.hist.getmax()
        if last_hist is not None:
            prog = self.progs.get({'id': str(last_hist['programa_id'])})
            data = {
                'titulo': prog['titulo'],
                'desde': last_hist['created'],
            }
        else:
            data = {
                'titulo': False,
                'desde': False,
            }
        self.write(escape.json_encode(data))
        self.set_header("Content-Type", "application/json")