From f1359b078f9b08a5897b87a169f30c856657410a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Mon, 25 Mar 2019 17:05:28 +0300 Subject: [PATCH 1/2] login added --- server_side/api/app.py | 11 +----- server_side/api/login/__init__.py | 0 .../login/__pycache__/__init__.cpython-37.pyc | Bin 159 -> 0 bytes .../login/__pycache__/login.cpython-37.pyc | Bin 884 -> 0 bytes server_side/api/login/login.py | 28 --------------- server_side/api/login/userdata.json | 3 -- server_side/api/user_info/user_info.py | 32 ++++++++++++++++-- server_side/api/user_info/users.json | 1 + .../__pycache__/voting_system.cpython-37.pyc | Bin 3087 -> 0 bytes 9 files changed, 32 insertions(+), 43 deletions(-) delete mode 100644 server_side/api/login/__init__.py delete mode 100644 server_side/api/login/__pycache__/__init__.cpython-37.pyc delete mode 100644 server_side/api/login/__pycache__/login.cpython-37.pyc delete mode 100644 server_side/api/login/login.py delete mode 100644 server_side/api/login/userdata.json delete mode 100644 server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc diff --git a/server_side/api/app.py b/server_side/api/app.py index 04751c8..c2e5ffd 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -3,9 +3,7 @@ from flask_restful import Resource, Api from voting_system import voting_system from rating_system import rating_system -from login import login from user_info import user_info -from login import login app = Flask(__name__) api = Api(app) @@ -27,14 +25,7 @@ if __name__ == '__main__': api.add_resource(rating_system.Rating, '/ratings/', '/ratings//') api.add_resource(rating_system.Rate, '/rate', '/rate/') - api.add_resource(login.Login, '/login', '/login/') - - app.run(host='0.0.0.0', port=5000, ssl_context=context) - 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) - api.add_resource(login.Login, '/login', '/login/') - + api.add_resource(user_info.Login, '/login', '/login/') app.run(host='0.0.0.0', port=5000, ssl_context=context) diff --git a/server_side/api/login/__init__.py b/server_side/api/login/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/server_side/api/login/__pycache__/__init__.cpython-37.pyc b/server_side/api/login/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 0d4eb0bf03def46829c6b685d4b68284e3f7ca76..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 159 zcmZ?b<>g`kf+)MmF(CReh=2h`Aj1KOi&=m~3PUi1CZpd~vm6}{qtRI}4SX2`4o0ylFo|>DQSE65>T2z)=6knW~lB%Crkg1=OpPreg gA0MBYmst`YuUAlci^C>2KczG$)edCCXCP((00%56dH?_b diff --git a/server_side/api/login/__pycache__/login.cpython-37.pyc b/server_side/api/login/__pycache__/login.cpython-37.pyc deleted file mode 100644 index b9c987d77651e4be1f22c7e7a88204e7638597c0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 884 zcmYjP&2H2%5VoCUll|?sU7)Y9mkkeqPyqoFf=aXpP=sW;>)G8bNu1gVQde-=9=ITG zoZBN32P7mO##c_f0w=~vE7j3_V|(K9_s#h6aM&X_Ui|v@?l&dmk6YY~E=~^7%ufVK zB+W?DdYaH?%@T&5WnS(lejX$Nb#X82Rnf?KT%R4ZU zUD>_20QdDBLHma~x&s~O_!EkbjW+n(gTm?`)mF$8fp7iiW-v%$$GA-s<$Y!`B z&Yqx|J%oy^=_gvTk9132sqhdzntIy9nDvET8XJ^4Eh=Q~8^{*cL(<=g9^w!Gp2jPc zLwuetQ>o%ot2N9k6QATlSNxSI#1e8Ssu)##0L@J*Ar@sCXO2{hv+~^fr81RcV|)a{ zI&{-(c+Xtm2xUZ^7a|AFZIAO@$vSiM0q1A6$l8eB!J`eWTQG8a5yCE-ampjwtX;-j zqy^`jPD^AHUenry_h-$Qe%>d<>0TiVM^abQdzL9<{iYq^N|;rap4wpH+NoXows6B{ zgKO;VqRzBar2_{Y2V(^5iL!K%T3?k=V0uxlFtJJt+to@{yxE`&>t{+x>$_*rQXdE@ sUGD4mqU++8V%Hb#WZURLTd0Sw`Q`v2qWG~fo%qfl(1|yWB06IK0r&pkfB*mh diff --git a/server_side/api/login/login.py b/server_side/api/login/login.py deleted file mode 100644 index 7ca16de..0000000 --- a/server_side/api/login/login.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import json -import hashlib - -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, 'userdata.json'), 'r') as f: - data = json.load(f) - - -class Login( Resource ): - def post(self): - - req = request.args - print(req) - - - -if __name__ == '__main__': - api.add_resource(Login, '/login', '/login/') - - app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/login/userdata.json b/server_side/api/login/userdata.json deleted file mode 100644 index 0e0dcd2..0000000 --- a/server_side/api/login/userdata.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} \ No newline at end of file diff --git a/server_side/api/user_info/user_info.py b/server_side/api/user_info/user_info.py index 8e78d93..4663c79 100644 --- a/server_side/api/user_info/user_info.py +++ b/server_side/api/user_info/user_info.py @@ -1,5 +1,6 @@ import os import json +import hashlib from flask import Flask, request from flask_restful import Resource, Api, abort @@ -10,6 +11,9 @@ api = Api(app) with open(os.path.join(app.root_path, 'users.json'), 'r') as f: users = json.load(f) +def md5(s): + return hashlib.md5(s.encode()).hexdigest() + class Users(Resource): def get(self): user = [ @@ -25,6 +29,7 @@ class Users(Resource): """ Example POST Data: username=& + password=& realname=& # OPTIONAL avatar=& # OPTIONAL """ @@ -35,6 +40,7 @@ class Users(Resource): 'username': args['username'], 'realname': args.get('realname'), 'avatar' : args.get('avatar'), + 'password': md5(args['password']), 'stats': { 'bus_usage_week': 0, 'bus_usage_month': 0, @@ -53,12 +59,34 @@ class Users(Resource): class User(Resource): def get(self, user_id): try: - return users[user_id - 1] + user = users[user_id - 1] + del user['password'] + return user except: - abort(404, error="User {} doesn't exist".format(voting_id)) + abort(404, error="User {} doesn't exist".format(user_id)) + +class Login(Resource): + def post(self): + """ + Example POST Data: + username=& + password= + """ + args = request.form + username = args['username'] + passsword = md5(args['password']) + for user in users: + if user['username'] == username: + if user['password'] == passsword: + return {'message': 'Login successful!'} + + return {'error': 'Wrong password!'} + + return {'error': 'User not found!'} if __name__ == '__main__': api.add_resource(Users, '/users', '/users/') api.add_resource(User, '/users/', '/users//') + api.add_resource(Login, '/login', '/login/') 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 index 239bc78..f7606bf 100644 --- a/server_side/api/user_info/users.json +++ b/server_side/api/user_info/users.json @@ -4,6 +4,7 @@ "username": "efe", "realname": "Efe Aydın", "avatar": "img/user1.png", + "password": "e10adc3949ba59abbe56e057f20f883e", "stats": { "bus_usage_week": 1, "bus_usage_month": 5, diff --git a/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc b/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc deleted file mode 100644 index 81618b12e8fa40101635de7bdb5d13b1f159ab7b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3087 zcmb7G&5s*36u0Lqlds*h`&|%?wq@HuNfv=Xs|}Ra4xN#o{cO*({oeciJRcW}IRek0 zmwvnRSC)`}P}zGxC+DHW|AJtI(GF=QHEmMRjgAqTO*6Eb7R7$EV~0-D2{X-1=r&zS zxXH6Sw3*`>o`X8i3!JozqbZ(iPcv)RVD^sjbE#S4baaH5c4_nI*RaYpa-G~JpAqJ; z%oD;g+}SakGoZUH3wl=Tv!Lf#9`wA{=XjPC?i=E7o*&Kg8CGOdyC&=awOKaJN~t!- zjiM}5)a%Dm7E zlMAN)XkhO#pm82b{5l9u&}mJBQD!_LO_Q0-g4$v>EbOSv zt;A4^!d!gtV=SLQiI0LvK9uAl*`eEXi2F4g2c~CW);dmq3&bWFlDo?|k&z_=l4@ya zX)Y0F{cinYJg~rwt&D&vtLukcS&YXmWd`A9#S)kS0uw~!Q4~P@LBKmkbyd-aqKKU+ zp5Lt9i$Y%83N`~4)p{ao^OlTj>!B~C_m$uEH+jgrvKDjkkP9yk7_a%gpq6;gi??FQ z!-HD2xAi=?)(K+Sio#yK1k1$e562lCAN#7h$z^q|6Se$KT!+Tq+zM5haa>@tR?Isa z%1JsVdlqO>nP6fHO*bDj`sOJ|gU8JQ)rOLr)Sk-5Ly+om)}?hxivBC|5r&W{SR*q&lp z07wo1GQVv}r#&srp~dp7@D$P-U{XwAGBhE6Q~oMsO|CqCK7Hhey$&y5y|R9ze9@Qw z*>ng)d~Pjqg@;OgB^|@Sr(>wpSIQ^KSFYap{7U1(<#ZZkMmh`HFsb~qX|M*5&wZPI zhI@N_K*OG8+{5zO@-ps;Cp%g`^Wrqz&SaY2-15s-MVs3Pzdv2ARx7E+yScRa43Tyy zyVG!+FqXpU3@92-C+`^B6ao#9S|CG!GJwe#0TV)y0nQUZiTQ)Jaj6&Zq-^b^3KerW zYzj<7MejE)>_3#L;s^}B3uO-k!HKk_5CvoKcLD9x(vYD zsFVb{L0KD-2$k94U4cBLcdN3z+Yh<$B~XUuKD!h7ELM)+>+vqINd%QLqF7lyU*3aB zQP5Q;jH;}NBIzZ)tRMDZk2885Uu?!I3x@|r^#Uelv6pTf@&p)!6CZ=Ico77dc4)yc zs6$=q0)UTMIb)F?GX{nI4r&0&xt>li&r>kbIDA7G7E3kgjnXE?#8thR(R)S4gzMLSnxBc)QFZ9(Pa4GJgcz2%)vP z5RrgX(}6_JQw~PgmlZ_Mio8^AxNY4ZNzf*&m=GiKJ}}PrNF}8Za|xz}MQh<|1f# z3oa394UMF2gTI;iAu-9C=IeH5bY|1xGOtlo?AQmQg){;v|YUQLLaih2l7h(;y(y2-YM| z-pAJ4D0F-$LB{BR2_?o~24YXqDSDb}RJs_sSIwFCz{nS9Zk#iyBb-A&=z8A39sfDR zgX~_bHdsuXYjEVVsf+7_rT-hL4Qw3Z14mc2L1wk8-{40$GyY241Li^g5wfzFI13%4 z><#=|6u7&DGK;#y6Y%S}(eDUMP=S9C0wI?`1$~Qr7$lybI9>Z2ovJLP)C8)CJPj40 z&tC8uL+j%oQqk}3i$EF%<*p?(SI7G&eggz@WQooiST90d`k<64O}P*sc#Z!6dSjh~ From 3bffb236bbbc03cd27a914c66f5347e481cdeb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Mon, 25 Mar 2019 20:57:12 +0300 Subject: [PATCH 2/2] app only portrait, added user auth, user id system changed --- MyCity/app/src/main/AndroidManifest.xml | 3 +- server_side/api/app.py | 9 ++-- .../{rating_system => modules}/__init__.py | 0 .../databases}/ratings.json | 14 ++--- .../databases}/users.json | 2 +- .../databases}/votings.json | 6 ++- .../rating_system.py | 51 ++++++++++++------- .../api/{user_info => modules}/user_info.py | 36 +++++-------- server_side/api/modules/utils.py | 16 ++++++ .../voting_system.py | 44 ++++++++++++---- server_side/api/user_info/__init__.py | 0 server_side/api/voting_system/__init__.py | 0 12 files changed, 110 insertions(+), 71 deletions(-) rename server_side/api/{rating_system => modules}/__init__.py (100%) rename server_side/api/{rating_system => modules/databases}/ratings.json (57%) rename server_side/api/{user_info => modules/databases}/users.json (86%) rename server_side/api/{voting_system => modules/databases}/votings.json (87%) rename server_side/api/{rating_system => modules}/rating_system.py (54%) rename server_side/api/{user_info => modules}/user_info.py (69%) create mode 100644 server_side/api/modules/utils.py rename server_side/api/{voting_system => modules}/voting_system.py (60%) delete mode 100644 server_side/api/user_info/__init__.py delete mode 100644 server_side/api/voting_system/__init__.py diff --git a/MyCity/app/src/main/AndroidManifest.xml b/MyCity/app/src/main/AndroidManifest.xml index cfb4487..cd1f147 100644 --- a/MyCity/app/src/main/AndroidManifest.xml +++ b/MyCity/app/src/main/AndroidManifest.xml @@ -12,7 +12,8 @@ + android:theme="@style/AppTheme.NoActionBar" + android:screenOrientation="portrait"> diff --git a/server_side/api/app.py b/server_side/api/app.py index c2e5ffd..6d84ce5 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -1,15 +1,11 @@ from flask import Flask, send_from_directory from flask_restful import Resource, Api -from voting_system import voting_system -from rating_system import rating_system -from user_info import user_info +from modules import voting_system, rating_system, user_info app = Flask(__name__) api = Api(app) - - @app.route('/img/') def send_img(path): return send_from_directory('images', path) @@ -26,6 +22,7 @@ if __name__ == '__main__': api.add_resource(rating_system.Rate, '/rate', '/rate/') api.add_resource(user_info.Users, '/users', '/users/') - api.add_resource(user_info.User, '/users/', '/users//') + api.add_resource(user_info.User, '/users/', '/users//') api.add_resource(user_info.Login, '/login', '/login/') + app.run(host='0.0.0.0', port=5000, ssl_context=context) diff --git a/server_side/api/rating_system/__init__.py b/server_side/api/modules/__init__.py similarity index 100% rename from server_side/api/rating_system/__init__.py rename to server_side/api/modules/__init__.py diff --git a/server_side/api/rating_system/ratings.json b/server_side/api/modules/databases/ratings.json similarity index 57% rename from server_side/api/rating_system/ratings.json rename to server_side/api/modules/databases/ratings.json index eadb56e..ef98f5e 100644 --- a/server_side/api/rating_system/ratings.json +++ b/server_side/api/modules/databases/ratings.json @@ -8,21 +8,13 @@ { "id": 1, "score": 10, + "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "note": "IT WAS AWESOME" }, { "id": 2, - "score": 0, - "note": "The worst bus ride I have ever had" - }, - { - "id": 3, - "score": 7, - "note": null - }, - { - "id": 4, - "score": 1, + "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", + "score": 10, "note": null } ] diff --git a/server_side/api/user_info/users.json b/server_side/api/modules/databases/users.json similarity index 86% rename from server_side/api/user_info/users.json rename to server_side/api/modules/databases/users.json index f7606bf..58ac429 100644 --- a/server_side/api/user_info/users.json +++ b/server_side/api/modules/databases/users.json @@ -1,6 +1,6 @@ [ { - "id": 1, + "id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "username": "efe", "realname": "Efe Aydın", "avatar": "img/user1.png", diff --git a/server_side/api/voting_system/votings.json b/server_side/api/modules/databases/votings.json similarity index 87% rename from server_side/api/voting_system/votings.json rename to server_side/api/modules/databases/votings.json index e8f6ace..665ae40 100644 --- a/server_side/api/voting_system/votings.json +++ b/server_side/api/modules/databases/votings.json @@ -4,11 +4,14 @@ "name": "Test Voting", "desc": "Sample voting description", "img": "/img/voting.jpg", + "voters": [ + "9vard12ty0ad2yvwp3q53rsf3h43r2vq" + ], "votes": { "1": { "name": "Sample Vote 1", "desc": "Sample description", - "votes": 0 + "votes": 1 }, "2": { "name": "Sample Vote 2", @@ -22,6 +25,7 @@ "name": "wooting", "desc": "wooting desc", "img": "/img/voting.jpg", + "voters": [], "votes": { "1": { "name": "woote 1", diff --git a/server_side/api/rating_system/rating_system.py b/server_side/api/modules/rating_system.py similarity index 54% rename from server_side/api/rating_system/rating_system.py rename to server_side/api/modules/rating_system.py index e129dde..be907ee 100644 --- a/server_side/api/rating_system/rating_system.py +++ b/server_side/api/modules/rating_system.py @@ -1,15 +1,22 @@ import os import json +from . 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', 'ratings.json') +user_db = os.path.join(app.root_path, 'databases', 'users.json') -with open(os.path.join(app.root_path, 'ratings.json'), 'r') as f: +with open(db_path, 'r') as f: ratings = json.load(f) +with open(user_db, 'r') as f: + users = json.load(f) + class Ratings(Resource): def get(self): rating = [ @@ -42,7 +49,7 @@ class Ratings(Resource): ratings.append(rating) - with open(os.path.join(app.root_path, 'ratings.json'), 'w') as f: + with open(db_path, 'w') as f: json.dump(ratings, f, indent=4) return rating @@ -50,7 +57,10 @@ class Ratings(Resource): class Rating(Resource): def get(self, rating_id): try: - return ratings[rating_id - 1] + rating = ratings[rating_id - 1] + del rating['rates'] + del rating['raters'] + return rating except: abort(404, error="Rating {} doesn't exist".format(rating_id)) @@ -59,24 +69,29 @@ class Rate(Resource): """ Example URL Query: /rate? - rating_id=>& + rating_id=& score=& - note= # ADDITIONAL + note=& # ADDITIONAL + rater_id= """ - rating_id = int(request.args['rating_id']) - score = int(request.args['score']) - if 0 >= score >= 10: - abort(500, 'Score should be between 0 and 10') - note = request.args.get('note') - ratings[rating_id - 1]['rates'].append({ - 'id': len(ratings[rating_id - 1]['rates']) + 1, - 'score': score, - 'note': note - }) - with open(os.path.join(app.root_path, 'ratings.json'), 'w') as f: - json.dump(ratings, f, indent=4) + if utils.find_by_id(users, request.args['rater_id']): + rating_id = int(request.args['rating_id']) + score = int(request.args['score']) + if 0 >= score >= 10: + abort(500, 'Score should be between 0 and 10') + note = request.args.get('note') + ratings[rating_id - 1]['rates'].append({ + 'id': len(ratings[rating_id - 1]['rates']) + 1, + 'rater': request.args['rater_id'], + 'score': score, + 'note': note + }) + with open(db_path, 'w') as f: + json.dump(ratings, f, indent=4) + + return {'message': 'Success'} - return ratings[rating_id - 1] + return {'error': 'User doesn\'t exists'} if __name__ == '__main__': api.add_resource(Ratings, '/ratings', '/ratings/') diff --git a/server_side/api/user_info/user_info.py b/server_side/api/modules/user_info.py similarity index 69% rename from server_side/api/user_info/user_info.py rename to server_side/api/modules/user_info.py index 4663c79..4ff09c5 100644 --- a/server_side/api/user_info/user_info.py +++ b/server_side/api/modules/user_info.py @@ -1,30 +1,19 @@ import os import json -import hashlib + +from . 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(os.path.join(app.root_path, 'users.json'), 'r') as f: +with open(db_path, 'r') as f: users = json.load(f) -def md5(s): - return hashlib.md5(s.encode()).hexdigest() - 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: @@ -34,13 +23,13 @@ class Users(Resource): avatar=& # OPTIONAL """ args = request.form - user_id = len(users) + 1 + user_id = utils.generate_id() user = { 'id': user_id, 'username': args['username'], 'realname': args.get('realname'), 'avatar' : args.get('avatar'), - 'password': md5(args['password']), + 'password': utils.md5(args['password']), 'stats': { 'bus_usage_week': 0, 'bus_usage_month': 0, @@ -51,7 +40,7 @@ class Users(Resource): users.append(user) - with open(os.path.join(app.root_path, 'users.json'), 'w') as f: + with open(db_path, 'w') as f: json.dump(users, f, indent=4) return user @@ -59,7 +48,9 @@ class Users(Resource): class User(Resource): def get(self, user_id): try: - user = users[user_id - 1] + user = utils.find_by_id(users, user_id) + if not user: + raise Exception('User not found!') del user['password'] return user except: @@ -73,12 +64,13 @@ class Login(Resource): password= """ args = request.form + print(args) username = args['username'] - passsword = md5(args['password']) + passsword = utils.md5(args['password']) for user in users: if user['username'] == username: if user['password'] == passsword: - return {'message': 'Login successful!'} + return {'message': 'Login successful!', 'id': user['id']} return {'error': 'Wrong password!'} @@ -86,7 +78,7 @@ class Login(Resource): if __name__ == '__main__': api.add_resource(Users, '/users', '/users/') - api.add_resource(User, '/users/', '/users//') + api.add_resource(User, '/users/', '/users//') api.add_resource(Login, '/login', '/login/') app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/modules/utils.py b/server_side/api/modules/utils.py new file mode 100644 index 0000000..4e12db9 --- /dev/null +++ b/server_side/api/modules/utils.py @@ -0,0 +1,16 @@ +import string +import random +import hashlib + +def md5(s): + return hashlib.md5(s.encode()).hexdigest() + +def find_by_id(iterable, _id): + for i in iterable: + if i['id'] == _id: + return i + + return None + +def generate_id(length=32): + return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length)) \ No newline at end of file diff --git a/server_side/api/voting_system/voting_system.py b/server_side/api/modules/voting_system.py similarity index 60% rename from server_side/api/voting_system/voting_system.py rename to server_side/api/modules/voting_system.py index 2ec9a6d..704185d 100644 --- a/server_side/api/voting_system/voting_system.py +++ b/server_side/api/modules/voting_system.py @@ -1,15 +1,22 @@ import os import json +from . 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', 'votings.json') +user_db = os.path.join(app.root_path, 'databases', 'users.json') -with open(os.path.join(app.root_path, 'votings.json'), 'r') as f: +with open(db_path, 'r') as f: votings = json.load(f) +with open(user_db, 'r') as f: + users = json.load(f) + class Votings(Resource): def get(self): voting = [ @@ -45,6 +52,7 @@ class Votings(Resource): 'name': args['name'], 'desc': args.get('desc'), 'img' : args.get('img'), + 'voters': [], 'votes': [ { 'id' : k + 1, @@ -58,16 +66,21 @@ class Votings(Resource): votings.append(voting) - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: + with open(db_path, 'w') as f: json.dump(votings, f, indent=4) - return voting + return {'message': 'Success'} class Voting(Resource): def get(self, voting_id): try: - return votings[voting_id - 1] + voting = votings[voting_id - 1] + for i in range(len(voting['votes'])): + del voting['votes'][str(i + 1)]['votes'] + + del voting['voters'] + return voting except: abort(404, error="Voting {} doesn't exist".format(voting_id)) @@ -75,15 +88,24 @@ class Vote(Resource): def get(self): """ Example URL Query: - /vote?voting_id=&vote_id= + /vote?voting_id=&vote_id=&voter_id= """ - voting_id = int(request.args['voting_id']) - vote_id = int(request.args['vote_id']) - votings[int(voting_id-1)]['votes'][str(vote_id)]['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] + voter_id = request.args['voter_id'] + voting_id = int(request.args['voting_id']) - 1 + if utils.find_by_id(users, voter_id): + if voter_id not in votings[voting_id]['voters']: + vote_id = int(request.args['vote_id']) + votings[voting_id]['votes'][str(vote_id)]['votes'] += 1 + votings[voting_id]['voters'].append(voter_id) + with open(db_path, 'w') as f: + json.dump(votings, f, indent=4) + + return {'message': 'Success'} + + return {'error': 'Already voted'} + + return {'error': 'User doesn\'t exists'} if __name__ == '__main__': api.add_resource(Votings, '/votings', '/votings/') diff --git a/server_side/api/user_info/__init__.py b/server_side/api/user_info/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/server_side/api/voting_system/__init__.py b/server_side/api/voting_system/__init__.py deleted file mode 100644 index e69de29..0000000