Browse Source

Merge remote-tracking branch 'origin/efe'

pull/2/head
Yiğit Çolakoğlu 6 years ago
parent
commit
0e13efafe6
4 changed files with 106 additions and 23 deletions
  1. +0
    -1
      server_side/api/requirements.txt
  2. +64
    -22
      server_side/api/voting_system/app.py
  3. BIN
      server_side/api/voting_system/images/voting.jpg
  4. +42
    -0
      server_side/api/voting_system/votings.json

+ 0
- 1
server_side/api/requirements.txt View File

@ -6,7 +6,6 @@ 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

+ 64
- 22
server_side/api/voting_system/app.py View File

@ -1,43 +1,85 @@
import os
import ssl
import json
from flask import Flask
from flask_restful import Resource, Api, reqparse, abort
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask import Flask, send_from_directory
from flask_restful import Resource, Api, abort, reqparse
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
parser = reqparse.RequestParser()
parser.add_argument('name', required=True)
parser.add_argument('desc')
parser.add_argument('img')
parser.add_argument('votes', required=True)
with open(os.path.join(app.root_path, 'votings.json'), 'r') as f:
votings = json.load(f)
class Votings(Resource):
def get(self):
votings = [
voting = [
{
'id' : str(doc['_id']),
'name': doc['name'],
'desc': doc['desc'],
'img' : doc['img']
'id' : v['id'],
'name': v['name'],
'desc': v['desc'],
'img' : v['img']
}
for doc in collection.find({})
for v in votings
]
return votings
return voting
def post(self):
args = parser.parse_args()
voting_id = max(len(votings)) + 1
voting = {
'id': voting_id,
'name': args['name'],
'desc': args['desc'],
'img' : args['img'],
'votes': [
{
'id': k,
'name': vote['name'],
'desc': vote['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 Vote(Resource):
class Voting(Resource):
def get(self, voting_id):
try:
doc = collection.find_one({'_id': ObjectId(voting_id)})
doc['_id'] = str(doc['_id'])
return doc
return votings[voting_id - 1]
except:
abort(404, error="Voting {} doesn't exist".format(voting_id))
class Vote(Resource):
def get(self, voting_id, 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]
api.add_resource(Votings, '/votings', '/votings/')
api.add_resource(Voting, '/votings/<int:voting_id>')
api.add_resource(Vote, '/vote/<int:voting_id>/<int:vote_id>')
api.add_resource(Votings, '/votings')
api.add_resource(Vote, '/votings/<voting_id>')
@app.route('/img/<path:path>')
def send_img(path):
return send_from_directory('images', path)
if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0', port=5000, debug=True)

BIN
server_side/api/voting_system/images/voting.jpg View File

Before After
Width: 640  |  Height: 623  |  Size: 36 KiB

+ 42
- 0
server_side/api/voting_system/votings.json View File

@ -0,0 +1,42 @@
[
{
"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
}
]
}
]

Loading…
Cancel
Save