-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
169 lines (108 loc) · 3.94 KB
/
app.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from flask import Flask, request, jsonify,render_template
from flask_mongoengine import MongoEngine
from databases.models import *
app = Flask(__name__)
db = MongoEngine()
app.config['MONGODB_SETTINGS'] = {
'host': 'mongodb://127.0.0.1/d'
}
db.init_app(app)
@app.route('/',methods=['GET','POST'])
def index_page():
print(request.method)
#print(request.user_agent)
#print(dir(request))
data = request.form.get('data')
print(data)
return jsonify('Index Page')
@app.route('/fakenews',methods=['POST'])
def addFakenews():
title = request.form.get('title')
body = request.form.get('body')
if body and title :
post = FakeNews(title=title, body=body,).save()
return 'POST added successfully.'
else:
return jsonify('Fields are missing.')
@app.route('/fakenews',methods=['GET'])
def getFakenewslist():
posts = FakeNews.objects()
if posts:
return jsonify(posts)
else:
return jsonify('No posts existed.')
@app.route('/fakenews/id/<string:id>',methods=['GET'])
def getFakenews(id):
try:
post = FakeNews.objects(id=id).get()
fakecounts = VoteFakeNews.objects(post=id)
return jsonify(post,fakecounts)
except Exception as e :
return jsonify(str(e))
@app.route('/fakenews/id/<string:id>',methods=['DELETE'])
def deleteFakenews(id):
try:
post = FakeNews.objects(id=id).delete()
VoteFakeNews.objects(post=id).delete()
return jsonify('Post Deleted successfully.')
except Exception as e :
return jsonify(str(e))
@app.route('/fakenews/id/<string:id>',methods=['PUT'])
def updateFakenews(id):
title = request.form.get('title')
body = request.form.get('body')
if (body is None or body == '') and ( title is None or title == '') :
return jsonify('Update fields are empty or none.')
else:
try:
post = FakeNews.objects(id=id).get()
if post.isTitleChanged(title) and post.isBodyChanged(body):
post.update(
title=title,
body=body
)
return jsonify('Post title and body updated successfully.')
elif post.isTitleChanged(title):
post.update(
title=title
)
return jsonify('Post title updated successfully.')
elif post.isBodyChanged(body):
post.update(
body=body
)
return jsonify('Post body updated successfully.')
else:
return jsonify('Post is not updated.')
except Exception as e :
return jsonify(str(e))
@app.route('/fakenews/user',methods=['POST'])
def adduser():
fullname = request.form.get('fullname')
email = request.form.get('email')
if fullname and email :
try:
user = User(full_name=fullname,email=email).save()
return jsonify('User Created Successfully.')
except Exception as e:
return jsonify(str(e))
else:
return jsonify('User not created.')
@app.route('/fakenews/markfakenews/id/<string:id>',methods=['POST'])
def markfakenews(id):
is_fake = request.form.get('is_fake')
if is_fake:
is_fake = is_fake.lower()
if is_fake in ('true','false'):
try:
post = FakeNews.objects(id=id).get()
VoteFakeNews(post=post, is_fake=is_fake).save()
return jsonify('Post is marked successfully.')
except Exception as e:
return jsonify(str(e))
else:
return jsonify('Bool value required.')
else:
return jsonify('Bool value required.')
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port='8000')