-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartment.py
More file actions
124 lines (92 loc) · 4.01 KB
/
Department.py
File metadata and controls
124 lines (92 loc) · 4.01 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from pymongo import MongoClient
class Department:
def __init__(self, database):
self.database = database
self.collection = database["departments"]
def validate_department(self,department):
"""Validates a department document."""
# Check if the required fields are present.
if not department.get("abbreviation"):
raise ValueError("The department abbreviation is required.")
if not department.get("chair_name"):
raise ValueError("The department chair name is required.")
if not department.get("building"):
raise ValueError("The department building is required.")
if not department.get("office"):
raise ValueError("The department office is required.")
if not department.get("description"):
raise ValueError("The department description is required.")
# Check the maximum lengths of the fields.
if len(department["name"]) > 50:
raise ValueError("The department name must be no longer than 50 characters.")
if len(department["abbreviation"]) > 6:
raise ValueError("The department abbreviation must be no longer than 6 characters.")
if len(department["chair_name"]) > 80:
raise ValueError("The department chair name must be no longer than 80 characters.")
if len(department["building"]) > 10:
raise ValueError("The department building must be no longer than 10 characters.")
if len(department["description"]) > 80:
raise ValueError("The department description must be no longer than 80 characters.")
# Return the validated department document.
return department
def cursor_to_list(self,cursor):
"""Converts a Cursor object to a list."""
list = []
for document in cursor:
list.append(document)
return list
def exists(self,department):
existing_departments = self.database.departments.find({
"$or": [
{"abbreviation": department["abbreviation"]},
{"chair_name": department["chair_name"]},
{"building": department["building"], "office": department["office"]},
]
})
# Convert the Cursor object to a list.
existing_departments_list = self.cursor_to_list(existing_departments)
# Count the number of existing departments.
num_existing_departments = len(existing_departments_list)
if num_existing_departments > 0:
return 1
def add_department(self):
name = input("Department name --> ")
abbreviation = input("Department's abbreviation --> ")
chairName = input("Department Chair name --> ")
building = input("Building name --> ")
office = int(input("Office number --> "))
description = input("Description of department --> ")
department = {
"name": name,
"abbreviation": abbreviation,
"chair_name": chairName,
"building": building,
"office": office,
"description": description
}
# Get the departments collection from the database
departments_collection = self.database.get_collection("departments")
# Check for existing departments with the same abbreviation
if self.exists(department):
print("There is already a department with the same abbreviation, chair, or building+ office.")
return
# Insert the department document into the collection
departments_collection.insert_one(department)
def delete_department(self):
# Get the departments collection from the database
departments_collection = self.database.get_collection("departments")
abb = input("Enter the abbreviation of the department you want to delete: ")
existing_departments = self.database.departments.find({
"$or": [
{"abbreviation": (abb)}]
})
# Convert the Cursor object to a list.
existing_departments_list = self.cursor_to_list(existing_departments)
# Count the number of existing departments.
num_existing_departments = len(existing_departments_list)
if num_existing_departments > 0:
departments_collection.delete_one({
"abbreviation": abb,
})
else:
print("No department exists with that abbreviation")