예제 #1
0
def insert_student(version):
    """
    Controller for API Function that inserts new cakes in the database
    @return: Response and HTTP code
    """

    # GET POST DATA
    s_fname = request.form['fname']
    s_lname = request.form['lname']
    s_org_id = request.form['organization_id']
    s_assign_id = request.form['assign_id']
    s_reputation = request.form['reputation']


    # API Version 1.X
    if math.floor(version) == 1:

        student = Student(fname=s_fname, lname=s_lname, org_id=s_org_id,
                          assign_id=s_assign_id, reputation=s_reputation)
        db.session.add(student)

        try:
            db.session.commit()
        except IntegrityError as ex:
            return jsonify(errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR

        student_schema = StudentSchema()
        result = student_schema.dump(student)
        return jsonify(
            responses.create_multiple_object_response('success', result.data,
                                                      'student')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(version)), statuscodes.HTTP_VERSION_UNSUPPORTED
예제 #2
0
def insert_cake(version):
    """
    Controller for API Function that inserts new cakes in the database
    @return: Response and HTTP code
    """

    # GET POST DATA
    cakename = request.form['cakename']
    baker = request.form['baker']
    price = request.form['price']

    # API Version 1.X
    if math.floor(version) == 1:

        cake = Cake(cakename, baker, price)
        db.session.add(cake)

        try:
            db.session.commit()
        except IntegrityError as ex:
            return jsonify(
                errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR

        return jsonify(
            responses.create_multiple_object_response(
                'success',
                CakeSerializer(cake).data, 'cakes')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_assignment(version):
    """
    Controller for API Function that inserts new cakes in the database
    @return: Response and HTTP code
    """

    # GET POST DATA
    assign_name = request.form['name']

    # API Version 1.X
    if math.floor(version) == 1:

        assignment = Assignment(name=assign_name)
        db.session.add(assignment)

        try:
            db.session.commit()
        except IntegrityError as ex:
            return jsonify(
                errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR

        assign_schema = AssignmentSchema()
        result = assign_schema.dump(assignment)
        return jsonify(
            responses.create_multiple_object_response(
                'success', result.data, 'assignment')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
예제 #4
0
def insert_org(version):
    """
    Controller for API Function that inserts new organization in the database
    @return: Response and HTTP code
    """

    # GET POST DATA
    org_name = request.form['name']
    org_address = request.form['address']

    # API Version 1.X
    if math.floor(version) == 1:

        org = Organization(name=org_name, address=org_address)
        db.session.add(org)

        try:
            db.session.commit()
        except IntegrityError as ex:
            return jsonify(
                errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR

        org_schema = OrganizationSchema()
        result = org_schema.dump(org)
        return jsonify(
            responses.create_multiple_object_response(
                'success', result.data, 'organization')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def insert_score(version):
    """
    Controller for API Function that inserts new score in the database
    @return: Response and HTTP code
    """

    # GET POST DATA
    student_id = request.form['student_id']
    assignment_id = request.form['assignment_id']
    organization_id = request.form['organization_id']
    task_id = request.form['task_id']
    review_type = request.form['review_type']
    score = request.form['score']
    max_score = request.form['max_score']

    # API Version 1.X
    if math.floor(version) == 1:

        score = Score(student_id=student_id,
                      assignment_id=assignment_id,
                      organization_id=organization_id,
                      task_id=task_id,
                      review_type=review_type,
                      score=score,
                      max_score=max_score)
        db.session.add(score)

        try:
            db.session.commit()
        except IntegrityError as ex:
            return jsonify(
                errors.error_commit_error(ex)), statuscodes.HTTP_INTERNAL_ERROR

        score_schema = ScoreSchema()
        result = score_schema.dump(score)
        return jsonify(
            responses.create_multiple_object_response(
                'success', result.data, 'score')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
예제 #6
0
def get_all_students(version):
    """
    Controller for API Function that gets all students in the database.
    @return: Response and HTTP code
    """

    # API Version 1.X
    if math.floor(version) == 1:

        students = Student.query.all()
        student_schema = StudentSchema(only=("id", "fname", "lname", "organization_id", "assign_id", "reputation"),
                                          # Here you can filter out stuff.
                                          many=True)
        results = student_schema.dump(students)
        return jsonify(
            responses.create_multiple_object_response('success', results.data, 'students')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(version)), statuscodes.HTTP_VERSION_UNSUPPORTED
예제 #7
0
def get_all_orgs(version):
    """
    Controller for API Function that gets all organizations in the database.
    @return: Response and HTTP code
    """

    # API Version 1.X
    if math.floor(version) == 1:

        organizations = Organization.query.all()
        org_schema = OrganizationSchema(
            only=("id", "name", "address"),
            # Here you can filter out stuff.
            many=True)
        results = org_schema.dump(organizations)
        return jsonify(
            responses.create_multiple_object_response(
                'success', results.data, 'organizations')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
예제 #8
0
def get_all_cakes(version):
    """
    Controller for API Function that gets all cakes in the database.
    @return: Response and HTTP code
    """

    # API Version 1.X
    if math.floor(version) == 1:

        cakes = Cake.query.all()
        serialized_cakes = CakeSerializer(
            cakes,
            only=("id", "cakename", "bakername", "price", "price_range"),
            # Here you can filter out stuff.
            many=True)
        return jsonify(
            responses.create_multiple_object_response(
                'success', serialized_cakes.data,
                'cakes')), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED