fix(upload): 处理重复 ID,出现 DuplicateKey 时替换现有文档并启用
This commit is contained in:
9
app.py
9
app.py
@@ -33,6 +33,7 @@ from flask_session import Session
|
||||
from flask_wtf.csrf import CSRFProtect, generate_csrf, CSRFError
|
||||
from ffmpy import FFmpeg
|
||||
from pymongo import MongoClient
|
||||
from pymongo.errors import DuplicateKeyError
|
||||
from redis import Redis
|
||||
|
||||
def take_config(name, required=False):
|
||||
@@ -852,8 +853,12 @@ def upload_file():
|
||||
db_entry['enabled'] = True
|
||||
pprint.pprint(db_entry)
|
||||
|
||||
# mongoDBにデータをぶち込む
|
||||
client['taiko']["songs"].insert_one(db_entry)
|
||||
# mongoDBにデータをぶち込む(重複IDは上書き)
|
||||
coll = client['taiko']["songs"]
|
||||
try:
|
||||
coll.insert_one(db_entry)
|
||||
except DuplicateKeyError:
|
||||
coll.replace_one({"id": db_entry["id"]}, db_entry, upsert=True)
|
||||
# キャッシュ削除(/api/songs)
|
||||
try:
|
||||
app.cache.delete_memoized(route_api_songs)
|
||||
|
||||
Reference in New Issue
Block a user