From 049504cb5d554d7169f49cca10bb40d8659d1c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 13 Mar 2019 18:26:09 +0300 Subject: [PATCH] voting system api --- server_side/api/requirements.txt | 12 ++++++++ server_side/api/voting_system/app.py | 43 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 server_side/api/requirements.txt create mode 100644 server_side/api/voting_system/app.py diff --git a/server_side/api/requirements.txt b/server_side/api/requirements.txt new file mode 100644 index 0000000..2bbbdf0 --- /dev/null +++ b/server_side/api/requirements.txt @@ -0,0 +1,12 @@ +aniso8601==6.0.0 +Click==7.0 +dnspython==1.16.0 +Flask==1.0.2 +Flask-RESTful==0.3.7 +itsdangerous==1.1.0 +Jinja2==2.10 +MarkupSafe==1.1.1 +pymongo==3.7.2 +pytz==2018.9 +six==1.12.0 +Werkzeug==0.14.1 diff --git a/server_side/api/voting_system/app.py b/server_side/api/voting_system/app.py new file mode 100644 index 0000000..b5fc631 --- /dev/null +++ b/server_side/api/voting_system/app.py @@ -0,0 +1,43 @@ +import ssl + +from flask import Flask +from flask_restful import Resource, Api, reqparse, abort + +from pymongo import MongoClient +from bson.objectid import ObjectId + +app = Flask(__name__) +api = Api(app) + +client = MongoClient("mongodb+srv://mycity:mycity123@mycity-3v9y3.mongodb.net/test?retryWrites=true", ssl_cert_reqs=ssl.CERT_NONE) +db = client.voting_system +collection = db.votings + +class Votings(Resource): + def get(self): + votings = [ + { + 'id' : str(doc['_id']), + 'name': doc['name'], + 'desc': doc['desc'], + 'img' : doc['img'] + } + for doc in collection.find({}) + ] + return votings + +class Vote(Resource): + def get(self, voting_id): + try: + doc = collection.find_one({'_id': ObjectId(voting_id)}) + doc['_id'] = str(doc['_id']) + return doc + except: + abort(404, error="Voting {} doesn't exist".format(voting_id)) + + +api.add_resource(Votings, '/votings') +api.add_resource(Vote, '/votings/') + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file