Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added forum/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions forum/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from . import create_app
app = create_app()

app.config['SITE_NAME'] = 'Schooner'
app.config['SITE_DESCRIPTION'] = 'a schooner forum'
app.config['SITE_NAME'] = 'Something, Anything, that is not that'
app.config['SITE_DESCRIPTION'] = 'a forum for Data to learn from'
app.config['FLASK_DEBUG'] = 1

def init_site():
Expand Down
42 changes: 42 additions & 0 deletions forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class Comment(db.Model):
postdate = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey("post.id"))
replies = db.relationship('Reply', backref='comment', cascade='all, delete-orphan')


lastcheck = None
savedresponce = None
Expand Down Expand Up @@ -115,6 +117,46 @@ def get_time_string(self):
self.savedresponce = "Just a moment ago!"
return self.savedresponce

class Reply(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text)
postdate = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comment_id = db.Column(db.Integer, db.ForeignKey("comment.id"))

user = db.relationship('User', backref='replies')


lastcheck = None
savedresponce = None
def __init__(self, content, postdate, user_id, comment_id):
self.content = content
self.postdate = postdate
self.user_id = user_id
self.comment_id = comment_id
def get_time_string(self):
#this only needs to be calculated every so often, not for every request
#this can be a rudamentary chache
now = datetime.datetime.now()
if self.lastcheck is None or (now - self.lastcheck).total_seconds() > 30:
self.lastcheck = now
else:
return self.savedresponce

diff = now - self.postdate
seconds = diff.total_seconds()
if seconds / (60 * 60 * 24 * 30) > 1:
self.savedresponce = " " + str(int(seconds / (60 * 60 * 24 * 30))) + " months ago"
elif seconds / (60 * 60 * 24) > 1:
self.savedresponce = " " + str(int(seconds / (60* 60 * 24))) + " days ago"
elif seconds / (60 * 60) > 1:
self.savedresponce = " " + str(int(seconds / (60 * 60))) + " hours ago"
elif seconds / (60) > 1:
self.savedresponce = " " + str(int(seconds / 60)) + " minutes ago"
else:
self.savedresponce = "Just a moment ago!"
return self.savedresponce

def error(errormessage):
return "<b style=\"color: red;\">" + errormessage + "</b>"

Expand Down
55 changes: 53 additions & 2 deletions forum/routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from flask import render_template, request, redirect, url_for
from flask import render_template, request, redirect, url_for, flash
from flask_login import current_user, login_user, logout_user
from flask_login.utils import login_required
import datetime
from flask import Blueprint, render_template, request, redirect, url_for
from forum.models import User, Post, Comment, Subforum, valid_content, valid_title, db, generateLinkPath, error
from forum.models import User, Post, Comment, Subforum, valid_content, valid_title, db, generateLinkPath, error, Reply
from forum.user import username_taken, email_taken, valid_username
from werkzeug.security import generate_password_hash, check_password_hash

##
# This file needs to be broken up into several, to make the project easier to work on.
Expand Down Expand Up @@ -117,6 +118,17 @@ def comment():
db.session.commit()
return redirect("/viewpost?post=" + str(post_id))

@login_required
@rt.route('/action_reply/comment/<int:comment_id>', methods=['POST']) #attempt to mimic comment as a reply
def action_reply(comment_id):
comment = Comment.query.get_or_404(comment_id)
content = request.form['content']
postdate = datetime.datetime.now()
reply = Reply(content=content, postdate=postdate, user_id=current_user.id, comment_id=comment_id)
db.session.add(reply)
db.session.commit()
return redirect(url_for('routes.viewpost', post=comment.post_id))

@login_required
@rt.route('/action_post', methods=['POST'])
def action_post():
Expand Down Expand Up @@ -145,3 +157,42 @@ def action_post():
db.session.commit()
return redirect("/viewpost?post=" + str(post.id))

@login_required
@rt.route('/user')
def user():
user_id = request.args.get('user_id')

if user_id:
user = User.query.get_or_404(user_id)
posts = Post.query.filter(Post.user_id == user.id).all()
comments = Comment.query.filter(Comment.user_id == user.id).all()
replies = Reply.query.filter(Reply.user_id == user.id).all()
return render_template('user.html', user=user, posts=posts, comments= comments, replies= replies)
elif current_user.is_authenticated:
return redirect(url_for('routes.user', user_id=current_user.id))
else:
return redirect ('/loginform')

@login_required
@rt.route('/change_password', methods=['POST', 'GET'])
def change_password():
if request.method == 'POST':
old_password = request.form['old_password']
new_password = request.form['new_password']
confirm_password = request.form['confirm_password']

if not check_password_hash(current_user.password_hash, old_password):
flash('Old password does not match records.', 'danger')
return redirect(url_for('routes.change_password'))

if new_password != confirm_password:
flash('New and confirm do not match.', 'warning')
return redirect(url_for('routes.change_password'))

current_user.password_hash = generate_password_hash(new_password)
db.session.commit()

flash('Password has been updated', 'success')
return redirect(url_for('routes.change_password'))

return render_template('change_password.html')
4 changes: 2 additions & 2 deletions forum/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

}
.header{
border: 1px solid black;
border: 5px solid black;
overflow: hidden;
vertical-align: middle;
padding: 0.5%;
padding: 3.5%;
margin-bottom: none;
}
.content{
Expand Down
Binary file added forum/templates/.DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions forum/templates/change_password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends 'layout.html' %}
{% block body %}
{{ path|safe }}

