-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
301 lines (246 loc) · 10.6 KB
/
models.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
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
"""
SWEatshop Database Models
Note: Pylint does not work well with SQLAlchemy since it is
dynamically generated
"""
# pylint: disable=E1101
# pylint: disable=too-many-arguments
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-instance-attributes
# pylint: disable=invalid-name
from datetime import date
import getpass
from enum import Enum
import flask_whooshalchemyplus
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
APP = Flask(__name__)
APP.config['SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_DATABASE_URI
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
APP.config['TESTING'] = True
APP.config['WTF_CSRF_ENABLED'] = False
if getpass.getuser() == 'www-data': # pragma: no cover
APP.config['WHOOSH_BASE'] = '/home/ubuntu/idb/index-whoosh'
db = SQLAlchemy(APP)
class Ownership(Enum):
"""IPO or not"""
PUBLIC = 'Public'
PRIVATE = 'Private'
investment = db.Table(
'investment',
db.Column('company_id', db.Integer, db.ForeignKey('company.idnum')),
db.Column('investor_id', db.Integer, db.ForeignKey('investor.idnum')))
school_investment = db.Table(
'school_investment',
db.Column('school_id', db.Integer, db.ForeignKey('school.idnum')),
db.Column('investor_id', db.Integer, db.ForeignKey('investor.idnum')))
employment = db.Table(
'employment',
db.Column('person_id', db.Integer, db.ForeignKey('person.idnum')),
db.Column('company_id', db.Integer, db.ForeignKey('company.idnum')))
education = db.Table(
'education',
db.Column('person_id', db.Integer, db.ForeignKey('person.idnum')),
db.Column('school_id', db.Integer, db.ForeignKey('school.idnum')))
company_category = db.Table(
'company_category',
db.Column('category_id', db.Integer, db.ForeignKey('category.idnum')),
db.Column('company_id', db.Integer, db.ForeignKey('company.idnum')))
investor_category = db.Table(
'investor_category',
db.Column('category_id', db.Integer, db.ForeignKey('category.idnum')),
db.Column('investor_id', db.Integer, db.ForeignKey('investor.idnum')))
class Person(db.Model):
"""Person Model"""
__tablename__ = 'person'
idnum = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
title = db.Column(db.String(200), nullable=True)
location = db.Column(db.String(500), nullable=True)
dob = db.Column(db.DateTime, nullable=True)
image_url = db.Column(db.String(512), nullable=True)
website = db.Column(db.String(512), nullable=True)
companies = db.relationship('Company', secondary=employment,
backref=db.backref('employees', lazy='dynamic'))
schools = db.relationship('School', secondary=education,
backref=db.backref('alumni', lazy='dynamic'))
crunch_id = db.Column(db.String(25), nullable=True)
country = db.Column(db.String(50), nullable=True)
description = db.Column(db.String(10000), nullable=True)
__searchable__ = ['name', 'title', 'location', 'website',
'companies', 'schools', 'country', 'description']
def __init__(self, name, title, location, dob, image_url, website):
"""Initializes a Person, pass in dob as datetime object"""
# Name not nullable
assert name is not None and isinstance(name, str) and len(name) <= 50
assert title is None or (isinstance(title, str) and len(title) <= 200)
assert location is None or (
isinstance(location, str) and len(name) <= 500)
assert dob is None or isinstance(dob, date)
assert image_url is None or (
isinstance(image_url, str) and len(name) <= 512)
assert website is None or (
isinstance(website, str) and len(name) <= 512)
self.name = name
self.title = title
self.location = location
self.dob = dob
self.image_url = image_url
self.website = website
def __repr__(self):
return '<Person %s>' % self.name
@classmethod
def get_all_rows(cls):
"""Get all person rows"""
return cls.query.all()
class Company(db.Model):
"""Company Model"""
__tablename__ = 'company'
idnum = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
location = db.Column(db.String(500), nullable=True)
# ownership_type = db.Column(db.Enum(Ownership), nullable=True)
ownership_type = db.Column(db.String(50), nullable=False)
funding = db.Column(db.Integer, nullable=True)
description = db.Column(db.String(10000), nullable=True)
ceo_id = db.Column(
db.Integer, db.ForeignKey('person.idnum'), nullable=True)
image_url = db.Column(db.String(512), nullable=True)
size = db.Column(db.Integer, nullable=True)
website = db.Column(db.String(512), nullable=True)
country = db.Column(db.String(50), nullable=True)
investors = db.relationship('Investor', secondary=investment,
backref=db.backref('companies', lazy='dynamic'))
crunch_id = db.Column(db.String(25), nullable=True)
__searchable__ = ['name', 'location',
'funding', 'website', 'country', 'description', 'investors']
def __init__(self, name, location, ownership_type, funding, description,
ceo_id, image_url, size, website):
"""Initializes Company, ceo as foreign key and ownership as enum"""
# Name not nullable
assert name is not None and isinstance(name, str) and len(name) <= 50
assert location is None or (
isinstance(location, str) and len(name) <= 500)
assert funding is None or isinstance(funding, int)
assert description is None or (
isinstance(description, str) and len(description) <= 10000)
assert ceo_id is None or isinstance(ceo_id, int)
assert image_url is None or (
isinstance(image_url, str) and len(image_url) <= 512)
assert size is None or isinstance(size, int)
assert website is None or (
isinstance(website, str) and len(website) <= 512)
self.name = name
self.location = location
self.ownership_type = ownership_type
self.funding = funding
self.description = description
self.ceo_id = ceo_id
self.image_url = image_url
self.size = size
self.website = website
def __repr__(self):
return '<Company %s>' % self.name
@classmethod
def get_all_rows(cls):
"""Get all company rows"""
return cls.query.all()
class School(db.Model):
"""School Model"""
__tablename__ = 'school'
idnum = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False)
location = db.Column(db.String(50), nullable=True)
description = db.Column(db.String(10000), nullable=True)
size = db.Column(db.Integer, nullable=True)
image_url = db.Column(db.String(512), nullable=True)
website = db.Column(db.String(512), nullable=True)
country = db.Column(db.String(200), nullable=True)
investors = db.relationship('Investor', secondary=school_investment,
backref=db.backref('schools', lazy='dynamic'))
__searchable__ = [
'name', 'location', 'website', 'country', 'investors', 'description']
def __init__(self, name, location, description, image_url, size, website):
"""Initializes School"""
# Name not nullable
assert name is not None and isinstance(name, str) and len(name) <= 150
assert location is None or (
isinstance(location, str) and len(name) <= 50)
assert description is None or (
isinstance(description, str) and len(description) <= 10000)
assert image_url is None or (
isinstance(image_url, str) and len(image_url) <= 512)
assert size is None or isinstance(size, int)
assert website is None or (
isinstance(website, str) and len(website) <= 512)
self.name = name
self.location = location
self.description = description
self.image_url = image_url
self.size = size
self.website = website
def __repr__(self):
return '<School %s>' % self.name
@classmethod
def get_all_rows(cls):
"""Get all school rows"""
return cls.query.all()
class Investor(db.Model):
"""Investor Model"""
__tablename__ = 'investor'
idnum = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
location = db.Column(db.String(50), nullable=True)
funding = db.Column(db.Integer, nullable=True)
description = db.Column(db.String(10000), nullable=True)
image_url = db.Column(db.String(512), nullable=True)
country = db.Column(db.String(50), nullable=True)
website = db.Column(db.String(512), nullable=True)
__searchable__ = [
'name', 'location', 'funding', 'website', 'country', 'description']
def __init__(self, name, location, funding, description, image_url, website):
"""Initializes Investor"""
# Name not nullable
assert name is not None and isinstance(name, str) and len(name) <= 150
assert location is None or (
isinstance(location, str) and len(name) <= 50)
assert funding is None or (isinstance(funding, int))
assert description is None or(
isinstance(description, str) and len(description) <= 10000)
assert image_url is None or (
isinstance(image_url, str) and len(image_url) <= 512)
assert website is None or (
isinstance(website, str) and len(website) <= 512)
self.name = name
self.location = location
self.funding = funding
self.description = description
self.image_url = image_url
self.website = website
def __repr__(self):
return '<Investor %s>' % self.name
@classmethod
def get_all_rows(cls):
"""Get all investor rows"""
return cls.query.all()
class Category(db.Model):
"""Categories"""
__tablename__ = 'category'
idnum = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
def __init__(self, name):
assert name is not None
assert isinstance(name, str) and len(name) <= 50
self.name = name
def __repr__(self):
return '<Category %s>' % self.name
@classmethod
def get_all_rows(cls):
"""Get all category rows"""
return cls.query.all()
if getpass.getuser() == 'www-data': # pragma: no cover
flask_whooshalchemyplus.init_app(APP)
if __name__ == "__main__": # pragma: no cover
with APP.app_context():
flask_whooshalchemyplus.index_all(APP)