-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.py
247 lines (216 loc) · 5.56 KB
/
example.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'''
flask-ponywhoosh example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Perform full-text searches over your database with Pony ORM and PonyWhoosh,
for flask applications.
:copyright: (c) 2015-2018 by Jonathan Prieto-Cubides & Felipe Rodriguez.
:license: MIT (see LICENSE.md)
'''
import os
from datetime import datetime, timedelta, date
from flask import Flask, jsonify, render_template
from flask_bootstrap import Bootstrap
from flask_ponywhoosh import PonyWhoosh
from flask_script import Manager, Shell
from pony.orm import *
from pony.orm.serialization import to_json
app = Flask(__name__)
app.debug = True
manager = Manager(app)
manager.add_command("shell", Shell(use_bpython=True))
# -----------------------------------------------------------------------------
#
# Options for PonyWhoosh package
#
# -----------------------------------------------------------------------------
app.config['PONYWHOOSH_DEBUG'] = True
app.config['PONYWHOOSH_DIR'] = 'whooshes'
app.config['PONYWHOOSH_MIN_STRING_LEN'] = 1
app.config['PONYWHOOSH_URL_ROUTE'] = '/'
app.config['PONYWHOOSH_WRITER_TIMEOUT'] = 3
app.config['SECRET_KEY'] = 'hard to guess string'
# -----------------------------------------------------------------------------
bootstrap = Bootstrap(app)
db = Database()
pw = PonyWhoosh(app)
@pw.register_model('number', 'name')
class Department(db.Entity):
number = PrimaryKey(int, auto=True)
name = Required(str, unique=True)
groups = Set("Group")
courses = Set("Course")
@pw.register_model('number', 'major')
class Group(db.Entity):
number = PrimaryKey(int)
major = Required(str)
dept = Required("Department")
students = Set("Student")
@pw.register_model('name', 'semester', 'lect_hours', 'lab_hours', 'credits')
class Course(db.Entity):
name = Required(str)
semester = Required(int)
lect_hours = Required(int)
lab_hours = Required(int)
credits = Required(int)
dept = Required(Department)
students = Set("Student")
PrimaryKey(name, semester)
@pw.register_model('name', 'tel', 'gpa')
class Student(db.Entity):
id = PrimaryKey(int, auto=True)
name = Required(str)
dob = Required(date)
tel = Optional(str)
picture = Optional(buffer, lazy=True)
gpa = Required(float, default=0)
group = Required(Group)
courses = Set(Course)
db.bind('sqlite', 'example.sqlite', create_db=True)
#db.bind('mysql', host="localhost", user="pony", passwd="pony", db="university1")
#db.bind('postgres', user='pony', password='pony', host='localhost'
# , database='university1')
#db.bind('oracle', 'university1/pony@localhost')
db.generate_mapping(create_tables=True)
@db_session
def populate_database():
if select(s for s in Student).count() > 0: return
d1 = Department(name="Department of Computer Science")
d2 = Department(name="Department of Mathematical Sciences")
d3 = Department(name="Department of Applied Physics")
c1 = Course(
name="Web Design"
, semester=1
, dept=d1
, lect_hours=30
, lab_hours=30
, credits=3
)
c2 = Course(
name="Data Structures and Algorithms"
, semester=3
, dept=d1
, lect_hours=40
, lab_hours=20
, credits=4
)
c3 = Course(
name="Linear Algebra"
, semester=1
, dept=d2
, lect_hours=30
, lab_hours=30
, credits=4
)
c4 = Course(
name="Statistical Methods"
, semester=2
, dept=d2
, lect_hours=50
, lab_hours=25
, credits=5
)
c5 = Course(
name="Thermodynamics"
, semester=2
, dept=d3
, lect_hours=25
, lab_hours=40
, credits=4
)
c6 = Course(
name="Quantum Mechanics"
, semester=3
, dept=d3
, lect_hours=40
, lab_hours=30
, credits=5
)
g101 = Group(
number=101
, major='B.E. in Computer Engineering'
, dept=d1
)
g102 = Group(
number=102
, major='B.S./M.S. in Computer Science'
, dept=d1
)
g103 = Group(
number=103
, major='B.S. in Applied Mathematics and Statistics'
, dept=d2
)
g104 = Group(
number=104
, major='B.S./M.S. in Pure Mathematics'
, dept=d2
)
g105 = Group(
number=105
, major='B.E in Electronics'
, dept=d3
)
g106 = Group(
number=106
, major='B.S./M.S. in Nuclear Engineering'
, dept=d3
)
s1 = Student(
name='John Smith'
, dob=date(1991, 3, 20)
, tel='123-456'
, gpa=3
, group=g101
, courses=[c1, c2, c4, c6]
)
s2 = Student(
name='Matthew Reed'
, dob=date(1990, 11, 26)
, gpa=3.5
, group=g101
, courses=[c1, c3, c4, c5]
)
s3 = Student(
name='Chuan Qin'
, dob=date(1989, 2, 5)
, gpa=4
, group=g101
, courses=[c3, c5, c6]
)
s4 = Student(
name='Rebecca Lawson'
, dob=date(1990, 4, 18)
, tel='234-567'
, gpa=3.3
, group=g102
, courses=[c1, c4, c5, c6]
)
s5 = Student(
name='Maria Ionescu'
, dob=date(1991, 4, 23)
, gpa=3.9
, group=g102
, courses=[c1, c2, c4, c6]
)
s6 = Student(
name='Oliver Blakey'
, dob=date(1990, 9, 8)
, gpa=3.1
, group=g102
, courses=[c1, c2, c5]
)
s7 = Student(
name='Jing Xia'
, dob=date(1988, 12, 30)
, gpa=3.2
, group=g102
, courses=[c1, c3, c5, c6]
)
commit()
@app.route("/database")
@db_session
def index():
return to_json(Student.select())
if __name__ == "__main__":
populate_database()
manager.run()