From ec48e67bd0bbd4b0c0994464a7870f3c030b6d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Mon, 18 Mar 2019 09:07:08 +0300 Subject: [PATCH] rating system created, main app created, voting system improved --- server_side/api/app.py | 23 +++++ .../api/{voting_system => }/images/voting.jpg | Bin .../api/rating_system/rating_system.py | 86 ++++++++++++++++++ server_side/api/rating_system/ratings.json | 30 ++++++ .../{app.py => voting_system.py} | 16 ++-- 5 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 server_side/api/app.py rename server_side/api/{voting_system => }/images/voting.jpg (100%) create mode 100644 server_side/api/rating_system/rating_system.py create mode 100644 server_side/api/rating_system/ratings.json rename server_side/api/voting_system/{app.py => voting_system.py} (83%) diff --git a/server_side/api/app.py b/server_side/api/app.py new file mode 100644 index 0000000..8af748f --- /dev/null +++ b/server_side/api/app.py @@ -0,0 +1,23 @@ +from flask import Flask +from flask_restful import Resource, Api + +from voting_system import voting_system +from rating_system import rating_system + +app = Flask(__name__) +api = Api(app) + +@app.route('/img/') +def send_img(path): + return send_from_directory('images', path) + +if __name__ == '__main__': + api.add_resource(voting_system.Votings, '/votings', '/votings/') + api.add_resource(voting_system.Voting, '/votings/') + api.add_resource(voting_system.Vote, '/vote', '/vote/') + + api.add_resource(rating_system.Ratings, '/ratings', '/ratings/') + api.add_resource(rating_system.Rating, '/ratings/', '/ratings//') + api.add_resource(rating_system.Rate, '/rate', '/rate/') + + app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/voting_system/images/voting.jpg b/server_side/api/images/voting.jpg similarity index 100% rename from server_side/api/voting_system/images/voting.jpg rename to server_side/api/images/voting.jpg diff --git a/server_side/api/rating_system/rating_system.py b/server_side/api/rating_system/rating_system.py new file mode 100644 index 0000000..7f2a966 --- /dev/null +++ b/server_side/api/rating_system/rating_system.py @@ -0,0 +1,86 @@ +import os +import json + +from flask import Flask, request +from flask_restful import Resource, Api + +app = Flask(__name__) +api = Api(app) + +with open(os.path.join(app.root_path, 'ratings.json'), 'r') as f: + ratings = json.load(f) + +class Ratings(Resource): + def get(self): + rating = [ + { + 'id' : v['id'], + 'name': v['name'], + 'desc': v['desc'], + 'img' : v['img'] + } + for v in ratings + ] + return rating + + def post(self): + """ + Example POST Data: + name=& + desc=& # OPTIONAL + img=& # OPTIONAL + """ + args = request.form + rating_id = len(ratings) + 1 + rating = { + 'id': rating_id, + 'name': args['name'], + 'desc': args.get('desc'), + 'img' : args.get('img'), + 'rates': [] + } + + ratings.append(rating) + + with open(os.path.join(app.root_path, 'ratings.json'), 'w') as f: + json.dump(ratings, f, indent=4) + + return rating + +class Rating(Resource): + def get(self, rating_id): + try: + return ratings[rating_id - 1] + except: + abort(404, error="Rating {} doesn't exist".format(rating_id)) + +class Rate(Resource): + def get(self): + """ + Example URL Query: + /rate? + rating_id=>& + score=& + note= # ADDITIONAL + """ + rating_id = int(request.args['rating_id']) + score = int(request.args['score']) + if 0 >= score >= 10: + abort(500, 'Score should be between 0 and 10') + note = request.args.get('note') + ratings[rating_id - 1]['rates'].append({ + 'id': len(ratings[rating_id - 1]['rates']) + 1, + 'score': score, + 'note': note + }) + with open(os.path.join(app.root_path, 'ratings.json'), 'w') as f: + json.dump(ratings, f, indent=4) + + return ratings[rating_id - 1] + +if __name__ == '__main__': + api.add_resource(Ratings, '/ratings', '/ratings/') + api.add_resource(Rating, '/ratings/', '/ratings//') + api.add_resource(Rate, '/rate', '/rate/') + + app.run(host='0.0.0.0', port=5000) diff --git a/server_side/api/rating_system/ratings.json b/server_side/api/rating_system/ratings.json new file mode 100644 index 0000000..eadb56e --- /dev/null +++ b/server_side/api/rating_system/ratings.json @@ -0,0 +1,30 @@ +[ + { + "id": 1, + "name": "Bus Service", + "desc": "Our bus service in Ankara", + "img": "/img/voting.jpg", + "rates": [ + { + "id": 1, + "score": 10, + "note": "IT WAS AWESOME" + }, + { + "id": 2, + "score": 0, + "note": "The worst bus ride I have ever had" + }, + { + "id": 3, + "score": 7, + "note": null + }, + { + "id": 4, + "score": 1, + "note": null + } + ] + } +] \ No newline at end of file diff --git a/server_side/api/voting_system/app.py b/server_side/api/voting_system/voting_system.py similarity index 83% rename from server_side/api/voting_system/app.py rename to server_side/api/voting_system/voting_system.py index 4f46742..b3928d7 100644 --- a/server_side/api/voting_system/app.py +++ b/server_side/api/voting_system/voting_system.py @@ -1,7 +1,7 @@ import os import json -from flask import Flask, send_from_directory, request +from flask import Flask, request from flask_restful import Resource, Api, abort app = Flask(__name__) @@ -47,7 +47,7 @@ class Votings(Resource): 'img' : args.get('img'), 'votes': [ { - 'id' : k, + 'id' : k + 1, 'name': vote['name'], 'desc': vote.get('desc'), 'votes': 0 @@ -85,13 +85,9 @@ class Vote(Resource): return votings[voting_id - 1] -api.add_resource(Votings, '/votings', '/votings/') -api.add_resource(Voting, '/votings/') -api.add_resource(Vote, '/vote', '/vote/') - -@app.route('/img/') -def send_img(path): - return send_from_directory('images', path) - if __name__ == '__main__': + api.add_resource(Votings, '/votings', '/votings/') + api.add_resource(Voting, '/votings/', '/votings//') + api.add_resource(Vote, '/vote', '/vote/') + app.run(host='0.0.0.0', port=5000)