From 28eaf53dfda522bf71b3fa4276649f53385d033c Mon Sep 17 00:00:00 2001 From: Ruin0x11 Date: Sun, 9 May 2021 00:49:23 -0700 Subject: [PATCH] Add first/last pool post to pool navigator --- client/css/colors.styl | 2 +- client/css/pool-navigator-control.styl | 9 ++++-- client/css/post-main-view.styl | 5 +++- client/html/pool_navigator.tpl | 18 ++++++++++++ client/js/controls/pool_navigator_control.js | 2 ++ server/szurubooru/func/posts.py | 30 ++++++++++++++------ server/szurubooru/migrations/functions.py | 24 +++++++++++++++- server/szurubooru/tests/func/test_posts.py | 4 +++ 8 files changed, 80 insertions(+), 14 deletions(-) diff --git a/client/css/colors.styl b/client/css/colors.styl index e9458139..51ae30ed 100644 --- a/client/css/colors.styl +++ b/client/css/colors.styl @@ -18,7 +18,7 @@ $message-error-border-color = #FCC $message-error-background-color = #FFF5F5 $message-success-border-color = #D3E3D3 $message-success-background-color = #F5FFF5 -$pool-navigator-border-color = #888 +$pool-navigator-border-color = #AAA $pool-navigator-background-color = #EEE $input-bad-border-color = #FCC $input-bad-background-color = #FFF5F5 diff --git a/client/css/pool-navigator-control.styl b/client/css/pool-navigator-control.styl index e34bdb4e..ed6cd16a 100644 --- a/client/css/pool-navigator-control.styl +++ b/client/css/pool-navigator-control.styl @@ -1,7 +1,7 @@ @import colors .pool-navigator-container - padding: 0 0.50em 0 0.50em + padding: 0 margin: 0 auto .pool-info-wrapper @@ -14,6 +14,8 @@ background: $pool-navigator-background-color &.active font-weight: bold + font-size: 1.10em; + padding: 0.58em 1em .pool-name flex: 1 1; @@ -23,7 +25,10 @@ -o-text-overflow: ellipsis; text-overflow: ellipsis; - .prev, .next + .first, .last + flex-basis: 1em; + + .first, .prev, .next, .last flex: 0 1; margin: 0 .25em; white-space: nowrap; diff --git a/client/css/post-main-view.styl b/client/css/post-main-view.styl index 48f3c158..c372e4a2 100644 --- a/client/css/post-main-view.styl +++ b/client/css/post-main-view.styl @@ -40,11 +40,14 @@ width: 100% .post-container - margin-bottom: 2em + margin-bottom: 1em .post-content margin: 0 + .pool-navigators-container + margin-bottom: 2em + @media (max-width: 800px) .post-view flex-wrap: wrap diff --git a/client/html/pool_navigator.tpl b/client/html/pool_navigator.tpl index 8841da39..b70c238d 100644 --- a/client/html/pool_navigator.tpl +++ b/client/html/pool_navigator.tpl @@ -1,5 +1,14 @@
+ + <% if (ctx.canViewPosts && ctx.firstPost) { %> + + <% } %> + « + <% if (ctx.canViewPosts && ctx.firstPost) { %> + + <% } %> + <% if (ctx.canViewPosts && ctx.prevPost) { %> @@ -27,5 +36,14 @@ <% } %> + + <% if (ctx.canViewPosts && ctx.lastPost) { %> + + <% } %> + » + <% if (ctx.canViewPosts && ctx.lastPost) { %> + + <% } %> +
diff --git a/client/js/controls/pool_navigator_control.js b/client/js/controls/pool_navigator_control.js index 54de6c7a..5961ac70 100644 --- a/client/js/controls/pool_navigator_control.js +++ b/client/js/controls/pool_navigator_control.js @@ -22,8 +22,10 @@ class PoolNavigatorControl extends events.EventTarget { linkClass: misc.makeCssName(poolPostAround.pool.category, "pool"), canViewPosts: api.hasPrivilege("posts:view"), canViewPools: api.hasPrivilege("pools:view"), + firstPost: poolPostAround.firstPost, prevPost: poolPostAround.prevPost, nextPost: poolPostAround.nextPost, + lastPost: poolPostAround.lastPost, isActivePool: isActivePool }) ); diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 7b0e81e2..9dd3b1c6 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -974,7 +974,7 @@ def search_by_image(image_content: bytes) -> List[Tuple[float, model.Post]]: return [] -PoolPostsAround = namedtuple('PoolPostsAround', 'pool prev_post next_post') +PoolPostsAround = namedtuple('PoolPostsAround', 'pool first_post prev_post next_post last_post') def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]: around = dict() @@ -990,11 +990,15 @@ def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]: for order, pool_id, post_id, delta in db.session.execute(dbquery, {"post_id": post.post_id}): if pool_id not in around: - around[pool_id] = [None, None] - if delta < 0: + around[pool_id] = [None, None, None, None] + if delta == -2: around[pool_id][0] = post_id - elif delta > 0: + elif delta == -1: around[pool_id][1] = post_id + elif delta == 1: + around[pool_id][2] = post_id + elif delta == 2: + around[pool_id][3] = post_id pool_ids.add(pool_id) post_ids.add(post_id) @@ -1011,13 +1015,19 @@ def get_pool_posts_around(post: model.Post) -> List[PoolPostsAround]: results = [] for pool_id, entry in around.items(): + first_post = None prev_post = None next_post = None - if entry[0] is not None: - prev_post = posts[entry[0]] + last_post = None if entry[1] is not None: - next_post = posts[entry[1]] - results.append(PoolPostsAround(pools[pool_id], prev_post, next_post)) + prev_post = posts[entry[1]] + if entry[0] is not None: + first_post = posts[entry[0]] + if entry[2] is not None: + next_post = posts[entry[2]] + if entry[3] is not None: + last_post = posts[entry[3]] + results.append(PoolPostsAround(pools[pool_id], first_post, prev_post, next_post, last_post)) return results @@ -1033,8 +1043,10 @@ def serialize_pool_posts_around(around: List[PoolPostsAround]) -> Optional[rest. return [ { "pool": pools.serialize_micro_pool(entry.pool), + "firstPost": entry.first_post, "prevPost": entry.prev_post, - "nextPost": entry.next_post + "nextPost": entry.next_post, + "lastPost": entry.last_post } for entry in sort_pool_posts_around(around) ] diff --git a/server/szurubooru/migrations/functions.py b/server/szurubooru/migrations/functions.py index b6ebb069..ae39e62b 100644 --- a/server/szurubooru/migrations/functions.py +++ b/server/szurubooru/migrations/functions.py @@ -39,8 +39,30 @@ BEGIN WHERE pool_post.ord < main.ord AND pool_post.pool_id = main.pool_id ORDER BY pool_post.ord DESC LIMIT 1) + UNION + (SELECT pool_post.ord, + pool_post.pool_id, + pool_post.post_id, + 2 as delta, + main.ord AS target_ord, + main.pool_id AS target_pool_id + FROM pool_post, main + WHERE pool_post.ord = (SELECT MAX(pool_post.ord) FROM pool_post) + AND pool_post.pool_id = main.pool_id + ORDER BY pool_post.ord DESC LIMIT 1) + UNION + (SELECT pool_post.ord, + pool_post.pool_id, + pool_post.post_id, + -2 as delta, + main.ord AS target_ord, + main.pool_id AS target_pool_id + FROM pool_post, main + WHERE pool_post.ord = (SELECT MIN(pool_post.ord) FROM pool_post) + AND pool_post.pool_id = main.pool_id + ORDER BY pool_post.ord DESC LIMIT 1) ) - SELECT around.ord, around.pool_id, around.post_id, around.delta FROM around; + SELECT around.ord, around.pool_id, around.post_id, around.delta FROM around; END $$ """) diff --git a/server/szurubooru/tests/func/test_posts.py b/server/szurubooru/tests/func/test_posts.py index 360f4022..5e08a4a8 100644 --- a/server/szurubooru/tests/func/test_posts.py +++ b/server/szurubooru/tests/func/test_posts.py @@ -1166,7 +1166,11 @@ def test_get_pool_posts_around(post_factory, pool_factory, config_injector): db.session.add_all([post1, post2, post3, post4, pool1, pool2]) db.session.flush() around = posts.get_pool_posts_around(post2) + assert around[0].first_post["id"] == post1.post_id assert around[0].prev_post["id"] == post1.post_id assert around[0].next_post["id"] == post3.post_id + assert around[0].last_post["id"] == post4.post_id + assert around[1].first_post["id"] == post3.post_id assert around[1].prev_post["id"] == post4.post_id assert around[1].next_post == None + assert around[1].last_post == None