-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (49 loc) · 1.63 KB
/
app.py
File metadata and controls
75 lines (49 loc) · 1.63 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
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.sqlite')
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Entry(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(15))
content = db.Column(db.String(144))
def __init__(self,title, content):
self.title = title
self.content = content
class EntrySchema(ma.Schema):
class Meta:
fields = ('id','title', 'content')
entry_schema = EntrySchema()
entries_schema = EntrySchema(many = True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/entry', methods = ['POST'])
def create_entry():
title = request.form['title']
content = request.form['content']
new_entry = Entry(title, content)
db.session.add(new_entry)
db.session.commit()
return render_template('index.html')
# End point to create a new entry
@app.route('/entries', methods = ['GET'])
def get_entries():
entries = Entry.query.all()
result = entries_schema.dump(entries)
return jsonify(result)
# End point to get all entries
@app.route("/entry/<id>", methods=["DELETE"])
def delete_entry(id):
entry = Entry.query.get(id)
db.session.delete(entry)
db.session.commit()
return entry_schema.jsonify(entry)
# End point for deleting a entry
if __name__ == '__main__':
app.run(debug = True)
# Push to repo