From 9ba61fdfde67edc0c7e148fd35e171b9e9e0cc10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Wed, 20 Mar 2019 00:27:49 +0300 Subject: [PATCH] voting page done --- .../mycity/voteFragment/VoteFragment.java | 88 +++++++++++---- .../__pycache__/__init__.cpython-37.pyc | Bin 167 -> 167 bytes .../__pycache__/rating_system.cpython-37.pyc | Bin 2864 -> 2878 bytes .../api/rating_system/rating_system.py | 10 +- .../__pycache__/__init__.cpython-37.pyc | Bin 167 -> 167 bytes .../__pycache__/voting_system.cpython-37.pyc | Bin 3237 -> 3078 bytes ...{voting_system.py~efe => voting_system.py} | 0 .../api/voting_system/voting_system.py~HEAD | 101 ------------------ 8 files changed, 66 insertions(+), 133 deletions(-) rename server_side/api/voting_system/{voting_system.py~efe => voting_system.py} (100%) delete mode 100644 server_side/api/voting_system/voting_system.py~HEAD 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 875a314..432b3a0 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 @@ -10,36 +10,31 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.TextView; +import android.widget.*; import gq.yigit.mycity.R; import gq.yigit.mycity.tools.*; import org.json.JSONObject; +import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link VoteFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link VoteFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class VoteFragment extends Fragment implements responseListener, imageListener { - // TODO: Rename parameter arguments, choose names that match - // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; private String url; private String vote_id; - + private String user_vote = ""; private OnFragmentInteractionListener mListener; private ImageView header_img; private TextView title; private TextView desc; + private Spinner spinner; private Button submit; + private Context cntxt; public VoteFragment() { // Required empty public constructor @@ -66,21 +61,23 @@ public class VoteFragment extends Fragment implements responseListener, imageLis @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - header_img = container.findViewById(R.id.header); - title = container.findViewById(R.id.name); - desc = container.findViewById(R.id.description); - submit = container.findViewById(R.id.submit); + final View rootView = inflater.inflate(R.layout.fragment_vote, container, false); + header_img = rootView.findViewById(R.id.header); + title = rootView.findViewById(R.id.name); + desc = rootView.findViewById(R.id.description); + submit = rootView.findViewById(R.id.submit); + spinner = rootView.findViewById(R.id.vote); FileActions file_manager = new FileActions(); url = file_manager.readFromFile(getContext(),"server.config").trim(); String url_vote = url + "/votings/" + vote_id; - WebRequest request_manager = new WebRequest(url_vote,true,new HashMap()); - request_manager.addListener(this); - request_manager.execute(); - return inflater.inflate(R.layout.fragment_vote, container, false); + WebRequest data_request = new WebRequest(url_vote,true,new HashMap()); + data_request.addListener(this); + data_request.execute(); + cntxt = getContext(); + return rootView; } - // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); @@ -110,6 +107,7 @@ public class VoteFragment extends Fragment implements responseListener, imageLis } public void receivedResponse(boolean success,String response){ + Log.i("[INFO]",response); if(success){ try{ JSONObject vote_data = new JSONObject(response); @@ -118,13 +116,57 @@ public class VoteFragment extends Fragment implements responseListener, imageLis ImageDownload img_manager = new ImageDownload(); img_manager.addListener(this); img_manager.execute(url+(String)vote_data.get("img")); + + ArrayAdapter adapter; + List list; + list = new ArrayList(); + + Iterator keys = vote_data.getJSONObject("votes").keys(); + list.add("Please Select..."); + while (keys.hasNext()) { + Object key = keys.next(); + JSONObject value = vote_data.getJSONObject("votes").getJSONObject((String) key); + String component = value.getString("name"); + list.add(component); + } + + adapter = new ArrayAdapter(getContext(), + android.R.layout.simple_spinner_item, list); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + }catch (Exception e){ Log.e("[ERROR]",e.getMessage()); } + + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parentView, View selectedItemView, int position, long id) { + Log.i("[INFO]",String.valueOf(position)); + user_vote = String.valueOf(position); + } + @Override + public void onNothingSelected(AdapterView parentView) { + } + }); } } public void imageDownloaded(Bitmap img){ header_img.setImageBitmap(img); + submit.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(!user_vote.isEmpty()){ + HashMap params = new HashMap<>(); + params.put("voting_id",vote_id); + params.put("vote_id",user_vote); + WebRequest vote_request = new WebRequest(url+"/vote",true, params); + vote_request.execute(); + }else{ + Toast.makeText(cntxt,"Please selet a vote!",Toast.LENGTH_SHORT); + } + } + }); } } diff --git a/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc index b49d79ee6259a7f5836b453cf22e9815488487ce..50508aac74b8a1b3ba7dbc715e59d41e0caaa7c1 100644 GIT binary patch delta 19 ZcmZ3^xSWyOiIW8RwF zTb=}E^mEc^ukVo#K?XOuJ4PLkFK|z-KF@HYA562{>n}2EhM7IauV{yIG+p9!hB~>) z%krK4b%PP+zA%`}ow3;|DD5#{Xd7eXqM}69Tp(d_YFwoe|>%w=O2=sR+Os z$PN77pPQiR!7qFRRsYV4ZSij{dDq%>a*z?qx7JmZmp`m9Ur>$&?H-rb^(3+Hw?i;v zK-x)L6nsPh)X-8Oh-3tjht6gB#ok7dtUAq>Zse$F;_ME@UUw8@kylbQ0BKEKE|kxm z+QvDEoCjP0=n2vv)8D-a}_2ZzoZgc|Z0*$N^^Eh@yOaOEdbSucU73?3ww z)p9eYiP?KR5+(#z0WjyhpC0=J$cn7Gt8GzNwmQso^fm-90nP%_0grbwbk}PCvlIE* zeN9T@6cnk+p4ZycQ|Qz)%9)tm{hl}p;nRRBU0LDzylD5-f-iL|L6lNz|rHq9+ex z3La~mi^t@s2N8PrAMm6H4~wY(NWghxanX~z>}THhy`A@C=41Io+1v6w$57AnPX~7@ zW3P&Sj#t(;kikt(MyO4BmQ%Gl+~r0uH}N>_%`t0=S!RR}g0|1mB;fuOwe#aw)!T(@ z1|y8_8;o*$WVVY+JIqzu)jH&a~-ZP`cWsUb3p+xgm4w!8WB!#Yc)YnMGl-fc@@8Rf19BF!x_Gg zmj2!qWyHU^WXHN}=V3=E-&vbcL79mai=9D2MLg{8?%eA#Q3NmQ9F@}fWifMiBLwp_ zAg!z|AADH_PkjSRfgqC89(fa$<;-4?Uo*E*NjB|st5845#P|DRnAn}ZcpMKSET9$9 z0Hn!v-cY`@moG*TIRiKg(5<9Drd>2ubX|P|tU!=D|||4-!?i+=zn2?CuVQ34uDG1<+;n`PD!!02XDPHk#_N(+SqVe;%*`Nc%h* z=owvK{-1&57y5?Gi(`=FloU?ug5I^HbYjMPU9kv}KALLAhCY)~O+4qp5V9q^ca diff --git a/server_side/api/rating_system/rating_system.py b/server_side/api/rating_system/rating_system.py index ec95b75..0380fdf 100644 --- a/server_side/api/rating_system/rating_system.py +++ b/server_side/api/rating_system/rating_system.py @@ -2,15 +2,7 @@ import os import json from flask import Flask, request -<<<<<<< HEAD from flask_restful import Resource, Api, abort -======= -<<<<<<< HEAD -from flask_restful import Resource, Api -======= -from flask_restful import Resource, Api, abort ->>>>>>> efe ->>>>>>> yigit app = Flask(__name__) api = Api(app) @@ -91,4 +83,4 @@ if __name__ == '__main__': api.add_resource(Rating, '/ratings/', '/ratings//') api.add_resource(Rate, '/rate', '/rate/') - app.run(host='0.0.0.0', port=5000) +app.run(host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc b/server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc index d488601a0d3eaa9ef97570b0bcbac3e1881ffe58..7471bb5d360722f22705ac54961d8227f9f97b5a 100644 GIT binary patch delta 19 ZcmZ3^xSWyOiIlYF5HbM( D7|bd* delta 418 zcmYjMOH0E*5T4CDX%j>tHU%Yu2$f!hdJ&cSz+<6SsDdKW4bmtnj;lyX@KEq3J>=%8 zJ@^B}i+8n-W47_XVbPV;PLv|Kdj}RoEO8<`DpUo;(7U& zei;5OFcL~s_Mk@+nJS5LQ(-bwV&VZ+1-cDKexkt-YQViiLV#&Z4~>DztT0E7p+wD|)OX8?#`2-fZ5p*`?p`L)1e&kIF6@t%QM&*h zG@#^kqx8V6*A}ijEWqP7U0WS@>z4l;xX6E&>4M;j%or& - 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)