forked from padfoot18/SAHEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparagraph_api.py
48 lines (42 loc) · 1.4 KB
/
paragraph_api.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
from flask import request
from flask_restful import Resource
import sqlite3
class Paragraph(Resource):
def get(self):
"""
Accepts a get request.
:return: entire paragraph present in the database
"""
# DONE (1) when a get request is performed, return the existing paragraph to the caller
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
c.execute('SELECT * FROM paragraph')
para = c.fetchall()
print(para[0][0])
connection.commit()
except Exception as exception:
print(exception)
finally:
if connection:
connection.close()
return para[0][0]
def post(self):
"""
Accepts the modified paragraph in a post request. Updates the paragraph in the database.
:return: updated paragraph
"""
if request.form['paragraph']:
paragraph = request.form['paragraph']
try:
connection = sqlite3.connect('test.db')
c = connection.cursor()
c.execute('UPDATE paragraph SET para="' + paragraph + '";')
print(paragraph)
connection.commit()
except Exception as exception:
print(exception)
finally:
if connection:
connection.close()
return paragraph