Пример #1
0
def index():
    """ Index page
	  Show list of `asks`, and cheer count of each ask
	"""
    arcus_client = getarcus.get_client()
    cached_data = []

    with get_db().cursor() as cursor:
        # Get data in `ask` only (not with cheer count)
        # cursor.execute("SELECT * FROM `ask`")
        cursor.execute(
            "SELECT *, (SELECT COUNT(*) FROM `cheer` WHERE ask_id = ask.id) AS cheer_cnt FROM `ask`"
        )

        result = cursor.fetchall()

        cache_hit = True

        for id, message, ip_address, register_time, cheer_cnt in result:
            cache = arcus_client.get('ipaddr_' + str(id)).get_result()

            if cache == None:
                cache_hit = False
                break
            else:
                cached_data.append(
                    (id, message, cache, register_time, cheer_cnt))

    if not cache_hit:
        with get_db().cursor() as cursor:
            # Get data with cheer count
            # print(bcolors.WARNING + "Cache not exists. Create cache" + bcolors.ENDC)

            cursor.execute(
                "SELECT *, (SELECT COUNT(*) FROM `cheer` WHERE ask_id = ask.id) AS cheer_cnt FROM `ask`"
            )
            result = cursor.fetchall()

            cached_data = []

            for id, message, ip_address, register_time, cheer_cnt in result:
                cached_data.append(
                    (id, message, ip_address, register_time, cheer_cnt))
                arcus_client.set('ipaddr_' + str(id), ip_address)

    return render_template(
        'main.html',
        dataset=cached_data,
    )
Пример #2
0
def add_cheer(ask_id):
    """ Add new cheer to ask

	:param ask_id: Primary key of `ask` table
	:post-param message: Message of `cheer`
	"""
    conn = get_db()
    message = request.form.get('message')

    def run():
        with conn.cursor() as cursor:
            sql = "INSERT INTO `cheer` (`ask_id`, `message`, `ip_address`) VALUES (%s, %s, %s)"
            r = cursor.execute(sql, (ask_id, message, request.remote_addr))

        conn.commit()

        with conn.cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM `cheer` WHERE ask_id = %s",
                           (ask_id, ))
            row = cursor.fetchone()
            cheer_cnt = row[0]

    run()

    redirect_url = request.form.get('back', '/#c' + str(ask_id))
    return redirect(redirect_url)
Пример #3
0
def add_ask():
    conn = get_db()
    message = request.form.get('message')

    with conn.cursor() as cursor:
        sql = "INSERT INTO `ask` (`message`, `ip_address`) VALUES (%s, %s)"
        r = cursor.execute(sql, (message, request.remote_addr))

    id = conn.insert_id()
    conn.commit()

    with conn.cursor() as cursor:

        cursor.execute("SELECT * FROM `ask` WHERE id = %s", (id, ))
        item = cursor.fetchone()
        client = redisdriver.get_client()
        #print(item)

        data = "%s/%s/%s/%s/%s" % (
            str(item[0]),  # id
            item[1],  # message
            item[2],  # ip_address
            item[3].strftime("%Y-%m-%d %H:%M:%S"),  # register_time
            str(0),  # cheer_cnt
        )
        #print(bcolors.OKGREEN + data + bcolors.ENDC)

        client.lpush('askhy:asktable_', data)
        #print(finish)

    return redirect("/#a" + str(id))
Пример #4
0
def index():
    with get_db().cursor() as cursor:
        cursor.execute(
            "SELECT *, (SELECT COUNT(*) FROM `cheer` WHERE ask_id = ask.id) AS cheer_cnt FROM `ask`"
        )
        result = cursor.fetchall()

    return render_template(
        'main.html',
        dataset=result,
    )
Пример #5
0
def add_ask():
    conn = get_db()
    message = request.form.get('message')

    with conn.cursor() as cursor:
        sql = "INSERT INTO `ask` (`message`, `ip_address`) VALUES (%s, %s)"
        r = cursor.execute(sql, (message, request.remote_addr))

    id = conn.insert_id()
    conn.commit()

    return redirect("/#a" + str(id))
Пример #6
0
def index():
    """ Index page
	  Show list of `asks`, and cheer count of each ask
	"""
    with get_db().cursor() as cursor:
        cursor.execute(
            "SELECT *, (SELECT COUNT(*) FROM `cheer` WHERE ask_id = ask.id) AS cheer_cnt FROM `ask`"
        )
        result = cursor.fetchall()

    return render_template(
        'main.html',
        dataset=result,
    )
