From 513122cf0e3478500a72c2ce775480ea80fb039a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Fri, 15 Mar 2019 16:10:23 +0300 Subject: [PATCH] replaced reqparse, added docstrings, changed vote system --- server_side/api/voting_system/app.py | 138 +++++++++++++++------------ 1 file changed, 75 insertions(+), 63 deletions(-) diff --git a/server_side/api/voting_system/app.py b/server_side/api/voting_system/app.py index e2514c8..4f46742 100644 --- a/server_side/api/voting_system/app.py +++ b/server_side/api/voting_system/app.py @@ -1,85 +1,97 @@ import os -import ssl import json -from flask import Flask, send_from_directory -from flask_restful import Resource, Api, abort, reqparse +from flask import Flask, send_from_directory, request +from flask_restful import Resource, Api, abort app = Flask(__name__) api = Api(app) -parser = reqparse.RequestParser() -parser.add_argument('name', required=True) -parser.add_argument('desc') -parser.add_argument('img') -parser.add_argument('votes', required=True) - with open(os.path.join(app.root_path, 'votings.json'), 'r') as f: - votings = json.load(f) + votings = json.load(f) class Votings(Resource): - def get(self): - voting = [ - { - 'id' : v['id'], - 'name': v['name'], - 'desc': v['desc'], - 'img' : v['img'] - } - for v in votings - ] - return voting - - def post(self): - args = parser.parse_args() - voting_id = len(votings) + 1 - voting = { - 'id': voting_id, - 'name': args['name'], - 'desc': args['desc'], - 'img' : args['img'], - 'votes': [ - { - 'id': k, - 'name': vote['name'], - 'desc': vote['desc'], - 'votes': 0 - } - for k, vote in enumerate(json.loads(args['votes'])) - ] - } - - votings.append(voting) - - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: - json.dump(votings, f, indent=4) - - return voting - + def get(self): + voting = [ + { + 'id' : v['id'], + 'name': v['name'], + 'desc': v['desc'], + 'img' : v['img'] + } + for v in votings + ] + return voting + + def post(self): + """ + Example POST Data: + name=& + desc=& # OPTIONAL + img=& # OPTIONAL + votes=[ + { + "name": "", + "desc": "" # OPTIONAL + }, + (...) + ] + + """ + args = request.form + voting_id = len(votings) + 1 + voting = { + 'id': voting_id, + 'name': args['name'], + 'desc': args.get('desc'), + 'img' : args.get('img'), + 'votes': [ + { + 'id' : k, + 'name': vote['name'], + 'desc': vote.get('desc'), + 'votes': 0 + } + for k, vote in enumerate(json.loads(args['votes'])) + ] + } + + votings.append(voting) + + with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: + json.dump(votings, f, indent=4) + + return voting + class Voting(Resource): - def get(self, voting_id): - try: - return votings[voting_id - 1] - except: - abort(404, error="Voting {} doesn't exist".format(voting_id)) + def get(self, voting_id): + try: + return votings[voting_id - 1] + except: + abort(404, error="Voting {} doesn't exist".format(voting_id)) class Vote(Resource): - def get(self, voting_id, vote_id): - votings[voting_id - 1]['votes'][vote_id - 1]['votes'] += 1 - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: - json.dump(votings, f, indent=4) - - return votings[voting_id - 1] + def get(self): + """ + Example URL Query: + /vote?voting_id=&vote_id= + """ + voting_id = int(request.args['voting_id']) + vote_id = int(request.args['vote_id']) + votings[voting_id - 1]['votes'][vote_id - 1]['votes'] += 1 + with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: + json.dump(votings, f, indent=4) + + return votings[voting_id - 1] api.add_resource(Votings, '/votings', '/votings/') api.add_resource(Voting, '/votings/') -api.add_resource(Vote, '/vote//') +api.add_resource(Vote, '/vote', '/vote/') @app.route('/img/') def send_img(path): - return send_from_directory('images', path) + return send_from_directory('images', path) if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) - + app.run(host='0.0.0.0', port=5000)