Пример #1
0
def delete_cake(version, cake_id):
    """
    Controller for API Function that gets a cake by ID
    @param cake_id: cake id
    @return: Response and HTTP code
    """

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

        cake = Cake.query.filter_by(id=cake_id).first()

        if cake is None:
            return jsonify(
                errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND

        db.session.delete(cake)

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

        return jsonify(
            responses.create_single_object_response(
                'success',
                CakeSerializer(cake).data, "cake")), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
Пример #2
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
Пример #3
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
Пример #4
0
def delete_student(version, student_id):
    """
    Controller for API Function that deletes a student by ID
    @param student_id: student id
    @return: Response and HTTP code
    """

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

        student = Student.query.filter_by(id=student_id).first()

        if student is None:
            return jsonify(errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND

        db.session.delete(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_single_object_response('success', result.data, "student")), 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
Пример #6
0
def yahoo_weather(version, woeid):
    """
    Controller for API Function that checks the weather from a woeid
    @param woeid: yahoo woeid
    @return: Response and HTTP code
    """

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

        url = create_api_url_forecastrss(woeid, True)
        http = httplib2.Http()
        response, content = http.request(url)
        parsed_content = xmltodict.parse(content)
        cleaned_parsed_content = parsed_content["rss"][
            "channel"]  # Clean out silly structure from yahoo.
        data_content = filters.filter_nested_dicts_values(
            cleaned_parsed_content, constants.REMOTE_META_KEYS)
        return jsonify(
            responses.create_single_object_response(
                "success", data_content, "weather")), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
Пример #7
0
def delete_org(version, org_id):
    """
    Controller for API Function that gets an organization by ID
    @param org_id: org id
    @return: Response and HTTP code
    """

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

        org = Organization.query.filter_by(id=org_id).first()

        if org is None:
            return jsonify(
                errors.error_object_not_found()), statuscodes.HTTP_NOT_FOUND

        db.session.delete(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_single_object_response(
                'success', result.data, "organization")), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
Пример #8
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
Пример #9
0
def get_student(version, student_id):
    """
    Controller for API Function that gets a student by ID
    @param student_id: student id
    @return: Response and HTTP cod
    """

    if math.floor(version) == 1:
        student = Student.query.filter_by(id=student_id).first()

        if student is None:
            return jsonify(errors.error_object_not_found(), statuscodes.HTTP_NOT_FOUND)

        student_schema = StudentSchema()
        result = student_schema.dump(student)
        return jsonify(
            responses.create_single_object_response('success', result.data, "student")), statuscodes.HTTP_OK
    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
Пример #11
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
Пример #12
0
def yahoo_weather(version, woeid):
    """
    Controller for API Function that checks the weather from a woeid
    @param woeid: yahoo woeid
    @return: Response and HTTP code
    """

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

        url = create_api_url_forecastrss(woeid, True)
        http = httplib2.Http()
        response, content = http.request(url)
        parsed_content = xmltodict.parse(content)
        cleaned_parsed_content = parsed_content["rss"]["channel"]  # Clean out silly structure from yahoo.
        data_content = filters.filter_nested_dicts_values(cleaned_parsed_content, constants.REMOTE_META_KEYS)
        return jsonify(
            responses.create_single_object_response("success", data_content, "weather")), statuscodes.HTTP_OK

    # Unsupported Versions
    else:
        return jsonify(errors.error_incorrect_version(version)), statuscodes.HTTP_VERSION_UNSUPPORTED
Пример #13
0
def get_organization(version, org_id):
    """
    Controller for API Function that gets a organization by ID
    @param org_id: org id
    @return: Response and HTTP cod
    """

    if math.floor(version) == 1:
        org = Organization.query.filter_by(id=org_id).first()

        if org is None:
            return jsonify(errors.error_object_not_found(),
                           statuscodes.HTTP_NOT_FOUND)

        org_schema = OrganizationSchema()
        result = org_schema.dump(org)
        return jsonify(
            responses.create_single_object_response(
                'success', result.data, "organization")), statuscodes.HTTP_OK
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
def get_sapa_factor(version, team_id, student_id, self_score_id,
                    team_score_id):
    if math.floor(version) == 1:
        team = Team.query.filter_by(id=team_id).first()
        student = Student.query.filter_by(id=student_id).first()
        self_score = Score.query.filter_by(id=self_score_id).first()
        team_score = Score.query.filter_by(id=team_score_id).first()

        if self_score is None:
            return jsonify(errors.error_object_not_found(),
                           statuscodes.HTTP_NOT_FOUND)
        if team_score is None:
            return jsonify(errors.error_object_not_found(),
                           statuscodes.HTTP_NOT_FOUND)

        score_schema = ScoreSchema()
        result = sapa_factor(self_score=self_score, team_scores=team_score)
        return jsonify(
            responses.create_single_object_response(
                'success', result, "sapa factor")), statuscodes.HTTP_OK
    else:
        return jsonify(errors.error_incorrect_version(
            version)), statuscodes.HTTP_VERSION_UNSUPPORTED
Пример #15
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
Пример #16
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