basic support for pagination

This commit is contained in:
iou1name 2018-07-26 09:15:28 -04:00
parent 3b4d24bd7c
commit 5ea5020a16
8 changed files with 44 additions and 29 deletions

View File

@ -57,7 +57,6 @@ app.wsgi_app = ProxyFix(app.wsgi_app)
app.register_blueprint(views)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['SESSION_TYPE'] = 'filesystem'
app.url_map.strict_slashes = False
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
#app.jinja_env.undefined = "StrictUndefined"

View File

@ -27,6 +27,7 @@ CREATE TABLE `quest_meta` (
CREATE TABLE `quest_data` (
`post_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
`quest_id` SMALLINT UNSIGNED NOT NULL,
`page_num` TINYINT UNSIGNED NOT NULL,
`post_type` ENUM('text', 'dice', 'poll') NOT NULL,
`post` MEDIUMTEXT NOT NULL,
`timestamp` INT UNSIGNED NOT NULL,

View File

@ -130,12 +130,13 @@ def insert_quest(canon_title, owner_id):
return quest_id
def insert_quest_post(quest_id, post_type, post, timestamp):
def insert_quest_post(quest_id, page_num, post_type, post, timestamp):
"""Insers a new quest post."""
_DB.execute(
"INSERT INTO `quest_data`" \
+ "(`quest_id`, `post_type`, `post`, `timestamp`) " \
+ "VALUES (%s, %s, %s, %s)", (quest_id, post_type, post, timestamp))
+ "(`quest_id`, `page_num`, `post_type`, `post`, `timestamp`) " \
+ "VALUES (%s, %s, %s, %s, %s)",
(quest_id, page_num, post_type, post, timestamp))
post_id = _DB.execute(
"SELECT `post_id` FROM `quest_data` WHERE `quest_id` = %s " \
+ "ORDER BY `post_id` DESC", (quest_id,)).fetchone()[0]

View File

@ -28,17 +28,16 @@ def qm_only(msg=""):
def _nop(*args, **kwargs):
data = args[0]
room = data.get("room")
res = db.get_quest_meta(quest_id=room)
res = db.get_quest_meta(room)
if not res:
return msg
if session.get("user_id") != res[3]:
if session.get("user_id") != res[2]:
return msg
return func(*args, **kwargs)
return _nop
return actual_decorator
@socketio.on('joined')
def joined(data):
"""
@ -167,12 +166,14 @@ def new_post(data, internal=False):
"""
room = data.get("room")
post = data.get("post")
page_num = data.get("page_num")
post = bleach.clean(post.strip())
post = post.replace("\n", "<br />")
post = tools.handle_img(post)
date = int(time.time())
post_id = db.insert_quest_post(room, "text", post, date)
post_id = db.insert_quest_post(room, page_num, "text", post, date)
db.set_post_closed(room)
data = {}
@ -229,6 +230,7 @@ def dice_post(data):
except (ValueError, AssertionError):
return
diceStrict = bool(data.get("diceStrict"))
page_num = data.get("page_num")
dice_roll = f"{data['diceNum']}d{data['diceSides']}"
if diceMod:
@ -241,7 +243,7 @@ def dice_post(data):
post += " vs DC" + str(diceChal)
date = int(time.time())
post_id = db.insert_quest_post(room, "dice", post, date)
post_id = db.insert_quest_post(room, page_num, "dice", post, date)
new_call = (dice_roll, diceStrict, diceChal, diceRollsTaken)
db.insert_dice_call(post_id, room, new_call)
db.set_post_open(post_id, room)
@ -295,6 +297,7 @@ def poll_post(data):
room = data.pop("room", None)
multi_choice = bool(data.pop("pollAllowMultipleChoices", None))
allow_writein = bool(data.pop("pollAllowUserOptions", None))
page_num = data.get("page_num")
options = []
for key, value in data.items():
if not value or not key.startswith("pollOption-"):
@ -307,7 +310,7 @@ def poll_post(data):
post = "Poll"
date = int(time.time())
post_id = db.insert_quest_post(room, "poll", post, date)
post_id = db.insert_quest_post(room, page_num, "poll", post, date)
db.insert_poll(post_id, room, multi_choice, allow_writein)
new_options = []
for option in options:

View File

@ -5,7 +5,7 @@ var tid = setInterval( function () {
clearInterval( tid );
document.getElementById('chatWindow').scrollTop = document.getElementById('chatWindow').scrollHeight;
socket = io.connect('https://' + document.domain + ':' + location.port);
socket = io.connect('https://' + document.domain + location.port);
socket.on('connect', function() {
socket.emit('joined', {room: room_id});
});
@ -223,7 +223,7 @@ function makePost() {
let text = qparea.value.trim();
qparea.value = '';
if (text == '') { return; }
socket.emit('new_post', {post: text, room: room_id});
socket.emit('new_post', {post: text, page_num: page_num, room: room_id});
}
function edit_post(post_id) {
document.getElementById('questPostData-' + post_id).contentEditable = 'true';
@ -247,6 +247,7 @@ function form_post(form_id, emit_msg) {
obj[key] = value;
});
obj.room = room_id;
obj.page_num = page_num;
socket.emit(emit_msg, obj);
document.getElementById(form_id).reset();
}

View File

@ -2,7 +2,7 @@
{% block title %}{{ quest_title }}{% endblock %}
{% block head %}
<script type="text/javascript" src="/static/socket.io.slim.js"></script>
<script>var room_id = {{ room_id }};</script>
<script>var room_id = {{ room_id }}; var page_num = {{ page_num }};</script>
{% if session.get("user_id") == owner_id %}
<script type="text/javascript" src="/static/anonkunQM.js"></script>
{% else %}
@ -11,7 +11,14 @@
{% endblock %}
{% block header %}
{% if session.get("user_id") == owner_id %}
<li><a href="{{ url_for('.edit_quest', quest_id=quest_id) }}">Edit Quest</a></li>
<li><a href="{{ url_for('.edit_quest', quest_id=quest_id) }}">Edit Quest</a></li>
<li><a href="{{ url_for('.quest', quest_id=quest_id, page_num=1) }}">Page 1</a></li>
<li>
<select onChange="window.location.href=this.value">
<option value="{{ url_for('.quest', quest_id=quest_id, page_num=1) }}">Page 1</option>
<option value="{{ url_for('.quest', quest_id=quest_id, page_num=2) }}">Page 2</option>
</select>
</li>
{% endif %}
{% endblock %}
{% block content %}
@ -20,20 +27,20 @@
<center><h1>{{ quest_title }}</h1></center>
<div id="questPosts">
{% for quest_post in quest_posts %}
{% if quest_post[2] == "text" %}
{% if quest_post[3] == "text" %}
<div class="questPost textPost">
{% elif quest_post[2] == "dice" %}
{% elif quest_post[3] == "dice" %}
<div class="questPost dicePost{% if quest_post == quest_posts|last %} active_post{% endif %}">
{% elif quest_post[2] == "poll" %}
{% elif quest_post[3] == "poll" %}
<div class="questPost pollPost{% if quest_post == quest_posts|last %} active_post{% endif %}">
{% endif %}
<div class="questPostMeta">
{{ quest_post[4] | strftime }}
{{ quest_post[5] | strftime }}
{% if session.get("user_id") == owner_id %}
{% if quest_post[2] == "text" %}
{% if quest_post[3] == "text" %}
<br /><a href="javascript:void(0);" id="editPost-{{ quest_post[0] }}" onclick="edit_post({{ quest_post[0] }})">Edit</a>
<a href="javascript:void(0);" id="savePost-{{ quest_post[0] }}" onclick="save_post('{{ quest_post[0] }}')" style="display:none;">Save</a>
{% elif quest_post[2] == "dice" or quest_post[2] == "poll" and quest_post == quest_posts|last %}
{% elif quest_post[3] == "dice" or quest_post[3] == "poll" and quest_post == quest_posts|last %}
{% if quest_post[0] == open_post_id %}
<br /><a href="javascript:void(0);" id="close_post_id-{{ quest_post[0] }}" onclick="close_post({{ quest_post[0] }})">Close</a>
<a href="javascript:void(0);" id="open_post_id-{{ quest_post[0] }}" onclick="open_post({{ quest_post[0] }})" style="display:none;">Open</a>
@ -45,19 +52,19 @@
{% endif %}
</div>
<div class="questPostData" id="questPostData-{{ quest_post[0] }}">
{% if quest_post[2] == "text" %}
{% if quest_post[3] == "text" %}
{% autoescape false %}
{{ quest_post[3] }}
{{ quest_post[4] }}
{% endautoescape %}
{% elif quest_post[2] == "dice" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% elif quest_post[3] == "dice" %}
<h3>{{ quest_post[4] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% for dice_roll in dice_rolls[quest_post[0]] %}
<div id="questRollId-{{ dice_roll[0] }}">
<b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if quest_post[0]|dice_chal != 0 %} - {% if dice_roll[5] >= quest_post[0]|dice_chal %}Pass{% else %} Fail{% endif %}{% endif %}</b>
</div>
{% endfor %}
{% elif quest_post[2] == "poll" %}
<h3>{{ quest_post[3] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
{% elif quest_post[3] == "poll" %}
<h3>{{ quest_post[4] }} - {% if quest_post[0] == open_post_id %}Open{% else %}Closed{% endif %}</h3>
<table class="poll" id="poll-{{ quest_post[0] }}">
<col{% if quest_post[0] != open_post_id %} style="visibility: collapse;"{% endif %}/>
<col/>

1
todo
View File

@ -13,6 +13,7 @@ Account managament/logout
Display profile link in header bar
Tagging system
Quote backlinks
Make chat hideable
Improvements:
Revamp post editing

View File

@ -43,6 +43,7 @@ def login_required(url=None):
@views.route("/quest/<int:quest_id>", strict_slashes=False)
@views.route("/quest/<int:quest_id>/<int:page_num>/")
def quest(quest_id, page_num=1):
"""
An arbituary quest page.
@ -53,8 +54,8 @@ def quest(quest_id, page_num=1):
quest_id = room_id = data[0]
quest_title = data[1]
owner_id = data[3]
open_post_id = data[4]
owner_id = data[2]
open_post_id = data[2]
ip_address = request.remote_addr
quest_posts = db.get_quest_data(quest_id)
@ -127,9 +128,10 @@ def create_quest():
owner_id = session.get("user_id")
timestamp = int(time.time())
page_num = 1
quest_id = db.insert_quest(quest_title, owner_id)
db.insert_quest_post(quest_id, "text", quest_body, timestamp)
db.insert_quest_post(quest_id, page_num, "text", quest_body, timestamp)
return redirect(url_for('.quest', quest_id=quest_id))