Spaces:
Sleeping
Sleeping
File size: 5,518 Bytes
0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec ecf06e7 0ba59ec 19a6cbe f8a47c9 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec f8a47c9 0ba59ec f8a47c9 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec 19a6cbe 0ba59ec f8a47c9 0ba59ec 19a6cbe 0ba59ec ecf06e7 0ba59ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask_socketio import SocketIO, emit
import pymongo
from bson.objectid import ObjectId
import redis
import json
from redis.exceptions import RedisError
# Khởi tạo Flask app và SocketIO
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
# Khởi tạo Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
CACHE_EXPIRE_TIME = 300 # 5 phút
# MongoDB connection
mongo_url = "mongodb+srv://ip6ofme:[email protected]/"
client = pymongo.MongoClient(mongo_url)
db = client["test"]
pdf_collection = db["PdfDetails"]
voter_collection = db["Voters"]
# API để upload file và lưu thông tin vào MongoDB
@app.route('/upload-files', methods=['POST'])
def upload_file():
# Chỉ lưu thông tin vào MongoDB
title = request.form.get('title')
group = request.form.get('group')
firebase_url = request.form.get('url')
new_pdf = {
'title': title,
'group': group,
'url': firebase_url,
'votes': 0
}
result = pdf_collection.insert_one(new_pdf)
return jsonify({'status': 'ok', 'id': str(result.inserted_id)})
# API để lấy số lượng votes của file theo ID
@app.route('/get-votes', methods=['GET'])
def get_votes():
file_id = request.args.get('id')
try:
file = pdf_collection.find_one({'_id': ObjectId(file_id)})
if not file:
return jsonify({'status': 'error', 'message': 'File not found'}), 404
return jsonify({'status': 'ok', 'votes': file.get('votes', 0)})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# API để lấy danh sách tất cả các file
@app.route('/get-files', methods=['GET'])
def get_files():
try:
files = pdf_collection.find({})
file_list = []
for file in files:
file_list.append({
'id': str(file['_id']),
'title': file['title'],
'group': file['group'],
'url': file['url'],
'votes': file.get('votes', 0)
})
return jsonify({'status': 'ok', 'data': file_list})
except Exception as e:
print(f"Error: {str(e)}")
return jsonify({'status': 'error', 'message': str(e)}), 500
# API để đăng ký người bình chọn
@app.route('/register-voter', methods=['POST'])
def register_voter():
data = request.json
name = data.get('name')
group = data.get('group')
role = data.get('role') # Thêm trường role
new_voter = {
'name': name,
'group': group,
'role': role, # Lưu role vào MongoDB
'number_of_votes': 0
}
result = voter_collection.insert_one(new_voter)
return jsonify({'status': 'ok', 'id': str(result.inserted_id)})
# API để bình chọn
@app.route('/vote-by-voter', methods=['POST'])
def vote_by_voter():
data = request.json
voter_id = data.get('voter_id')
file_id = data.get('file_id')
vote_count = data.get('vote_count', 1)
if vote_count <= 0:
return jsonify({'status': 'error', 'message': 'Vote count must be greater than 0'}), 400
voter = voter_collection.find_one({'_id': ObjectId(voter_id)})
if not voter:
return jsonify({'status': 'error', 'message': 'Voter not found'}), 404
max_votes = 10 if voter['role'] == 'judge' else 2
if voter['number_of_votes'] + vote_count > max_votes:
return jsonify({'status': 'error', 'message': 'Maximum votes exceeded'}), 400
try:
# Cập nhật MongoDB
voter_collection.update_one({'_id': ObjectId(voter_id)}, {'$inc': {'number_of_votes': vote_count}})
pdf_collection.update_one({'_id': ObjectId(file_id)}, {'$inc': {'votes': vote_count}})
# Xóa cache
redis_client.delete('all_files')
redis_client.delete(f'voter:{voter_id}')
# Emit socket event
updated_file = pdf_collection.find_one({'_id': ObjectId(file_id)})
socketio.emit('vote_update', {
'file_id': file_id,
'votes': updated_file.get('votes', 0)
})
return jsonify({'status': 'ok', 'message': f'Vote recorded successfully with {vote_count} votes'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# API để lấy thông tin người bình chọn
@app.route('/get-voter', methods=['GET'])
def get_voter():
voter_id = request.args.get('id')
# Kiểm tra cache
cached_voter = redis_client.get(f'voter:{voter_id}')
if cached_voter:
return jsonify(json.loads(cached_voter))
voter = voter_collection.find_one({'_id': ObjectId(voter_id)})
if not voter:
return jsonify({'status': 'error', 'message': 'Voter not found'}), 404
voter_data = {
'status': 'ok',
'name': voter['name'],
'group': voter['group'],
'role': voter['role'],
'number_of_votes': voter['number_of_votes']
}
# Lưu vào cache
redis_client.setex(f'voter:{voter_id}', CACHE_EXPIRE_TIME, json.dumps(voter_data))
return jsonify(voter_data)
# Socket events
@socketio.on('connect')
def handle_connect():
print('Client connected')
@socketio.on('disconnect')
def handle_disconnect():
print('Client disconnected')
# Khởi chạy server
if __name__ == "__main__":
socketio.run(app, port=5000, debug=True)
|