Пример #7
0
def add_cheer():
    conn = get_db()
    ask_id = request.form.get('ask_id')
    message = request.form.get('message')

    with conn.cursor() as cursor:
        sql = "INSERT INTO `cheer` (`ask_id`, `message`, `ip_address`) VALUES (%s, %s, %s)"
        r = cursor.execute(sql, (ask_id, message, request.remote_addr))

    conn.commit()

    back = request.form.get('back')
    if back:
        return redirect(back)
    else:
        return redirect("/#c" + ask_id)
Пример #8
0
def add_cheer(ask_id):
    """ Add new cheer to ask

	:param ask_id: Primary key of `ask` table
	:post-param message: Message of `cheer`
	"""
    conn = get_db()
    message = request.form.get('message')

    with conn.cursor() as cursor:
        sql = "INSERT INTO `cheer` (`ask_id`, `message`, `ip_address`) VALUES (%s, %s, %s)"
        r = cursor.execute(sql, (ask_id, message, request.remote_addr))

    conn.commit()

    redirect_url = request.form.get('back', '/#c' + str(ask_id))
    return redirect(redirect_url)
Пример #9
0
def view_ask(ask_id):
    conn = get_db()

    with conn.cursor() as cursor:
        cursor.execute("SELECT * FROM `ask` WHERE id = %s", (ask_id, ))
        row = cursor.fetchone()

        cursor.execute("SELECT * FROM `cheer` WHERE ask_id = %s", (ask_id, ))
        rows2 = cursor.fetchall()

    return render_template(
        'detail.html',
        id=row[0],
        message=row[1],
        ip_address=row[2],
        register_time=row[3],
        current_url=request.url,
        cheers=rows2,
    )
Пример #10
0
def view_ask(ask_id):
	""" Show detail of one `ask`
	  See all cheers in this ask

	:param ask_id: Primary key of `ask` table
	"""
	conn = get_db()

	with conn.cursor() as cursor :
		cursor.execute("SELECT * FROM `ask` WHERE id = %s", (ask_id, ))
		row = cursor.fetchone()

		cursor.execute("SELECT * FROM `cheer` WHERE ask_id = %s", (ask_id, ))
		rows2 = cursor.fetchall()

	return render_template('detail.html',
		id=row[0],
		message=row[1],
		ip_address=row[2],
		register_time=row[3],
		current_url=request.url,
		cheers=rows2,
	)
Пример #11
0
def index():

    client = redisdriver.get_client()

    #ret = client.set('test:string1', 'test...', 20)
    #print(ret.get_result())
    #assert ret.get_result() == True

    #ret = client.get('test:string1')
    #print(ret.get_result())
    #assert ret.get_result() == 'test...'

    success = True
    cache = client.lrange('askhy:asktable_', 0, -1)

    #print(cache)

    if not cache:
        success = False

    else:
        #print(bcolors.OKGREEN + "Cache hit!" + bcolors.ENDC)
        #print(cache)

        result = []

        from datetime import datetime

        for row in cache:
            item = row.decode().split("/")
            #print(item)
            result.append((
                int(item[0]),  # id
                item[1],  # message
                item[2],  # ip_address
                datetime.strptime(item[3],
                                  '%Y-%m-%d %H:%M:%S'),  # register_time
                int(item[4]),  # cheer_cnt
            ))

    #cache = set()
    if not success:
        cache = []

        with get_db().cursor() as cursor:
            #print(bcolors.WARNING + "Cache not exists. Create cache" + bcolors.ENDC)

            #print(ret)

            cursor.execute(
                "SELECT *, (SELECT COUNT(*) FROM `cheer` WHERE ask_id = ask.id) AS cheer_cnt FROM `ask`"
            )

            result = cursor.fetchall()
            #cache2 = list(result)
            #print(bcolors.OKGREEN + str(len(result)) + bcolors.ENDC)

            for item in result:
                #print(item)

                data = "%s/%s/%s/%s/%s" % (
                    str(item[0]),  # id
                    item[1],  # message
                    item[2],  # ip_address
                    item[3].strftime("%Y-%m-%d %H:%M:%S"),  # register_time
                    str(item[4]),  # cheer_cnt
                )

                #print(bcolors.OKGREEN + data + bcolors.ENDC)

                finish = client.lpush('askhy:asktable_', data)
                #print(finish)

    return render_template(
        'main.html',
        dataset=result,
    )