Skip to content

Commit c50d038

Browse files
committed
Add comments recursion, add mail notification for user who is a parent of comment
1 parent 4eb788e commit c50d038

File tree

8 files changed

+97
-26
lines changed

8 files changed

+97
-26
lines changed

CommentModel.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@ def __init__(self):
99

1010
def comments(self, post):
1111

12-
comments = self.Comment.query.filter_by(Post=post.Id).all()
12+
comments = self.Comment.query.filter_by(Post=post.Id,Parent=None).all()
1313

1414
if comments:
1515
return comments
1616
return False
1717

18-
def addcomment(self, comment, email, nick, post):
18+
def addcomment(self, comment, email, nick, post, cid=None):
1919

2020
from models import db
21-
new_comment = self.Comment(nick, email, comment, post)
21+
if cid:
22+
new_comment = self.Comment(nick, email, comment, post, cid)
23+
24+
25+
else:
26+
new_comment = self.Comment(nick, email, comment, post)
2227
db.session.add(new_comment)
2328
db.session.commit()
2429

2530
return new_comment.Id
31+
32+
33+
def getCommentEmail(self, id):
34+
comments = self.Comment.query.filter_by(Id=id).first()
35+
36+
if comments:
37+
return comments
38+
return []

appconfig.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
CRYPT_KEY = 'p@s$VV0Rd'
66

77
URL = "https://pregmatch.org/admin"
8+
URL_FE = "https://pregmatch.org/"
89

910
# administrator list
1011
ADMINS = ['[email protected]']

controllers/fe.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from UserModel import *
55
from CommentModel import *
66
from htmlmin.minify import html_minify
7+
from appconfig import *
8+
79
fe = Blueprint('fe', __name__)
810

911

@@ -42,10 +44,21 @@ def read(slug=None):
4244

4345
if request.method == "POST":
4446

45-
if 'comment' in request.form:
47+
if 'comment' in request.form and 'cid' not in request.form:
4648

4749
comment.addcomment(request.form['comment'], request.form['email'], request.form['nick'], id.Id)
4850

51+
if 'comment' in request.form and 'cid' in request.form:
52+
53+
comment.addcomment(request.form['comment'], request.form['email'], request.form['nick'], id.Id, request.form['cid'])
54+
55+
##send reply to if any email address defined
56+
c = comment.getCommentEmail(request.form['cid'])
57+
if c.Email:
58+
from UserModel import UserModel
59+
u = UserModel()
60+
u.send_email(request.form['nick'] + " just commented on post "+id.Title, c.Email, request.form['comment']+ "<p>"+URL_FE+"read/"+id.Slug+"</p>", c.Nick)
61+
4962
if 'password' in request.form:
5063

5164
from werkzeug import check_password_hash
@@ -95,4 +108,12 @@ def utility_processor():
95108
def getimages(id):
96109
p = PostModel()
97110
return p.getPostImages(id)
98-
return dict(getimages=getimages)
111+
return dict(getimages=getimages)
112+
113+
114+
@fe.context_processor
115+
def utility_processor():
116+
def getChildren(id):
117+
p = CommentModel()
118+
return p.getChildren(id)
119+
return dict(getChildren=getChildren)

db.sql

+5-1
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,8 @@ CREATE TABLE IF NOT EXISTS `pregmatch`.`PostImage` (
219219
REFERENCES `pregmatch`.`Post` (`Id`)
220220
ON DELETE NO ACTION
221221
ON UPDATE NO ACTION)
222-
ENGINE = InnoDB
222+
ENGINE = InnoDB
223+
224+
ALTER table Comment add `Parent` int(11) DEFAULT NULL;
225+
ALTER TABLE `Comment` ADD KEY `Parent` (`Parent`);
226+
ALTER TABLE `Comment` ADD CONSTRAINT `comment_ibfk_1` FOREIGN KEY (`Parent`) REFERENCES `Comment` (`Id`);

eer/db.mwb

112 Bytes
Binary file not shown.

eer/db.mwb.bak

17 Bytes
Binary file not shown.

models/Comment.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
from models.Post import Post
33
import time
44

