dhruv575 commited on
Commit
bacb197
·
1 Parent(s): 95f7879
models/department.py CHANGED
@@ -55,23 +55,22 @@ class Department:
55
  if self._id:
56
  # Update existing department
57
  department_dict["updated_at"] = datetime.now()
58
-
59
- # Remove _id field when updating an existing document to avoid MongoDB error
60
  if "_id" in department_dict:
61
  del department_dict["_id"]
62
-
63
  result = departments_collection.update_one(
64
  {"_id": ObjectId(self._id)},
65
  {"$set": department_dict}
66
  )
67
- return result.modified_count > 0
68
  else:
69
  # Insert new department
70
  department_dict["created_at"] = datetime.now()
71
  department_dict["updated_at"] = datetime.now()
72
  result = departments_collection.insert_one(department_dict)
73
  self._id = result.inserted_id
74
- return result.acknowledged
 
 
75
 
76
  @classmethod
77
  def find_by_id(cls, department_id):
 
55
  if self._id:
56
  # Update existing department
57
  department_dict["updated_at"] = datetime.now()
 
 
58
  if "_id" in department_dict:
59
  del department_dict["_id"]
 
60
  result = departments_collection.update_one(
61
  {"_id": ObjectId(self._id)},
62
  {"$set": department_dict}
63
  )
64
+ success = result.modified_count > 0
65
  else:
66
  # Insert new department
67
  department_dict["created_at"] = datetime.now()
68
  department_dict["updated_at"] = datetime.now()
69
  result = departments_collection.insert_one(department_dict)
70
  self._id = result.inserted_id
71
+ success = result.acknowledged
72
+
73
+ return success
74
 
75
  @classmethod
76
  def find_by_id(cls, department_id):
models/incident.py CHANGED
@@ -69,6 +69,8 @@ class Incident:
69
  if self._id:
70
  # Update existing incident
71
  incident_dict["updated_at"] = datetime.now()
 
 
