Browse Source

Done receiveing user data

Made some changes to the user_data.json data format
old
Yiğit Çolakoğlu 6 years ago
parent
commit
186a06767c
20 changed files with 69 additions and 45 deletions
  1. +25
    -14
      MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java
  2. BIN
      server_side/api/__pycache__/__init__.cpython-37.pyc
  3. +12
    -11
      server_side/api/app.py
  4. BIN
      server_side/api/login/__pycache__/__init__.cpython-37.pyc
  5. BIN
      server_side/api/login/__pycache__/login.cpython-37.pyc
  6. BIN
      server_side/api/modules/__pycache__/__init__.cpython-37.pyc
  7. BIN
      server_side/api/modules/__pycache__/rating_system.cpython-37.pyc
  8. BIN
      server_side/api/modules/__pycache__/user_info.cpython-37.pyc
  9. BIN
      server_side/api/modules/__pycache__/utils.cpython-37.pyc
  10. BIN
      server_side/api/modules/__pycache__/voting_system.cpython-37.pyc
  11. +12
    -5
      server_side/api/modules/databases/users.json
  12. +2
    -2
      server_side/api/modules/rating_system.py
  13. +16
    -11
      server_side/api/modules/user_info.py
  14. +2
    -2
      server_side/api/modules/voting_system.py
  15. BIN
      server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc
  16. BIN
      server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc
  17. BIN
      server_side/api/user_info/__pycache__/__init__.cpython-37.pyc
  18. BIN
      server_side/api/user_info/__pycache__/user_info.cpython-37.pyc
  19. BIN
      server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc
  20. BIN
      server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc

+ 25
- 14
MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java View File

@ -2,6 +2,7 @@ package gq.yigit.mycity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
@ -19,9 +20,7 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; 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.voteFragment.VoteFragment;
import gq.yigit.mycity.votesFragment.VotesContent; import gq.yigit.mycity.votesFragment.VotesContent;
import gq.yigit.mycity.votesFragment.VotesFragment; import gq.yigit.mycity.votesFragment.VotesFragment;
@ -30,18 +29,20 @@ import org.json.JSONObject;
import java.util.HashMap; import java.util.HashMap;
import static java.security.AccessController.getContext;
public class MainActivity extends AppCompatActivity public class MainActivity extends AppCompatActivity
implements implements
NavigationView.OnNavigationItemSelectedListener, NavigationView.OnNavigationItemSelectedListener,
VotesFragment.OnListFragmentInteractionListener, VotesFragment.OnListFragmentInteractionListener,
MainFragment.OnFragmentInteractionListener, MainFragment.OnFragmentInteractionListener,
VoteFragment.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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -64,9 +65,10 @@ public class MainActivity extends AppCompatActivity
drawer.addDrawerListener(toggle); drawer.addDrawerListener(toggle);
toggle.syncState(); toggle.syncState();
FileActions file_manager = new FileActions(); FileActions file_manager = new FileActions();
String url = file_manager.readFromFile(cntxt,"server.config").trim();
url = file_manager.readFromFile(cntxt,"server.config").trim();
HashMap<String,String> request = new HashMap<>(); HashMap<String,String> request = new HashMap<>();
request.put("a","b");
request.put("username","efe");
request.put("password","12345");
WebRequest login_manager = new WebRequest(url + "/login/",false,request); WebRequest login_manager = new WebRequest(url + "/login/",false,request);
login_manager.addListener(this); login_manager.addListener(this);
login_manager.execute(); login_manager.execute();
@ -175,14 +177,23 @@ public class MainActivity extends AppCompatActivity
public void receivedResponse(boolean success,String response){ public void receivedResponse(boolean success,String response){
if(success) { if(success) {
try { 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(); 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){ }catch (Exception e){
Log.e("[ERROR]","Cannot receive userdata"); Log.e("[ERROR]","Cannot receive userdata");
} }
} }
} }
public void imageDownloaded(Bitmap img){
userAvatar = img;
}
} }

BIN
server_side/api/__pycache__/__init__.cpython-37.pyc View File


+ 12
- 11
server_side/api/app.py View File