5+
56
class Comment(db.Model):
7+
__tablename__ = 'Comment'
8+
9+
Id = db.Column(db.Integer, primary_key=True)
10+
Post = db.Column(db.Integer, db.ForeignKey(Post.Id))
11+
Comment = db.Column(db.Text)
12+
Email = db.Column(db.String(45), unique=True)
13+
Nick = db.Column(db.String(6))
14+
DateCreated = db.Column(db.DateTime)
15+
Parent = db.Column(db.Integer, db.ForeignKey("Comment.Id"))
16+
children = db.relationship("Comment")
17+
18+
def __init__(self, nick, email, comment, post, cid=None):
19+
self.Post = post
20+
self.Comment = comment
21+
self.Email = email
22+
self.Nick = nick
23+
self.Parent = cid
24+
self.DateCreated = time.strftime('%Y-%m-%d %H:%M:%S')
625

7-
__tablename__ = 'Comment'
826

9-
Id = db.Column(db.Integer, primary_key = True)
10-
Post = db.Column(db.Integer, db.ForeignKey(Post.Id))
11-
Comment = db.Column(db.Text)
12-
Email = db.Column(db.String(45), unique=True)
13-
Nick = db.Column(db.String(6))
14-
DateCreated = db.Column(db.DateTime)
15-
16-
def __init__(self, nick, email, comment, post):
1727

1828

19-
self.Post = post
20-
self.Comment = comment
21-
self.Email = email
22-
self.Nick = nick
23-
self.DateCreated = time.strftime('%Y-%m-%d %H:%M:%S')

templates/home/read.html

+33-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h3>{{ post.Title }}</h3>
4343

4444
</div>
4545
<div class="col-xs-12">
46-
<form method="post" action="/read/{{post.Slug}}">
46+
<form method="post" action="/read/{{post.Slug}}" id="c_{{post.Slug}}">
4747
<br />
4848
<div class="form-group">
4949
<input type="text" class="form-control" name="nick" placeholder="Nick (optional)" />
@@ -54,16 +54,25 @@ <h3>{{ post.Title }}</h3>
5454
<textarea name="comment" class="form-control" rows="10" placeholder="Comment (mandatory)"></textarea>
5555
<span style="font-size: 11px; float: left">You can use any html tag (p, pre ...)</span><br />
5656
<input type="submit" value="Add comment" class="btn btn-primary" style="float: right" />
57+
<input type="hidden" class="cid" name="cid" />
5758
</form>
5859
<div class="col-lg-12" style="text-align: left">
5960
{% if comments %}
6061
<hr />
61-
{% for comment in comments%}
62-
<span>Comment by: {{ comment.Nick }} - <span style="font-size: 11px;">created at {{ comment.DateCreated }}</span></span><br />
63-
<div class="col-lg-12" style="text-align: left; margin-top: 10px">
64-
{{comment.Comment | safe}}
65-
</div>
62+
<ul style="list-style: none; panel panel-info">
63+
{% for comment in comments recursive%}
64+
<li class="">
65+
<span style="font-size: 11px">{{ comment.Nick }} - <span style="font-size: 11px;">created at {{ comment.DateCreated }}</span> </span> |
66+
<a href="javascript: void(0)" onclick="javascript: reply($(this), '{{post.Slug}}', {{comment.Id}})">Reply</a> <br />
67+
<div class="col-lg-12" style="text-align: left; margin-top: 10px">
68+
{{comment.Comment | safe}}
69+
</div>
70+
{% if comment.children %}
71+
<ul style="list-style: none" >{{ loop(comment.children) }}</ul>
72+
{% endif %}
73+
</li>
6674
{% endfor %}
75+
</ul>
6776
{% endif %}
6877
</div>
6978
</div>
@@ -82,4 +91,22 @@ <h3>{{ post.Title }}</h3>
8291

8392
}
8493
</style>
94+
<script>
95+
96+
function reply(el, slug, id){
97+
98+
99+
if(document.getElementsByClassName("d_"+slug).length){
100+
$(".d_"+slug).remove();
101+
el.text("Reply");
102+
}
103+
else {
104+
$("#c_"+slug).clone().insertAfter(el.next().next()).attr("class","d_"+slug).find($(".cid").val(id));
105+
106+
el.text("Remove");
107+
108+
}
109+
}
110+
111+
</script>
85112
{% endblock %}

0 commit comments

Comments
 (0)