Browse Source

working on voting page

pull/2/head
Yiğit Çolakoğlu 6 years ago
parent
commit
98030325ff
20 changed files with 175 additions and 174 deletions
  1. +5
    -4
      MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java
  2. +1
    -1
      MyCity/app/src/main/java/gq/yigit/mycity/tools/FileActions.java
  3. +1
    -1
      MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java
  4. +1
    -1
      MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java
  5. +1
    -1
      MyCity/app/src/main/java/gq/yigit/mycity/tools/imageListener.java
  6. +1
    -1
      MyCity/app/src/main/java/gq/yigit/mycity/tools/responseListener.java
  7. +4
    -12
      MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java
  8. +2
    -2
      MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java
  9. +10
    -7
      MyCity/app/src/main/res/layout/fragment_vote.xml
  10. +0
    -0
      server_side/api/__init__.py
  11. BIN
      server_side/api/__pycache__/__init__.cpython-37.pyc
  12. +10
    -10
      server_side/api/app.py
  13. +0
    -0
      server_side/api/rating_system/__init__.py
  14. BIN
      server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc
  15. BIN
      server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc
  16. +0
    -0
      server_side/api/voting_system/__init__.py
  17. BIN
      server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc
  18. BIN
      server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc
  19. +101
    -93
      server_side/api/voting_system/voting_system.py
  20. +38
    -41
      server_side/api/voting_system/votings.json

+ 5
- 4
MyCity/app/src/main/java/gq/yigit/mycity/MainActivity.java View File

@ -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){


MyCity/app/src/main/java/gq/yigit/mycity/FileActions.java → MyCity/app/src/main/java/gq/yigit/mycity/tools/FileActions.java View File


MyCity/app/src/main/java/gq/yigit/mycity/ImageDownload.java → MyCity/app/src/main/java/gq/yigit/mycity/tools/ImageDownload.java View File


MyCity/app/src/main/java/gq/yigit/mycity/WebRequest.java → MyCity/app/src/main/java/gq/yigit/mycity/tools/WebRequest.java View File


MyCity/app/src/main/java/gq/yigit/mycity/imageListener.java → MyCity/app/src/main/java/gq/yigit/mycity/tools/imageListener.java View File


MyCity/app/src/main/java/gq/yigit/mycity/responseListener.java → MyCity/app/src/main/java/gq/yigit/mycity/tools/responseListener.java View File


+ 4
- 12
MyCity/app/src/main/java/gq/yigit/mycity/voteFragment/VoteFragment.java View File

@ -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


+ 2
- 2
MyCity/app/src/main/java/gq/yigit/mycity/votesFragment/VotesFragment.java View File

@ -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;


+ 10
- 7
MyCity/app/src/main/res/layout/fragment_vote.xml View File

@ -1,14 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".voteFragment.VoteFragment">
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">
<!-- TODO: Update blank fragment layout -->
<TextView
<ImageView
android:src="@android:color/background_dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
android:layout_height="236dp" android:id="@+id/imageView2" android:layout_weight="1"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/spinner2"/>
</FrameLayout>

+ 0
- 0
server_side/api/__init__.py View File


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


+ 10
- 10
server_side/api/app.py View File

@ -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/<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/' )
app.run(host='0.0.0.0', port=5000)

+ 0
- 0
server_side/api/rating_system/__init__.py View File


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


+ 0
- 0
server_side/api/voting_system/__init__.py 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


+ 101
- 93
server_side/api/voting_system/voting_system.py View File

@ -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=<voting_name>&
desc=<voting_desc>& # OPTIONAL
img=<voting_img>& # OPTIONAL
votes=[
{
"name": "<vote_name>",
"desc": "<vote_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=<voting_id>&vote_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/<int:voting_id>', '/votings/<int:voting_id>/')
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=<voting_name>&
desc=<voting_desc>& # OPTIONAL
img=<voting_img>& # OPTIONAL
votes=[
{
"name": "<vote_name>",
"desc": "<vote_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=<voting_id>&vote_id=<vote_id>&voter_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/<int:voting_id>', '/votings/<int:voting_id>/')
api.add_resource(Vote, '/vote', '/vote/')
app.run(host='0.0.0.0', port=5000)

+ 38
- 41
server_side/api/voting_system/votings.json View File

@ -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": []
}
}
}
]

Loading…
Cancel
Save