<div class="loginbox">
<h3>Change Password</h3>

{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="flashes">
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}

<form action="/change_password" method="POST">
<input class="loginelement" type="password" placeholder="Old Password" name="old_password" required><br>
<input class="loginelement" type="password" placeholder="New Password" name="new_password" required><br>
<input class="loginelement" type="password" placeholder="Confirm New Password" name="confirm_password" required><br>
<input class="loginelement" type="submit" value="Change Password">
</form>
</div>

{% endblock %}
7 changes: 4 additions & 3 deletions forum/templates/createpost.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

<form action="action_post?sub={{ subforum.id }}" method="POST">

<input class="inputbox" size="100" type="text" placeholder="Title" name="title"><br>

<input class="form-control" size="100" type="text" placeholder="Title" name="title"><br>

<textarea class="inputbox" rows="25" cols="100" name="content" placeholder="Enter post here"></textarea><br>
<textarea class="form-control" rows="25" cols="100" name="content" placeholder="Enter post here"></textarea><br>

<input class="inputbox" type="submit" value="Submit Post">
<input class="btn btn-outline-primary" type="submit" value="Submit Post">
</form>
</div>
{% endblock %}
25 changes: 22 additions & 3 deletions forum/templates/header.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<a href="/" id="nolinks"><b>{{ config.SITE_NAME }}</b></a>{% if config.SITE_DESCRIPTION %} - {% endif %} <i>{{ config.SITE_DESCRIPTION }}</i>
<div class="login">
<nav class="navbar navbar-expand-lg navbar-light bg-secondary">
<div class="container-fluid d-flex justify-content-center">
<a href="/" id="nolinks" class="navbar-brand">
<b>{{ config.SITE_NAME }}</b>

</a>
{% if config.SITE_DESCRIPTION %}
<span class="text-muted"> - </span>
<i class="text-muted">{{ config.SITE_DESCRIPTION }}</i>

{% endif %}
</div>
</nav>
</nav><button class="login">
{% if current_user.is_authenticated %}
<b {% if current_user.admin %} style="color: red;" {% endif %}>{{ current_user.username }}</b><a class="logoutlink" href="/action_logout">(logout)</a>
{% else %}
<a href="/loginform">Click here to login or register!</a>
{% endif %}
</div>
</div>

{% if current_user.is_authenticated %}
<div class="actualuser">
<a href="{{ url_for('routes.user', user_id=current_user.id) }}">Profile</a>
<a href="{{ url_for('routes.change_password') }}">Change Password</a>
</div>
{% endif %}
66 changes: 47 additions & 19 deletions forum/templates/layout.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename="bootstrap.min.css")}}">
<link rel="stylesheet" type="text/css" href="{{url_for('static', filename="style.css")}}">
<title>{{ config.SITE_NAME }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">

<title>Bootstrap</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<title>{{ config.SITE_NAME }}</title>
</head>

<body>
<div class="page">
<div class="header">
{% include 'header.html' %}
</div>
{% if errors %}
<div class="errors">
{% for error in errors %}
<p class="error">
{{ error }}
</p>

{% endfor %}
</div>
<body style="background-color:powderblue;">





<div class="row">
<div class="row"></div>
<div class="col-sm-3"> <img src="https://i.ibb.co/czb9V1q/logo.png" width="150" height="150"alt="Your Logo"></div>
<div class="col-sm-9"></div>

<div class="col-sm-2"></div>
<div class="col-sm-8">{% include 'header.html' %}</div>
<div class="col-sm-2"></div>


</div>
{% if errors %}
<div class="errors">
{% for error in errors %}
<p class="error">
{{ error }}
</p>

{% endfor %}

</div>
{% endif %}
<div class="content">
{% block body %}{% endblock %}
</div>
<div class="row">
<div class="row"></div>
<div class="col-sm-2"></div>
<div class="col-sm-8">{% block body %}{% endblock %}</div>
<div class="col-sm-2"></div>
</div>
</div>


</body>



</html>

52 changes: 52 additions & 0 deletions forum/templates/user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% extends 'layout.html' %}
{% block body %}
{{ path|safe }}

{% if current_user.is_authenticated %}
<h2> Welcome, {{ user.username }}</h2>
<h3>These are your posts:</h3>
{% if posts %}
<ul>
{% for post in posts %}
<li>
<a href="/viewpost?post={{ post.id }}">{{post.title}}</a>
<p>{{ post.content }}</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>You have no posts at this time.</p>
{% endif %}
<h3>These are your comments:</h3>
{% if comments %}
<ul>
{% for comment in comments %}
<li>
<a href="/viewpost?post={{ comment.post_id }}">{{comment.post.title}}</a>
<p>{{ comment.content }}</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>You have no comments at this time.</p>
{% endif %}

<h3>These are your replies:</h3>
{% if replies %}
<ul>
{% for reply in replies %}
<li>
<a href="/viewpost?post={{ reply.comment.post_id }}">{{reply.comment.post.title}}</a>
<p>{{ reply.content }}</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>You have no replies at this time.</p>
{% endif %}

{% else %}
<a href="/loginform">Click here to login or register!</a>
{% endif %}

{% endblock %}
Loading