forked from programmable-web-project-unioulu/PWP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
695 lines (607 loc) · 23.3 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import uuid
from datetime import datetime
from enum import Enum
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash
# Initialize SQLAlchemy
db = SQLAlchemy()
# User Model
class User(db.Model):
"""
User model represents a user in the system. It includes information such as username, email, password,
role, and timestamps for account creation and last login.
"""
__tablename__ = "USER"
user_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
username = db.Column(db.String(255), unique=True, nullable=False)
email = db.Column(db.String(255), unique=True, nullable=False)
password_hash = db.Column(db.Text, nullable=False)
role = db.Column(db.String(50), default="member")
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
last_login = db.Column(db.DateTime)
teams = db.relationship("Team", backref="leader", lazy=True, cascade="save-update")
tasks = db.relationship(
"Task",
foreign_keys="Task.assignee_id",
backref="assignee",
lazy=True,
cascade="save-update",
)
created_tasks = db.relationship(
"Task", foreign_keys="Task.created_by", backref="creator", lazy=True
)
updated_tasks = db.relationship(
"Task", foreign_keys="Task.updated_by", backref="updater", lazy=True
)
memberships = db.relationship(
"TeamMembership", backref="user", lazy=True, cascade="all, delete-orphan"
)
def to_dict(self):
"""
Convert the User object into a dictionary format suitable for JSON serialization.
Returns:
dict: Dictionary containing user information.
"""
try:
return {
"user_id": str(self.user_id), # Convert UUID to string
"username": self.username,
"email": self.email,
"role": self.role,
"created_at": (
self.created_at.isoformat() if self.created_at else None
), # Format datetime
"last_login": self.last_login.isoformat() if self.last_login else None,
"_links": {"self": f"/users/{self.user_id}"},
}
except Exception as e:
return {"error": f"Error serializing user: {str(e)}"}
# Team Model
class Team(db.Model):
"""
Team model represents a team within the system. It includes information such as team name, description,
and a reference to the team lead (user).
"""
__tablename__ = "TEAM"
team_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
lead_id = db.Column(UUID(as_uuid=True), db.ForeignKey("USER.user_id", ondelete="SET NULL"))
projects = db.relationship("Project", backref="team", lazy=True, cascade="all, delete-orphan")
memberships = db.relationship(
"TeamMembership", backref="team", lazy=True, cascade="all, delete-orphan"
)
def to_dict(self):
"""
Convert the Team object into a dictionary format suitable for JSON serialization.
Returns:
dict: Dictionary containing team information.
"""
try:
return {
"team_id": str(self.team_id),
"name": self.name,
"description": self.description,
"lead_id": str(self.lead_id) if self.lead_id else None,
"_links": {
"self": f"/teams/{self.team_id}",
"members": f"/teams/{self.team_id}/members",
},
}
except Exception as e:
return {"error": f"Error serializing team: {str(e)}"}
# Category Model
class Category(db.Model):
"""
Category model represents a category for projects. It includes a name, description, and color code for
visual representation.
"""
__tablename__ = "CATEGORY"
category_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = db.Column(db.String(255), unique=True, nullable=False)
description = db.Column(db.Text)
color = db.Column(db.String(7), default="#64748b")
# Define a single relationship with back_populates to avoid conflicts
projects = db.relationship("Project", back_populates="category", lazy=True)
# Project Model
class Project(db.Model):
"""
Project model represents a project within the system. It includes project title, description, status,
deadline, and relationships with teams and categories.
"""
__tablename__ = "PROJECT"
project_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text)
status = db.Column(db.String(50), default="planning")
deadline = db.Column(db.DateTime)
team_id = db.Column(UUID(as_uuid=True), db.ForeignKey("TEAM.team_id", ondelete="CASCADE"))
category_id = db.Column(
UUID(as_uuid=True), db.ForeignKey("CATEGORY.category_id", ondelete="SET NULL")
)
# Use back_populates to properly link with Category.projects
category = db.relationship("Category", back_populates="projects", lazy=True)
tasks = db.relationship("Task", backref="project", lazy=True, cascade="all, delete-orphan")
def to_dict(self):
"""
Convert the Project object into a dictionary format suitable for JSON serialization.
Returns:
dict: Dictionary containing project information.
"""
try:
return {
"project_id": str(self.project_id),
"title": self.title,
"description": self.description,
"status": self.status,
"deadline": self.deadline.isoformat() if self.deadline else None,
"team_id": str(self.team_id) if self.team_id else None,
"category_id": str(self.category_id) if self.category_id else None,
"_links": {
"self": f"/projects/{self.project_id}",
"tasks": f"/tasks?project_id={self.project_id}",
},
}
except Exception as e:
return {"error": f"Error serializing project: {str(e)}"}
# Priority Enum
class PriorityEnum(int, Enum):
"""
Enum for defining task priorities.
"""
HIGH = 1
MEDIUM = 2
LOW = 3
# Status Enum
class StatusEnum(str, Enum):
"""
Enum for defining task status.
"""
PENDING = "pending"
IN_PROGRESS = "in_progress"
COMPLETED = "completed"
# Task Model
class Task(db.Model):
"""
Task model represents a task within a project. It includes task title, description, priority, status,
deadlines, and relationships with users (assignees, creators, and updaters).
"""
__tablename__ = "TASK"
task_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
title = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text)
status = db.Column(db.String(50), default=StatusEnum.PENDING.value)
priority = db.Column(db.Integer, default=PriorityEnum.LOW.value)
deadline = db.Column(db.DateTime)
project_id = db.Column(
UUID(as_uuid=True), db.ForeignKey("PROJECT.project_id", ondelete="CASCADE")
)
assignee_id = db.Column(UUID(as_uuid=True), db.ForeignKey("USER.user_id", ondelete="SET NULL"))
created_by = db.Column(UUID(as_uuid=True), db.ForeignKey("USER.user_id", ondelete="SET NULL"))
updated_by = db.Column(UUID(as_uuid=True), db.ForeignKey("USER.user_id", ondelete="SET NULL"))
def __init__(
self,
title,
description=None,
priority=PriorityEnum.LOW.value,
deadline=None,
status=StatusEnum.PENDING.value,
project_id=None,
assignee_id=None,
created_by=None,
updated_by=None,
):
"""
Constructor for creating a new Task.
Args:
title (str): The title of the task.
description (str, optional): The description of the task.
priority (int, optional): The priority level of the task.
deadline (datetime, optional): The task's deadline.
status (str, optional): The current status of the task.
project_id (UUID, optional): The ID of the related project.
assignee_id (UUID, optional): The ID of the user assigned to the task.
created_by (UUID, optional): The ID of the user who created the task.
updated_by (UUID, optional): The ID of the user who last updated the task.
"""
self.title = title
self.description = description
self.priority = priority
self.deadline = deadline
self.status = status
self.project_id = project_id
self.assignee_id = assignee_id
self.created_by = created_by
self.updated_by = updated_by
def to_dict(self):
"""
Convert the Task object into a dictionary format suitable for JSON serialization.
Returns:
dict: Dictionary containing task information.
"""
try:
return {
"task_id": str(self.task_id),
"title": self.title,
"description": self.description,
"priority": self.priority,
"deadline": self.deadline.isoformat() if self.deadline else None,
"status": self.status,
"project_id": str(self.project_id) if self.project_id else None,
"assignee_id": str(self.assignee_id) if self.assignee_id else None,
"created_by": str(self.created_by) if self.created_by else None,
"updated_by": str(self.updated_by) if self.updated_by else None,
"_links": {
"self": f"/tasks/{self.task_id}",
"project": f"/projects/{self.project_id}" if self.project_id else None,
},
}
except Exception as e:
return {"error": f"Error serializing task: {str(e)}"}
# Team Membership Model
class TeamMembership(db.Model):
"""
Team Membership model represents a membership record for a user within a team, including the role of
the user within the team.
"""
__tablename__ = "TEAM_MEMBERSHIP"
membership_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
user_id = db.Column(UUID(as_uuid=True), db.ForeignKey("USER.user_id", ondelete="CASCADE"))
team_id = db.Column(UUID(as_uuid=True), db.ForeignKey("TEAM.team_id", ondelete="CASCADE"))
role = db.Column(db.String(50), default="member")
# Function to initialize the database
def init_db(app):
"""
Initialize the database for the Flask application.
Args:
app (Flask): The Flask application instance.
"""
try:
if app.config.get("TESTING"):
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
else:
app.config["SQLALCHEMY_DATABASE_URI"] = (
"postgresql://admin:helloworld123@localhost:5432/task_management_db"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
with app.app_context():
db.create_all()
return True
except Exception as e:
print(f"Database initialization error: {str(e)}")
return False
# CRUD Functions
def create_user(username, email, password, role="member"):
"""
Create a new user in the system.
Args:
username (str): The username of the new user.
email (str): The email of the new user.
password (str): The password of the new user.
role (str, optional): The role of the user (default is 'member').
Returns:
User: The created User object.
"""
try:
password_hash = generate_password_hash(password) # Hash the password
user = User(username=username, email=email, password_hash=password_hash, role=role)
db.session.add(user)
db.session.commit()
return user
except IntegrityError:
db.session.rollback()
raise ValueError("Username or email already exists")
except Exception as e:
db.session.rollback()
raise Exception(f"Error creating user: {str(e)}")
def get_user_by_id(user_id):
"""
Retrieve a user by their ID.
Args:
user_id (UUID): The ID of the user to retrieve.
Returns:
User: The user object, or None if not found.
"""
try:
if not isinstance(user_id, uuid.UUID):
try:
user_id = uuid.UUID(str(user_id))
except (ValueError, TypeError):
return None
return User.query.get(user_id)
except Exception as e:
print(f"Error retrieving user: {str(e)}")
return None
def update_user(user_id, **kwargs):
"""
Update a user's details.
Args:
user_id (UUID): The ID of the user to update.
**kwargs: The fields to update with their new values.
Returns:
User: The updated User object.
"""
try:
user = get_user_by_id(user_id)
if not user:
raise ValueError(f"User with ID {user_id} not found")
for key, value in kwargs.items():
if hasattr(user, key):
setattr(user, key, value)
else:
raise ValueError(f"Invalid attribute: {key}")
db.session.commit()
return user
except IntegrityError:
db.session.rollback()
raise ValueError("Update violates unique constraints (username or email)")
except Exception as e:
db.session.rollback()
raise Exception(f"Error updating user: {str(e)}")
def delete_user(user_id):
"""
Delete a user from the system.
Args:
user_id (UUID): The ID of the user to delete.
Returns:
User | None: The deleted User object if found and deleted, otherwise None.
"""
try:
user = get_user_by_id(user_id)
if user:
db.session.delete(user)
db.session.commit()
return user
return None
except Exception as e:
db.session.rollback()
raise Exception(f"Error deleting user: {str(e)}")
def get_all_users():
"""
Retrieve all users from the system.
Returns:
list: A list of all User objects.
"""
try:
return User.query.all()
except Exception as e:
print(f"Error retrieving users: {str(e)}")
return []
def create_team(name, description, lead_id):
"""
Create a new team in the system.
Args:
name (str): The name of the team.
description (str): The description of the team.
lead_id (UUID): The ID of the user leading the team.
Returns:
Team: The created Team object.
"""
try:
# Verify lead_id exists if provided
if lead_id:
lead = get_user_by_id(lead_id)
if not lead:
raise ValueError(f"User with ID {lead_id} not found")
team = Team(name=name, description=description, lead_id=lead_id)
db.session.add(team)
db.session.commit()
return team
except Exception as e:
db.session.rollback()
raise Exception(f"Error creating team: {str(e)}")
def assign_task(title, description, priority, project_id, assignee_id, created_by=None):
"""
Assign a new task to a user.
Args:
title (str): The title of the task.
description (str): The description of the task.
priority (int): The priority level of the task.
project_id (UUID): The ID of the project the task belongs to.
assignee_id (UUID): The ID of the user assigned to the task.
created_by (UUID, optional): The ID of the user creating the task.
Returns:
Task: The created Task object.
"""
try:
# Validate project_id
if project_id:
project = Project.query.get(project_id)
if not project:
raise ValueError(f"Project with ID {project_id} not found")
# Validate assignee_id
if assignee_id:
assignee = get_user_by_id(assignee_id)
if not assignee:
raise ValueError(f"User with ID {assignee_id} not found")
# Validate created_by
if created_by:
creator = get_user_by_id(created_by)
if not creator:
raise ValueError(f"User with ID {created_by} not found")
# Validate priority
try:
priority_value = int(priority)
if priority_value not in [e.value for e in PriorityEnum]:
raise ValueError(f"Invalid priority value: {priority}")
except (ValueError, TypeError):
raise ValueError(f"Priority must be a valid integer: {priority}")
task = Task(
title=title,
description=description,
priority=priority,
project_id=project_id,
assignee_id=assignee_id,
created_by=created_by,
)
db.session.add(task)
db.session.commit()
return task
except Exception as e:
db.session.rollback()
raise Exception(f"Error assigning task: {str(e)}")
def get_project_tasks(project_id):
"""
Retrieve all tasks associated with a specific project.
Args:
project_id (UUID): The ID of the project.
Returns:
list: A list of Task objects associated with the project.
"""
try:
if not isinstance(project_id, uuid.UUID):
try:
project_id = uuid.UUID(str(project_id))
except (ValueError, TypeError):
raise ValueError(f"Invalid project ID format: {project_id}")
# Verify project exists
project = Project.query.get(project_id)
if not project:
raise ValueError(f"Project with ID {project_id} not found")
return Task.query.filter_by(project_id=project_id).all()
except Exception as e:
print(f"Error retrieving project tasks: {str(e)}")
return []
def create_task(
title,
description=None,
priority=PriorityEnum.LOW.value,
deadline=None,
status=StatusEnum.PENDING.value,
project_id=None,
assignee_id=None,
created_by=None,
updated_by=None,
):
"""
Create a new task.
Args:
title (str): The title of the task.
description (str, optional): The description of the task.
priority (int, optional): The priority level of the task.
deadline (datetime, optional): The task's deadline.
status (str, optional): The current status of the task.
project_id (UUID, optional): The ID of the related project.
assignee_id (UUID, optional): The ID of the user assigned to the task.
created_by (UUID, optional): The ID of the user who created the task.
updated_by (UUID, optional): The ID of the user who last updated the task.
Returns:
Task: The created Task object.
"""
try:
# Validate title
if not title or not isinstance(title, str):
raise ValueError("Task title is required and must be a string")
# Validate priority
try:
priority_value = int(priority)
if priority_value not in [e.value for e in PriorityEnum]:
raise ValueError(f"Invalid priority value: {priority}")
except (ValueError, TypeError):
raise ValueError(f"Priority must be a valid integer: {priority}")
# Validate status
if status not in [e.value for e in StatusEnum]:
raise ValueError(f"Invalid status value: {status}")
# Validate project_id
if project_id:
if not isinstance(project_id, uuid.UUID):
try:
project_id = uuid.UUID(str(project_id))
except (ValueError, TypeError):
raise ValueError(f"Invalid project ID format: {project_id}")
project = Project.query.get(project_id)
if not project:
raise ValueError(f"Project with ID {project_id} not found")
# Validate assignee_id
if assignee_id:
if not isinstance(assignee_id, uuid.UUID):
try:
assignee_id = uuid.UUID(str(assignee_id))
except (ValueError, TypeError):
raise ValueError(f"Invalid assignee ID format: {assignee_id}")
assignee = get_user_by_id(assignee_id)
if not assignee:
raise ValueError(f"User with ID {assignee_id} not found")
# Validate created_by
if created_by:
if not isinstance(created_by, uuid.UUID):
try:
created_by = uuid.UUID(str(created_by))
except (ValueError, TypeError):
raise ValueError(f"Invalid creator ID format: {created_by}")
creator = get_user_by_id(created_by)
if not creator:
raise ValueError(f"User with ID {created_by} not found")
# Validate updated_by
if updated_by:
if not isinstance(updated_by, uuid.UUID):
try:
updated_by = uuid.UUID(str(updated_by))
except (ValueError, TypeError):
raise ValueError(f"Invalid updater ID format: {updated_by}")
updater = get_user_by_id(updated_by)
if not updater:
raise ValueError(f"User with ID {updated_by} not found")
task = Task(
title=title,
description=description,
priority=priority,
deadline=deadline,
status=status,
project_id=project_id,
assignee_id=assignee_id,
created_by=created_by,
updated_by=updated_by,
)
db.session.add(task)
db.session.commit()
return task
except Exception as e:
db.session.rollback()
raise Exception(f"Error creating task: {str(e)}")
def get_task(task_id):
"""
Retrieve a task by its ID.
Args:
task_id (UUID): The ID of the task to retrieve.
Returns:
Task: The task object, or None if not found.
"""
try:
if not isinstance(task_id, uuid.UUID):
try:
task_id = uuid.UUID(str(task_id))
except (ValueError, TypeError):
return None
return Task.query.get(task_id)
except Exception as e:
print(f"Error retrieving task: {str(e)}")
return None
def delete_task(task_id):
"""
Delete a task from the system.
Args:
task_id (UUID): The ID of the task to delete.
Returns:
bool: True if the task was deleted, False otherwise.
"""
try:
if not isinstance(task_id, uuid.UUID):
try:
task_id = uuid.UUID(str(task_id))
except (ValueError, TypeError):
raise ValueError(f"Invalid task ID format: {task_id}")
task = get_task(task_id)
if task:
db.session.delete(task)
db.session.commit()
return True
return False
except Exception as e:
db.session.rollback()
print(f"Error deleting task: {str(e)}")
return False