-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
115 lines (94 loc) · 3.09 KB
/
app.py
File metadata and controls
115 lines (94 loc) · 3.09 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
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
from flask import Flask, render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from summarizer import summarize
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///notes.db'
db = SQLAlchemy(app)
class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
description = db.Column(db.Text)
summary = db.Column(db.Text)
date = db.Column(db.String(20))
def create_database():
with app.app_context():
db.create_all()
@app.route('/notes', methods=['GET'])
def get_notes():
notes = Note.query.all()
notes_data = []
for note in notes:
notes_data.append({
'id': note.id,
'title': note.title,
'description': note.description,
'summary': note.summary,
'date': note.date
})
return jsonify(notes_data)
@app.route('/notes', methods=['POST'])
def add_note():
print("GET")
data = request.get_json()
title = data['title']
description = data['description']
summary = summarize(description)
date = data['date']
note = Note(title=title, description=description, summary=summary, date=date)
db.session.add(note)
db.session.commit()
return jsonify({
'id': note.id,
'title': note.title,
'description': note.description,
'summary': note.summary,
'date': note.date
})
@app.route('/notes/<int:id>', methods=['GET'])
def get_single_note(id):
print("GET SINGLE NOTE")
if request.method == 'GET':
# Handle the GET request to fetch the note contents
note = Note.query.get(id)
return jsonify({
'id': note.id,
'title': note.title,
'description': note.description,
'summary': note.summary,
'date': note.date
})
@app.route('/notes/<int:id>', methods=['PUT'])
def update_note(id):
if request.method == 'PUT':
# Handle the PUT request to update the note
data = request.get_json()
note = Note.query.get(id)
if note:
note.title = data.get('title', note.title)
note.description = data.get('description', note.description)
note.summary = summarize(note.description)
note.date = data.get('date', note.date)
db.session.commit()
return jsonify({
'id': note.id,
'title': note.title,
'description': note.description,
'summary': note.summary,
'date': note.date
})
else:
return jsonify({'message': 'Note not found'}), 404
@app.route('/notes/<int:id>', methods=['DELETE'])
def delete_note(id):
note = Note.query.get(id)
db.session.delete(note)
db.session.commit()
return jsonify({'message': 'Note deleted successfully'})
@app.route("/")
def index():
print("Started")
# Return a response to the client
return render_template("index.html")
if __name__ == "__main__":
create_database() # Create the table if it doesn't exist
app.run(debug=True)