Exemple #1
0
 def DELETE(self):
     i = web.input()
     if session.logged_in:
         try:
             reviewer = db.select('users', what="id", where="usr is " + "\"%s\"" % session.usr).list()[0].id
             who_reviewed = db.select('review', what="reviewer", where='id is %d' % int(i.review_id)).list()[0].id
             if reviewer == who_reviewed:
                 db.delete('review', where='id is %d' % int(i.review_id))
             else:
                 web.error("You may only delete your own reviews")
         except AttributeError:
             return render.error(sys.exc_value)
     else:
         raise web.error("You must be logged in to do that")
     return False
Exemple #2
0
while True:
    try:
        conn, addr = s.accept()
        request = conn.recv(1024)
        request = str(request)
        config = ure.search(r'\/tables\/led\/(\d-\d-\d)', request)
        gc.collect()
        if config is not None:
            values = config.group(1).split('-')
            values = [int(v) for v in values]
            try:
                led_values = tableController.set_table_state(
                    values[0], values[1], values[2])
                response = web.result(led_values)
            except Exception as e:
                response = web.error(e)
                conn.send('HTTP/1.1 200 OK\n')
                conn.send('Content-Type: text/html\n')
                conn.send('Connection: close\n\n')
                conn.sendall(response)
                conn.close()
        else:
            gc.collect()
            print(gc.mem_free())
            print("Returning web page.")
            web.web_page(conn, tableController.get_table_state())
        gc.collect()
    except:
        gc.collect()
Exemple #3
0
 def __call__(self, request):
     """
     Handle the given request.
     """
     # Make sure this plays nice with Web.
     request.auto_finish = False
     
     response = []
     status = '200 OK'
     headers = {}
     
     def start_response(status, head):
         status = status
         headers.update(head)
         return response.append
     
     # Build an environment for the WSGI application.
     environ = {
         'REQUEST_METHOD'    : request.method,
         'SCRIPT_NAME'       : '',
         'PATH_INFO'         : request.path,
         'QUERY_STRING'      : request.query,
         'SERVER_NAME'       : request.headers.get('Host','127.0.0.1'),
         'SERVER_PORT'       : request.connection.server.local_addr[1],
         'SERVER_PROTOCOL'   : request.version,
         'wsgi.version'      : (1,0),
         'wsgi.url_scheme'   : request.protocol,
         'wsgi.input'        : cStringIO.StringIO(request.body),
         'wsgi.errors'       : sys.stderr,
         'wsgi.multithread'  : False,
         'wsgi.multiprocess' : False,
         'wsgi.run_once'     : False
     }
     
     if 'Content-Type' in request.headers:
         environ['CONTENT_TYPE'] = request.headers['Content-Type']
     if 'Content-Length' in request.headers:
         environ['CONTENT_LENGTH'] = request.headers['Content-Length']
     
     for k,v in request.headers.iteritems():
         environ['HTTP_%s' % k.replace('-','_').upper()] = v
     
     # Run the WSGI Application.
     try:
         result = self.app(environ, start_response)
     except Exception, e:
         log.exception('Exception running WSGI application for: %s %s',
             request.method, request.path)
         
         if not self.debug:
             body, status, headers = error(500, request=request, debug=False)
         else:
             resp = u''.join([
                 u"<h2>Traceback</h2>\n",
                 u"<pre>%s</pre>\n" % traceback.format_exc(),
                 u"<h2>HTTP Request</h2>\n",
                 request.__html__(),
                 ])
             body, status, headers = error(resp, 500, request=request,
                 debug=True)
         
         status = '500 Internal Server Error'
         response = [body]
         result = []
