diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java b/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java index 03782af..935cd2d 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java @@ -2,6 +2,7 @@ package gq.yigit.mycity; import android.content.Context; import android.content.DialogInterface; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; @@ -19,9 +20,7 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.EditText; import android.widget.Toast; -import gq.yigit.mycity.tools.FileActions; -import gq.yigit.mycity.tools.WebRequest; -import gq.yigit.mycity.tools.responseListener; +import gq.yigit.mycity.tools.*; import gq.yigit.mycity.voteFragment.VoteFragment; import gq.yigit.mycity.votesFragment.VotesContent; import gq.yigit.mycity.votesFragment.VotesFragment; @@ -30,18 +29,20 @@ import org.json.JSONObject; import java.util.HashMap; -import static java.security.AccessController.getContext; - public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, VotesFragment.OnListFragmentInteractionListener, MainFragment.OnFragmentInteractionListener, VoteFragment.OnFragmentInteractionListener, - responseListener{ - - public static Context cntxt; - public static JSONArray userData; + responseListener, + imageListener { + + public Context cntxt; + public static JSONObject userData; + public static Bitmap userAvatar; + private JSONArray receivedData; + private String url; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -64,9 +65,10 @@ public class MainActivity extends AppCompatActivity drawer.addDrawerListener(toggle); toggle.syncState(); FileActions file_manager = new FileActions(); - String url = file_manager.readFromFile(cntxt,"server.config").trim(); + url = file_manager.readFromFile(cntxt,"server.config").trim(); HashMap request = new HashMap<>(); - request.put("a","b"); + request.put("username","efe"); + request.put("password","12345"); WebRequest login_manager = new WebRequest(url + "/login/",false,request); login_manager.addListener(this); login_manager.execute(); @@ -175,14 +177,23 @@ public class MainActivity extends AppCompatActivity public void receivedResponse(boolean success,String response){ if(success) { try { - userData = new JSONArray(response); - if(!(boolean)userData.get(0)){ + receivedData = new JSONArray(response); + if(!(boolean)receivedData.get(0)){ Toast.makeText(cntxt,"Please login again!",Toast.LENGTH_LONG).show(); + }else{ + String user_data_temp = receivedData.get(1).toString(); + userData = new JSONObject(user_data_temp); + Log.i("[INFO]",userData.toString()); + ImageDownload avatar_downloader = new ImageDownload(); + avatar_downloader.addListener(this); + avatar_downloader.execute(url + userData.get("avatar")); } }catch (Exception e){ Log.e("[ERROR]","Cannot receive userdata"); } } } - + public void imageDownloaded(Bitmap img){ + userAvatar = img; + } } diff --git a/server_side/api/__pycache__/__init__.cpython-37.pyc b/server_side/api/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..7495876 Binary files /dev/null and b/server_side/api/__pycache__/__init__.cpython-37.pyc differ diff --git a/server_side/api/app.py b/server_side/api/app.py index 6d84ce5..712f0fe 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -1,7 +1,8 @@ from flask import Flask, send_from_directory -from flask_restful import Resource, Api +from flask_restful import Api -from modules import voting_system, rating_system, user_info +from api.modules import rating_system +from api.modules import user_info, voting_system app = Flask(__name__) api = Api(app) @@ -13,16 +14,16 @@ def send_img(path): if __name__ == '__main__': context = ('encryption/mycity.crt','encryption/mycity-decrypted.key') - api.add_resource(voting_system.Votings, '/votings', '/votings/') - api.add_resource(voting_system.Voting, '/votings/') - api.add_resource(voting_system.Vote, '/vote', '/vote/') + api.add_resource( voting_system.Votings, '/votings', '/votings/' ) + api.add_resource( voting_system.Voting, '/votings/' ) + api.add_resource( voting_system.Vote, '/vote', '/vote/' ) - api.add_resource(rating_system.Ratings, '/ratings', '/ratings/') - api.add_resource(rating_system.Rating, '/ratings/', '/ratings//') - api.add_resource(rating_system.Rate, '/rate', '/rate/') + api.add_resource( rating_system.Ratings, '/ratings', '/ratings/' ) + 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//') - api.add_resource(user_info.Login, '/login', '/login/') + api.add_resource( user_info.Users, '/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/login/__pycache__/__init__.cpython-37.pyc b/server_side/api/login/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index d639695..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 676917d..0000000 Binary files a/server_side/api/login/__pycache__/login.cpython-37.pyc and /dev/null differ diff --git a/server_side/api/modules/__pycache__/__init__.cpython-37.pyc b/server_side/api/modules/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..97d8b24 Binary files /dev/null and b/server_side/api/modules/__pycache__/__init__.cpython-37.pyc differ diff --git a/server_side/api/modules/__pycache__/rating_system.cpython-37.pyc b/server_side/api/modules/__pycache__/rating_system.cpython-37.pyc new file mode 100644 index 0000000..7dcaf94 Binary files /dev/null and b/server_side/api/modules/__pycache__/rating_system.cpython-37.pyc differ diff --git a/server_side/api/modules/__pycache__/user_info.cpython-37.pyc b/server_side/api/modules/__pycache__/user_info.cpython-37.pyc new file mode 100644 index 0000000..a400010 Binary files /dev/null and b/server_side/api/modules/__pycache__/user_info.cpython-37.pyc differ diff --git a/server_side/api/modules/__pycache__/utils.cpython-37.pyc b/server_side/api/modules/__pycache__/utils.cpython-37.pyc new file mode 100644 index 0000000..e7a9c8c Binary files /dev/null and b/server_side/api/modules/__pycache__/utils.cpython-37.pyc differ diff --git a/server_side/api/modules/__pycache__/voting_system.cpython-37.pyc b/server_side/api/modules/__pycache__/voting_system.cpython-37.pyc new file mode 100644 index 0000000..6f48cb3 Binary files /dev/null and b/server_side/api/modules/__pycache__/voting_system.cpython-37.pyc differ diff --git a/server_side/api/modules/databases/users.json b/server_side/api/modules/databases/users.json index 58ac429..e96dd26 100644 --- a/server_side/api/modules/databases/users.json +++ b/server_side/api/modules/databases/users.json @@ -1,16 +1,23 @@ -[ - { +{ + "efe":{ "id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", - "username": "efe", "realname": "Efe Aydın", "avatar": "img/user1.png", - "password": "e10adc3949ba59abbe56e057f20f883e", + "password": "827ccb0eea8a706c4c34a16891f84e7b", + "email": "efeaydin@mycity.com", "stats": { "bus_usage_week": 1, "bus_usage_month": 5, "bus_usage_year": 67 }, + "daily_electricity_usage": [ + 10, + 5, + 5, + 6, + 7 + ], "points": 50 } -] +} diff --git a/server_side/api/modules/rating_system.py b/server_side/api/modules/rating_system.py index be907ee..2410675 100644 --- a/server_side/api/modules/rating_system.py +++ b/server_side/api/modules/rating_system.py @@ -1,7 +1,7 @@ import os import json -from . import utils +from api.modules import utils from flask import Flask, request from flask_restful import Resource, Api, abort @@ -74,7 +74,7 @@ class Rate(Resource): note=& # ADDITIONAL rater_id= """ - if utils.find_by_id(users, request.args['rater_id']): + 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: diff --git a/server_side/api/modules/user_info.py b/server_side/api/modules/user_info.py index 4ff09c5..31f74de 100644 --- a/server_side/api/modules/user_info.py +++ b/server_side/api/modules/user_info.py @@ -1,7 +1,7 @@ import os import json -from . import utils +from api.modules import utils from flask import Flask, request from flask_restful import Resource, Api, abort @@ -29,7 +29,7 @@ class Users(Resource): 'username': args['username'], 'realname': args.get('realname'), 'avatar' : args.get('avatar'), - 'password': utils.md5(args['password']), + 'password': utils.md5( args[ 'password' ] ), 'stats': { 'bus_usage_week': 0, 'bus_usage_month': 0, @@ -48,7 +48,7 @@ class Users(Resource): class User(Resource): def get(self, user_id): try: - user = utils.find_by_id(users, user_id) + user = utils.find_by_id( users, user_id ) if not user: raise Exception('User not found!') del user['password'] @@ -63,18 +63,23 @@ class Login(Resource): username=& password= """ + #Password for efe is 12345 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']} + passsword = utils.md5( args[ 'password' ] ) + + if not username in users: + return [False,{}] + + user = (users[username]).copy() + if user['password'] == passsword: + user.pop("password") + return [True,json.dumps(user)] + else: + return [False,{}] + - return {'error': 'Wrong password!'} - return {'error': 'User not found!'} if __name__ == '__main__': api.add_resource(Users, '/users', '/users/') diff --git a/server_side/api/modules/voting_system.py b/server_side/api/modules/voting_system.py index 704185d..05f6ee8 100644 --- a/server_side/api/modules/voting_system.py +++ b/server_side/api/modules/voting_system.py @@ -1,7 +1,7 @@ import os import json -from . import utils +from api.modules import utils from flask import Flask, request from flask_restful import Resource, Api, abort @@ -93,7 +93,7 @@ class Vote(Resource): voter_id = request.args['voter_id'] voting_id = int(request.args['voting_id']) - 1 - if utils.find_by_id(users, voter_id): + 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 diff --git a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 50508aa..0000000 Binary files a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc and /dev/null differ diff --git a/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc b/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc deleted file mode 100644 index 8b64d3f..0000000 Binary files a/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc and /dev/null differ diff --git a/server_side/api/user_info/__pycache__/__init__.cpython-37.pyc b/server_side/api/user_info/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 495d818..0000000 Binary files a/server_side/api/user_info/__pycache__/__init__.cpython-37.pyc and /dev/null differ diff --git a/server_side/api/user_info/__pycache__/user_info.cpython-37.pyc b/server_side/api/user_info/__pycache__/user_info.cpython-37.pyc deleted file mode 100644 index e69b13e..0000000 Binary files a/server_side/api/user_info/__pycache__/user_info.cpython-37.pyc and /dev/null differ diff --git a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc deleted file mode 100644 index 7471bb5..0000000 Binary files a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc and /dev/null differ 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 96184de..0000000 Binary files a/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc and /dev/null differ