77from flask_login import login_user , current_user , login_required
88
99from firefly .forms .user import LoginForm , RegisterForm
10- from firefly .models .topic import Category , Post
10+ from firefly .models .topic import Category , Post , Comment
1111from firefly .models .user import User
1212
1313
@@ -20,7 +20,7 @@ def get(self):
2020 return render_template ('index.html' , posts = posts )
2121
2222
23- class CreateView (MethodView ):
23+ class CreateTopicView (MethodView ):
2424 decorators = [login_required ]
2525
2626 def post (self ):
@@ -40,6 +40,28 @@ def post(self):
4040 return jsonify (ok = 0 , html = html )
4141
4242
43+ class CreateCommentView (MethodView ):
44+ decorators = [login_required ]
45+
46+ def post (self ):
47+ ref_id = request .form .get ('ref_id' , 0 )
48+ content = request .form .get ('content' )
49+ author = User .objects .get_or_404 (id = current_user .id )
50+ c = Comment (ref_id = ref_id , content = content , author = author )
51+ c .save ()
52+ context = Post .objects (id = int (ref_id ))
53+ if not context :
54+ context = Comment .objects (id = int (ref_id ))
55+ if not context :
56+ return jsonify (ok = 1 , msg = 'not exists' )
57+ else :
58+ context = context [0 ]
59+ context .comments .append (c )
60+ context .save ()
61+
62+ return jsonify (ok = 0 )
63+
64+
4365class LoginView (MethodView ):
4466 def get (self ):
4567 return redirect (url_for ('home.index' ))
@@ -65,6 +87,9 @@ def post(self):
6587
6688
6789bp .add_url_rule ('/' , view_func = HomeView .as_view ('index' ))
68- bp .add_url_rule ('create' , view_func = CreateView .as_view ('create' ))
90+ bp .add_url_rule ('create/topic' ,
91+ view_func = CreateTopicView .as_view ('create_topic' ))
92+ bp .add_url_rule ('create/comment' ,
93+ view_func = CreateCommentView .as_view ('create_comment' ))
6994bp .add_url_rule ('login' , view_func = LoginView .as_view ('login' ))
7095bp .add_url_rule ('register' , view_func = RegisterView .as_view ('register' ))
0 commit comments