forked from CincyPy/LibraryWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
226 lines (188 loc) · 7.64 KB
/
models.py
File metadata and controls
226 lines (188 loc) · 7.64 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
import argparse
import datetime
import os
import sys
from sqlalchemy import Boolean, Column, Date, ForeignKey, Integer, String, Text
from sqlalchemy.orm import backref, relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Staff(Base):
__tablename__ = 'staff'
username = Column(String, primary_key=True)
password = Column(String)
f_name = Column(String)
l_name = Column(String)
phonenumber = Column(String)
emailaddress = Column(String)
bio = Column(Text, default='')
email = Column(Boolean, default=False)
phone = Column(Boolean, default=True)
chat = Column(Boolean, default=False)
irl = Column(Boolean, default=True)
interests = Column(Text)
patron_contacts = relationship('PatronContact', 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):
if os.path.isfile('static/uploads/' + self.username + '.jpg'):
pic_file_name = 'uploads/'+ self.username + '.jpg'
else:
pic_file_name = 'uploads/anon.jpg'
return pic_file_name
class ReadingList(Base):
__tablename__ = 'readinglist'
RLID = Column(Integer, primary_key=True)
ISBN = Column(Text)
recdate = Column(Date)
username = Column(String, ForeignKey('staff.username'))
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)
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)
status = Column(Text)
def __getitem__(self, attr):
return getattr(self, attr)
def init_models(session):
admin = Staff(username='admin', password='admin', f_name='Admin', emailaddress='KentonCountyLibrary@gmail.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',
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=datetime.date(2015,10,1),
ISBN='9780394800301',
book='ABCs',
author='Dr. Suess',
comment='best seller',
category='Mystery')
rl2 = ReadingList(recdate=datetime.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.'''
session.add(Staff(username='ernie',
password='ernie',
f_name='Ernie',
l_name='Ernieston',
phonenumber=3333333333,
emailaddress='KentonCountyLibrary@gmail.com',
bio=loremipsum))
session.add(Staff(username='bert',
password='bert',
f_name='Bert',
l_name='Burterson',
phonenumber=4444444444,
emailaddress='KentonCountyLibrary@gmail.com',
bio=loremipsum))
session.add(Staff(username='bigbird',
password='bigbird',
f_name='Big',
l_name='Bird',
phonenumber=5555555555,
emailaddress='KentonCountyLibrary@gmail.com',
bio=loremipsum))
session.add(Staff(username='oscar',
password='oscar',
f_name='Oscar',
l_name='Thegrouch',
phonenumber=6666666666,
emailaddress='KentonCountyLibrary@gmail.com',
bio=loremipsum))
session.add(Staff(username='elmo',
password='elmo',
f_name='Elmo',
l_name='Elmostein',
phonenumber=7777777777,
emailaddress='KentonCountyLibrary@gmail.com',
bio=loremipsum))
session.commit()
session.query(Staff).get('elmo').readinglist = [
ReadingList(
recdate=datetime.date(2015,12,21),
ISBN='9789380028293',
book='The Invinsible Man',
author='H. G. Wells',
comment='my fav',
category='History'),
ReadingList(
recdate=datetime.date(2015,12,21),
ISBN='9780393972832',
book='Moby Dick',
author='Herman Melville',
comment='a whale of a tale',
category='Music')
]
session.commit()
def ArgParser():
parser = argparse.ArgumentParser(description=main.__doc__)
_a = parser.add_argument
_a('--echo', action='store_true', help='echo SQL')
return parser
def main():
"""
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
parser = ArgParser()
cmdargs = parser.parse_args()
def raise_unable_to_parse(uri):
raise RuntimeError('unable to parse URI: %s' % uri)
def confirm(msg):
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 = cmdargs.echo
Base.metadata.create_all(bind=db.engine)
init_models(db.session)
if __name__ == '__main__':
main()