def upsertSchedule(self, year, quarter, d_key, course_id): ''' insert or update Schedule entity''' schedule = Schedule(year=year, quarter=quarter, department=d_key, course=ndb.Key(Course, course_id)) #print("Inserted the course under dept = ", d_key.get().name) sch_key = schedule.put() return R_SUCC_INSERT.format("Schedule", str(sch_key))
def upsertSchedule(self, request, d_key, message_ID): ''' insert or update Schedule entity''' schedule = Schedule(year=request.year, quarter=request.quarter, department=d_key, course=ndb.Key(Course, request.course_id)) print("Inserted the course under dept = ", d_key.get().name) schedule.put() return STORED_RESPONSES.items[message_ID]
def deleteDept(self, dept_id): ''' delete department given department ID. delete in the order Schedule, Student and Course first''' # Enhancement. Deleting entity using other details like department name try: # Creating the key for given department id d_key = ndb.Key(Department, int(dept_id)) # Fetching and deleting the entity dept = Department.get_by_id(dept_id) ''' Dependencies''' # check if department exists in Student kind st_query = Student.query(Student.department == d_key) co_query = Course.query(Course.department == d_key) sch_query = Schedule.query(Schedule.department == d_key) if sch_query.get(): # entity found in Schedule. Invoke Dependency error response = R_DEP_ERR_DELCHILD.format("Schedule", "Department") elif st_query.get(): # entity found in Student. Invoke Dependency error response = R_DEP_ERR_DELCHILD.format("Student", "Department") elif co_query.get(): # entity found in Course. Invoke Dependency error response = R_DEP_ERR_DELCHILD.format("Course", "Department") else: # no dependencies found. delete department d_key.delete() response = R_SUCC_DEL.format("Department", str(d_key)) except Exception, e: response = R_SERV_ERR.format(str(e))
def deleteDept(self, request): ''' delete department given department ID. delete in the order Schedule, Student and Course first''' # Enhancement. Deleting entity using other details like department name try: # Creating the key for given department id d_key = ndb.Key(Department, int(request.id)) # Fetching and deleting the entity dept = Department.get_by_id(request.id) ''' Dependencies''' # check if department exists in Student kind st_query = Student.query(Student.department == d_key) co_query = Course.query(Course.department == d_key) sch_query = Schedule.query(Schedule.department == d_key) if sch_query.get(): # entity found in Schedule. Invoke Dependency error response = STORED_RESPONSES.items[DEP_ERR_DEPT_SCH_ID] elif st_query.get(): # entity found in Student. Invoke Dependency error response = STORED_RESPONSES.items[DEP_ERR_DEPT_ST_ID] elif co_query.get(): # entity found in Course. Invoke Dependency error response = STORED_RESPONSES.items[DEP_ERR_DEPT_CO_ID] else: # no dependencies found. delete department print("Department to be deleted = ", dept) d_key.delete() response = STORED_RESPONSES.items[DELETE_ID] return response except: raise endpoints.EndpointsErrorMessage( "Unable to delete entity. Please try again later!")
def view_dept_courses(self, request): ''' Allows user to list courses a department provides in a specific quarter''' d_key = ndb.Key(Department, request.department_id) query = Schedule.query(Schedule.department == d_key, Schedule.quarter == request.quarter, Schedule.year == request.year) print("view_dept_courses method: converting query to messages") return self.queryToCoursesMessage(query)
def insertSchedule(self, request): ''' insert a course schedule for given quarter. Require course ID, quarter and year.''' response = "" # read all inputs course_id = request.get('course_id') year = request.get('year') quarter = request.get('quarter') # check if required fields are not blank if (course_id and year and quarter): # validate inputs if (is_int(course_id) and is_int(year) and is_int(quarter)): course_id = int(course_id) year = int(year) quarter = int(quarter) # validate inputs continued # Get current year # Year needs to be prev, curr and next year if (self.validate_year(year) and self.validate_quarter(quarter)): # Validate course id c_key = ndb.Key(Course, course_id) # Check if entity exists if c_key.get(): # fetch the respective dept for that course id d_key = c_key.get().department # check for duplicate entries query = Schedule.query(Schedule.course == c_key, Schedule.quarter == quarter, Schedule.year == year) if (not query.get()): # insert schedule response = self.upsertSchedule(year=year, quarter=quarter, d_key=d_key, course_id=course_id) else: response = R_DUP_ID_ERR.format("Schedule") else: response = R_DEP_ERR_INVCOUR else: response = R_PROP_VAL_ERR.format( "Year and Quarter", "integers of values (previous year, current year and next year) and range(1-4) respectively" ) else: response = R_ID_VAL_ERR.format( "Course ID and Year and Quarter") else: response = R_REQ_FIELD_ERR.format("Course ID and Year and Quarter") return response
def deleteSchedule(self, request): ''' delete a schedule given course ID quarter and year''' try: # Get Course id c_key = ndb.Key(Course, int(request.course_id)) print("deleteSchedule Method: Going to query object") # Get schedule id from given year, quarter and course query = Schedule.query(Schedule.course == c_key, Schedule.quarter == request.quarter, Schedule.year == request.year) print("deleteSchedule method: After query = ", query) schedule = query.get() if schedule: print("deleteSchedule method: schedule -", schedule.key, " exists for course key = ", c_key) # delete all enrollments for that schedule query = Enrollment.query( Enrollment.schedule == schedule.key) #ndb.Key(Schedule, schedule.id)) for enrollment in query.fetch(): print("deleteSchedule Method: Fetching query = ", enrollment) # delete course from Student model student = enrollment.student.get() print("===========Student list for course========", student) if (c_key in student.study_lists): student.study_lists.remove(c_key) # update student entity student.put() print( "deleteSchedule method: course not in student list" ) print( "deleteSchedule method: Going to delete enrollment with Key = ", enrollment.key) # delete enrollment #enrollment = q.get() #ndb.Key(Enrollment, enrollment.id).delete() enrollment.key.delete() print( "deleteSchedule method: deleted enrollment successfully" ) #ndb.Key(Enrollment, q.id).delete() # delete schedule schedule.key.delete() except: raise endpoints.EndpointsErrorMessage( 'Error while deleting a schedule.') return STORED_RESPONSES.items[DELETE_ID]
def view_students_in_course(self, request): ''' List students enrolled in a course''' # fetch schedule ids containing the course listed c_key = ndb.Key(Course, request.course_id) query = Schedule.query(Schedule.course == c_key) schedule_keys = [] for key in query.iter(keys_only=True): schedule_keys.append(key) # query for enrollments with matching schedule_keys query = Enrollment.query(Enrollment.schedule.IN(schedule_keys)) print( "view_students_in_course method: going to convert query to student names message" ) return self.enrollmentToStudentsMessage(query)
def deleteCourse(self, course_id): '''Delete a course given course ID. Delete course from Schedule first.''' try: response = "" # Creating the key for given course id c_key = ndb.Key(Course, course_id) # Check if course exists if c_key.get(): '''Dependency Check''' # Check if course exists in schedule query = Schedule.query(Schedule.course == c_key) if query.get(): # course exists in schedule. Invoke dependency error response = R_DEP_ERR_DELCHILD.format("Schedule", "Course") else: # Fetching and deleting the entity with given course_id c_key.delete() response = R_SUCC_DEL.format("Course", str(c_key)) except Exception, e: response = R_SERV_ERR.format(str(e))
def insertSchedule(self, request): ''' insert a course schedule for given quarter. Require course ID, quarter and year.''' response = "" # Validate course id c_key = ndb.Key(Course, request.course_id) # Check if entity exists if c_key.get(): # fetch the respective dept for that course id d_key = c_key.get().department # check for duplicate entries query = Schedule.query(Schedule.course == c_key, Schedule.quarter == request.quarter, Schedule.year == request.year) if not query.get(): # insert schedule response = self.upsertSchedule(request, d_key, ADD_ID) else: response = STORED_RESPONSES.items[DUPLICATE_ID] else: response = STORED_RESPONSES.items[INV_COURSE_ID] return response
def deleteCourse(self, request): '''Delete a course given course ID. Delete course from Schedule first.''' try: response = "" # Creating the key for given course id c_key = ndb.Key(Course, request.id) # Check if course exists if c_key.get(): '''Dependency Check''' # Check if course exists in schedule query = Schedule.query(Schedule.course == c_key) if query.get(): # course exists in schedule. Invoke dependency error response = STORED_RESPONSES.items[DEP_ERR_COURSE_ID] else: # Fetching and deleting the entity with given course_id c_key.delete() response = STORED_RESPONSES.items[DELETE_ID] return response except: raise endpoints.EndpointsErrorMessage( 'Unable to delete course entity. Please try again later. ')
def getSchedule(self, course_key, quarter, year): return Schedule.query(Schedule.course == course_key, Schedule.quarter == quarter, Schedule.year == year)