From 6b7d2e15f502cd8d3218f7a45bcb2df708296821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Tue, 9 Apr 2019 13:32:33 +0300 Subject: [PATCH 1/2] bug fix --- server_side/api/modules/denunciation.py | 79 ++++++++++++++----------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/server_side/api/modules/denunciation.py b/server_side/api/modules/denunciation.py index 19fe87c..3583764 100644 --- a/server_side/api/modules/denunciation.py +++ b/server_side/api/modules/denunciation.py @@ -1,3 +1,5 @@ +from api.modules import utils + from flask import Flask, request from flask_restful import Resource, Api, abort @@ -7,47 +9,56 @@ import os app = Flask(__name__) api = Api(app) -db_path = os.path.join(app.root_path, 'databases', 'users.json') +db_path = os.path.join(app.root_path, 'databases', 'denunciations.json') with open(db_path, 'r') as f: - users = json.load(f) + denunciations = json.load(f) + +users_path = os.path.join(app.root_path, 'databases', 'users.json') +with open(users_path, 'r') as f: + users = json.load(f) class Alert(Resource): - def post(self): - args = request.form - username= "" - for user in users: - if users[user]["id"] == args["id"]: - username=user - break - trust = int(users[username]["trustability"]) - if trust > 20 or args["accepted"] == "true": - return {"success":True} - else: - return {"success":False,"penalty":"{}".format(100*(20-trust))} + def post(self): + args = request.form + username= "" + for user in users: + if users[user]["id"] == args["id"]: + username=user + break + trust = int(users[username]["trustability"]) + if trust > 20 or args["accepted"] == "true": + return {"success":True} + else: + return {"success":False,"penalty":"{}".format(100*(20-trust))} + class Denounce(Resource): def post(self): args = request.form reporter = args['id'] - denunciation_info = args['info'] - denunciation_priority = args['priority'] - denunciation_location = { - "latitude": args['latitude'], - "longitude": args['longitude'] - } - - denunciation = { - 'reporter': reporter, - 'info': denunciation_info, - 'priority': denunciation_priority, - 'location': denunciation_location - } - - denunciations.append(denunciation) - - with open(db_path, 'w') as f: - json.dump(denunciations, f, indent=4) - - return denunciation + if utils.find_by_id(users.values(), reporter): + denunciation_info = args['info'] + denunciation_priority = args['priority'] + denunciation_location = { + "latitude": args['latitude'], + "longitude": args['longitude'] + } + + denunciation = { + 'reporter': reporter, + 'info': denunciation_info, + 'priority': denunciation_priority, + 'location': denunciation_location + } + + denunciations.append(denunciation) + + with open(db_path, 'w') as f: + json.dump(denunciations, f, indent=4) + + return denunciation + + else: + return {'error': 'User doesn\'t exists'} From de404d760fca77b7fb6789ede98866e6c667efb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Tue, 9 Apr 2019 14:12:54 +0300 Subject: [PATCH 2/2] list denunciations --- server_side/api/app.py | 3 ++- .../api/modules/databases/denunciations.json | 11 +++++++++++ server_side/api/modules/denunciation.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/server_side/api/app.py b/server_side/api/app.py index f9fc734..653df3f 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, utility,denunciation,navigation +from api.modules import user_info, voting_system, rating_system, utility, denunciation, navigation app = Flask(__name__) api = Api(app) @@ -29,6 +29,7 @@ if __name__ == '__main__': api.add_resource(denunciation.Alert, '/denunciation', '/denunciation/') api.add_resource(denunciation.Denounce, '/denounce', '/denounce/') + api.add_resource(denunciation.Denunciations, '/denunciations', '/denunciations/') api.add_resource(navigation.Transit, '/transit', '/transit/') diff --git a/server_side/api/modules/databases/denunciations.json b/server_side/api/modules/databases/denunciations.json index df2f122..08765f1 100644 --- a/server_side/api/modules/databases/denunciations.json +++ b/server_side/api/modules/databases/denunciations.json @@ -1,5 +1,6 @@ [ { + "id": 1, "reporter": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "info": "There is a fire", "priority": 8, @@ -7,5 +8,15 @@ "latitude": 39.9404, "longitude": 32.9101 } + }, + { + "id": 2, + "reporter": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", + "info": "Vandalism", + "priority": "3", + "location": { + "latitude": "35.3", + "longitude": "37.7" + } } ] \ No newline at end of file diff --git a/server_side/api/modules/denunciation.py b/server_side/api/modules/denunciation.py index 3583764..b77d4dc 100644 --- a/server_side/api/modules/denunciation.py +++ b/server_side/api/modules/denunciation.py @@ -34,6 +34,19 @@ class Alert(Resource): return {"success":False,"penalty":"{}".format(100*(20-trust))} +class Denunciations(Resource): + def get(self): + return [ + { + 'id' : v['id'], + 'info': v['info'], + 'priority': v['priority'], + 'location' : v['location'] + } + for v in denunciations + ] + + class Denounce(Resource): def post(self): args = request.form @@ -47,6 +60,7 @@ class Denounce(Resource): } denunciation = { + 'id': len(denunciations) + 1, 'reporter': reporter, 'info': denunciation_info, 'priority': denunciation_priority,