Ejemplo n.º 1
0
    def deleteStudent(self, request):
        ''' Delete student entity given student id'''
        try:
            response = ""
            # Create a key for given student id
            s_key = ndb.Key(Student, int(request.id))
            student = Student.get_by_id(request.id)
            print("Key generated for student = ", s_key, student)
            # Fetching student entity
            #student = Student.get_by_id(s_key)
            #student = s_key.get()
            #print ("Student Record to be deleted = ", student)

            if student:
                # check if the student is enrolled in a course or not.
                query = Enrollment.query(Enrollment.student == s_key)
                if query.get():
                    # cannot delete student without deleting his enrollments
                    response = STORED_RESPONSES.items[DEP_ERR_STUDENT_ID]
                else:
                    # deleting the entity with a given student id
                    s_key.delete()
                    response = STORED_RESPONSES.items[DELETE_ID]
            else:
                response = STORED_RESPONSES.items[INV_STUDENT_ID]

            return response
        except:
            raise endpoints.EndpointsErrorMessage(
                'Unable to delete student entity. Please try again later.')
Ejemplo n.º 2
0
    def insertStudent(self, request):
        ''' Insert student entity given student ID'''
        try:
            response = ""
            # Check if student id already exists
            student = self.getStudent(request.id)
            if not student:
                # Check if department is valid
                dept = self.getDepartment(request.department_id)
                if (not dept):
                    # Invalid department id
                    print("Department record not found!", dept)
                    response = STORED_RESPONSES.items[INV_DEPT_ID]
                else:
                    print("New student record to be added", student)
                    # insert student entity
                    response = self.upsertStudent(request, ADD_ID)

            else:
                #Send Error Message
                response = STORED_RESPONSES.items[DUPLICATE_ID]
            return response
        except:
            raise endpoints.EndpointsErrorMessage(
                'Unable to add student entity. Please try again later. ')
Ejemplo n.º 3
0
    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!")
Ejemplo n.º 4
0
    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]
Ejemplo n.º 5
0
    def insertDept(self, request):
        try:
            ''' insert new department given department ID'''
            # Checking if the record exists already
            department = self.getDepartment(request.id)
            if not department:
                # New record
                print "New Department to be added"
                response = self.upsertDepartment(request, ADD_ID)
            else:
                # Record exists. Cannot insert. Need to update. Send Error message back
                response = STORED_RESPONSES.items[DUPLICATE_ID]
            return response

        except:
            raise endpoints.EndpointsErrorMessage(
                'Unable to add department record. Please try again later.')
Ejemplo n.º 6
0
 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. ')
Ejemplo n.º 7
0
    def insertCourse(self, request):
        ''' insert a new course given course ID and department ID. course ID is unique across depts.'''
        try:
            response = ""

            course = self.getCourse(request.id)
            # check for valid department
            department = self.getDepartment(request.department_id)
            # Course Not found. Insert new course
            if not course:
                # Validate department
                if department:
                    # create new course
                    response = self.upsertCourse(request, ADD_ID)
                else:
                    response = STORED_RESPONSES.items[INV_DEPT_ID]
            else:
                # Course found. cannot insert but update
                response = STORED_RESPONSES.items[DUPLICATE_ID]
            return response
        except:
            raise endpoints.EndpointsErrorMessage(
                'Unable to add course record. Please try again later. ')