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.

114 lines
3.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import os
  2. import copy
  3. import json
  4. from api.modules import utils
  5. from flask import Flask, request
  6. from flask_restful import Resource, Api, abort
  7. app = Flask(__name__)
  8. api = Api(app)
  9. db_path = os.path.join(app.root_path, 'databases', 'votings.json')
  10. user_db = os.path.join(app.root_path, 'databases', 'users.json')
  11. with open(db_path, 'r') as f:
  12. votings = json.load(f)
  13. with open(user_db, 'r') as f:
  14. users = json.load(f)
  15. class Votings(Resource):
  16. def get(self):
  17. voting = [
  18. {
  19. 'id' : v['id'],
  20. 'name': v['name'],
  21. 'desc': v['desc'],
  22. 'img' : v['img']
  23. }
  24. for v in votings
  25. ]
  26. return voting
  27. def post(self):
  28. """
  29. Example POST Data:
  30. name=<voting_name>&
  31. desc=<voting_desc>& # OPTIONAL
  32. img=<voting_img>& # OPTIONAL
  33. votes=[
  34. {
  35. "name": "<vote_name>",
  36. "desc": "<vote_desc>" # OPTIONAL
  37. },
  38. (...)
  39. ]
  40. """
  41. args = request.form
  42. voting_id = len(votings) + 1
  43. voting = {
  44. 'id': voting_id,
  45. 'name': args['name'],
  46. 'desc': args.get('desc'),
  47. 'img' : args.get('img'),
  48. 'voters': [],
  49. 'votes': [
  50. {
  51. 'id' : k + 1,
  52. 'name': vote['name'],
  53. 'desc': vote.get('desc'),
  54. 'votes': 0
  55. }
  56. for k, vote in enumerate(json.loads(args['votes']))
  57. ]
  58. }
  59. votings.append(voting)
  60. with open(db_path, 'w') as f:
  61. json.dump(votings, f, indent=4)
  62. return {'message': 'Success'}
  63. class Voting(Resource):
  64. def get(self, voting_id):
  65. try:
  66. voting = copy.deepcopy(votings[voting_id - 1])
  67. del voting['voters']
  68. return voting
  69. except:
  70. abort(404, error="Voting {} doesn't exist".format(voting_id))
  71. class Vote(Resource):
  72. def post(self):
  73. """
  74. Example URL Query:
  75. /vote?voting_id=<voting_id>&vote_id=<vote_id>&voter_id=<user_id>
  76. """
  77. voter_id = request.form['voter_id']
  78. voting_id = int(request.form['voting_id']) - 1
  79. if utils.find_by_id( users.values(), voter_id ):
  80. if voter_id not in votings[voting_id]['voters']:
  81. vote_id = int(request.form['vote_id'])
  82. votings[voting_id]['votes'][str(vote_id)]['votes'] += 1
  83. votings[voting_id]['voters'].append(voter_id)
  84. with open(db_path, 'w') as f:
  85. json.dump(votings, f, indent=4)
  86. return {'message': 'Success'}
  87. return {'error': 'Already voted'}
  88. return {'error': 'User doesn\'t exists'}
  89. if __name__ == '__main__':
  90. api.add_resource(Votings, '/votings', '/votings/')
  91. api.add_resource(Voting, '/votings/<int:voting_id>', '/votings/<int:voting_id>/')
  92. api.add_resource(Vote, '/vote', '/vote/')
  93. app.run(host='0.0.0.0', port=5000)