Пример #1
0
    def deleteStudent(self, student_id):
        ''' Delete student entity given student id'''
        try:
            response = ""
            # Create a key for given student id
            s_key = ndb.Key(Student, student_id)
            # Fetching student entity
            #student = Student.get_by_id(student_id)
            #print ("Student Record to be deleted = ", student)

            if s_key.get():
                # 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 = R_DEP_ERR_DELCHILD.format(
                        "Enrollments", "Student")
                else:
                    # deleting the entity with a given student id
                    s_key.delete()
                    response = R_SUCC_DEL.format("Student", str(s_key))
            else:
                response = R_SERV_ERR.format(
                    "Unable to retrieve Student entity")

        except Exception, e:
            response = R_SERV_ERR.format(str(e))
Пример #2
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.')
Пример #3
0
    def insertEnrollment(self, request):
        ''' Enrollment a student for a course in a given quarter'''
        response = ""
        # validate all the Referential properties
        s_key = ndb.Key(Student, request.student_id)
        student = s_key.get()
        c_key = ndb.Key(Course, request.course_id)

        # retrieve schedule id from schedule
        query = self.getSchedule(c_key, request.quarter, request.year)
        schedule = query.get()

        # Check if schedule exists
        if schedule:
            schedule_key = schedule.key
            # query for existing enrollment. if student id and schedule id exists, duplicate entry
            query = self.getEnrollment(s_key, schedule_key)
            enrollment = query.get()

            print("insertEnrollment method: Enrollment = ", enrollment)
            # check if enrollment exists already
            if not enrollment:
                # validate student ID
                if student:
                    # insert enrollment
                    enrollment = Enrollment(student=s_key,
                                            schedule=schedule_key)
                    enrollment.put()

                    # add course to student study lists
                    student.study_lists.append(c_key)
                    student.put()
                    response = STORED_RESPONSES.items[ADD_ID]
                else:
                    # in valid student ID
                    response = STORED_RESPONSES.items[INV_STUDENT_ID]
            else:
                # enrollment already exists
                response = STORED_RESPONSES.items[DUPLICATE_ID]
        else:
            response = STORED_RESPONSES.items[NOT_FOUND_ID]

        return response
Пример #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]
Пример #5
0
 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)
Пример #6
0
 def deleteSchedule(self, schedule):
     ''' delete a schedule given course ID quarter and year'''
     try:
         # Get Course id
         c_key = schedule.course
         # Get schedule id from given year, quarter and course
         '''query = Schedule.query(Schedule.course == c_key, Schedule.quarter == int(schedule.quarter), 
                                Schedule.year == int(schedule.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()
             response = R_SUCC_DEL.format(
                 "Schedule",
                 str(schedule.year) + "-" + str(schedule.quarter) + "-" +
                 str(schedule.course_id))
         else:
             response = R_SERV_ERR.format(
                 "Unable to read data from Schedule Entity")
     except Exception, e:
         response = R_SERV_ERR.format(str(e))
Пример #7
0
    def insertEnrollment(self, request):
        ''' Enrollment a student for a course in a given quarter'''
        response = ""

        # read all input variables
        student_id = request.get('student_id')
        course_id = request.get('course_id')
        year = request.get('year')
        quarter = request.get('quarter')

        # check for required fields
        if (student_id and course_id and year and quarter):

            # validate inputs
            if (is_int(student_id) and is_int(course_id)):
                if (is_int(year) and is_int(quarter)):

                    student_id = int(student_id)
                    course_id = int(course_id)
                    year = int(year)
                    quarter = int(quarter)
                    # validate all the Referential properties
                    s_key = ndb.Key(Student, student_id)
                    student = s_key.get()
                    c_key = ndb.Key(Course, course_id)

                    # retrieve schedule id from schedule
                    query = self.getSchedule(c_key, quarter, year)
                    schedule = query.get()

                    # Check if schedule exists
                    if schedule:
                        #schedule_key = schedule.key
                        # query for existing enrollment. if student id and schedule id exists, duplicate entry
                        query = self.getEnrollment(s_key, schedule.key)
                        enrollment = query.get()

                        # check if enrollment exists already
                        if not enrollment:
                            # validate student ID
                            if student:
                                # insert enrollment
                                enrollment = Enrollment(student=s_key,
                                                        schedule=schedule.key)
                                e_key = enrollment.put()

                                # add course to student study lists
                                student.study_lists.append(c_key)
                                student.put()
                                response = R_SUCC_INSERT.format(
                                    "Enrollments", str(e_key))
                            else:
                                # in valid student ID
                                response = R_DEP_ERR_INVST
                        else:
                            # enrollment already exists
                            response = R_DUP_ID_ERR.format("Enrollment")
                    else:
                        response = R_INV_ID_ERR.format("Enrollments")

                else:
                    response = R_PROP_VAL_ERR.format(
                        "Year and Quarter",
                        "integers of values (previous year, current year and next year) and range(1-4)"
                    )
            else:
                response = R_ID_VAL_ERR.format("Student and Course")
        else:
            response = R_REQ_FIELD_ERR.format(
                "Student ID, Course ID, Year and Quarter")
        return response
Пример #8
0
 def getEnrollment(self, student_key, schedule_key):
     return Enrollment.query(Enrollment.student == student_key,
                             Enrollment.schedule == schedule_key)