From 4c64631b8a3c27b0d50fcf42ea93e6d0d053a346 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Thu, 20 Jun 2024 17:27:36 +0800 Subject: [PATCH] put recently used languages first in the list #210 --- app/community/routes.py | 5 ----- app/main/routes.py | 4 +++- app/post/routes.py | 2 -- app/utils.py | 25 +++++++++++++++++++++++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/community/routes.py b/app/community/routes.py index 494c3719..0ccbf59e 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -550,7 +550,6 @@ def add_discussion_post(actor): else: form.communities.data = community.id form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() if community.posting_warning: flash(community.posting_warning) @@ -633,7 +632,6 @@ def add_image_post(actor): else: form.communities.data = community.id form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() return render_template('community/add_image_post.html', title=_('Add post to community'), form=form, community=community, markdown_editor=current_user.markdown_editor, low_bandwidth=False, actor=actor, @@ -714,7 +712,6 @@ def add_link_post(actor): else: form.communities.data = community.id form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() return render_template('community/add_link_post.html', title=_('Add post to community'), form=form, community=community, markdown_editor=current_user.markdown_editor, low_bandwidth=False, actor=actor, @@ -795,7 +792,6 @@ def add_video_post(actor): else: form.communities.data = community.id form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() return render_template('community/add_video_post.html', title=_('Add post to community'), form=form, community=community, markdown_editor=current_user.markdown_editor, low_bandwidth=False, actor=actor, @@ -862,7 +858,6 @@ def add_poll_post(actor): else: form.communities.data = community.id form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() form.finish_in.data = '3d' if community.posting_warning: flash(community.posting_warning) diff --git a/app/main/routes.py b/app/main/routes.py index 2ec21010..79288a50 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -28,7 +28,7 @@ from app.utils import render_template, get_setting, gibberish, request_etag_matc ap_datetime, ip_address, retrieve_block_list, shorten_string, markdown_to_text, user_filters_home, \ joined_communities, moderating_communities, parse_page, theme_list, get_request, markdown_to_html, allowlist_html, \ blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \ - generate_image_from_video_url, blocked_users, microblog_content_to_title, menu_topics + generate_image_from_video_url, blocked_users, microblog_content_to_title, menu_topics, languages_for_form from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic, File, Instance, \ InstanceRole, Notification, Language, community_language, PostReply from PIL import Image @@ -397,6 +397,8 @@ def list_files(directory): @bp.route('/test') def test(): + x = languages_for_form() + #for community in Community.query.filter(Community.content_retention != -1): # for post in community.posts.filter(Post.posted_at < utcnow() - timedelta(days=Community.content_retention)): # post.delete_dependencies() diff --git a/app/post/routes.py b/app/post/routes.py index cc9f8957..43f1b998 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -227,7 +227,6 @@ def show_post(post_id: int): else: replies = post_replies(post.id, sort) form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() og_image = post.image.source_url if post.image_id else None description = shorten_string(markdown_to_text(post.body), 150) if post.body else None @@ -838,7 +837,6 @@ def add_reply(post_id: int, comment_id: int): return redirect(url_for('post.continue_discussion', post_id=post_id, comment_id=reply.parent_id)) else: form.notify_author.data = True - form.language_id.data = current_user.language_id if current_user.is_authenticated and current_user.language_id else english_language_id() return render_template('post/add_reply.html', title=_('Discussing %(title)s', title=post.title), post=post, is_moderator=is_moderator, form=form, comment=in_reply_to, markdown_editor=current_user.is_authenticated and current_user.markdown_editor, diff --git a/app/utils.py b/app/utils.py index a8166e7b..71070d2a 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1049,9 +1049,30 @@ def recently_downvoted_post_replies(user_id) -> List[int]: def languages_for_form(): result = [] - for language in Language.query.order_by(Language.name).all(): - if language.code != 'und': + used_languages = [] + if current_user.is_authenticated: + recently_used_post_languages = db.session.execute(text("""SELECT language_id + FROM ( + SELECT language_id, posted_at + FROM "post" + WHERE user_id = :user_id + UNION ALL + SELECT language_id, posted_at + FROM "post_reply" + WHERE user_id = :user_id + ) AS subquery + GROUP BY language_id + ORDER BY MAX(posted_at) DESC + LIMIT 4"""), + {'user_id': current_user.id}).scalars() + for language in Language.query.filter(Language.id.in_(recently_used_post_languages)).all(): result.append((language.id, language.name)) + used_languages.append(language.id) + + for language in Language.query.order_by(Language.name).all(): + if language.code != 'und' and language.id not in used_languages: + result.append((language.id, language.name)) + return result