Browse Source

rating system based on location

old
Efe Aydın 6 years ago
parent
commit
cbfab8485b
2 changed files with 36 additions and 18 deletions
  1. +5
    -0
      server_side/api/modules/databases/ratings.json
  2. +31
    -18
      server_side/api/modules/rating_system.py

+ 5
- 0
server_side/api/modules/databases/ratings.json View File

@ -4,6 +4,11 @@
"name": "Bus Service", "name": "Bus Service",
"desc": "Our bus service in Ankara", "desc": "Our bus service in Ankara",
"img": "/img/voting.jpg", "img": "/img/voting.jpg",
"location": {
"latitude": 39.9404,
"longitude": 32.9101,
"diff": 0.3
},
"rates": [ "rates": [
{ {
"id": 1, "id": 1,


+ 31
- 18
server_side/api/modules/rating_system.py View File

@ -20,16 +20,30 @@ with open(user_db, 'r') as f:
class Ratings(Resource): class Ratings(Resource):
def get(self): def get(self):
rating = [
{
'id' : v['id'],
'name': v['name'],
'desc': v['desc'],
'img' : v['img']
}
for v in ratings
]
return rating
"""
Example URL Query:
latitude=<latitude>&
longitude=<longitude>
"""
latitude = float(request.args['latitude'])
longitude = float(request.args['longitude'])
ret_data = []
for rating in ratings:
diff = rating['location']['diff']
rating_latitude = rating['location']['latitude']
rating_longitude = rating['location']['longitude']
if (rating_latitude - diff) < latitude < (rating_latitude + diff) and \
(rating_longitude - diff) < longitude < (rating_longitude + diff):
ret_data.append({
'id': rating['id'],
'name': rating['name'],
'desc': rating['desc'],
'img': rating['img']
})
return ret_data
def post(self): def post(self):
""" """
@ -65,24 +79,23 @@ class Rating(Resource):
abort(404, error="Rating {} doesn't exist".format(rating_id)) abort(404, error="Rating {} doesn't exist".format(rating_id))
class Rate(Resource): class Rate(Resource):
def get(self):
def post(self):
""" """
Example URL Query:
/rate?
Example POST Data:
rating_id=<rating_id>& rating_id=<rating_id>&
score=<score>& score=<score>&
note=<note>& # ADDITIONAL note=<note>& # ADDITIONAL
rater_id=<user_id> rater_id=<user_id>
""" """
if utils.find_by_id( users.values(), request.args[ 'rater_id' ] ):
rating_id = int(request.args['rating_id'])
score = int(request.args['score'])
if utils.find_by_id( users.values(), request.form[ 'rater_id' ] ):
rating_id = int(request.form['rating_id'])
score = int(request.form['score'])
if 0 >= score >= 10: if 0 >= score >= 10:
abort(500, 'Score should be between 0 and 10') abort(500, 'Score should be between 0 and 10')
note = request.args.get('note')
note = request.form.get('note')
ratings[rating_id - 1]['rates'].append({ ratings[rating_id - 1]['rates'].append({
'id': len(ratings[rating_id - 1]['rates']) + 1, 'id': len(ratings[rating_id - 1]['rates']) + 1,
'rater': request.args['rater_id'],
'rater': request.form['rater_id'],
'score': score, 'score': score,
'note': note 'note': note
}) })


Loading…
Cancel
Save