-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
179 lines (125 loc) · 4.43 KB
/
app.py
File metadata and controls
179 lines (125 loc) · 4.43 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# ============================================
# IMPORT LIBRARIES
# ============================================
import json
from pathlib import Path
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
# ============================================
# CREATE FLASK APP
# ============================================
app = Flask(__name__)
# ============================================
# DATABASE CONFIGURATION
# ============================================
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///dhammapada.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# ============================================
# INITIALIZE EXTENSIONS
# ============================================
db = SQLAlchemy(app)
ma = Marshmallow(app)
BASE_DIR = Path(__file__).resolve().parent
DHAMMAPADA_JSON_PATH = BASE_DIR / "dhammapada.json"
# ============================================
# DATABASE MODELS
# ============================================
class Chapter(db.Model):
"""
Chapter table represents a chapter of Dhammapada.
"""
id = db.Column(db.Integer, primary_key=True)
number = db.Column(db.Integer, nullable=False, unique=True)
title = db.Column(db.String(200), nullable=False)
verses = db.relationship("Verse", backref="chapter", lazy=True)
class Verse(db.Model):
"""
Verse table stores each verse text.
"""
id = db.Column(db.Integer, primary_key=True)
verse_number = db.Column(db.Integer, nullable=False)
text = db.Column(db.Text, nullable=False)
chapter_id = db.Column(db.Integer, db.ForeignKey("chapter.id"), nullable=False)
# ============================================
# MARSHMALLOW SCHEMA
# ============================================
class VerseSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Verse
load_instance = True
class ChapterSchema(ma.SQLAlchemyAutoSchema):
verses = ma.Nested(VerseSchema, many=True)
class Meta:
model = Chapter
load_instance = True
chapter_schema = ChapterSchema()
chapters_schema = ChapterSchema(many=True)
verse_schema = VerseSchema()
verses_schema = VerseSchema(many=True)
# ============================================
# DATA IMPORT
# ============================================
def import_dhammapada(json_path=DHAMMAPADA_JSON_PATH):
"""
Import chapters and verses from a JSON file.
"""
with json_path.open("r", encoding="utf-8") as file:
data = json.load(file)
chapters = data.get("chapters", [])
for chapter_data in chapters:
chapter = Chapter(
number=chapter_data["number"],
title=chapter_data["title"],
)
db.session.add(chapter)
db.session.flush()
verses = [
Verse(
verse_number=verse_data["verse_number"],
text=verse_data["text"],
chapter_id=chapter.id,
)
for verse_data in chapter_data.get("verses", [])
]
db.session.add_all(verses)
db.session.commit()
# ============================================
# API ROUTES (ONLY GET)
# ============================================
@app.route("/")
def home():
chapters = Chapter.query.order_by(Chapter.number).all()
return chapters_schema.jsonify(chapters)
@app.route("/chapters")
def get_chapters():
chapters = Chapter.query.order_by(Chapter.number).all()
return chapters_schema.jsonify(chapters)
@app.route("/chapters/<int:id>")
def get_chapter(id):
chapter = Chapter.query.get_or_404(id)
return chapter_schema.jsonify(chapter)
@app.route("/verses")
def get_verses():
verses = Verse.query.order_by(Verse.chapter_id, Verse.verse_number).all()
return verses_schema.jsonify(verses)
@app.route("/verses/<int:id>")
def get_verse(id):
verse = Verse.query.get_or_404(id)
return verse_schema.jsonify(verse)
@app.route("/chapters/<int:chapter_id>/verses")
def get_chapter_verses(chapter_id):
verses = Verse.query.filter_by(chapter_id=chapter_id).order_by(Verse.verse_number).all()
return verses_schema.jsonify(verses)
# ============================================
# CREATE DATABASE
# ============================================
with app.app_context():
db.create_all()
if Chapter.query.count() == 0:
import_dhammapada()
# ============================================
# RUN SERVER
# ============================================
if __name__ == "__main__":
app.run(debug=True)