This commit is contained in:
NikolajDanger
2022-05-31 10:17:42 +02:00
parent 06b40f5158
commit 5fec84b6b7
5 changed files with 39 additions and 26 deletions

View File

@ -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/<int:group_id>", 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"

View File

@ -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

View File

@ -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 )
"""

View File

@ -1,6 +1,16 @@
{% extends "layout.html" %}
{% block content %}
<h1>{{ group.name }}</h1>
<p>TODO: Tilføj opslag og tråde som er tilknyttet denne gruppe</p>
{% for post in posts %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ post.author.first_name }} {{ post.author.last_name }}</a>
<small class="text-muted">{{ post.date_posted }}</small>
</div>
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
<p class="article-content">{{ post.content }}</p>
</div>
</article>
{% endfor %}
{% endblock content %}

View File

@ -8,13 +8,13 @@
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="#">{{ post.author_name }}</a>
<a class="mr-2" href="#">{{ post.author.first_name }} {{ post.author.last_name }}</a>
<small class="text-muted">{{ post.date_posted }}</small>
</div>
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
<p class="article-content">{{ post.content }}</p>
<a class="group-name" href="#">
{{ post.group_name }}
<a class="group-name" href="{{ url_for('Group.show', group_id = post.group.group_id) }}"">
{{ post.group.name }}
</a>
</div>
</article>