Tilføj oprettelse af tråde, grupper og opslag
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from flask import render_template, Blueprint
|
||||
from flask import render_template, Blueprint, flash, redirect
|
||||
from flask_login import current_user, login_required
|
||||
from aula.models import get_group
|
||||
from aula.models import get_group, insert_group
|
||||
from aula.forms import CreateThreadForm, CreateGroupForm, CreatePostForm
|
||||
|
||||
Group = Blueprint('Group', __name__)
|
||||
|
||||
@ -8,15 +9,26 @@ Group = Blueprint('Group', __name__)
|
||||
@login_required
|
||||
def groups():
|
||||
groups = current_user.get_groups_joinable()
|
||||
return render_template('groups.html', groups=groups)
|
||||
form = CreateGroupForm()
|
||||
return render_template('groups.html', groups=groups, form=form)
|
||||
|
||||
@Group.route("/group/<int:group_id>", methods=['GET'])
|
||||
@Group.route("/groups/<int:group_id>", methods=['GET'])
|
||||
@login_required
|
||||
def show(group_id):
|
||||
group = get_group(group_id)
|
||||
posts = group.get_posts()
|
||||
threads = group.get_threads()
|
||||
form = CreateThreadForm()
|
||||
formpost = CreatePostForm()
|
||||
if (group is not None):
|
||||
return render_template('group_show.html', group=group, posts=posts)
|
||||
return render_template('group_show.html', group=group, posts=posts, threads=threads, form=form, formpost=formpost)
|
||||
else:
|
||||
return f"Der findes ikke en gruppe med id {group_id}."
|
||||
|
||||
@Group.route("/groups/create", methods=['POST'])
|
||||
@login_required
|
||||
def create():
|
||||
form = CreateGroupForm()
|
||||
insert_group(form.title.data, form.hidden.data)
|
||||
flash('Gruppen blev oprettet', 'success')
|
||||
return redirect(f"/groups")
|
14
aula/Post/routes.py
Normal file
14
aula/Post/routes.py
Normal file
@ -0,0 +1,14 @@
|
||||
from flask import render_template, Blueprint, flash, redirect
|
||||
from flask_login import current_user, login_required
|
||||
from aula.models import insert_post
|
||||
from aula.forms import CreatePostForm
|
||||
|
||||
Post = Blueprint('Post', __name__)
|
||||
|
||||
@Post.route("/posts/create", methods=['POST'])
|
||||
@login_required
|
||||
def create():
|
||||
form = CreatePostForm()
|
||||
insert_post(form.group_id.data, form.author_id.data, form.title.data, form.content.data)
|
||||
flash('Opslag blev oprettet', 'success')
|
||||
return redirect(f"/groups/{form.group_id.data}")
|
@ -1,7 +1,7 @@
|
||||
from flask import redirect, render_template, Blueprint
|
||||
from flask import redirect, render_template, Blueprint, flash
|
||||
from flask_login import login_required, current_user
|
||||
from aula.models import get_thread, insert_message
|
||||
from aula.forms import SendMessageForm
|
||||
from aula.models import get_thread, insert_message, insert_thread
|
||||
from aula.forms import SendMessageForm, CreateThreadForm
|
||||
|
||||
Threads = Blueprint('Threads', __name__)
|
||||
|
||||
@ -27,3 +27,12 @@ def show(thread_id):
|
||||
return redirect(f"/threads/{thread_id}")
|
||||
|
||||
return render_template("thread_show.html", thread=thread, messages=thread.get_messages(), form=form)
|
||||
|
||||
@Threads.route("/threads/create", methods=['POST'])
|
||||
@login_required
|
||||
def create():
|
||||
form = CreateThreadForm()
|
||||
insert_thread(form.group_id.data, form.title.data)
|
||||
flash('Tråden blev oprettet', 'success')
|
||||
return redirect(f"/groups/{form.group_id.data}")
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, SubmitField, BooleanField
|
||||
from wtforms import StringField, PasswordField, SubmitField, BooleanField, HiddenField
|
||||
from wtforms.validators import DataRequired, Length
|
||||
|
||||
class AddUserForm(FlaskForm):
|
||||
@ -19,3 +19,21 @@ class UserLoginForm(FlaskForm):
|
||||
class SendMessageForm(FlaskForm):
|
||||
besked = StringField('Besked', validators=[DataRequired()])
|
||||
submit = SubmitField('Send')
|
||||
|
||||
class CreateThreadForm(FlaskForm):
|
||||
group_id = HiddenField('GroupID', validators=[DataRequired()])
|
||||
title = StringField('Title', validators=[DataRequired()])
|
||||
submit = SubmitField()
|
||||
|
||||
class CreateGroupForm(FlaskForm):
|
||||
title = StringField('Title', validators=[DataRequired()])
|
||||
hidden = BooleanField('Skjul gruppe')
|
||||
submit = SubmitField("Opret gruppe")
|
||||
|
||||
class CreatePostForm(FlaskForm):
|
||||
title = StringField('Title', validators=[DataRequired()])
|
||||
content = StringField('Indhold', validators=[DataRequired()])
|
||||
author_id = HiddenField('AuthorID', validators=[DataRequired()])
|
||||
group_id = HiddenField('GroupID', validators=[DataRequired()])
|
||||
submit = SubmitField("Opret opslag")
|
||||
|
||||
|
@ -193,7 +193,7 @@ class User(tuple, UserMixin):
|
||||
sql_call = """
|
||||
SELECT groups.* FROM groups INNER JOIN users_groups ON groups.group_id = users_groups.group_id WHERE users_groups.user_id = %s
|
||||
UNION
|
||||
SELECT groups.* FROM groups WHERE groups.hidden = TRUE
|
||||
SELECT groups.* FROM groups WHERE groups.hidden = FALSE
|
||||
ORDER BY hidden ASC, name DESC
|
||||
"""
|
||||
cur.execute(sql_call, (self.user_id,))
|
||||
@ -321,6 +321,33 @@ def get_thread(thread_id):
|
||||
cur.close()
|
||||
return thread
|
||||
|
||||
def insert_thread(group_id, title):
|
||||
cur = conn.cursor()
|
||||
sql = """
|
||||
INSERT INTO threads(group_id, title) VALUES (%s, %s)
|
||||
"""
|
||||
cur.execute(sql, (group_id, title))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
def insert_group(name, hidden):
|
||||
cur = conn.cursor()
|
||||
sql = """
|
||||
INSERT INTO groups(name, hidden) VALUES (%s, %s)
|
||||
"""
|
||||
cur.execute(sql, (name, hidden))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
def insert_post(group_id, author_id, title, content):
|
||||
cur = conn.cursor()
|
||||
sql = """
|
||||
INSERT INTO posts(group_id, author_id, title, content) VALUES (%s, %s, %s, %s)
|
||||
"""
|
||||
cur.execute(sql, (group_id, author_id, title, content))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
def insert_message(content, thread_id, author_id):
|
||||
cur = conn.cursor()
|
||||
sql_call = """
|
||||
|
@ -2,16 +2,49 @@
|
||||
{% block content %}
|
||||
<h1>{{ group.name }}</h1>
|
||||
<hr>
|
||||
{% if posts|length == 0 %}
|
||||
<p>Gruppen har ingen opslag endnu</p>
|
||||
{% endif %}
|
||||
{% for post in posts %}
|
||||
<article class="media content-section">
|
||||
<div class="media-body">
|
||||
<div class="article-metadata">
|
||||
<a class="mr-2 author-name" href="#">{{ post.author.first_name }} {{ post.author.last_name }}</a>
|
||||
<small class="text-muted">{{ post.date_posted }}</small>
|
||||
<a class="mr-2 author-name" href="#">{{ post.author.first_name }} {{ post.author.last_name }} </a>
|
||||
<small class="text-muted float-right">{{ post.created_date.strftime('%H:%I %d-%m-%Y')}}</small>
|
||||
</div>
|
||||
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
|
||||
<p class="article-content">{{ post.content }}</p>
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<p style="font-weight: bold;">Skriv opslag</p>
|
||||
<form method="POST" action="{{ url_for('Post.create')}}">
|
||||
{{ formpost.group_id(value=group.group_id) }}
|
||||
{{ formpost.author_id(value=current_user.user_id) }}
|
||||
<div class="row">
|
||||
<div class="col-md-4">{{ formpost.title(class="form-control", placeholder="Title") }}</div>
|
||||
<div class="col-md-8">{{ formpost.content(class="form-control", placeholder="Indhold") }}</div>
|
||||
<div class="col-md-12 mt-2">{{ formpost.submit(class="btn btn-primary btn-block btn-sm", value="Opret opslag") }}</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
||||
{% block sidebar %}
|
||||
<p style="font-weight: bold;">Gruppe tråde</p>
|
||||
{% if threads|length == 0 %}
|
||||
<p>Gruppen har ingen tråde endnu</p>
|
||||
{% endif %}
|
||||
{% for thread in threads %}
|
||||
<p><a href="{{ url_for('Threads.show', thread_id = thread.thread_id) }}">{{ thread.title }}</a></p>
|
||||
{% endfor %}
|
||||
<hr>
|
||||
<form method="POST" action="{{ url_for('Threads.create')}}">
|
||||
{{ form.group_id(value=group.group_id) }}
|
||||
<p>{{ form.title(class="form-control", placeholder="Trådens title") }}</p>
|
||||
<p>{{ form.submit(class="btn btn-primary btn-block", value="Start tråd") }}</p>
|
||||
</form>
|
||||
{% endblock sidebar %}
|
||||
|
@ -1,5 +1,7 @@
|
||||
{% extends "layout.html" %}
|
||||
{% block content %}
|
||||
<h1>Grupper</h1>
|
||||
<hr>
|
||||
{% if groups|length == 0%}
|
||||
<p>Ikke medlem af nogle grupper</p>
|
||||
{% else %}
|
||||
@ -12,3 +14,13 @@
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endblock content %}
|
||||
|
||||
{% block sidebar %}
|
||||
<p style="font-weight: bold;">Opret gruppe</p>
|
||||
<form method="POST" action="{{ url_for('Group.create')}}">
|
||||
<p>{{ form.title(class="form-control", placeholder="Gruppens navn") }}</p>
|
||||
|
||||
<p>{{ form.hidden() }} {{ form.hidden.label(class="form-check-label") }}</p>
|
||||
<p>{{ form.submit(class="btn btn-primary btn-block") }}</p>
|
||||
</form>
|
||||
{% endblock sidebar %}
|
||||
|
@ -9,7 +9,7 @@
|
||||
<div class="media-body">
|
||||
<div class="article-metadata">
|
||||
<a class="mr-2 author-name" href="#">{{ post.author.first_name }} {{ post.author.last_name }}</a>
|
||||
<small class="text-muted">{{ post.date_posted }}</small>
|
||||
<small class="text-muted float-right">{{ post.created_date.strftime('%H:%I %d-%m-%Y')}}</small>
|
||||
</div>
|
||||
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
|
||||
<p class="article-content">{{ post.content }}</p>
|
||||
|
@ -50,10 +50,21 @@
|
||||
<main role="main" class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<div class="alert alert-{{ category }}">
|
||||
{{ message }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="content-section">
|
||||
{% block sidebar %}{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user