def get_all_professors(): try: designation = request.args.get('designation') gname = request.args.get('groupName') if designation or gname: logging.error(designation) logging.error(gname) output = [] if gname: grp = ResearchGroup.objects.get_or_404(name=gname) professors = Professor.objects(researchGroups=grp) for prof in professors: output.append({'name': prof.name, 'email': prof.email}) elif designation: professors = Professor.objects(designation=designation) for prof in professors: output.append({'name': prof.name, 'email': prof.email}) else: output = {'message': 'Keyword not found'} else: output = {'message': 'Message body empty'} return Response(json.dumps(output), mimetype='application/json', status=200) except Exception as e: logging.exception(e) output = {'message': 'Server Error'} return output, 500 # Only for local testing without docker #app.run() # FLASK_APP=app.py FLASK_ENV=development flask run
def get_professor_by_designation_or_groupname(): designation = request.args.get('designation') groupname = request.args.get('groupName') if designation: # get professor by designation worked---------- professor_list = Professor.objects(designation=designation) if professor_list: response = [{ 'name': professor.name, 'email': professor.email } for professor in professor_list] response = jsonify(response) return response, 200 else: response = {'message': 'Not Found'} return response, 404 elif groupname: # get professors by groupname worked-------------- group = ResearchGroup.objects.get(name=groupname) professor_list = Professor.objects(researchGroups=group) if professor_list: response = [{ 'name': professor.name, 'email': professor.email } for professor in professor_list] response = jsonify(response) return response, 200 else: response = {'message': 'Not Found'} return response, 404
def add_group(): body = request.get_json() founder = body['founder'] group = ResearchGroup(**body).save() group_id = group.id Professor.objects(id=founder).update_one(push__researchGroups=group_id) output = {'message': 'Group successfully created', 'id': str(group_id)} return output, 201
def get_all_professors(): keys = [key for key in request.args.keys()] if keys: if keys[0] == 'designation': value = request.args.get(keys[0]) if value: professors = Professor.objects(designation=value).to_json() all_prof = json.loads(professors) if all_prof: output = [{ 'name': str(p['name']), 'email': str(p['email']), 'designation': str(p['designation']) } for p in all_prof] return jsonify(output), 200 else: output = {'message': 'Invalid designation'} return output, 200 else: output = {'message': 'Params value empty'} return output, 200 elif 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] professors = Professor.objects( researchGroups=group_id[0]).to_json() all_prof = json.loads(professors) output = [{ 'name': str(p['name']), 'email': str(p['email']), 'designation': str(p['designation']) } for p in all_prof] 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 else: professors = Professor.objects().to_json() all_prof = json.loads(professors) output = [{ 'name': str(p['name']), 'email': str(p['email']), 'designation': str(p['designation']) } for p in all_prof] return jsonify(output), 200
def add_professor(): print(request) body = request.get_json() if (request.args.get('researchGroups')): rg = (body['researchGroups']) rg = [ObjectId(rg_id) for rg_id in rg] body['researchGroups'] = rg professor = Professor(**body) professor.save() id = professor.id output = {'message': "Professor successfully created", 'id': str(id)} print(type((output))) return (output), 201
def testing(name): body = {'name': name, 'designation': 'Professor', 'email': '*****@*****.**'} professor = Professor(**body).save() id = professor.id r = ResearchGroup(**{ 'name': 'jokfgj', 'description': 'dkfhjdfd', 'founder': professor }).save() Professor.objects(id=id).update(researchGroups=[r]) return {'id': str(id)}, 200
def delete_professor(prof_id): print(prof_id) professor = Professor.objects(id=prof_id) output = {'message': "Professor successfully deleted", 'id': str(prof_id)} professor.delete() return output, 200
def list_professors(): designation = request.args.get('designation') professors = None if designation is None: professors = Professor.objects() else: professors = Professor.objects(designation=designation) res = list( map( lambda p: { 'name': p.name, 'email': p.email, 'designation': p.designation }, professors)) return Response(json.dumps(res), mimetype="application/json", status=200)
def add_professor(): # add professor worked----------- body = request.get_json() professor = Professor(**body).save() response = { 'message': 'Professor successfully created', 'id': str(professor.id) } return response, 201
def get_professor_by_property(): designation = request.args.get('designation') if designation: professors = Professor.objects(designation=designation) output = [{ 'name': professor.name, 'email': professor.email } for professor in professors] else: groupName = request.args.get('groupName') research_group = ResearchGroup.objects.get(name=groupName) print(research_group.name) professors = Professor.objects(researchGroups=research_group) output = [{ 'name': professor.name, 'email': professor.email } for professor in professors] return jsonify(output), 200
def add_professor(): # Update the code here. body = request.get_json() professor = Professor(**body).save() output = { 'message': "Professor successfully created", 'id': str(professor.id) } # Update the status code status_code = 201 return output, status_code
def update_professor(prof_id): professor = Professor.objects(id=prof_id) body = request.get_json() try: rg = body["researchGroups"] rg = [ObjectId(rg_id) for rg_id in rg] print(rg) body['researchGroups'] = rg except KeyError: pass finally: professor.update(**body) output = { 'message': "Professor successfully updated", 'id': str(prof_id) } return output, 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 get_professor(prof_id): professors = None try: professors = Professor.objects(id=prof_id) except: app.logger.info("catching error") return abort(404) if len(professors) == 0: return abort(404) p = professors[0] res = { "name": p.name, "email": p.email, "designation": p.designation, "interests": p.interests } app.logger.info("professor %s", res) return Response(json.dumps(res), mimetype="application/json", status=200)
def add_professor(): body = request.get_json() professor = Professor(**body).save() prof_id = professor.id output = {'message': 'Professor successfully created', 'id': str(prof_id)} return output, 201
def get_professor_by_id(prof_id): if request.method == "GET": professor = json.loads( Professor.objects().get_or_404(id=prof_id).to_json()) output = { 'name': str(professor['name']), 'email': str(professor['email']), 'designation': str(professor['designation']), 'interests': professor['interests'] } return jsonify(output), 200 elif request.method == "PUT": body = request.get_json() keys = body.keys() if body and keys: if 'researchGroups' in keys: for group_id in body['researchGroups']: Professor.objects(id=prof_id).update( push__researchGroups=ObjectId((group_id))) if 'name' in keys: Professor.objects(id=prof_id).update(set__name=body['name']) if 'designation' in keys: Professor.objects(id=prof_id).update( set__designation=body['designation']) if 'email' in keys: Professor.objects(id=prof_id).update(set__email=body['email']) if 'interests' in keys: for interest in body['interests']: Professor.objects(id=prof_id).update( push__interests=interest) output = { 'message': 'Professor successfully updated', 'id': str(prof_id) } else: output = {'message': 'Message body is empty'} return output, 200 elif request.method == "DELETE": Professor.objects().get(id=prof_id).delete() groups = ResearchGroup.objects(founder=prof_id) if groups: group_ids = [group.id for group in groups] ResearchGroup.objects(founder=prof_id).delete() for gid in group_ids: Professor.objects(researchGroups=ObjectId(gid)).update( pull__researchGroups=ObjectId(gid)) output = { 'message': 'Professor successfully deleted', 'id': str(prof_id) } return output, 200
def testing2(): professors = Professor.objects().to_json() return Response(professors, mimetype="application/json", status=200)