From 98030325ffd803d6ddfd1ee2d3ffe557b58e8263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Tue, 19 Mar 2019 22:46:35 +0300 Subject: [PATCH] working on voting page --- .../java/gq/yigit/mycity/MainActivity.java | 9 +- .../yigit/mycity/{ => tools}/FileActions.java | 2 +- .../mycity/{ => tools}/ImageDownload.java | 2 +- .../yigit/mycity/{ => tools}/WebRequest.java | 2 +- .../mycity/{ => tools}/imageListener.java | 2 +- .../mycity/{ => tools}/responseListener.java | 2 +- .../mycity/voteFragment/VoteFragment.java | 16 +- .../mycity/votesFragment/VotesFragment.java | 4 +- .../app/src/main/res/layout/fragment_vote.xml | 17 +- server_side/api/__init__.py | 0 .../api/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 153 bytes server_side/api/app.py | 20 +- server_side/api/rating_system/__init__.py | 0 .../__pycache__/__init__.cpython-37.pyc | Bin 0 -> 167 bytes .../__pycache__/rating_system.cpython-37.pyc | Bin 0 -> 2864 bytes server_side/api/voting_system/__init__.py | 0 .../__pycache__/__init__.cpython-37.pyc | Bin 0 -> 167 bytes .../__pycache__/voting_system.cpython-37.pyc | Bin 0 -> 3237 bytes .../api/voting_system/voting_system.py | 194 +++++++++--------- server_side/api/voting_system/votings.json | 79 ++++--- 20 files changed, 175 insertions(+), 174 deletions(-) rename MyCity/app/src/main/java/gq/yigit/mycity/{ => tools}/FileActions.java (97%) rename MyCity/app/src/main/java/gq/yigit/mycity/{ => tools}/ImageDownload.java (96%) rename MyCity/app/src/main/java/gq/yigit/mycity/{ => tools}/WebRequest.java (98%) rename MyCity/app/src/main/java/gq/yigit/mycity/{ => tools}/imageListener.java (78%) rename MyCity/app/src/main/java/gq/yigit/mycity/{ => tools}/responseListener.java (74%) create mode 100644 server_side/api/__init__.py create mode 100644 server_side/api/__pycache__/__init__.cpython-37.pyc create mode 100644 server_side/api/rating_system/__init__.py create mode 100644 server_side/api/rating_system/__pycache__/__init__.cpython-37.pyc create mode 100644 server_side/api/rating_system/__pycache__/rating_system.cpython-37.pyc create mode 100644 server_side/api/voting_system/__init__.py create mode 100644 server_side/api/voting_system/__pycache__/__init__.cpython-37.pyc create mode 100644 server_side/api/voting_system/__pycache__/voting_system.cpython-37.pyc 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 0000000000000000000000000000000000000000..b78818a2766739fc256cc17037b8124f03416ade GIT binary patch literal 153 zcmZ?b<>g`k0=rB7F(CReh=2h`Aj1KOi&=m~3PUi1CZpd~vm6}{qtRI}4SX2`4o0ylFo|>DQSE65>T2z)=6knW~lB%Crkf|RZpP83g a5+AQuP') - 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 0000000000000000000000000000000000000000..b49d79ee6259a7f5836b453cf22e9815488487ce GIT binary patch literal 167 zcmZ?b<>g`kg1}4tF(CReh=2h`Aj1KOi&=m~3PUi1CZpd~vm6}{qtRI}4SX2`4o0ylFo|>DQSE65>T2z)=6knW~lB%Crkf~pkSdy8S o9$#EpT#}lrA0MBYmst`YuUAlci^C>2KczG$)edCSXCP((0MQ&Qc>n+a literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..3105b8a12baaa6f3f972cc4af2552cab5132ec2c GIT binary patch literal 2864 zcmb7GTW=dh6rS0e*Ow%2o90q(OA7@9ic@%^3Wh=fA(ix!R)7UsEuKlbuD$Edjten~ zgp@uYA*hge?1S^jU*IqB4^})N@zi&o`owo;9Vad2p}U?lbIzPOGkY%I`2Crg3d8gL zANRgoDl_&sF$WKL_8waH7l>ej_gK5=d7Fc`dRE_V+kL0)aFW|Sx9_#ReyLsJOxn`l zmjgaWNxi_idbjxD#Sd)Ti9MI3|u8 zuH0P^C&aus`ICjS^9|Nmcz~~D>kTV+Kk7xpv-r9;(`xqV>}k7_E? zak7=Ibh9+cx$4l$gU7<)J+$mu5Xs0>ZA)-r-DPcC*up{Y2p2ngdFfhVsKcR44=twl z0a|t(q*$ogMYhLx`3@b_UVlWr1g#BHe+^`d?Xa&-lcHeD43ryc8EP*x;oNtAv2Hss zcwvbh~^ z#UgDERN9ptoi#W5k<#I(Q4(#*zD#s8lj@FCVHOM7j0SPD@Lrg0XIl0j1p66Sm>Jjr(|GJnaPnd8h3^eN^4BFE69(=pVcC8%?UN-(6JHp7T6jqAb1NJry24uLy|k2 zsp{u;PbTUZ?J-Y6FB%MFf=o@(4;%}fHP4A zrTs8?b0$d;yzC>s?O@C;f;Ucpv1BY85WkVpQD zDaaBh_llEss9flwo=Vx|?kWb`HEj@K3COU)xOp{lSJ-3Z@IDs~Vc{1nu|5Zy6GE)3 zlUNu3ihz$63-iHuyMah$mb{_^`7LS>VRWTZscP6JXt^7GohqGs6w3(UX@a26!qAbg zHy=(P^A=i0$7I~%HF$eeDK?@qUk4gBFr^6E)ayiO%@I3?UVH-+Z=xBv@VO%XseR~R zZ~5fv{~h);)4U5TUp8Po;ZXa4W_WSOa+u9-xoTB7psfVAVp7qzaD}%A&(d08LusD_ z6YHKrURo_{n2PW6~ja%{oPJ@_??I_#aBNV1tYX+0As=6T#OKKmCoANw6A3i3rZE z;?9C)lbVVu6^UoAtaBJE%BM&1MlF(ji4?h7Mz`Lm`j!Pviw60g)GpoF;OH$V(s-s4231dJgpp&6^ZG%v0LsJX%I4V*V_jx5>0AeXfWneCkJW5{4sxa@A&|^1-M%TAB{m;F9yxfVW1=|2NYd zxilq5p6QyS(&~zN;~#*mbobr_HUgyq1C7PPd*rFy-K76jicVMH{frTW3V*UThdo93 zr|8Npik|#T_#PKgI1GUCJ^40wdTAtzyMj>E6n*j8wMYmuJo(qAhRKmYOrw$eYXxMt eCg`k0=-N9F(CReh=2h`Aj1KOi&=m~3PUi1CZpd~vm6}{qtRI}4SX2`4o0ylFo|>DQSE65>T2z)=6knW~lB%Crkf~pmUy_-Z o9$#EpT#}lrA0MBYmst`YuUAlci^C>2KczG$)edCSXCP((0KRD~XaE2J literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..7d63e7918f899b178841515c3d4158e1e5285039 GIT binary patch literal 3237 zcmb7GOOG2x5bo}IcGXzfvg?cz*xq z=NlVyg#3lf{!@g`I(+HhKse#FN7`9S+Z1@#W0BRiBD-x<9JhK-wG`d zPVj9a)v$awQDMBDHoIvOYbraaVE>zG`j@)m-f*ouHSlL1$mKL3Lpo7ckaNMQ=;H z*}%+Zi5j&9Dypd31wxqXdTcm%Kz;6C2d2>zCLBLPN0BasK`~|Rf^Qeen2i{C+4y&2 zb88Pg53RjNxx?N2L^;Zx6hNY|O!nw5#V~U3fit$0HStCuPl+n3Qg?1rR+Vm*7ok8( zP#_CqTe;mM${N{xj+cLew1zUNWMwk4Ab!>0EM!foK7BsC7exJ@s9(Ccd9{8aP{Fx; z3Pb$vYUTtwDQ{HVrb4x%ow-wq+L>OgWvt!NwE<4JEZmMo`L7Ol$&32tkH&o)wf_IIJ7DdaRzbW1Q5W@&ry> z5))C=+ie@iAIVgC6eiz*Z@&oY*QUtRPtqq=fm}qb&XbDROg)NS-#xT(0o868b12r0 zMopp{w7r$cNLxJ-OT-~#tuBgq5D6J5fDD6uPA>^~s@$*h5y3Z1#)ZE^q74@__4V(^b)JYceo58EJ-|i?ts|sN zB&3=NBx0U+F}i_jFcSocSI2>~n`2{oUVtw}BMD`+4Bj4=vTYEAHz2oPhbh@a(n5l3 z4(OTr5YJ~0YEDfh@=xtS8}=4?v0|a(Rb|xv0UbpY?V9q$t8&T#G6Tn^9Nd7wLD7!w zdv7UA*&`@s?%?PJaAZWj~6z{Y~=`cK&c6=HE8at zLf4xZOy?e;dC{P`tIFL9ta9-OW{T$J49!s&uVhhgREKenRr`?s<(1Ftmj^=b%pe_a zP4OO(tY~J~%-7D~6cmyf!%j9L^C9f5<(rt;o0}75wTG)w;v)kWSBJIpaoyxkJ?V4? zQt-nFBA3DQ66+EW?HOpYoNQF#Zz5I(N@N3uwIhRgTVyHtJg!+m@&c06NM1y82FXiE zP9b?2NTYBdMc=~Fw~-(yY8uK{kuiBTOE%2H&)`eZEaFrtzBXN;OMn(Owb@DN0akd| z;H*!Z$!~(nPf__alaWV|iGncp{h>GgpGk+s{a$Ohly_IbDd+OAtqqs|Z>BYLa7qka z)3k<#vrY4cKLBjv56&&XLqtWX8cUfU(P!G(!hcwa)@49mF$2DYAKI-!Pa=p({I8J+ z1X%&2+YzP1EGV;hm{6dLwT(cZMGx`76tFaBF9)2X_UUi39K;9Wkq1S4s~PyN;jNfH Z2LzG3M&}u}OVHNdsTFDqo{3hK{Rf0%#Df3; literal 0 HcmV?d00001 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