-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
61 lines (48 loc) · 2.36 KB
/
Copy pathmodels.py
File metadata and controls
61 lines (48 loc) · 2.36 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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from enum import Enum
import bcrypt
db = SQLAlchemy()
class UserRole(Enum):
ADMIN = "admin"
PROJECT_MANAGER = "project_manager"
COORDINATOR = "coordinator"
class TaskStatus(Enum):
TODO = "todo"
IN_PROGRESS = "in_progress"
COMPLETED = "completed"
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True, nullable=False)
username = db.Column(db.String(100), unique=True, nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
role = db.Column(db.Enum(UserRole), nullable=False, default=UserRole.COORDINATOR)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Relationships
owned_projects = db.relationship('Project', backref='owner', lazy=True)
assigned_tasks = db.relationship('Task', backref='assignee', lazy=True)
def set_password(self, password):
self.password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password_hash.encode('utf-8'))
class Project(db.Model):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
description = db.Column(db.Text)
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
tasks = db.relationship('Task', backref='project', lazy=True, cascade='all, delete-orphan')
class Task(db.Model):
__tablename__ = 'tasks'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
description = db.Column(db.Text)
status = db.Column(db.Enum(TaskStatus), nullable=False, default=TaskStatus.TODO)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), nullable=False)
assignee_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)