-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
51 lines (40 loc) · 1.92 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
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
import json
from werkzeug.security import generate_password_hash
from dataclasses import dataclass
USERS_PATH = "default_users.json"
db = SQLAlchemy()
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
has_private_profile = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
def to_dict(self):
return dict(username=self.username)
def get_id(self):
return str(self.id)
def create_default_users():
with open(USERS_PATH, 'r') as f:
data = json.load(f)
for user in data:
if not User.query.filter_by(username=user['username']).first():
new_user = User(username=user['username'], password=user['password'])
new_user.password = generate_password_hash(new_user.password)
db.session.add(new_user)
db.session.commit()
class Task(db.Model):
__tablename__ = "tasks"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.String(1000), nullable=False)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
is_completed = db.Column(db.Boolean, default=False)
def to_dict(self):
return dict(title=self.title, content=self.content, is_completed=self.is_completed)