-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
105 lines (85 loc) · 4.15 KB
/
Copy pathmodels.py
File metadata and controls
105 lines (85 loc) · 4.15 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
from datetime import datetime, timedelta
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from database import db
WEEKDAY_NAMES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True, nullable=False, index=True)
name = db.Column(db.String(100), nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
habits = db.relationship('Habit', backref='owner', lazy=True, cascade='all, delete-orphan')
notes = db.relationship('Note', backref='owner', lazy=True, cascade='all, delete-orphan')
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def to_dict(self):
return {'id': self.id, 'email': self.email, 'name': self.name}
class Habit(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
name = db.Column(db.String(100), nullable=False)
category = db.Column(db.String(50), nullable=False, default='General')
color = db.Column(db.String(20), nullable=False, default='#2F5D50')
icon = db.Column(db.String(50), nullable=True) # key into the front-end icon set
frequency = db.Column(db.String(20), nullable=False, default='Daily') # Daily, Weekdays, Weekends, Custom
custom_days = db.Column(db.String(20), nullable=True) # "0,2,4" (Mon=0 ... Sun=6), used when frequency='Custom'
archived = db.Column(db.Boolean, nullable=False, default=False)
sort_order = db.Column(db.Integer, nullable=False, default=0)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
completions = db.relationship('Completion', backref='habit', lazy=True, cascade='all, delete-orphan')
def is_due_on(self, d):
"""Whether this habit is scheduled for calendar date d."""
weekday = d.weekday() # Monday = 0 ... Sunday = 6
if self.frequency == 'Daily':
return True
if self.frequency == 'Weekdays':
return weekday <= 4
if self.frequency == 'Weekends':
return weekday >= 5
if self.frequency == 'Custom':
if not self.custom_days:
return False
days = {int(x) for x in self.custom_days.split(',') if x != ''}
return weekday in days
return True
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'category': self.category,
'color': self.color,
'icon': self.icon,
'frequency': self.frequency,
'custom_days': [int(x) for x in self.custom_days.split(',')] if self.custom_days else [],
'archived': self.archived,
'sort_order': self.sort_order,
'created_at': self.created_at.isoformat() if self.created_at else None,
}
class Completion(db.Model):
id = db.Column(db.Integer, primary_key=True)
habit_id = db.Column(db.Integer, db.ForeignKey('habit.id'), nullable=False)
date = db.Column(db.Date, nullable=False)
completed = db.Column(db.Boolean, default=True)
__table_args__ = (db.UniqueConstraint('habit_id', 'date', name='uq_habit_date'),)
def to_dict(self):
return {
'id': self.id,
'habit_id': self.habit_id,
'date': self.date.isoformat() if self.date else None,
'completed': self.completed,
}
class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
date = db.Column(db.Date, nullable=False)
content = db.Column(db.Text, nullable=False)
__table_args__ = (db.UniqueConstraint('user_id', 'date', name='uq_user_date'),)
def to_dict(self):
return {
'id': self.id,
'date': self.date.isoformat() if self.date else None,
'content': self.content,
}