-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
137 lines (110 loc) · 3.61 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
from flask import Flask, render_template, request, jsonify
from flask_pymongo import PyMongo
from dotenv import load_dotenv
import os
import json
from bson import ObjectId
load_dotenv()
app = Flask(__name__)
app.config['MONGO_URI']=os.getenv('MONGO_URI')
mongo=PyMongo(app)
'''
List of class, functions and routes
class JSONEncoder - To convert the Json having ObjectId to simple String
/
index() - index Page
/api/notes/all
notes_all() - API to get all the notes in the database
/api/notes/new
notes_new() - API To add a new note in the database
/api/notes/delete
notes_delete() - API To delete a note in the database
'''
'''--------------- Classes ---------------'''
# Class to convert ObjectId to simple String
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
'''---------------- Functions -------------'''
# Funciton to insert a new note
def insertNote(heading, description):
insert_value = mongo.db.notes.insert_one({ "heading": heading, "description": description })
return str(insert_value.inserted_id)
def deleteNote(id):
mongo.db.notes.delete_one({ "_id": id }) #check if it is deleting or not
'''--------------- Routes -----------------'''
''' Main Page to be added soon '''
@app.route('/')
def index():
headers = request.headers
return "Headers are" + str(headers)
'''--------------- API Routes --------------'''
'''
API to get all the notes
Example:
Request:
GET (No parameters needed)
Response:
{ notes: [ {'_id': '5eb55594da6e679996f27b66', 'heading': 'hello', 'description': 'Note description' } ]}
list in notes,
String in _id, heading, description
'''
@app.route('/api/notes/all') # This can give errors, add a try catch also
def notes_all():
# Return all the notes_all
online_notes = mongo.db.notes.find({})
data = { 'notes': list(online_notes) }
#print(data)
return JSONEncoder().encode(data) #This converts the ObjectId to simple Iid
'''
API to add a new note
Example:
Request:
POST Body in form or json type
Parameters: heading String, description String
Response:
data = { success: 1, msg="Error message or success message" or inserted_id: "id" }
'''
@app.route('/api/notes/new', methods=['POST'])
def notes_new():
#Create a new note
try:
if(request.is_json):
#print("Yes a json")
data = request.get_json()
heading = data['heading']
description = data['description']
else:
#print("Will think it as a form urlencoded")
heading = request.form['heading']
description = request.form['description']
inserted_id = insertNote(heading, description)
return ({ "success":1, "inserted_id": inserted_id })
except Exception as e:
return ({'success':0, "msg": str(e)})
''' //Need to think of how this needs to be done
API to delete a note
Request: (POST) In form url encoded or Json body
Just send the note id and { 'id': 'Note id'}
Response:
data = { success: 1, msg="Error message or success message" }
'''
@app.route('/api/notes/delete', methods=['POST'])
def notes_delete():
#Delete a note
try:
if(request.is_json):
#print("Yes a json")
data = request.get_json()
id = data['id']
else:
#print("Will think it as a form urlencoded")
id = request.form['id']
deleteNote(id)
return ({ "success":1, "msg": "successfully deleted!" })
except Exception as e:
return ({'success':0, "msg": str(e)})
if __name__ == "__main__":
app.run()