Ejemplo n.º 1
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/html; charset=utf-8')]

    if URI_PATH == '/favicon.ico':
        status = '404 Not Found'
        start_response(status, headers)
        return [b'']

    if URI_PATH.startswith(STATIC_URL):
        with_static_root = URI_PATH.replace(STATIC_URL,
                                            '{}/'.format(STATIC_ROOT), 1)
        absolute_path = path.abspath(with_static_root)
        if not absolute_path.startswith(
                path.abspath(STATIC_ROOT)) or not path.isfile(absolute_path):
            status = '404 Not Found'
            start_response(status, headers)
            return [b'']
        start_response(status, headers)
        #with open(absolute_path, 'br') as file:
        #    return [file.read()]
        return get_file_bytes(absolute_path)

    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n".format(**locals()))

    with open('main.html', 'rb') as f:
        template_bytes = f.read()

    if REQUEST_METHOD == 'POST':
        status = '303 See Other'
        headers.append(('Location', '/'))
        name = get_first_element(POST, 'name', '')
        message = get_first_element(POST, 'message', '')
        data_message_text = "Name: {0}<br>Message: {1}".format(name, message)
        data_message_bytes = data_message_text.encode('utf-8')
        data_messages.append(data_message_bytes)
        start_response(status, headers)
        return [b'']

    messages = b'<hr>'.join(data_messages)
    template_bytes = template_bytes.replace(b'{{messages}}', messages)

    start_response(status, headers)
    return [template_bytes]
Ejemplo n.º 2
0
def messages_controller(request):
    if request.REQUEST_METHOD == 'POST':
        name = get_first_element(request.POST, 'name', '')
        message = get_first_element(request.POST, 'message', '')
        entry = Entry(name, message)
        manager.save(entry)

        response = Response('303 See Other', b'303 See Other')
        response.headers.append(('Location', '/messages'))
        return response

    entries = manager.get_all()
    entries_encoded = b'<hr />'.join(
        e.name.encode('utf-8') + b': ' + e.message.encode('utf-8')
        for e in entries)
    body = messages_view.render(messages=entries_encoded)
    return Response('200 OK', body)
Ejemplo n.º 3
0
 def test_get_info():
     mapping = {'name': '.test1', 'value': '.asdfg'}
     default = None
     output = u.get_info(basic_soup, mapping)
     expected_output = {}
     for key, selector in mapping.items():
         expected_output[key] = u.get_first_element(basic_soup,
                                                    selector,
                                                    default=default)
     assert output == expected_output
def index(method, get, post, headers):
    messages = message_manager.all()
    status = '200 OK'
    view = View('main.html')
    body = view.render(messages=messages)

    if method == 'POST':
        status = '303 See Other'
        body = b''

        headers.append(('Location', '/'))

        message_name = get_first_element(post, 'name', '')
        message_message = get_first_element(post, 'message', '')

        message = Message(message_name, message_message)
        message_manager.save(message)

    return status, body
Ejemplo n.º 5
0
def index(method, get, post, headers):
    messages = manager.all()
    byte_messages = b'<hr>'.join([m.name.encode() + b" <br> Message: "
                                  + m.message.encode() for m in messages])
    status = '200 OK'
    body = view.render(messages=byte_messages)

    if method == 'POST':
        status = '303 See Other'
        body = b''

        headers.append(('Location', '/'))

        message_name = get_first_element(post, 'name', '')
        message_message = get_first_element(post, 'message', '')

        message = Message(message_name, message_message)
        manager.save(message)

    return status, body
Ejemplo n.º 6
0
def index(method, get, post, headers):
    messages = manager.all()
    byte_messages = b'<hr>'.join([
        m.name.encode() + b" <br> Message: " + m.message.encode()
        for m in messages
    ])
    status = '200 OK'
    body = view.render(messages=byte_messages)

    if method == 'POST':
        status = '303 See Other'
        body = b''

        headers.append(('Location', '/'))

        message_name = get_first_element(post, 'name', '')
        message_message = get_first_element(post, 'message', '')

        message = Message(message_name, message_message)
        manager.save(message)

    return status, body
