-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (47 loc) · 1.56 KB
/
main.py
File metadata and controls
66 lines (47 loc) · 1.56 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
import os
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///practical.sql"
app.config["TESTING"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
"""
Data models
"""
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(), nullable=False)
lastname = db.Column(db.String(), nullable=False)
def __repr__(self):
return "<Author %s, %s>" % (self.lastname, self.firstname)
def as_dict(self):
return {"lastname": self.lastname, "firstname": self.firstname}
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(), unique=True, nullable=False)
author_id = db.Column(db.Integer, db.ForeignKey("author.id"), nullable=False)
author = db.relationship("Author", backref=db.backref("articles", lazy=True))
def __repr__(self):
return "<Article %s (%r)>" % (self.title, self.author)
def as_dict(self):
return {"title": self.title, "author": self.author.as_dict()}
"""
Flask endpoints
"""
@app.route("/articles.json")
def articles():
"""Implement this first."""
raise Exception("please implement")
@app.route("/article.json")
def article():
"""Implement this second."""
raise Exception("please implement")
"""
Flask application
"""
if __name__ == "__main__":
if not os.path.isfile("practical.sql"):
with app.app_context():
db.create_all()
app.run()