From 34fafc0ce8a10468ea3e5c6ce38d6697784056e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Sat, 30 Mar 2019 23:23:57 +0300 Subject: [PATCH 1/2] updates --- server_side/api/app.py | 8 ++- .../api/modules/databases/ratings.json | 22 ++----- server_side/api/modules/databases/users.json | 55 ++++++++++++++-- .../api/modules/databases/votings.json | 6 +- server_side/api/modules/rating_system.py | 62 ++++++++++--------- server_side/api/modules/utility.py | 28 +++++++++ 6 files changed, 125 insertions(+), 56 deletions(-) create mode 100644 server_side/api/modules/utility.py diff --git a/server_side/api/app.py b/server_side/api/app.py index 1a1d3e1..db10a96 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -1,7 +1,7 @@ from flask import Flask, send_from_directory from flask_restful import Api -from api.modules import user_info, voting_system, rating_system +from api.modules import user_info, voting_system, rating_system, utility app = Flask(__name__) api = Api(app) @@ -12,7 +12,7 @@ def send_img(path): if __name__ == '__main__': - context = ('encryption/mycity.crt','encryption/mycity-decrypted.key') + context = ('encryption/mycity.crt', 'encryption/mycity-decrypted.key') api.add_resource( voting_system.Votings, '/votings', '/votings/' ) api.add_resource( voting_system.Voting, '/votings/' ) api.add_resource( voting_system.Vote, '/vote', '/vote/' ) @@ -24,5 +24,7 @@ if __name__ == '__main__': api.add_resource( user_info.Users, '/users', '/users/' ) api.add_resource( user_info.User, '/users/', '/users//' ) api.add_resource( user_info.Login, '/login', '/login/' ) + + api.add_resource(utility.Resources, '/resources', '/resources/') - app.run(host='0.0.0.0', port=5000, ssl_context=context) \ No newline at end of file + app.run(host='0.0.0.0', port=5000, ssl_context=context) diff --git a/server_side/api/modules/databases/ratings.json b/server_side/api/modules/databases/ratings.json index 1231874..fbebfd4 100644 --- a/server_side/api/modules/databases/ratings.json +++ b/server_side/api/modules/databases/ratings.json @@ -9,25 +9,13 @@ "longitude": 32.9101, "diff": 0.3 }, - "rates": [ - { - "id": 1, - "score": 10, - "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", - "note": "IT WAS AWESOME" - }, - { + "rates": { + "9vard12ty0ad2yvwp3q53rsf3h43r2vq": { "id": 2, "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", - "score": 10, - "note": null - }, - { - "id": 3, - "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", - "score": 5, - "note": null + "score": 8, + "note": "Fuck you" } - ] + } } ] \ No newline at end of file diff --git a/server_side/api/modules/databases/users.json b/server_side/api/modules/databases/users.json index b5019a8..6360859 100644 --- a/server_side/api/modules/databases/users.json +++ b/server_side/api/modules/databases/users.json @@ -11,11 +11,56 @@ "bus_usage_year": 67 }, "daily_electricity_usage": [ - 10, - 5, - 5, - 6, - 7 + 32, + 34, + 29, + 28, + 35, + 31, + 36, + 38, + 37, + 40, + 45, + 47, + 45, + 47, + 48, + 44, + 49, + 53, + 55, + 49, + 46, + 42, + 37, + 34 + ], + "daily_water_usage": [ + 32, + 34, + 29, + 28, + 35, + 31, + 36, + 38, + 37, + 40, + 45, + 47, + 45, + 47, + 48, + 44, + 49, + 53, + 55, + 49, + 46, + 42, + 37, + 34 ], "points": 50 } diff --git a/server_side/api/modules/databases/votings.json b/server_side/api/modules/databases/votings.json index 665ae40..017b9ca 100644 --- a/server_side/api/modules/databases/votings.json +++ b/server_side/api/modules/databases/votings.json @@ -25,12 +25,14 @@ "name": "wooting", "desc": "wooting desc", "img": "/img/voting.jpg", - "voters": [], + "voters": [ + "9vard12ty0ad2yvwp3q53rsf3h43r2vq" + ], "votes": { "1": { "name": "woote 1", "desc": "woote desc", - "votes": 2 + "votes": 3 }, "2": { "id": 1, diff --git a/server_side/api/modules/rating_system.py b/server_side/api/modules/rating_system.py index b8a069b..08cba22 100644 --- a/server_side/api/modules/rating_system.py +++ b/server_side/api/modules/rating_system.py @@ -18,15 +18,16 @@ with open(db_path, 'r') as f: with open(user_db, 'r') as f: users = json.load(f) + class Ratings(Resource): - def get(self): + def post(self): """ - Example URL Query: + Example POST Data: latitude=& longitude= """ - latitude = float(request.args['latitude']) - longitude = float(request.args['longitude']) + latitude = float(request.form['latitude']) + longitude = float(request.form['longitude']) ret_data = [] for rating in ratings: @@ -45,29 +46,30 @@ class Ratings(Resource): return ret_data - 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(db_path, 'w') as f: - json.dump(ratings, f, indent=4) + # 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(db_path, 'w') as f: + # json.dump(ratings, f, indent=4) + # + # return rating - return rating class Rating(Resource): def get(self, rating_id): @@ -78,6 +80,7 @@ class Rating(Resource): except: abort(404, error="Rating {} doesn't exist".format(rating_id)) + class Rate(Resource): def post(self): """ @@ -93,12 +96,12 @@ class Rate(Resource): if 0 >= score >= 10: abort(500, 'Score should be between 0 and 10') note = request.form.get('note') - ratings[rating_id - 1]['rates'].append({ + ratings[rating_id - 1]['rates']['9vard12ty0ad2yvwp3q53rsf3h43r2vq'] = { 'id': len(ratings[rating_id - 1]['rates']) + 1, 'rater': request.form['rater_id'], 'score': score, 'note': note - }) + } with open(db_path, 'w') as f: json.dump(ratings, f, indent=4) @@ -106,9 +109,10 @@ class Rate(Resource): return {'error': 'User doesn\'t exists'} + 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) \ No newline at end of file + app.run(host='0.0.0.0', port=5000) diff --git a/server_side/api/modules/utility.py b/server_side/api/modules/utility.py new file mode 100644 index 0000000..65525e8 --- /dev/null +++ b/server_side/api/modules/utility.py @@ -0,0 +1,28 @@ +import os +import copy +import json + +from api.modules import utils + +from flask import Flask, request +from flask_restful import Resource, Api, abort + +app = Flask(__name__) +api = Api(app) +db_path = os.path.join(app.root_path, 'databases', 'users.json') + +with open(db_path, 'r') as f: + users = json.load(f) + +class Resources(Resource): + def post(self): + user = utils.find_by_id(users.values(), request.form['user_id']) + return { + 'daily_electricity_usage': user['daily_electricity_usage'], + 'daily_water_usage': user['daily_water_usage'] + } + +if __name__ == '__main__': + api.add_resource(Resources, '/resources', '/resources/') + + app.run(host='0.0.0.0', port=5000) From 279a79a98070dcf187dc8086c646450af006dd11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Sat, 30 Mar 2019 23:28:01 +0300 Subject: [PATCH 2/2] fixed an error with rating --- server_side/api/modules/rating_system.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server_side/api/modules/rating_system.py b/server_side/api/modules/rating_system.py index 08cba22..b7efc65 100644 --- a/server_side/api/modules/rating_system.py +++ b/server_side/api/modules/rating_system.py @@ -96,7 +96,7 @@ class Rate(Resource): if 0 >= score >= 10: abort(500, 'Score should be between 0 and 10') note = request.form.get('note') - ratings[rating_id - 1]['rates']['9vard12ty0ad2yvwp3q53rsf3h43r2vq'] = { + ratings[rating_id - 1]['rates'][rater_id] = { 'id': len(ratings[rating_id - 1]['rates']) + 1, 'rater': request.form['rater_id'], 'score': score,