Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·74 lines (58 loc) · 2.08 KB
/
Copy pathapp.py
File metadata and controls
executable file
·74 lines (58 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
__authors__ = "Akas Antony"
__email__ = "antony.akas@gmail.com"
__date__ = "27th, October 2015"
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, jsonify
import os
from pymongo import MongoClient
from bson.objectid import ObjectId
app = Flask(__name__, static_url_path='')
client = MongoClient()
db = client.Forum
def get_featured_results():
posts = list(reversed(list(db.posts.find())))
posts = posts[:5]
print(posts)
return posts
@app.route('/', methods = ['GET'])
def index():
if request.method == 'GET':
results = get_featured_results()
return render_template('index.html', results=results)
@app.route('/browse', methods = ['GET','POST'])
def browse():
if request.method == 'GET':
posts = reversed(list(db.posts.find()))
print(posts)
return render_template('browse.html', results=posts)
@app.route('/about', methods = ['GET'])
def about():
if request.method == 'GET':
return render_template('about.html')
@app.route('/ask', methods = ['POST', 'GET'])
def post():
if request.method == 'GET':
return render_template('ask.html')
if request.method == 'POST':
question = request.form['question']
print (question)
post_query = {'question': question}
print(post_query)
posts = db.posts
posts.insert_one(post_query)
return render_template('ask.html')
@app.route('/<path:path>')
def static_proxy(path):
return app.send_static_file(path)
@app.route('/<id>', methods = ['GET', 'POST'])
def show_discussion(id):
if request.method == 'GET':
post = db.posts.find_one({'_id': ObjectId(id)})
return render_template('discuss.html', result = post)
if request.method == 'POST':
answer = request.form['answer']
db.posts.update({'_id': ObjectId(id)}, { '$push': {'answer': answer} })
post = db.posts.find_one({'_id': ObjectId(id)})
return render_template('discuss.html', result = post)
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000, debug=True)