Example #1
0
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 add_research_group():
    body = request.get_json()
    body['founder'] = ObjectId(body['founder'])

    research_group = ResearchGroup(**(body))
    # research_group.founder = professor
    research_group.save()
    output = {
        'message': "Group successfully created",
        'id': str(research_group.id)
    }

    return jsonify(output), 201
Example #3
0
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
Example #4
0
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
Example #5
0
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_all_groups():
    """
    This function lists all groups
    """

    groups = ResearchGroup.objects().to_json()
    output = groups
    # Update the status code
    status_code = 200
    return output, status_code
def add_group():

    # Update the code here.
    body = request.get_json()
    group = ResearchGroup(**body).save()
    output = {'message': "Group successfully created", 'id': str(group.id)}
    # Update the status code
    return Response(json.dumps(output),
                    mimetype='application/json',
                    status=201)
Example #8
0
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
Example #9
0
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
Example #10
0
def add_researchgroup():
    # add researchgroup worked--------------
    body = request.get_json()
    researchgroup = ResearchGroup(**body).save()

    if researchgroup:
        response = {
            'message': 'Group successfully created',
            'id': str(researchgroup.id)
        }
        return response, 201
    else:
        response = {'message': 'Conflict'}
        return response, 409
def update_group(group_id):
    group = ResearchGroup.objects(id=group_id)
    body = request.get_json()
    try:

        rg = body['founder']
        print(body['founder'])
        rg = ObjectId(rg)
        body['founder'] = rg
    except KeyError:
        pass
    finally:
        print(body)
        output = {'message': "Group successfully updated", 'id': str(group_id)}
        group.update(**body)
        return output, 200
Example #12
0
def get_group_by_id(group_id):
    groups = None

    try:
        groups = ResearchGroup.objects(id=group_id)
    except:
        return abort(404)

    if len(groups) == 0:
        return abort(404)

    g = groups[0]

    res = {
        #"id": g.name,
        name: g.name
    }
    return Response(json.dumps(res), mimetype="application/json", status=200)
Example #13
0
def testing3():
    groups = ResearchGroup.objects().to_json()
    return Response(groups, mimetype="application/json", status=200)