From f5f1fa721b86961d956297326c2b9cd57f9c5281 Mon Sep 17 00:00:00 2001 From: Mikkel <4072916+Mikk3@users.noreply.github.com> Date: Thu, 2 Jun 2022 12:21:41 +0200 Subject: [PATCH] =?UTF-8?q?Tilf=C3=B8j=20seneste=20bedsked=20dato=20til=20?= =?UTF-8?q?threads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aula/models.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/aula/models.py b/aula/models.py index 3147a35..f22b9ba 100644 --- a/aula/models.py +++ b/aula/models.py @@ -128,6 +128,8 @@ class Thread(tuple): "mandatory": thread_data[4] } self.group_id = thread_data[2] + if len(thread_data) == 6: + self.last_message_date = thread_data[5] super().__init__() def get_messages(self): @@ -221,13 +223,19 @@ class User(tuple, UserMixin): def get_threads(self): cur = conn.cursor() sql_call = """ - SELECT thread_id, title, g.group_id, g.name, g.mandatory FROM threads - INNER JOIN groups g on g.group_id = threads.group_id - WHERE g.group_id IN - ( - SELECT group_id FROM users_groups - WHERE user_id = %s - ) + SELECT threads.thread_id, title, g.group_id, g.name, g.mandatory, lm.last_message_date FROM threads + INNER JOIN + groups g + ON g.group_id = threads.group_id + LEFT JOIN + (SELECT thread_id, MAX(created_date) as last_message_date FROM messages GROUP BY thread_id) lm + ON threads.thread_id = lm.thread_id + WHERE g.group_id IN + ( + SELECT group_id FROM users_groups + WHERE user_id = %s + ) + ORDER BY last_message_date DESC NULLS LAST; """ cur.execute(sql_call, (self.user_id,)) threads = cur.fetchall() @@ -247,8 +255,6 @@ class User(tuple, UserMixin): cur.execute(sql_call, (self.user_id, thread_id)) return cur.rowcount > 0 - - def insert_users(user_id, first_name, last_name, password, email, adresse, role): cur = conn.cursor() sql_call = """