Exemplo n.º 1
0
 def patch(self, id=None):
     if id:
         m = self.query().get(id)
         if m:
             for key, value in self.model.from_dict(request.json, self.db).items():
                 # if value being replaced is a model or list of models,
                 # delete it/them from the database first
                 if isinstance(value, list):
                     old = getattr(m, key)
                     newitems = [i for i in value if i not in old]
                     olditems = [i for i in old if i not in value]
                     for item in olditems:
                         old.remove(item)
                         if issubclass(type(item), Association):
                             self.db.delete(item)
                     for item in newitems:
                         old.append(item)
                 else:
                     setattr(m, key, value)
             self.db.commit()
             self.db.refresh(m)
             response = HTTPResponse(status=200, body=json.dumps(m.to_dict()))
             response.content_type = "application/json"
             return response
     abort(404)
Exemplo n.º 2
0
 def default_error_handler(error):
     if isinstance(error, HTTPError):
         r = HTTPResponse()
         error.apply(r)
         r.content_type = error.content_type
         return r
     return error
Exemplo n.º 3
0
 def patch(self, id=None):
     if id:
         m = self.query().get(id)
         if m:
             for key, value in self.model.from_dict(request.json,
                                                    self.db).items():
                 # if value being replaced is a model or list of models,
                 # delete it/them from the database first
                 if isinstance(value, list):
                     old = getattr(m, key)
                     newitems = [i for i in value if i not in old]
                     olditems = [i for i in old if i not in value]
                     for item in olditems:
                         old.remove(item)
                         if issubclass(type(item), Association):
                             self.db.delete(item)
                     for item in newitems:
                         old.append(item)
                 else:
                     setattr(m, key, value)
             self.db.commit()
             self.db.refresh(m)
             response = HTTPResponse(status=200,
                                     body=json.dumps(m.to_dict()))
             response.content_type = "application/json"
             return response
     abort(404)
Exemplo n.º 4
0
 def get(self, id=None):
     if id:
         match = self.query().get(id).to_dict()
         if match:
             return json.dumps(match)
         abort(404)
     else:
         response = HTTPResponse(status=200, body=json.dumps([m.to_dict() for m in self.query().all()]))
         response.content_type = "application/json"
         return response
Exemplo n.º 5
0
 def get(self, id=None):
     if id:
         match = self.query().get(id).to_dict()
         if match:
             return json.dumps(match)
         abort(404)
     else:
         response = HTTPResponse(
             status=200,
             body=json.dumps([m.to_dict() for m in self.query().all()]))
         response.content_type = "application/json"
         return response
Exemplo n.º 6
0
def error404(error):
    mheader = request.get_header("host")
    output = call_lua("./src/static.lua", mheader, request.path)

    resp = HTTPResponse(body=output, status=200)
    mtype = "application/octet-stream"
    lowered = request.path.lower()
    if lowered.endswith("jpg") or lowered.endswith("jpeg"):
        mtype = "image/jpeg"
    elif lowered.endswith("gif"):
        mtype = "image/gif"
    elif lowered.endswith("png"):
        mtype = "image/png"
    elif lowered.endswith("webm"):
        mtype = "video/webm"

    resp.content_type = mtype
    return resp
Exemplo n.º 7
0
    def render_response(self, resp):
        # always ensure we have a http response instance
        if not isinstance(resp, HTTPResponse):
            resp = HTTPResponse(resp)

        # create new response type
        nresp = HTTPResponse()
        resp.apply(nresp)
        if not request.nctx.renderer: return nresp

        # apply rendering
        nresp.body = request.nctx.renderer.render(nresp.body)
        nresp.content_type = request.nctx.response_content_type
 
        # XXX: manually append charset due to bug
        # https://github.com/bottlepy/bottle/issues/1048
        if request.nctx.renderer.charset:
            to_append = '; charset={}'.format(request.nctx.renderer.charset.upper())
            nresp.content_type += to_append

        return nresp
Exemplo n.º 8
0
    def render_response(self, resp):
        # always ensure we have a http response instance
        if not isinstance(resp, HTTPResponse):
            resp = HTTPResponse(resp)

        # create new response type
        nresp = HTTPResponse()
        resp.apply(nresp)
        if not request.nctx.renderer: return nresp

        # apply rendering
        nresp.body = request.nctx.renderer.render(nresp.body)
        nresp.content_type = request.nctx.response_content_type

        # XXX: manually append charset due to bug
        # https://github.com/bottlepy/bottle/issues/1048
        if request.nctx.renderer.charset:
            to_append = '; charset={}'.format(
                request.nctx.renderer.charset.upper())
            nresp.content_type += to_append

        return nresp
Exemplo n.º 9
0
def root_post():
    mheader = request.get_header("host")
    if request.POST:
        json_val = {k:v for k,v in request.forms.items()}

        image = request.files.get("image")
        if image:
            json_val["image"] = TMPFILE_LOC + image.filename
            image.save(TMPFILE_LOC, overwrite=True)

        music = request.files.get("music")
        if music:
            json_val["music"] = TMPFILE_LOC + music.filename
            music.save(TMPFILE_LOC, overwrite=True)
        return call_lua("./src/root.lua", mheader, json.dumps(json_val))

    output = call_lua("./src/root.lua", mheader)
    resp = HTTPResponse(body=output, status=200)

    if mheader.startswith('api.'):
        resp.content_type = 'application/json'

    return resp
Exemplo n.º 10
0
 def post(self):
     m = self.insert(request.json)
     response = HTTPResponse(status=201, body=json.dumps(m.to_dict()))
     response.content_type = "application/json"
     response.add_header("Location", "{}/{}".format(self.base, m.id))
     return response
Exemplo n.º 11
0
 def post(self):
     m = self.insert(request.json)
     response = HTTPResponse(status=201, body=json.dumps(m.to_dict()))
     response.content_type = "application/json"
     response.add_header("Location", "{}/{}".format(self.base, m.id))
     return response