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.

115 lines
3.1 KiB

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