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 ef0eb2f..e8842e3 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java @@ -9,7 +9,6 @@ import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AlertDialog; -import android.util.Log; import android.view.*; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; @@ -18,6 +17,7 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.EditText; +import gq.yigit.mycity.tools.FileActions; import gq.yigit.mycity.voteFragment.VoteFragment; import gq.yigit.mycity.votesFragment.VotesContent; import gq.yigit.mycity.votesFragment.VotesFragment; @@ -91,7 +91,7 @@ public class MainActivity extends AppCompatActivity alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { FileActions file_manager = new FileActions(); - file_manager.writeToFile(input.getText().toString(),cntxt,"server.config"); + file_manager.writeToFile("http://" + input.getText().toString()+":5000",cntxt,"server.config"); } }); @@ -120,6 +120,7 @@ public class MainActivity extends AppCompatActivity VotesFragment fragment = new VotesFragment(); fragmentTransaction.replace(R.id.app_bar_main, fragment); fragmentTransaction.commit(); + fragmentTransaction.addToBackStack(null); } else if (id == R.id.parking) { @@ -139,11 +140,11 @@ public class MainActivity extends AppCompatActivity } public void onListFragmentInteraction(VotesContent.VoteItem vote){ - VoteFragment fragment = new VoteFragment(); + VoteFragment fragment = VoteFragment.newInstance(vote.id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.app_bar_main, fragment); - transaction.addToBackStack(null); transaction.commit(); + transaction.addToBackStack(null); } public void onFragmentInteraction(Uri uri){ diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/FileActions.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/FileActions.java similarity index 97% rename from MyCity/app/src/main/java/gq/yigit/mycity/FileActions.java rename to MyCity/app/src/main/java/gq/yigit/mycity/tools/FileActions.java index a872fec..647a2c4 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/FileActions.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/FileActions.java @@ -1,4 +1,4 @@ -package gq.yigit.mycity; +package gq.yigit.mycity.tools; import android.content.Context; import android.util.Log; diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/ImageDownload.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java similarity index 96% rename from MyCity/app/src/main/java/gq/yigit/mycity/ImageDownload.java rename to MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java index 43a9e7f..054c563 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/ImageDownload.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java @@ -1,4 +1,4 @@ -package gq.yigit.mycity; +package gq.yigit.mycity.tools; import android.graphics.Bitmap; import android.graphics.BitmapFactory; diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/WebRequest.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java similarity index 98% rename from MyCity/app/src/main/java/gq/yigit/mycity/WebRequest.java rename to MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java index f446766..bcee042 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/WebRequest.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java @@ -1,4 +1,4 @@ -package gq.yigit.mycity; +package gq.yigit.mycity.tools; import android.os.AsyncTask; import android.util.Log; diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/imageListener.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/imageListener.java similarity index 78% rename from MyCity/app/src/main/java/gq/yigit/mycity/imageListener.java rename to MyCity/app/src/main/java/gq/yigit/mycity/tools/imageListener.java index 005ffd9..7ab9b08 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/imageListener.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/imageListener.java @@ -1,4 +1,4 @@ -package gq.yigit.mycity; +package gq.yigit.mycity.tools; import android.graphics.Bitmap; diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/responseListener.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/responseListener.java similarity index 74% rename from MyCity/app/src/main/java/gq/yigit/mycity/responseListener.java rename to MyCity/app/src/main/java/gq/yigit/mycity/tools/responseListener.java index 2810e2c..515102c 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/responseListener.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/responseListener.java @@ -1,4 +1,4 @@ -package gq.yigit.mycity; +package gq.yigit.mycity.tools; public interface responseListener { void receivedResponse(boolean success, String response); diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java index 8a61147..d373271 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java @@ -4,6 +4,7 @@ import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -34,20 +35,11 @@ public class VoteFragment extends Fragment { // Required empty public constructor } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment VoteFragment. - */ // TODO: Rename and change types and number of parameters - public static VoteFragment newInstance(String param1, String param2) { + public static VoteFragment newInstance(String voteid) { VoteFragment fragment = new VoteFragment(); Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); + args.putString(ARG_PARAM1, voteid); fragment.setArguments(args); return fragment; } @@ -57,8 +49,8 @@ public class VoteFragment extends Fragment { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); } + Log.i("[INFO]","Voting right now"); } @Override diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java index 7da64b3..699415c 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java @@ -11,11 +11,11 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import gq.yigit.mycity.*; +import gq.yigit.mycity.R; +import gq.yigit.mycity.tools.*; import gq.yigit.mycity.votesFragment.VotesContent.VoteItem; import org.json.JSONArray; import org.json.JSONObject; - import java.util.HashMap; diff --git a/MyCity/app/src/main/res/layout/fragment_vote.xml b/MyCity/app/src/main/res/layout/fragment_vote.xml index fb676dd..90b90a1 100644 --- a/MyCity/app/src/main/res/layout/fragment_vote.xml +++ b/MyCity/app/src/main/res/layout/fragment_vote.xml @@ -1,14 +1,17 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".voteFragment.VoteFragment" android:orientation="vertical"> - + android:layout_height="236dp" android:id="@+id/imageView2" android:layout_weight="1"/> + \ No newline at end of file diff --git a/server_side/api/__init__.py b/server_side/api/__init__.py new file mode 100644 index 0000000..e69de29 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..b78818a 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 8af748f..8e14668 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -1,8 +1,8 @@ -from flask import Flask -from flask_restful import Resource, Api +from flask import Flask,send_from_directory +from flask_restful import Api -from voting_system import voting_system -from rating_system import rating_system +from api.voting_system import voting_system +from api.rating_system import rating_system app = Flask(__name__) api = Api(app) @@ -12,12 +12,12 @@ def send_img(path): return send_from_directory('images', path) if __name__ == '__main__': - 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/' ) app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/rating_system/__init__.py b/server_side/api/rating_system/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..b49d79e Binary files /dev/null and b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc 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 new file mode 100644 index 0000000..3105b8a Binary files /dev/null and b/server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc differ diff --git a/server_side/api/voting_system/__init__.py b/server_side/api/voting_system/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..d488601 Binary files /dev/null and b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc 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 new file mode 100644 index 0000000..7d63e79 Binary files /dev/null and b/server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc differ diff --git a/server_side/api/voting_system/voting_system.py b/server_side/api/voting_system/voting_system.py index b3928d7..729efda 100644 --- a/server_side/api/voting_system/voting_system.py +++ b/server_side/api/voting_system/voting_system.py @@ -1,93 +1,101 @@ -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, 'votings.json'), 'r') as f: - votings = json.load(f) - -class Votings(Resource): - def get(self): - voting = [ - { - 'id' : v['id'], - 'name': v['name'], - 'desc': v['desc'], - 'img' : v['img'] - } - for v in votings - ] - return voting - - def post(self): - """ - Example POST Data: - name=& - desc=& # OPTIONAL - img=& # OPTIONAL - votes=[ - { - "name": "", - "desc": "" # OPTIONAL - }, - (...) - ] - - """ - args = request.form - voting_id = len(votings) + 1 - voting = { - 'id': voting_id, - 'name': args['name'], - 'desc': args.get('desc'), - 'img' : args.get('img'), - 'votes': [ - { - 'id' : k + 1, - 'name': vote['name'], - 'desc': vote.get('desc'), - 'votes': 0 - } - for k, vote in enumerate(json.loads(args['votes'])) - ] - } - - votings.append(voting) - - with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: - json.dump(votings, f, indent=4) - - return voting - - -class Voting(Resource): - def get(self, voting_id): - try: - return votings[voting_id - 1] - except: - abort(404, error="Voting {} doesn't exist".format(voting_id)) - -class Vote(Resource): - def get(self): - """ - Example URL Query: - /vote?voting_id=&vote_id= - """ - voting_id = int(request.args['voting_id']) - vote_id = int(request.args['vote_id']) - votings[voting_id - 1]['votes'][vote_id - 1]['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] - -if __name__ == '__main__': - api.add_resource(Votings, '/votings', '/votings/') - api.add_resource(Voting, '/votings/', '/votings//') - api.add_resource(Vote, '/vote', '/vote/') - - app.run(host='0.0.0.0', port=5000) +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, 'votings.json'), 'r') as f: + votings = json.load(f) + +class Votings(Resource): + def get(self): + voting = [ + { + 'id' : v['id'], + 'name': v['name'], + 'desc': v['desc'], + 'img' : v['img'] + } + for v in votings + ] + return voting + + def post(self): + """ + Example POST Data: + name=& + desc=& # OPTIONAL + img=& # OPTIONAL + votes=[ + { + "name": "", + "desc": "" # OPTIONAL + }, + (...) + ] + + """ + args = request.form + voting_id = len(votings) + 1 + voting = { + 'id': voting_id, + 'name': args['name'], + 'desc': args.get('desc'), + 'img' : args.get('img'), + 'votes': [ + { + 'id' : k + 1, + 'name': vote['name'], + 'desc': vote.get('desc'), + 'votes': 0 + } + for k, vote in enumerate(json.loads(args['votes'])) + ] + } + + votings.append(voting) + + with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: + json.dump(votings, f, indent=4) + + return voting + + +class Voting(Resource): + def get(self, voting_id): + try: + return votings[voting_id - 1] + except: + abort(404, error="Voting {} doesn't exist".format(voting_id)) + +class Vote(Resource): + def get(self): + """ + Example URL Query: + /vote?voting_id=&vote_id=&voter_id= + """ + voting_id = int(request.args['voting_id']) + vote_id = int(request.args['vote_id']) + voter_id = int(request.args['voter_id']) + error = False + for i in range(votings[voting_id]["votes"]): + if(voter_id in votings[voting_id]["votes"][i]["votes"]): + error = True + if error: + return "An error occured" + + votings[voting_id]["votes"][vote_id]["votes"].append(voter_id) + with open(os.path.join(app.root_path, 'votings.json'), 'w') as f: + json.dump(votings, f, indent=4) + + return votings[voting_id - 1] + +if __name__ == '__main__': + api.add_resource(Votings, '/votings', '/votings/') + api.add_resource(Voting, '/votings/', '/votings//') + api.add_resource(Vote, '/vote', '/vote/') + + app.run(host='0.0.0.0', port=5000) diff --git a/server_side/api/voting_system/votings.json b/server_side/api/voting_system/votings.json index af13aa6..aea66db 100644 --- a/server_side/api/voting_system/votings.json +++ b/server_side/api/voting_system/votings.json @@ -1,42 +1,39 @@ -[ - { - "id": 1, - "name": "Test Voting", - "desc": "Sample voting description", - "img": "/img/voting.jpg", - "votes": [ - { - "id": 1, - "name": "Sample Vote 1", - "desc": "Sample description", - "votes": 5 - }, - { - "id": 2, - "name": "Sample Vote 2", - "desc": "Sample description", - "votes": 8 - } - ] - }, - { - "id": 2, - "name": "wooting", - "desc": "wooting desc", - "img": "/img/voting.jpg", - "votes": [ - { - "id": 0, - "name": "woote 1", - "desc": "woote desc", - "votes": 0 - }, - { - "id": 1, - "name": "woote 2", - "desc": "woote 3", - "votes": 0 - } - ] - } +[ + { + "id": 1, + "name": "Test Voting", + "desc": "Sample voting description", + "img": "/img/voting.jpg", + "votes": { + "1": { + "name": "Sample Vote 1", + "desc": "Sample description", + "votes": [] + }, + "2": { + "name": "Sample Vote 2", + "desc": "Sample description", + "votes": [] + } + } + }, + { + "id": 2, + "name": "wooting", + "desc": "wooting desc", + "img": "/img/voting.jpg", + "votes": { + "1":{ + "name": "woote 1", + "desc": "woote desc", + "votes": [] + }, + "2":{ + "id": 1, + "name": "woote 2", + "desc": "woote 3", + "votes": [] + } + } + } ] \ No newline at end of file