72
  result = incidents_collection.update_one(
73
  {"_id": ObjectId(self._id)},
74
  {"$set": incident_dict}
 
69
  if self._id:
70
  # Update existing incident
71
  incident_dict["updated_at"] = datetime.now()
72
+ if "_id" in incident_dict:
73
+ del incident_dict["_id"]
74
  result = incidents_collection.update_one(
75
  {"_id": ObjectId(self._id)},
76
  {"$set": incident_dict}
models/log.py CHANGED
@@ -59,6 +59,8 @@ class Log:
59
  if self._id:
60
  # Update existing log
61
  log_dict["updated_at"] = datetime.now()
 
 
62
  result = logs_collection.update_one(
63
  {"_id": ObjectId(self._id)},
64
  {"$set": log_dict}
 
59
  if self._id:
60
  # Update existing log
61
  log_dict["updated_at"] = datetime.now()
62
+ if "_id" in log_dict:
63
+ del log_dict["_id"]
64
  result = logs_collection.update_one(
65
  {"_id": ObjectId(self._id)},
66
  {"$set": log_dict}
models/user.py CHANGED
@@ -89,30 +89,49 @@ class User:
89
  users_collection = get_users_collection()
90
  user_dict = self.to_dict()
91
 
92
- # Add password for database storage
93
- if self.password:
94
- user_dict["password"] = self.password
95
-
 
96
  if self._id:
97
  # Update existing user
98
  user_dict["updated_at"] = datetime.now()
99
-
100
- # Remove _id field when updating an existing document
101
  if "_id" in user_dict:
102
- del user_dict["_id"]
103
-
104
  result = users_collection.update_one(
105
  {"_id": ObjectId(self._id)},
106
  {"$set": user_dict}
107
  )
108
- return result.modified_count > 0
109
  else:
110
- # Insert new user
 
 
 
 
 
 
 
 
 
 
 
111
  user_dict["created_at"] = datetime.now()
112
  user_dict["updated_at"] = datetime.now()
113
  result = users_collection.insert_one(user_dict)
114
  self._id = result.inserted_id
115
- return result.acknowledged
 
 
 
 
 
 
 
 
116
 
117
  @classmethod
118
  def find_by_id(cls, user_id):
 
89
  users_collection = get_users_collection()
90
  user_dict = self.to_dict()
91
 
92
+ # Remove password from the dictionary for safety before saving
93
+ # Password should only be updated via set_password method
94
+ if 'password' in user_dict:
95
+ del user_dict['password']
96
+
97
  if self._id:
98
  # Update existing user
99
  user_dict["updated_at"] = datetime.now()
100
+ # --- Remove _id before updating ---
 
101
  if "_id" in user_dict:
102
+ del user_dict["_id"]
103
+ # --- End Remove _id ---
104
  result = users_collection.update_one(
105
  {"_id": ObjectId(self._id)},
106
  {"$set": user_dict}
107
  )
108
+ success = result.modified_count > 0
109
  else:
110
+ # Insert new user - Requires password which was deleted above!
111
+ # We need the hashed password for insertion.
112
+ # Let's re-add the hashed password if it exists on the object
113
+ if self.password:
114
+ user_dict['password'] = self.password
115
+ else:
116
+ # This should ideally not happen if creating a new user
117
+ # Maybe throw an error or log a warning?
118
+ logger.warning(f"Attempting to insert new user {self.email} without a password hash.")
119
+ # Handle appropriately - perhaps prevent save or set a placeholder?
120
+ # For now, let it proceed but log it.
121
+
122
  user_dict["created_at"] = datetime.now()
123
  user_dict["updated_at"] = datetime.now()
124
  result = users_collection.insert_one(user_dict)
125
  self._id = result.inserted_id
126
+ success = result.acknowledged
127
+
128
+ # Add user to department if insert was successful
129
+ if success and self.department_id:
130
+ department = Department.find_by_id(self.department_id)
131
+ if department:
132
+ department.add_member(self._id)
133
+
134
+ return success
135
 
136
  @classmethod
137
  def find_by_id(cls, user_id):
models/workflow.py CHANGED
@@ -1,6 +1,7 @@
1
  from bson import ObjectId
2
  from datetime import datetime
3
  from db import get_workflows_collection
 
4
 
5
  class Workflow:
6
  def __init__(self, title, description, data_requirements=None, raw_forms=None, form_fields=None,
@@ -52,39 +53,31 @@ class Workflow:
52
  workflows_collection = get_workflows_collection()
53
  workflow_dict = self.to_dict()
54
 
55
- # Exclude _id from the dictionary for the $set operation
56
- update_data = {k: v for k, v in workflow_dict.items() if k != '_id'}
57
-
58
  if self._id:
59
  # Update existing workflow
60
- update_data["updated_at"] = datetime.now()
61
- # Ensure department_id is ObjectId if it exists in the update_data
62
- if 'department_id' in update_data and update_data['department_id'] is not None:
63
- update_data['department_id'] = ObjectId(update_data['department_id'])
64
-
65
  result = workflows_collection.update_one(
66
  {"_id": ObjectId(self._id)},
67
- {"$set": update_data} # Use the dictionary without _id
68
  )
69
- # Update the instance's updated_at timestamp after successful save
70
- if result.modified_count > 0:
71
- self.updated_at = update_data["updated_at"]
72
- return True
73
- return result.matched_count > 0 # Return True if found even if not modified
74
  else:
75
  # Insert new workflow
76
- # Ensure department_id is ObjectId before insert
77
- if 'department_id' in workflow_dict and workflow_dict['department_id'] is not None:
78
- workflow_dict['department_id'] = ObjectId(workflow_dict['department_id'])
79
-
80
  workflow_dict["created_at"] = datetime.now()
81
- workflow_dict["updated_at"] = workflow_dict["created_at"]
82
  result = workflows_collection.insert_one(workflow_dict)
83
  self._id = result.inserted_id
84
- # Update instance timestamps
85
- self.created_at = workflow_dict["created_at"]
86
- self.updated_at = workflow_dict["updated_at"]
87
- return result.acknowledged
 
 
 
 
 
88
 
89
  @classmethod
90
  def find_by_id(cls, workflow_id):
 
1
  from bson import ObjectId
2
  from datetime import datetime
3
  from db import get_workflows_collection
4
+ from models.department import Department
5
 
6
  class Workflow:
7
  def __init__(self, title, description, data_requirements=None, raw_forms=None, form_fields=None,
 
53
  workflows_collection = get_workflows_collection()
54
  workflow_dict = self.to_dict()
55
 
 
 
 
56
  if self._id:
57
  # Update existing workflow
58
+ workflow_dict["updated_at"] = datetime.now()
59
+ if "_id" in workflow_dict:
60
+ del workflow_dict["_id"]
 
 
61
  result = workflows_collection.update_one(
62
  {"_id": ObjectId(self._id)},
63
+ {"$set": workflow_dict}
64
  )
65
+ success = result.modified_count > 0
 
 
 
 
66
  else:
67
  # Insert new workflow
 
 
 
 
68
  workflow_dict["created_at"] = datetime.now()
69
+ workflow_dict["updated_at"] = datetime.now()
70
  result = workflows_collection.insert_one(workflow_dict)
71
  self._id = result.inserted_id
72
+ success = result.acknowledged
73
+
74
+ # Add workflow to department if insert was successful
75
+ if success and self.department_id:
76
+ department = Department.find_by_id(self.department_id)
77
+ if department:
78
+ department.add_workflow(self._id)
79
+
80
+ return success
81
 
82
  @classmethod
83
  def find_by_id(cls, workflow_id):