forked from yfalcon8/relationship_manager_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
320 lines (215 loc) · 10.3 KB
/
model.py
File metadata and controls
320 lines (215 loc) · 10.3 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
"""The muscle of my database. Models and database functions for contacts db."""
# SQLAlchemy is a Python-based Object Relational Model/Mapper (ORM). Helps me navigate my relational database and
# transforms Python into SQL.
import datetime
from flask_sqlalchemy import SQLAlchemy
# Here's where the idea of my database is created. This is pulled from my
# Flask-SQLAlchemy library. db allows me to find the 'session' object where
# the majority of my database interactions will occur (such as committing,
# querying, etc.)
db = SQLAlchemy()
# Create my ORM. Allows for object-orientation in SQL.
# All of my models will subclass db.Model.
# This declares that a class will be managed by SQLAlchemy.
# db is the object and Model is a class.
# The Model class contains the __init__() method, so I don't need to include it.
class User(db.Model):
"""Stores information about my users."""
# The instances of this class will be stored in a table named users.
__tablename__ = "users"
# db.Column creates a column in the users table called user_id. db.Integer
# specifies the type of column. This column sets my primary key to be
# an automatically increasing number.
id = db.Column(db.Integer,
primary_key=True,
autoincrement=True)
# If the user signs up with Facebook, grab their Facebook_user_id.
# This field is useful for when users who didn't store their email with
# FB wants to sign-in to my app. FB may not return an email address, but
# the user will have an email address stored in my database.
fb_id = db.Column(db.String(20),
unique=True)
# 'String' is the SQLAlchemy-managed version of the data type.
# It indicates that the data type of this column can contain
# letters, numbers and special characters.
# The (30) indicates that this field will fit a max of 30 characters.
first_name = db.Column(db.String(30),
nullable=False)
# The nullability of this field is set to False, as I want to require my
# users to input their name.
last_name = db.Column(db.String(30),
nullable=False)
# The users email address will be their username for their login. Users
# must have different usernames, meaning they must be unique.
email = db.Column(db.String(50),
nullable=False,
unique=True)
password = db.Column(db.String(20),
nullable=False)
def __repr__(self):
return "<User: {} {}, {}, {}>".format(self.first_name,
self.last_name,
self.email,
self.fb_id)
class Recommendation(db.Model):
"""Stores information about my users."""
__tablename__ = "recommendations"
id = db.Column(db.Integer,
primary_key=True,
autoincrement=True)
# The default is for those users who do not feel like specifying the type
# relationship they have with the contact they just imported. There will
# be a default set of tips on how to reach out.
relatp_type = db.Column(db.String(3),
default='fr')
rcmdn = db.Column(db.Text,
nullable=False)
# Join the recommendation table and relationship table through the
# relatp_code. This allows me to navigate from the a user's contact to his/her
# associated recommendations and vice versa.
relatp = db.relationship("Relationship",
secondary='rcmdns_relatps')
def __repr__(self):
return "<Recommendation: relatp_type={}, rcmdn={}>".format(self.relatp_type,
self.rcmdn)
class Event(db.Model):
"""Stores information about each event."""
__tablename__ = "events"
id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
user_id = db.Column(db.Integer,
db.ForeignKey('users.id'))
scheduled_at = db.Column(db.DateTime)
relatp_id = db.Column(db.Integer,
db.ForeignKey('relationships.id'))
rcmdn = db.Column(db.Text,
nullable=False)
user = db.relationship('User', backref=db.backref('event'))
relationship = db.relationship('Relationship', backref=db.backref('events'))
def __repr__(self):
return "<Event: id={}, user_id={}, scheduled_at={}, relatp_id={}, rcmdn={}>".format(self.id,
self.user_id,
self.scheduled_at,
self.relatp_id,
self.rcmdn)
class Relationship(db.Model):
"""Stores all of a users' contacts info."""
__tablename__ = "relationships"
id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
first_name = db.Column(db.String(30),
nullable=False)
last_name = db.Column(db.String(30))
relatp_type = db.Column(db.String(3),
nullable=False,
default='fr')
user_id = db.Column(db.Integer,
db.ForeignKey('users.id'))
created_date = db.Column(db.DateTime,
default=datetime.datetime.now())
rcmdn_list = db.Column(db.Text)
email = db.Column(db.String(50))
bday = db.Column(db.Date)
phone = db.Column(db.String(15))
work = db.Column(db.String(50))
edu = db.Column(db.String(50))
fb = db.Column(db.String(50))
linked_in = db.Column(db.String(50))
twitter = db.Column(db.String(50))
google_plus = db.Column(db.String(50))
github = db.Column(db.String(50))
pinterest = db.Column(db.String(50))
word_press = db.Column(db.String(50))
yelp = db.Column(db.String(50))
skype = db.Column(db.String(50))
other_social_media = db.Column(db.String(50))
gift_idea = db.Column(db.Text)
goal = db.Column(db.Text)
note = db.Column(db.Text)
pet = db.Column(db.Text)
family = db.Column(db.Text)
hobby = db.Column(db.Text)
likes = db.Column(db.Text)
dislike = db.Column(db.Text)
pet_peeve = db.Column(db.Text)
fav_food = db.Column(db.Text)
fav_drink = db.Column(db.Text)
fav_restaurant = db.Column(db.Text)
sports_team = db.Column(db.Text)
fav_brand = db.Column(db.Text)
other_fav = db.Column(db.Text)
convo = db.Column(db.Text)
trait = db.Column(db.Text)
user = db.relationship("User", backref=db.backref("relationships"))
recommendations = db.relationship("Recommendation",
secondary='rcmdns_relatps')
def __repr__(self):
"""Provide useful information about the relationship."""
return "<Relationship: id={}, first_name={}, last_name={}, created_date={}\
relatp_type={}, user_id={}, rcmdn_list={}, email={}, bday={}, phone={},\
work={}, edu={}, fb={}, linked_in={}, twitter={}, google_plus={},\
github={}, pinterest={}, word_press={}, yelp={}, skype={},\
other_social_media={}, gift_idea={}, goal={}, note={}, pet={},\
family={}, hobby={}, likes={}, dislike={}, pet_peeve={}, fav_food={},\
fav_drink={}, fav_restaurant={}, sports_team={}, fav_brand={},\
other_fav={}, convo={}, trait={}>".format(
self.id, self.first_name, self.last_name, self.created_date,
self.relatp_type, self.user_id, self.rcmdn_list, self.email, self.bday,
self.phone, self.work, self.edu, self.fb, self.linked_in, self.twitter,
self.google_plus, self.github, self.pinterest, self.word_press,
self.yelp, self.skype, self.other_social_media, self.gift_idea,
self.goal, self.note, self.pet, self.family, self.hobby, self.likes,
self.dislike, self.pet_peeve, self.fav_food, self.fav_drink,
self.fav_restaurant, self.sports_team, self.sports_team, self.fav_brand,
self.other_fav, self.convo, self.trait)
class RecommendationRelationship(db.Model):
"""Association table between recommendation and relationship table.
Describes the method of reaching out that's tied to each contact.
"""
__tablename__ = "rcmdns_relatps"
rcmdnRelatp_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
rcmdn_id = db.Column(db.Integer,
db.ForeignKey('recommendations.id'),
nullable=False)
relatp_id = db.Column(db.Integer,
db.ForeignKey('relationships.id'),
nullable=False)
def example_data():
"""This function exists to populate testable data in the database.
It is safer to test a sample database instead of testing an apps actual
database."""
user = User(id=1,
first_name='Gabriel',
last_name='Macht',
email='gabriel@gmail.com',
password='gabriel#1')
recommendation = Recommendation(relatp_type='fr',
rcmdn='Send a text')
friend = Relationship()
db.session.add(user)
db.session.commit()
# This set up allows my app the ability to talk to SQLite, PostgreSQL, MySQL
# among others.
##########################
#### Helper Functions ####
##########################
# When running my actual db, db_uri="postgresql:///contacts".
# For testing purposes, db_uri="postgresql:///testdb".
def connect_to_db(app, db_uri="postgresql:///contacts"):
"""Connect to database."""
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == "__main__":
"""This is useful for running this module interactively. This will leave me
in a state of being able to work with the database directly."""
from server import app
# Heroku configuration
# connect_to_db(app)
connect_to_db(app, "postgresql:///contacts")
print "Connected to DB."