You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

  1. import ssl
  2. from flask import Flask
  3. from flask_restful import Resource, Api, reqparse, abort
  4. from pymongo import MongoClient
  5. from bson.objectid import ObjectId
  6. app = Flask(__name__)
  7. api = Api(app)
  8. client = MongoClient("mongodb+srv://mycity:mycity123@mycity-3v9y3.mongodb.net/test?retryWrites=true", ssl_cert_reqs=ssl.CERT_NONE)
  9. db = client.voting_system
  10. collection = db.votings
  11. class Votings(Resource):
  12. def get(self):
  13. votings = [
  14. {
  15. 'id' : str(doc['_id']),
  16. 'name': doc['name'],
  17. 'desc': doc['desc'],
  18. 'img' : doc['img']
  19. }
  20. for doc in collection.find({})
  21. ]
  22. return votings
  23. class Vote(Resource):
  24. def get(self, voting_id):
  25. try:
  26. doc = collection.find_one({'_id': ObjectId(voting_id)})
  27. doc['_id'] = str(doc['_id'])
  28. return {
  29. k.replace('_id', 'id'): v
  30. for k, v in doc.items()
  31. }
  32. except:
  33. abort(404, error="Voting {} doesn't exist".format(voting_id))
  34. api.add_resource(Votings, '/votings')
  35. api.add_resource(Vote, '/votings/<voting_id>')
  36. if __name__ == '__main__':
  37. app.run(debug=True)