Exemple #4
0
                if not beer_id_list:
                    return render.error("Beer %s not found in list" % i.beer)
                beer_id = beer_id_list[0].id
                reviewer = db.select('users', what="id", where="usr is \"%s\"" % session.usr).list()[0].id
                reviews_added = db.insert('review', aroma=i.aroma, appearance=i.appearance,
                                                    taste=i.taste, palate=i.palate, bottle_style=i.bottle_style,
                                                    beer=beer_id, reviewer=reviewer)
                self.update_overall_beer_rating(i, False)
            except sqlite3.IntegrityError, KeyError:
                return render.error(sys.exc_value)
            except IndexError:
                return render.error("Beer %s not found in the list" % i.beer)
            except AttributeError:
                return render.error(sys.exc_value)
            raise web.seeother('/')
        raise web.error("You must have a logged_in cookie to add a Review")

    def PUT(self):
        i = web.input()
        if session.logged_in:
            # Make sure we were giveen a valid review_id
            if not db.select('review', what="id", where="id is %d" % int(i.review_id)).list():
                return render.error("The review id %d is invald" % int(i.review_id))
            try:
                # Update overall rating must be called before the database is updated
                # since the current rating numbers will be used to roll back the overall beer rating.
                self.update_overall_beer_rating(i, True)
                beer_id_list = db.select('beer', what="id", where="name is \"%s\"" % i.beer).list()
                if not beer_id_list:
                    return render.error("Beer %s not found in list" % i.beer)
                beer_id = beer_id_list[0].id
Exemple #5
0
    def __call__(self, request):
        """
        Handle the given request.
        """
        # Make sure this plays nice with Web.
        request.auto_finish = False

        def start_response(status, head):
            request.send_status(status)
            if isinstance(head, list):
                head = dict(head)
            request.send_headers(head)

            return request.write

        # Build an environment for the WSGI application.
        environ = {
            'REQUEST_METHOD'    : request.method,
            'SCRIPT_NAME'       : '',
            'PATH_INFO'         : request.path,
            'QUERY_STRING'      : request.query,
            'SERVER_NAME'       : request.headers.get('Host','127.0.0.1'),
            'SERVER_PROTOCOL'   : request.version,
            'REMOTE_ADDR'       : request.remote_ip,
            'GATEWAY_INTERFACE' : 'WSGI/1.0',
            'wsgi.version'      : (1,0),
            'wsgi.url_scheme'   : request.protocol,
            'wsgi.input'        : cStringIO.StringIO(request.body),
            'wsgi.errors'       : sys.stderr,
            'wsgi.multithread'  : False,
            'wsgi.multiprocess' : False,
            'wsgi.run_once'     : False
        }

        if isinstance(request.connection.server.local_addr, tuple):
            environ['SERVER_PORT'] = request.connection.server.local_addr[1]

        if hasattr(request, 'arguments'):
            environ['wsgiorg.routing_args'] = (request.arguments, {})
        elif hasattr(request, 'match'):
            environ['wsgiorg.routing_args'] = (request.match.groups(), {})

        if 'Content-Type' in request.headers:
            environ['CONTENT_TYPE'] = request.headers['Content-Type']
        if 'Content-Length' in request.headers:
            environ['CONTENT_LENGTH'] = request.headers['Content-Length']

        for k,v in request.headers.iteritems():
            environ['HTTP_%s' % k.replace('-','_').upper()] = v

        # Run the WSGI Application.
        try:
            result = self.app(environ, start_response)
        except Exception, e:
            log.exception('Exception running WSGI application for: %s %s',
                request.method, request.path)

            if not self.debug:
                body, status, headers = error(500, request=request, debug=False)
            else:
                resp = u''.join([
                    u"<h2>Traceback</h2>\n",
                    u"<pre>%s</pre>\n" % traceback.format_exc(),
                    u"<h2>HTTP Request</h2>\n",
                    request.__html__(),
                    ])
                body, status, headers = error(resp, 500, request=request,
                    debug=True)

            request.send_status(500)

            if not 'Content-Length' in headers:
                headers['Content-Length'] = len(body)

            request.send_headers(headers)
            request.write(body)
            request.finish()
            return