-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
60 lines (49 loc) · 1.69 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
from flask import Flask, render_template, request
from configparser import ConfigParser
import pymongo
app = Flask(__name__)
config = ConfigParser()
config.read('config.ini')
client = pymongo.MongoClient(config['connection']['connection_string'])
db = client[config['database']['db_name']]
collection = db[config['database']['collection_name']]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/save_student', methods=['POST'])
def save_student():
name = request.form.get('name')
usn = request.form.get('usn')
grade = request.form.get('grade')
linkedin = request.form.get('linkedin')
github = request.form.get('github')
if name and usn and grade:
existing_student = collection.find_one({'_id': usn})
if existing_student:
collection.update_one(
{'_id': usn},
{
'$set': {
'name': name,
'grade': grade,
'linkedin': linkedin,
'github': github
}
}
)
message = "Data updated successfully."
else:
student_data = {
'_id': usn,
'name': name,
'grade': grade,
'linkedin':linkedin,
'github': github
}
collection.insert_one(student_data)
message = "Data logged successfully."
return render_template('index.html', message=message, success=True)
else:
return render_template('index.html', message="Something went wrong.", success=False)
if __name__ == '__main__':
app.run(debug=True)