Ejemplo n.º 1
0
 def tearDown(self):
     db.close()
Ejemplo n.º 2
0
 def tearDown(self):
     db.close()
Ejemplo n.º 3
0
                if settings.DEBUG: print content
            if settings.DEBUG: print db.session
            # handle outgoing data
            body = SiteResource.render(self, request)
        except Http404, h:
            if settings.DEBUG: traceback.print_exc()
            request.setResponseCode(http.NOT_FOUND)
            body = {'error': str(h)}
        except Exception, e:  # *** this might catch too much
            if settings.DEBUG: traceback.print_exc()
            request.setResponseCode(http.INTERNAL_SERVER_ERROR)
            body = {'error': str(e)}
        else:
            db.commit()
        # close out db session -- very important with threads
        db.close()
        if body not in (None, ''):
            json_body = encode_response(body)
            if settings.DEBUG:
                print json_body
            return json_body
        return ''

def encode_response(response):
    # twisted web expects regular str, which encode('utf-8') gives
    return json.encode(response).encode('utf-8')
    
class ThreadedJSONSiteResource(JSONSiteResource):
    "Run each request in its own thread."
    def render(self, request):
        d = threads.deferToThread(JSONSiteResource.render, self, request)
Ejemplo n.º 4
0
                if settings.DEBUG: print content
            if settings.DEBUG: print db.session
            # handle outgoing data
            body = SiteResource.render(self, request)
        except Http404, h:
            if settings.DEBUG: traceback.print_exc()
            request.setResponseCode(http.NOT_FOUND)
            body = {'error': str(h)}
        except Exception, e:  # *** this might catch too much
            if settings.DEBUG: traceback.print_exc()
            request.setResponseCode(http.INTERNAL_SERVER_ERROR)
            body = {'error': str(e)}
        else:
            db.commit()
        # close out db session -- very important with threads
        db.close()
        if body not in (None, ''):
            json_body = encode_response(body)
            if settings.DEBUG:
                print json_body
            return json_body
        return ''


def encode_response(response):
    # twisted web expects regular str, which encode('utf-8') gives
    return json.encode(response).encode('utf-8')


class ThreadedJSONSiteResource(JSONSiteResource):
    "Run each request in its own thread."