@ -1,7 +1,8 @@
from flask import Flask, send_from_directory 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__) app = Flask(__name__)
api = Api(app) api = Api(app)
@ -13,16 +14,16 @@ def send_img(path):
if __name__ == '__main__': if __name__ == '__main__':
context = ('encryption/mycity.crt','encryption/mycity-decrypted.key') context = ('encryption/mycity.crt','encryption/mycity-decrypted.key')
api.add_resource(voting_system.Votings, '/votings', '/votings/')
api.add_resource(voting_system.Voting, '/votings/<int:voting_id>')
api.add_resource(voting_system.Vote, '/vote', '/vote/')
api.add_resource( voting_system.Votings, '/votings', '/votings/' )
api.add_resource( voting_system.Voting, '/votings/<int:voting_id>' )
api.add_resource( voting_system.Vote, '/vote', '/vote/' )
api.add_resource(rating_system.Ratings, '/ratings', '/ratings/')
api.add_resource(rating_system.Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
api.add_resource(rating_system.Rate, '/rate', '/rate/')
api.add_resource( rating_system.Ratings, '/ratings', '/ratings/' )
api.add_resource( rating_system.Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/' )
api.add_resource( rating_system.Rate, '/rate', '/rate/' )
api.add_resource(user_info.Users, '/users', '/users/')
api.add_resource(user_info.User, '/users/<path:user_id>', '/users/<path:user_id>/')
api.add_resource(user_info.Login, '/login', '/login/')
api.add_resource( user_info.Users, '/users', '/users/' )
api.add_resource( user_info.User, '/users/<path:user_id>', '/users/<path:user_id>/' )
api.add_resource( user_info.Login, '/login', '/login/' )
app.run(host='0.0.0.0', port=5000, ssl_context=context) app.run(host='0.0.0.0', port=5000, ssl_context=context)

BIN
server_side/api/login/__pycache__/__init__.cpython-37.pyc View File


BIN
server_side/api/login/__pycache__/login.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/__init__.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/rating_system.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/user_info.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/utils.cpython-37.pyc View File


BIN
server_side/api/modules/__pycache__/voting_system.cpython-37.pyc View File


+ 12
- 5
server_side/api/modules/databases/users.json View File

@ -1,16 +1,23 @@
[
{
{
"efe":{
"id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "id": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"username": "efe",
"realname": "Efe Aydın", "realname": "Efe Aydın",
"avatar": "img/user1.png", "avatar": "img/user1.png",
"password": "e10adc3949ba59abbe56e057f20f883e",
"password": "827ccb0eea8a706c4c34a16891f84e7b",
"email": "efeaydin@mycity.com",
"stats": { "stats": {
"bus_usage_week": 1, "bus_usage_week": 1,
"bus_usage_month": 5, "bus_usage_month": 5,
"bus_usage_year": 67 "bus_usage_year": 67
}, },
"daily_electricity_usage": [
10,
5,
5,
6,
7
],
"points": 50 "points": 50
} }
]
}

+ 2
- 2
server_side/api/modules/rating_system.py View File

@ -1,7 +1,7 @@
import os import os
import json import json
from . import utils
from api.modules import utils
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api, abort from flask_restful import Resource, Api, abort
@ -74,7 +74,7 @@ class Rate(Resource):
note=<note>& # ADDITIONAL note=<note>& # ADDITIONAL
rater_id=<user_id> rater_id=<user_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']) rating_id = int(request.args['rating_id'])
score = int(request.args['score']) score = int(request.args['score'])
if 0 >= score >= 10: if 0 >= score >= 10:


+ 16
- 11
server_side/api/modules/user_info.py View File

@ -1,7 +1,7 @@
import os import os
import json import json
from . import utils
from api.modules import utils
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api, abort from flask_restful import Resource, Api, abort
@ -29,7 +29,7 @@ class Users(Resource):
'username': args['username'], 'username': args['username'],
'realname': args.get('realname'), 'realname': args.get('realname'),
'avatar' : args.get('avatar'), 'avatar' : args.get('avatar'),
'password': utils.md5(args['password']),
'password': utils.md5( args[ 'password' ] ),
'stats': { 'stats': {
'bus_usage_week': 0, 'bus_usage_week': 0,
'bus_usage_month': 0, 'bus_usage_month': 0,
@ -48,7 +48,7 @@ class Users(Resource):
class User(Resource): class User(Resource):
def get(self, user_id): def get(self, user_id):
try: try:
user = utils.find_by_id(users, user_id)
user = utils.find_by_id( users, user_id )
if not user: if not user:
raise Exception('User not found!') raise Exception('User not found!')
del user['password'] del user['password']
@ -63,18 +63,23 @@ class Login(Resource):
username=<username>& username=<username>&
password=<password> password=<password>
""" """
#Password for efe is 12345
args = request.form args = request.form
print(args)
username = args['username'] 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__': if __name__ == '__main__':
api.add_resource(Users, '/users', '/users/') api.add_resource(Users, '/users', '/users/')


+ 2
- 2
server_side/api/modules/voting_system.py View File

@ -1,7 +1,7 @@
import os import os
import json import json
from . import utils
from api.modules import utils
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api, abort from flask_restful import Resource, Api, abort
@ -93,7 +93,7 @@ class Vote(Resource):
voter_id = request.args['voter_id'] voter_id = request.args['voter_id']
voting_id = int(request.args['voting_id']) - 1 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']: if voter_id not in votings[voting_id]['voters']:
vote_id = int(request.args['vote_id']) vote_id = int(request.args['vote_id'])
votings[voting_id]['votes'][str(vote_id)]['votes'] += 1 votings[voting_id]['votes'][str(vote_id)]['votes'] += 1


BIN
server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc View File


BIN
server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc View File


BIN
server_side/api/user_info/__pycache__/__init__.cpython-37.pyc View File


BIN
server_side/api/user_info/__pycache__/user_info.cpython-37.pyc View File


BIN
server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc View File


BIN
server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc View File


Loading…
Cancel
Save