Browse Source

rating system created, main app created, voting system improved

pull/2/head
Efe Aydın 6 years ago
committed by Yiğit Çolakoğlu
parent
commit
ec48e67bd0
5 changed files with 145 additions and 10 deletions
  1. +23
    -0
      server_side/api/app.py
  2. +0
    -0
      server_side/api/images/voting.jpg
  3. +86
    -0
      server_side/api/rating_system/rating_system.py
  4. +30
    -0
      server_side/api/rating_system/ratings.json
  5. +6
    -10
      server_side/api/voting_system/voting_system.py

+ 23
- 0
server_side/api/app.py View File

@ -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/<path:path>')
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/<int:voting_id>')
api.add_resource(voting_system.Vote, '/vote', '/vote/')
api.add_resource(rating_system.Ratings, '/ratings', '/ratings/')
api.add_resource(rating_system.Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
api.add_resource(rating_system.Rate, '/rate', '/rate/')
app.run(host='0.0.0.0', port=5000)

server_side/api/voting_system/images/voting.jpg → server_side/api/images/voting.jpg View File


+ 86
- 0
server_side/api/rating_system/rating_system.py View File

@ -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=<rating_name>&
desc=<rating_desc>& # OPTIONAL
img=<rating_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=<rating_id>>&
score=<score>&
note=<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/<int:rating_id>', '/ratings/<int:rating_id>/')
api.add_resource(Rate, '/rate', '/rate/')
app.run(host='0.0.0.0', port=5000)

+ 30
- 0
server_side/api/rating_system/ratings.json View File

@ -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
}
]
}
]

server_side/api/voting_system/app.py → server_side/api/voting_system/voting_system.py View File


Loading…
Cancel
Save