Ejemplo n.º 7
0
def application(environ, start_response):
    status = '200 OK'
    with open('index.html', encoding='utf-8') as f:
        response_template = f.read()

    GET = parse_http_get_data(environ)
    name = get_first_element(GET, "name", '')
    message = get_first_element(GET, "message", '')

    if message:
        db.messages.append({'name': name, 'message': message})
        start_response('302 Found', [('Content-Type', 'text/html'),
                                     ('Location', '/')])
        return [b'']

    response_body = response_template.format(name=name, messages=db.messages)

    response_headers = [('Content-Type', 'text/html'),
                        ('Content-Length', str(len(response_body)))]

    start_response(status, response_headers)
    return [response_body.encode('utf-8')]
Ejemplo n.º 8
0
def delete(method, get, post, headers):
    works = work_manager.all()
    status = '200 OK'
    view = View("templates\delete.html")
    body = b''

    if method == 'GET':
        headers.append(('Location', '/'))
        id = get_first_element(get, 'id', '')
        works_by_id = [w for w in works if w.id == id]
        if len(works_by_id) > 0:
            body = view.render(id=id, work=works_by_id[0])
        else:
            status = '404 Not Found'
    return status, body
Ejemplo n.º 9
0
def index(method, get, post, headers):
    works = work_manager.all()
    status = '200 OK'
    view = View("templates\index.html")
    body = view.render(works=works)

    if method == 'POST':
        status = '303 See Other'
        body = b''
        headers.append(('Location', '/'))

        work_name = get_first_element(post, 'work_name', '').strip()
        starting_date = get_first_element(post, 'starting_date', '')
        ending_date = get_first_element(post, 'ending_date', '')
        work_status = get_first_element(post, 'work_status', '').strip()
        id = get_first_element(post, 'id')
        is_delete = get_first_element(post, 'is_delete')

        work = Work(work_name, starting_date, ending_date, work_status, id=id)
        if is_delete:
            work_manager.delete(work)
        else:
            work_manager.save(work)
    return status, body
Ejemplo n.º 10
0
 def test_get_first_element():
     el = u.get_first_element(basic_soup, '.test1')
     assert isinstance(el, bs4.element.Tag)
     el = u.get_first_element(basic_soup, '.asdfgh', default='')
     assert el == ''
Ejemplo n.º 11
0
def application(environ, start_response):
    # https://www.python.org/dev/peps/pep-3333/#environ-variables
    REQUEST_METHOD = environ['REQUEST_METHOD']
    CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
    SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
    HEADERS = parse_http_headers(environ)
    URI_PATH = environ['PATH_INFO']
    URI_QUERY = environ['QUERY_STRING']
    URI = parse_http_uri(environ)
    POST = parse_http_x_www_form_urlencoded_post_data(environ)
    GET = parse_http_get_data(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/html; charset=utf-8')]

    callback = router.resolve(URI_PATH)
    status, body = callback

    if URI_PATH == '/favicon.ico':
        status = '404 Not Found'
        start_response(status, headers)
        return [b'']

    if URI_PATH.startswith(STATIC_URL):
        # import os
        import os.path

        path = URI_PATH[len(STATIC_URL):]
        normpath = os.path.normpath(path)
        # print(normpath)
        download_path = STATIC_ROOT + normpath
        # print(download_path)
        if not os.path.exists(download_path):
            start_response("404 Not found", headers)
            return [b'']
        else:
            headers = [('Content-type', 'text/plain; charset=utf-8')]
            start_response(status, headers)
            return [open(download_path, 'rb').read()]
            # with open(path, 'rb') as f:
            # start_response(status, headers)
            # return [f.read()]

    DEBUG = False
    if DEBUG:
        print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
              "CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
              "POST: {POST}\n"
              "GET: {GET}\n"
              ":HEADERS:\n{HEADERS}\n".format(**locals()))

    with open('main.html', 'rb') as f:
        template_bytes = f.read()

    if REQUEST_METHOD == 'POST':
        status = '303 See Other'
        headers.append(('Location', '/'))
        name = get_first_element(POST, 'name', '')
        message = get_first_element(POST, 'message', '')
        data_message_text = "Name: {0}<br>Message: {1}".format(name, message)
        data_message_bytes = data_message_text.encode('utf-8')
        data_messages.append(data_message_bytes)
        start_response(status, headers)
        return [b'']

    messages = b'<hr>'.join(data_messages)
    template_bytes = template_bytes.replace(b'{{messages}}', messages)

    start_response(status, headers)
    return [template_bytes]