-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
63 lines (45 loc) · 1.58 KB
/
model.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
import sys
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from faker import Factory
from bdays import get_birthdays
from env import SECRET_KEY
THIS_YEAR = 2017
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///birthdays.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # mute warnings
app.secret_key = SECRET_KEY
db = SQLAlchemy(app)
class Birthday(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120))
bday = db.Column(db.DateTime)
phone = db.Column(db.String(20))
def __init__(self, name, bday, phone=None):
self.name = name
self.bday = bday
self.phone = phone
def __repr__(self):
return '<Birthday %r %r %r>' % (self.name,
self.bday,
self.phone)
if __name__ == '__main__':
# if ran as script create the birthday table and load in all birthdays
test_mode = False
if len(sys.argv) > 1 and '-t' in sys.argv[1].lower():
test_mode = True
fake = Factory.create()
db.drop_all()
db.create_all()
for bd in sorted(get_birthdays('cal.ics'),
key=lambda x: (x.bday.month, x.bday.day)):
# test_mode = no real names
if test_mode:
name = fake.name()
else:
name = bd.name
# import all bdays with THIS_YEAR to make it easier to query later
bday = bd.bday.replace(year=THIS_YEAR)
bd_obj = Birthday(name, bday)
db.session.add(bd_obj)
db.session.commit()