121 lines
4.2 KiB
Python
121 lines
4.2 KiB
Python
from pymongo import MongoClient
|
||
from bson import ObjectId, json_util
|
||
|
||
# 主环境 (生产环境为production,开发环境为development)
|
||
setting = 'development'
|
||
|
||
# 获取数据集
|
||
def col(arg):
|
||
conn = MongoClient('mongodb://coc:qlSfefSor5@mongo:27017/coc')
|
||
if setting == 'development':
|
||
arg += '_test'
|
||
if arg == 'lost':
|
||
return conn.coc.lost
|
||
elif arg == 'found':
|
||
return conn.coc.found
|
||
elif arg == 'rank':
|
||
return conn.coc.rank
|
||
elif arg == 'lost_test':
|
||
return conn.coc.lost_test
|
||
elif arg == 'found_test':
|
||
return conn.coc.found_test
|
||
elif arg == 'rank_test':
|
||
return conn.coc.rank_test
|
||
else:
|
||
return False
|
||
|
||
# 新增失物招领信息
|
||
def addLAF(data, add_type):
|
||
try:
|
||
col(add_type).insert_one(data)
|
||
except Exception as e:
|
||
# 失败了
|
||
return { 'errcode': 331, 'errmsg': '插入数据库失败'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 获取全部失物信息
|
||
def getLost():
|
||
arr = []
|
||
try:
|
||
for i in col('lost').find({'close':'false'},{'title': 1, 'create_time':1, 'img_url':1, 'total_addr':1,'type':1,'content':1}):
|
||
arr.append(i)
|
||
except Exception as e:
|
||
return {'errcode': 351, 'errmsg': 'lost表获取失败'}
|
||
return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'}
|
||
|
||
# 获取全部招领信息
|
||
def getFound():
|
||
arr = []
|
||
try:
|
||
for i in col('found').find({'close':'false'},{'title': 1, 'create_time':1, 'img_url':1, 'total_addr':1,'type':1, 'content':1}):
|
||
arr.append(i)
|
||
except Exception as e:
|
||
return {'errcode': 352, 'errmsg': 'found表获取失败'}
|
||
return {'errcode': 200, 'arr': json_util.dumps(arr), 'errmsg': 'ok'}
|
||
|
||
# 获取指定失物信息
|
||
def getDetail(id, get_type, errcode):
|
||
try:
|
||
info = col(get_type).find_one({"_id": ObjectId(id)},{'user_info':0})
|
||
if info:
|
||
return {'errcode': 200, 'detail': json_util.dumps(info), 'errmsg': 'ok'}
|
||
else:
|
||
return {'errcode': errcode, 'errmsg': '数据不存在'}
|
||
except Exception as e:
|
||
# id不合法
|
||
return {'errcode': errcode+1, 'errmsg': 'id不合法'}
|
||
|
||
# 删除某个失物
|
||
def delLAF(id, del_type, del_user_info):
|
||
try:
|
||
col(del_type).update({"_id": ObjectId(id)},
|
||
{'$set': {'close': 'true','del_user_info': del_user_info}})
|
||
except Exception as e:
|
||
# id不合法
|
||
print(e)
|
||
return {'errcode': 341, 'errmsg': 'id不合法'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 向一条记录添加留言
|
||
def commentLAF(id, data, laf_type):
|
||
try:
|
||
col(laf_type).update({"_id": ObjectId(id)},
|
||
{'$push': {'comment': data}})
|
||
except Exception as e:
|
||
# 失败
|
||
return {'errcode': 371, 'errmsg': '评论数据库修改失败'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 向一条记录添加回复
|
||
def replyLAF(id, comment, laf_type):
|
||
try:
|
||
col(laf_type).update({"_id": ObjectId(id)},
|
||
{'$set': {'comment': comment}})
|
||
except Exception as e:
|
||
# 失败
|
||
return {'errcode': 372, 'errmsg': '回复数据库修改失败'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 向排名表里增加或覆写数据
|
||
def addRank(nick, count, time):
|
||
try:
|
||
# col('rank').update({"cid":cid}, {'$setOnInsert':{"nick":nick}, '$set':{"grade":grade,"time":time}}, {'upsert':'true'})
|
||
col('rank').insert_one({"count":count,"time":time,"nick":nick})
|
||
except Exception as e:
|
||
# 失败
|
||
return {'errcode': 401, 'errmsg': '排名表修改失败'}
|
||
return {'errcode': 200, 'errmsg': 'ok'}
|
||
|
||
# 获取排名表所有信息(除了id)
|
||
def getRank():
|
||
time_rank = []
|
||
count_rank = []
|
||
try:
|
||
for i in col('rank').find({},{"_id":0}).sort([("time",1),("count",1)]).limit(10):
|
||
time_rank.append(i)
|
||
for i in col('rank').find({},{"_id":0}).sort([("count",1),("time",1)]).limit(10):
|
||
count_rank.append(i)
|
||
except Exception as e:
|
||
print(e)
|
||
return {'errcode': 411, 'errmsg': '排名表获取失败'}
|
||
return {'errcode': 200, 'time_rank': time_rank, 'count_rank': count_rank, 'errmsg': 'ok'} |