Esempio n. 1
0
class ConfiguracionHandler(RequestHandler):

    def initialize(self):
        self.model = Configuracion(self.application.db)


    def prepare(self):
        json_methods = ['POST', 'PUT']
        if self.request.headers.get("Content-Type") is not None:
            if ("application/json" in self.request.headers.get("Content-Type")) & (self.request.method in json_methods):
                self.json_args = escape.json_decode(self.request.body)

    @staticmethod
    def ___nestedtosimple__(data, name):
        if type(data[name]) is dict:
            rel_id = data[name]['id']
            data[name] = rel_id
        return data

    @staticmethod
    def makegetdata(obj):
        args, _, _, values = inspect.getargvalues(obj)
        data = {}
        for i in args:
            if (i != 'self') & (values[i] is not None):
                if values[i]:
                    data[i] = values[i]
        return data

    @staticmethod
    def makepostdata(obj):
        data = {}
        for k, v in obj.items():
            if v is not None:
                data[k] = v
        return data

    def get(self):
        data = self.makegetdata(inspect.currentframe())
        query = self.model.get(data)
        self.write(escape.json_encode(query))
        self.set_header("Content-Type", "application/json")

    def post(self):
        data = self.makepostdata(self.json_args)
        query = self.model.save(data)
        self.write(escape.json_encode(query))
        self.set_header("Content-Type", "application/json")
Esempio n. 2
0
 def initialize(self):
     self.model = Configuracion(self.application.db)