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.

117 lines
3.2 KiB

6 years ago
6 years ago
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. args = request.form
  43. voting_id = len(votings) + 1
  44. voting = {
  45. 'id': voting_id,
  46. 'name': args['name'],
  47. 'desc': args.get('desc'),
  48. 'img' : args.get('img'),
  49. 'voters': [],
  50. 'votes': [
  51. {
  52. 'id' : k + 1,
  53. 'name': vote['name'],
  54. 'desc': vote.get('desc'),
  55. 'votes': 0
  56. }
  57. for k, vote in enumerate(json.loads(args['votes']))
  58. ]
  59. }
  60. votings.append(voting)
  61. with open(db_path, 'w') as f:
  62. json.dump(votings, f, indent=4)
  63. return {'message': 'Success'}
  64. class Voting(Resource):
  65. def get(self, voting_id):
  66. try:
  67. voting = copy.deepcopy(votings[voting_id - 1])
  68. for i in range(len(voting['votes'])):
  69. del voting['votes'][str(i + 1)]['votes']
  70. del voting['voters']
  71. return voting
  72. except:
  73. abort(404, error="Voting {} doesn't exist".format(voting_id))
  74. class Vote(Resource):
  75. def post(self):
  76. """
  77. Example URL Query:
  78. /vote?voting_id=<voting_id>&vote_id=<vote_id>&voter_id=<user_id>
  79. """
  80. voter_id = request.form['voter_id']
  81. voting_id = int(request.form['voting_id']) - 1
  82. if utils.find_by_id( users.values(), voter_id ):
  83. if voter_id not in votings[voting_id]['voters']:
  84. vote_id = int(request.form['vote_id'])
  85. votings[voting_id]['votes'][str(vote_id)]['votes'] += 1
  86. votings[voting_id]['voters'].append(voter_id)
  87. with open(db_path, 'w') as f:
  88. json.dump(votings, f, indent=4)
  89. return {'message': 'Success'}
  90. return {'error': 'Already voted'}
  91. return {'error': 'User doesn\'t exists'}
  92. if __name__ == '__main__':
  93. api.add_resource(Votings, '/votings', '/votings/')
  94. api.add_resource(Voting, '/votings/<int:voting_id>', '/votings/<int:voting_id>/')
  95. api.add_resource(Vote, '/vote', '/vote/')
  96. app.run(host='0.0.0.0', port=5000)