def get_student_by_id(student_id): """ Get (read), update or delete a student Args: student_id (Object id): The student Id of the student record that nees to be modified. Returns: dict: The dictionary with output values int : The status code """ if request.method == 'GET': # get studeny by id worked--------------- student = Student.objects.get(id=student_id) if student: # Update Code here researchgroup_ids = [str(tmp.id) for tmp in student.researchGroups] response = { 'name': str(student.name), 'studentNumber': str(student.studentNumber), 'researchGroups': researchgroup_ids } response = jsonify(response) return response, 200 # output = {'name': "", 'studentNumber': "", 'researchGroups': ""} else: # Update Code here response = {'message': 'Not Found'} return response, 404 elif request.method == 'PUT': # put worked----------------------- body = request.get_json() if body: # Update Code here if 'researchGroups' in body: researchgroup_ids = [ ObjectId(tmp) for tmp in body['researchGroups'] ] body['researchGroups'] = researchgroup_ids Student.objects(id=student_id).update(**body) response = { 'message': 'Student successfully updated', 'id': str(student_id) } return response, 200 else: # Update Code here response = {'message': 'No Content'} return response, 204 elif request.method == 'DELETE': # delete worked----------------- Student.objects.get_or_404(id=student_id).delete() response = { 'message': 'Student successfully deleted', 'id': str(student_id) } return response, 200
def get_group_by_id(group_id): if request.method == "GET": group = json.loads( ResearchGroup.objects(id=group_id).get_or_404().to_json()) output = { 'id': str(group_id), 'name': str(group['name']), 'founder': str(group['founder']['$oid']) } return jsonify(output), 200 elif request.method == "PUT": body = request.get_json() keys = body.keys() if body and keys: if 'name' in keys: ResearchGroup.objects(id=group_id).update( set__name=body['name']) if 'founder' in keys: prof_ids = [prof.id for prof in Professor.objects()] if ObjectId(body['founder']) in prof_ids: ResearchGroup.objects(id=group_id).update( set__founder=ObjectId(body['founder'])) Professor.objects(id=body['founder']).update( push__researchGroups=ObjectId(group_id)) else: output = {'message': 'Invalid founder ID'} return output, 404 if 'description' in keys: ResearchGroup.objects(id=group_id).update( set__description=body['description']) output = { 'message': 'Group successfully updated', 'id': str(group_id) } else: output = {'message': 'Message body is empty'} return output, 200 elif request.method == "DELETE": ResearchGroup.objects().get(id=group_id).delete() founder = Professor.objects(researchGroups=group_id) if founder: Professor.objects(researchGroups=group_id).update( pull__researchGroups=ObjectId(group_id)) student = Student.objects(researchGroups=group_id) if student: Student.objects(researchGroups=group_id).update( pull__researchGroups=ObjectId(group_id)) output = {'message': 'Group successfully deleted', 'id': str(group_id)} return output, 200
def All_students(): collection = db.Student # students =[doc for doc in Student.objects().all()] data = [] for i in Student.objects().all(): dataJson = i.to_json() res = json.loads(dataJson) data.append(res) cur = collection.find() if cur.count()==0: # no data exist return jsonify({ "success": False , },404) else: return jsonify({ "success": True , "data": data })
def get_all_student(): keys = [key for key in request.args.keys()] if keys: if keys[0] == 'groupName': value = request.args.get(keys[0]) if value: groups = ResearchGroup.objects(name=value) if groups: group_id = [group.id for group in groups] students = Student.objects( researchGroups=group_id[0]).to_json() all_students = json.loads(students) output = [{ 'name': str(p['name']), 'studentNumber': str(p['studentNumber']) } for p in all_students] return jsonify(output), 200 else: output = {'message': 'Invalid groupName'} return output, 200 else: output = {'message': 'Params value empty'} return output, 200 else: output = {'message': 'Incorrect search key'} return output, 200
def get_all_students(): """ This function lists all students """ try: gname = request.args.get('groupName') if gname: logging.error(gname) grp = ResearchGroup.objects.get(name=gname) students = Student.objects(researchGroups=grp) output = [] for st in students: output.append({ 'name': st.name, 'studentNumber': st.studentNumber }) else: output = {'message': 'Empty or Invalid body'} return Response(json.dumps(output), mimetype='application/json', status=200) except Exception as e: logging.exception(e) output = {'message': 'Server Error'} return output, 500
def get_students_by_property(): groupName = request.args.get('groupName') research_group = ResearchGroup.objects.get(name=groupName) students = Student.objects(researchGroups=research_group) output = [{ 'name': student.name, 'studentNumber': student.studentNumber } for student in students] return jsonify(output), 200
def sort_student(): # Order by ascending data data = [] sorted_data = Student.objects().order_by('name') for i in sorted_data: dataJson = i.to_json() res = json.loads(dataJson) data.append(res) # equivalent to .order_by('+date') return jsonify({ "success": True , "message":data })
def get_student_by_groupname(): # get students by groupname worked----------- groupname = request.args.get('groupName') if groupname: group = ResearchGroup.objects.get(name=groupname) student_list = Student.objects(researchGroups=group) response = [{ 'name': student.name, 'studentNumber': student.studentNumber } for student in student_list] response = jsonify(response) return response, 200 else: response = {'message': 'Not Found'} return response, 404
def update_students(student_id): body = request.get_json() student = Student.objects(id=student_id) try: rg = body["researchGroups"] rg = [ObjectId(rg_id) for rg_id in rg] print(rg) body['researchGroups'] = rg except KeyError: pass finally: student.update(**body) output = { 'message': "Student successfully updated", 'id': str(student_id) } return output, 200
def get_student_by_id(student_id): students = None try: students = Student.objects(id=student_id) except: app.logger.info("catching error") return abort(404) if len(students) == 0: return abort(404) s = students[0] res = { "name": s.name, "studentNumber": s.studentNumber, "researchGroups": s.researchGroups } return Response(json.dumps(res), mimetype="application/json", status=200)
def get_student_by_id(student_id): if request.method == "GET": student = json.loads( Student.objects().get_or_404(id=student_id).to_json()) if student: group_ids = [group['$oid'] for group in student['researchGroups']] output = { 'name': str(student['name']), 'studentNumber': str(student['studentNumber']), 'researchGroups': str(group_ids) } else: output = {'message': 'Invalid student ID'} return output, 200 elif request.method == "PUT": body = request.get_json() keys = body.keys() if body and keys: if 'name' in keys: Student.objects(id=student_id).update(set__name=body['name']) if 'studentNumber' in keys: Student.objects(id=student_id).update( set__studentNumber=body['studentNumber']) if 'researchGroups' in keys: for group_id in body['researchGroups']: Student.objects(id=student_id).update( push__researchGroups=ObjectId(group_id)) output = { 'message': 'Student successfully updated', 'id': str(student_id) } else: output = {'message': 'Message body empty'} return output, 200 elif request.method == "DELETE": Student.objects.get_or_404(id=student_id).delete() output = { 'message': 'Student successfully deleted', 'id': str(student_id) } return output, 200
def get_students(): students = Student.objects().to_json() return Response(students, mimetype="application/json", status=200)
def delete_student(id): Student.objects().get(id=id).delete() return "Deleted", 200
def update_student(id): body = request.get_json() Student.objects().get(id=id).update(**body) return "updated", 200
def get_all_students(): students = jsonify(json.loads(Student.objects().to_json())) students.headers.add('Access-Control-Allow-Origin', '*') return students, 200
def get_students(): students = json.loads(Student.objects().to_json()) return render_template("datatable.html", data=students)