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 04751c8..6d84ce5 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -1,17 +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 login import login -from user_info import user_info -from login import login +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) @@ -27,14 +21,8 @@ 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.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/login/__pycache__/__init__.cpython-37.pyc b/server_side/api/login/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 0d4eb0b..0000000 Binary files a/server_side/api/login/__pycache__/__init__.cpython-37.pyc and /dev/null differ 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 b9c987d..0000000 Binary files a/server_side/api/login/__pycache__/login.cpython-37.pyc and /dev/null differ 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/login/__init__.py b/server_side/api/modules/__init__.py similarity index 100% rename from server_side/api/login/__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 71% rename from server_side/api/user_info/users.json rename to server_side/api/modules/databases/users.json index 239bc78..58ac429 100644 --- a/server_side/api/user_info/users.json +++ b/server_side/api/modules/databases/users.json @@ -1,9 +1,10 @@ [ { - "id": 1, + "id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "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/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/modules/user_info.py b/server_side/api/modules/user_info.py new file mode 100644 index 0000000..4ff09c5 --- /dev/null +++ b/server_side/api/modules/user_info.py @@ -0,0 +1,84 @@ +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', 'users.json') + +with open(db_path, 'r') as f: + users = json.load(f) + +class Users(Resource): + def post(self): + """ + Example POST Data: + username=& + password=& + realname=& # OPTIONAL + avatar=& # OPTIONAL + """ + args = request.form + user_id = utils.generate_id() + user = { + 'id': user_id, + 'username': args['username'], + 'realname': args.get('realname'), + 'avatar' : args.get('avatar'), + 'password': utils.md5(args['password']), + 'stats': { + 'bus_usage_week': 0, + 'bus_usage_month': 0, + 'bus_usage_year': 0 + }, + 'points': 0 + } + + users.append(user) + + with open(db_path, 'w') as f: + json.dump(users, f, indent=4) + + return user + +class User(Resource): + def get(self, user_id): + try: + user = utils.find_by_id(users, user_id) + if not user: + raise Exception('User not found!') + del user['password'] + return user + except: + abort(404, error="User {} doesn't exist".format(user_id)) + +class Login(Resource): + def post(self): + """ + Example POST Data: + username=& + password= + """ + args = request.form + print(args) + username = args['username'] + passsword = utils.md5(args['password']) + for user in users: + if user['username'] == username: + if user['password'] == passsword: + return {'message': 'Login successful!', 'id': user['id']} + + 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/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/rating_system/__init__.py b/server_side/api/rating_system/__init__.py deleted file mode 100644 index e69de29..0000000 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/user_info/user_info.py b/server_side/api/user_info/user_info.py deleted file mode 100644 index 8e78d93..0000000 --- a/server_side/api/user_info/user_info.py +++ /dev/null @@ -1,64 +0,0 @@ -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/voting_system/__init__.py b/server_side/api/voting_system/__init__.py deleted file mode 100644 index e69de29..0000000 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 81618b1..0000000 Binary files a/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc and /dev/null differ