From 8f9a31421999c6d81e6c9259e1f130e47112efa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Fri, 22 Mar 2019 22:48:39 +0300 Subject: [PATCH] added user info api --- server_side/api/app.py | 4 ++ .../api/rating_system/rating_system.py | 2 +- server_side/api/user_info/__init__.py | 0 server_side/api/user_info/user_info.py | 64 +++++++++++++++++++ server_side/api/user_info/users.json | 15 +++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 server_side/api/user_info/__init__.py create mode 100644 server_side/api/user_info/user_info.py create mode 100644 server_side/api/user_info/users.json diff --git a/server_side/api/app.py b/server_side/api/app.py index 8c32dd7..46c6e99 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -3,6 +3,7 @@ from flask_restful import Resource, Api from voting_system import voting_system from rating_system import rating_system +from user_info import user_info app = Flask(__name__) api = Api(app) @@ -20,4 +21,7 @@ if __name__ == '__main__': api.add_resource(rating_system.Rating, '/ratings/', '/ratings//') api.add_resource(rating_system.Rate, '/rate', '/rate/') + api.add_resource(user_info.Users, '/users', '/users/') + api.add_resource(user_info.User, '/users/', '/users//') + app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/rating_system/rating_system.py b/server_side/api/rating_system/rating_system.py index 0380fdf..e129dde 100644 --- a/server_side/api/rating_system/rating_system.py +++ b/server_side/api/rating_system/rating_system.py @@ -83,4 +83,4 @@ if __name__ == '__main__': 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) \ No newline at end of file diff --git a/server_side/api/user_info/__init__.py b/server_side/api/user_info/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server_side/api/user_info/user_info.py b/server_side/api/user_info/user_info.py new file mode 100644 index 0000000..8e78d93 --- /dev/null +++ b/server_side/api/user_info/user_info.py @@ -0,0 +1,64 @@ +import os +import json + +from flask import Flask, request +from flask_restful import Resource, Api, abort + +app = Flask(__name__) +api = Api(app) + +with open(os.path.join(app.root_path, 'users.json'), 'r') as f: + users = json.load(f) + +class Users(Resource): + def get(self): + user = [ + { + 'id' : v['id'], + 'username': v['username'] + } + for v in users + ] + return user + + def post(self): + """ + Example POST Data: + username=& + realname=& # OPTIONAL + avatar=& # OPTIONAL + """ + args = request.form + user_id = len(users) + 1 + user = { + 'id': user_id, + 'username': args['username'], + 'realname': args.get('realname'), + 'avatar' : args.get('avatar'), + 'stats': { + 'bus_usage_week': 0, + 'bus_usage_month': 0, + 'bus_usage_year': 0 + }, + 'points': 0 + } + + users.append(user) + + with open(os.path.join(app.root_path, 'users.json'), 'w') as f: + json.dump(users, f, indent=4) + + return user + +class User(Resource): + def get(self, user_id): + try: + return users[user_id - 1] + except: + abort(404, error="User {} doesn't exist".format(voting_id)) + +if __name__ == '__main__': + api.add_resource(Users, '/users', '/users/') + api.add_resource(User, '/users/', '/users//') + + app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/user_info/users.json b/server_side/api/user_info/users.json new file mode 100644 index 0000000..239bc78 --- /dev/null +++ b/server_side/api/user_info/users.json @@ -0,0 +1,15 @@ +[ + { + "id": 1, + "username": "efe", + "realname": "Efe Aydın", + "avatar": "img/user1.png", + "stats": { + "bus_usage_week": 1, + "bus_usage_month": 5, + "bus_usage_year": 67 + }, + "points": 50 + } + +]