-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
338 lines (284 loc) · 11.7 KB
/
models.py
File metadata and controls
338 lines (284 loc) · 11.7 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import argparse
import datetime as dt
import os
import sys
import urllib
import random
import string
import re
from sqlalchemy import (Boolean, Column, Date, DateTime, ForeignKey, Integer,
String, Text)
from sqlalchemy.orm import backref, relationship
from sqlalchemy.ext.declarative import declarative_base
from passwordtype import PasswordType
Base = declarative_base()
class Meta(Base):
"""
The meta table is to store information about the database or application.
It is set up as a simple key/value store.
"""
__tablename__ = 'meta'
key = Column(String, primary_key=True)
value = Column(String)
class Staff(Base):
__tablename__ = 'staff'
username = Column(String, primary_key=True)
password = Column(PasswordType)
f_name = Column(String)
l_name = Column(String)
phonenumber = Column(String)
emailaddress = Column(String, unique=True)
bio = Column(Text, default='')
email = Column(Boolean, default=True)
phone = Column(Boolean, default=False)
chat = Column(Boolean, default=False)
irl = Column(Boolean, default=False)
interests = Column(Text, default='')
patron_contacts = relationship('PatronContact',
cascade='all, delete-orphan',
backref=backref('staff',
uselist=False))
def __getitem__(self, attr):
return getattr(self, attr)
@property
def category_list(self):
return set([item.category for item in self.readinglist])
def profile_path(self):
from library import app
uploadsdir = os.path.join(app.static_folder, 'uploads')
# uuid4 regex
# https://stackoverflow.com/a/18359032
fnre = re.compile(r'(.+)-[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}\.jpg')
for fn in os.listdir(uploadsdir):
match = fnre.match(fn)
if match:
fnusername = match.groups(1)[0]
else:
fnusername, _ = os.path.splitext(fn)
if fnusername == self.username:
pic_file_name = 'uploads/%s' % fn
break
else:
pic_file_name = 'uploads/anon.jpg'
return pic_file_name
def full_name(self):
return str(self.f_name) + ' ' + str(self.l_name)
class PasswordReset(Base):
__tablename__ = 'passwordreset'
secret = Column(String, primary_key=True)
created = Column(DateTime, default=dt.datetime.now)
username = Column(String, ForeignKey('staff.username', ondelete='CASCADE'))
staff = relationship('Staff', backref='passwordresets')
def __init__(self, **kwargs):
super(PasswordReset, self).__init__(**kwargs)
if 'secret' not in kwargs:
self.secret = self.gensecret(8)
@classmethod
def gensecret(self, n):
chars = string.ascii_uppercase + string.digits
return ''.join(random.SystemRandom().choice(chars) for _ in range(n))
@property
def valid(self):
# valid if not over an hour old
return (dt.datetime.now() - self.created).total_seconds() / 60 / 60 < 1
class ReadingList(Base):
__tablename__ = 'readinglist'
RLID = Column(Integer, primary_key=True)
ISBN = Column(Text)
recdate = Column(Date)
username = Column(String, ForeignKey('staff.username', ondelete='CASCADE'))
staff = relationship('Staff', backref='readinglist')
book = Column(Text)
author = Column(Text)
comment = Column(Text)
sticky = Column(Boolean, default=False)
category = Column(Text)
class PatronContact(Base):
__tablename__ = 'patroncontact'
PCID = Column(Integer, primary_key=True)
reqdate = Column(Text)
username = Column(String, ForeignKey('staff.username'))
name = Column(Text)
email = Column(Text)
contact = Column(Text) # Contact methods: phone, email, chat, irl, speak (for booking a speaking engagement)
phone = Column(Text)
times = Column(Text)
likes = Column(Text)
dislikes = Column(Text)
comment = Column(Text)
audience = Column(Text)
format_pref = Column(Text)
chat = Column(Text)
handle = Column(Text)
location = Column(Text)
org = Column(Text)
mult = Column(Boolean, default=False) # Tracks if selecting multiply dates for booking a speaking engagement
status = Column(Text)
def __getitem__(self, attr):
return getattr(self, attr)
def init_models(session):
version = Meta(key="DB_VERSION", value="0")
session.add(version)
session.commit()
def init_test_models(session):
admin = Staff(username='admin', password='admin', f_name='Admin', emailaddress='admin@test.com',
l_name='User', phonenumber=1111111111, bio='Admin bio')
session.add(admin)
fred = Staff(username='fred', password='fred', f_name='Fred', emailaddress='KentonCountyLibrary@gmail.com',
l_name='Fredderson', phonenumber=2222222222, bio='I am Fred\'s incomplete bio',
interests='long walks, hobbies, interests, model cars', irl=True,
patron_contacts=[
PatronContact(reqdate='2016-01-07',
status='open',
name='Joe Johnson',
email='jjohanson@bigpimpn.net',
contact='phone',
phone='5555555555',
times='M-Th 12-2 pm'),
])
rl1 = ReadingList(recdate=dt.date(2015,10,1),
ISBN='9780394800301',
book='ABCs',
author='Dr. Suess',
comment='best seller',
category='Mystery')
rl2 = ReadingList(recdate=dt.date(2015,10,2),
ISBN=' 9781402750656',
book='Night Before Christmas',
author='Santa',
comment='find holday fun',
category='Sci-fi')
fred.readinglist.append(rl1)
fred.readinglist.append(rl2)
session.add(fred)
loremipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'''
loreminterests = 'long walks on the beach, hobbies, interests, model cars, real python, meatballs, 80s jock jams, hangin with mr cooper, drawing with chalk, bird watching, trying to make long interests lists, trying not to run out of ideas and failing'
session.add(Staff(username='ernie',
password='ernie',
f_name='Ernie',
l_name='Ernieston',
phonenumber=3333333333,
emailaddress='ernie@test.com',
bio=loremipsum,
interests = loreminterests))
session.add(Staff(username='bert',
password='bert',
f_name='Bert',
l_name='Burterson',
phonenumber=4444444444,
emailaddress='bert@test.com',
bio=loremipsum,
interests = loreminterests))
session.add(Staff(username='bigbird',
password='bigbird',
f_name='Big',
l_name='Bird',
phonenumber=5555555555,
emailaddress='bigbird@test.com',
bio=loremipsum,
interests = loreminterests))
session.add(Staff(username='oscar',
password='oscar',
f_name='Oscar',
l_name='Thegrouch',
phonenumber=6666666666,
emailaddress='oscar@test.com',
bio=loremipsum,
interests = loreminterests))
elmo = Staff(username='elmo',
password='elmo',
f_name='Elmo',
l_name='Elmostein',
phonenumber=7777777777,
emailaddress='elmo@test.com',
bio=loremipsum,
interests = loreminterests)
session.add(elmo)
session.add(PasswordReset(staff=elmo))
session.commit()
session.query(Staff).get('elmo').readinglist = [
ReadingList(
recdate=dt.date(2015,12,21),
ISBN='9789380028293',
book='The Invinsible Man',
author='H. G. Wells',
comment='my fav',
category='History'),
ReadingList(
recdate=dt.date(2015,12,21),
ISBN='9780393972832',
book='Moby Dick',
author='Herman Melville',
comment='a whale of a tale',
category='Music')
]
session.add(Staff(username='elmoo',
password='elmoo',
f_name='Elmo',
l_name='Ornstein',
phonenumber=8888888888,
emailaddress='elmoo@sesamestreet.com',
bio=loremipsum,
interests=loreminterests))
session.commit()
def createdb(args):
"""
Create database.
If a sqlite file can be found in the database URI of the app, prompt to
remove it, if found on the filesystem, and then create it with the database
structure and initial data.
"""
from library import app, db
def raise_unable_to_parse(uri):
raise RuntimeError('unable to parse URI: %s' % uri)
def confirm(msg):
if args.noconfirm:
return True
return raw_input(msg).lower().startswith('y')
def abort():
sys.exit('aborted')
uri = db.engine.url
if 'sqlite' not in db.engine.driver:
raise_unable_to_parse(uri)
dbargs = db.engine.url.translate_connect_args()
path = os.path.abspath(dbargs['database'])
if os.path.exists(path):
if confirm('Remove database file "%s"? ' % path):
os.remove(path)
else:
abort()
else:
if not confirm('Create database file "%s"? ' % os.path.abspath(path)):
abort()
db.engine.echo = args.echo
Base.metadata.create_all(bind=db.engine)
init_models(db.session)
if args.test_models:
init_test_models(db.session)
def dumpschema(args):
"""
Show the database schema produced by the ORM metadata.
"""
from library import db
from sqlalchemy.schema import CreateTable
for class_ in (Staff, PasswordReset, ReadingList, PatronContact):
print CreateTable(class_.__table__).compile(db.engine)
def main():
"""
ORM related command line utility.
"""
parser = argparse.ArgumentParser(description=main.__doc__)
subparsers = parser.add_subparsers()
subparser = subparsers.add_parser('createdb', help=createdb.__doc__)
subparser.add_argument('--noconfirm', action='store_true', help='do not ask for confirmation')
subparser.add_argument('--echo', action='store_true', help='echo SQL')
subparser.add_argument('--test-models', action="store_true", help="Insert test data.")
subparser.set_defaults(func=createdb)
subparser = subparsers.add_parser('dumpschema', help=dumpschema.__doc__)
subparser.set_defaults(func=dumpschema)
args = parser.parse_args()
func = args.func
delattr(args, 'func')
func(args)
if __name__ == '__main__':
main()