From 5fec84b6b7ed8586f71df1667fa6e39501269c66 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Tue, 31 May 2022 10:17:42 +0200 Subject: [PATCH] :sparkles: --- aula/Group/routes.py | 10 ++++++---- aula/__init__.py | 2 +- aula/models.py | 33 +++++++++++++++++---------------- aula/templates/group_show.html | 14 ++++++++++++-- aula/templates/home.html | 6 +++--- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/aula/Group/routes.py b/aula/Group/routes.py index 28112ce..3d00e8b 100644 --- a/aula/Group/routes.py +++ b/aula/Group/routes.py @@ -1,20 +1,22 @@ -from flask import render_template, url_for, flash, redirect, request, Blueprint +from flask import render_template, Blueprint from flask_login import current_user, login_required -from aula.models import * -import sys, datetime +from aula.models import get_group Group = Blueprint('Group', __name__) @Group.route("/groups", methods=['GET']) +@login_required def groups(): groups = current_user.get_groups_joinable() return render_template('groups.html', groups=groups) @Group.route("/group/", methods=['GET']) +@login_required def show(group_id): group = get_group(group_id) + posts = group.get_posts() if (group is not None): - return render_template('group_show.html', group=group) + return render_template('group_show.html', group=group, posts=posts) else: return "Der findes ingen gruppe med det id" diff --git a/aula/__init__.py b/aula/__init__.py index c2b9cef..b4c3601 100644 --- a/aula/__init__.py +++ b/aula/__init__.py @@ -11,7 +11,7 @@ conn = psycopg2.connect(db) bcrypt = Bcrypt(app) login_manager = LoginManager(app) -login_manager.login_view = 'login' +login_manager.login_view = 'Login.login' login_manager.login_message_category = 'info' from aula.Login.routes import Login diff --git a/aula/models.py b/aula/models.py index 5256c65..fe80513 100644 --- a/aula/models.py +++ b/aula/models.py @@ -34,7 +34,7 @@ class Group(tuple): WHERE group_id = %s """ cur.execute(sql_call, (self.group_id,)) - Posts = Posts(cur.fetchall()) if cur.rowcount > 0 else None + Posts = cur.fetchall() if cur.rowcount > 0 else None result = [] for post_data in Posts: result.append(Post(post_data)) @@ -48,9 +48,9 @@ class Group(tuple): WHERE threads.group_id = %s """ cur.execute(sql_call, (self.group_id,)) - Thread = Thread(cur.fetchall()) if cur.rowcount > 0 else None + threads = cur.fetchall() if cur.rowcount > 0 else None result = [] - for thread_data in Thread: + for thread_data in threads: result.append(Thread(thread_data)) cur.close() return result @@ -68,31 +68,32 @@ class Post(tuple): def __init__(self, post_data): self.post_id = post_data[0] self.group_id = post_data[1] - self.group_name = "" + self.group = None self.author_id = post_data[2] - self.author_name = "" + self.author = None self.title = post_data[3] self.content = post_data[4] self.created_date = post_data[5] - self._get_group_name() - self._get_author_name() + self._get_group() + self._get_author() super().__init__() - def _get_author_name(self): + def _get_author(self): cur = conn.cursor() sql_call = """ - SELECT first_name, last_name FROM users WHERE user_id = %s + SELECT * FROM users WHERE user_id = %s """ cur.execute(sql_call, (self.author_id,)) - self.author_name = ' '.join(cur.fetchone()) + self.author = User(cur.fetchone()) + print(self.author) - def _get_group_name(self): + def _get_group(self): cur = conn.cursor() sql_call = """ - SELECT name FROM groups WHERE group_id = %s + SELECT * FROM groups WHERE group_id = %s """ cur.execute(sql_call, (self.group_id,)) - self.group_name = cur.fetchone()[0] + self.group = Group(cur.fetchone()) class Thread(tuple): def __init__(self, thread_data): @@ -188,13 +189,13 @@ class User(tuple, UserMixin): cur.execute(sql_call, (self.user_id, group_id)) conn.commit() cur.close() - + def get_all_threads(self): cur = conn.cursor() sql_call = """ - SELECT * FROM threads WHERE group_id = - (SELECT groups.group_id FROM + SELECT * FROM threads WHERE group_id = + (SELECT groups.group_id FROM groups INNER JOIN users_groups ON groups.group_id = users_groups.group_id WHERE users_groups.user_id = %s ) """ diff --git a/aula/templates/group_show.html b/aula/templates/group_show.html index 60cdb8e..a3fbdb7 100644 --- a/aula/templates/group_show.html +++ b/aula/templates/group_show.html @@ -1,6 +1,16 @@ {% extends "layout.html" %} {% block content %}

{{ group.name }}

-

TODO: Tilføj opslag og tråde som er tilknyttet denne gruppe

- + {% for post in posts %} + + {% endfor %} {% endblock content %} diff --git a/aula/templates/home.html b/aula/templates/home.html index 12750b5..3a092dd 100644 --- a/aula/templates/home.html +++ b/aula/templates/home.html @@ -8,13 +8,13 @@