Example #1
0
def stat(environ, start_response):
    """
    View info of region
    :param environ:
    :param start_response:
    :return:
    """
    try:
        status, result = query_to_db(sql_show_statistic, out='all')
    except Exception as ex:
        result = ''

    html = """
                    <tr class="statistic__tbody_row">
                        <td class="statistic__tbody_cell">
                            <a class="statistic__link" href="{2}"> {0}</a>
                        </td>
                        <td class="statistic__tbody_cell"> {1}</td>
    """
    html_table = [html.format(ru_encode(name), count, region_id, )
                  for name, count, region_id in result]
    mapping = {
        'data': ''.join(html_table),
    }
    start_response('200 OK', [('Content-Type', 'text/html')])
    return render('stat.html', mapping)
Example #2
0
def view(environ, start_response):
    """
    View comment all users.
    :param environ:
    :param start_response:
    :return:
    """
    try:
        status, result = query_to_db(sql_show_info, out='all')
    except Exception as ex:
        result = ''

    html = """
                    <tr class="view__tbody_row">
                        <td class="view__tbody_cell hidden">{0}</td>
                        <td class="view__tbody_cell">{1}</td>
                        <td class="view__tbody_cell">{2}</td>
                        <td class="view__tbody_cell view__tbody_delete"><a href="{3}" class="delete">удалить</a></td>
    """
    html_table = [html.format(ru_encode(name), ru_encode(lastname), ru_encode(cmt), user_id, )
                  for name, lastname, cmt, user_id in result]
    mapping = {
        'data': ''.join(html_table),
    }
    start_response('200 OK', [('Content-Type', 'text/html')])
    return render('view.html', mapping)
Example #3
0
def region_statistic(environ, start_response):
    """

    :param environ: environ
    :param start_response:
    :return:
    """
    status = False

    if environ["REQUEST_METHOD"] == "POST":
        content_length = int(environ["CONTENT_LENGTH"])
        env_request = environ["wsgi.input"].read(content_length)
        request = parse_qs(env_request)
        data = request.get('data', [''])[0]
        if data:
            status, out = query_to_db(sql_show_region_comments.format(data), 'all')

        response = {
            'status': status,
            'message': 'Данные получены успешно' if status else 'Непредвиденая ошибка',
            'data': out
        }
        start_response("200 OK", [("Content-Type", "application/json")], json.dumps(response))
        return [json.dumps(response)]

    start_response('200 OK', [('Content-Type', 'text/html')])
    return []
Example #4
0
def rm_comment(environ, start_response):
    """
    POST REQUEST remove comment from database.
    :param environ: environ
    :param start_response:
    :return: if post when return json request, if is GET
    """
    status = False

    if environ["REQUEST_METHOD"] == "POST":
        content_length = int(environ["CONTENT_LENGTH"])
        env_request = environ["wsgi.input"].read(content_length)
        request = parse_qs(env_request)
        data = request.get('data', [''])[0]
        if data:
            status, out = query_to_db(sql_rm_comment.format(data))

        response = {
            'status': status,
            'message': 'Запись удалена успешно' if status else 'Непредвиденая ошибка'
        }
        start_response("200 OK", [("Content-Type", "application/json")], json.dumps(response))
        return [json.dumps(response)]

    start_response('200 OK', [('Content-Type', 'text/html')])
    return []
Example #5
0
def city_search(environ, start_response):
    """
    POST REQUEST remove comment from database.
    :param environ: environ
    :param start_response:
    :return: if post when return json request, if is GET
    """
    query_str = parse_qs(environ['QUERY_STRING'])
    user_query = query_str.get('term', [''])[0]
    result = ''

    if user_query:
        # TODO: Это костыль, костылянский, но sqlite не может не ascii символы переводить в lower или upper.
        u_user_query = ru_decode(user_query)
        search_query = ''.join([u_user_query[0].upper(), u_user_query[1:].lower()])
        status, result = query_to_db(sql_city_autocomplete.format(ru_encode(search_query)), out='all')

    start_response('200 OK', [('Content-Type', 'text/html')])
    return [json.dumps({'data': zip(*result)[0] if result else ''})]
Example #6
0
def add_post(environ, start_response):
    """
    POST REQUEST to add new user
    :param environ: environ
    :param start_response:
    :return: if post when return json request, if is GET
    """
    if environ["REQUEST_METHOD"] == "POST":
        content_length = int(environ["CONTENT_LENGTH"])
        env_request = environ["wsgi.input"].read(content_length)
        request = parse_qs(env_request)
        input_data = unite_request_data(request)
        city_id = get_id_city(input_data)
        input_data['data']['id_city'] = city_id[0] if city_id else city_id
        status, out = query_to_db(sql_add_new_post.format(**input_data['data']))
        response = {
            'status': status,
            'message': 'Запись добавлена успешно' if status else 'Непредвиденая ошибка'
        }
        start_response("200 OK", [("Content-Type", "application/json")], json.dumps(response))
        return [json.dumps(response)]

    start_response('200 OK', [('Content-Type', 'text/html')])
    return []