2021-09-04 10:50:46 +08:00

408 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pymongo import MongoClient
from bson import ObjectId, json_util
# 主环境 (生产环境为production开发环境为development)
ENV = 'production'
def col(arg):
"""
获取数据集
"""
# 链接数据库
conn = MongoClient('mongodb://cherry:fR1jW2xG3bE9@39.96.28.83:27017/cherry')
# 判断环境
if ENV == 'development':
arg += '_test'
return conn.cherry[arg]
def updateCookie(new_cookie):
"""
更新cookie
"""
# 字符串化
new_cookie = str(new_cookie)
try:
col('config').update({'key': 'cookie'}, {'$set': {'value': new_cookie}}, {'upsert': 'true'})
except Exception as e:
print(e)
return 'cookie数据库更新失败', 400
def findCookie():
"""
获取cookie
"""
try:
res = col('config').find_one({'key': 'cookie'}, {'_id': 0})
value = res['value']
cookie = eval(value)
return cookie, 200
except Exception as e:
print(e)
return 'cookie数据库查询失败', 300
def updateLastUpdate(sid, now):
"""
更新最后一次课程更新信息
"""
try:
col('user').update({'sid': sid}, {'$set': {'last_update': now}})
except Exception as e:
print(e)
return '数据更新时间数据库更新失败', 403
def insertUser(userinfo):
""""
插入新学生信息
"""
try:
col('user').insert_one(userinfo)
return 'OK', 200
except Exception as e:
print(e)
return '学生信息数据库插入失败', 100
def findUser(cid):
"""
获取学生信息
在group_list里边存放了[{'group_id'}]
取出之后需要遍历组的信息
"""
try:
userinfo = col('user').aggregate([
{
'$match': {
'cid': cid
}
},
{
'$lookup': {
'from': 'link',
'localField': 'sid',
'foreignField': 'sid',
'as': 'group_list'
}
},
{
'$project': {
'_id': 0,
'group_list._id': 0,
'group_list.sid': 0
}
}
])
return userinfo, 200
except Exception as e:
print(e)
return '学生信息数据库查询失败', 301
def insertInvite(cid, group_id):
"""
对用户添加用户组邀请
测试多次重复邀请用户接受或者删除的时候是否能删掉所有前端Set去重
"""
try:
col('user').update({'cid': cid}, {'$push': {'invite_list': group_id}})
return 'OK', 200
except Exception as e:
print(e)
return '用户组邀请数据库插入失败', 101
def deleteInvite(cid, group_id):
"""
用户或者管理员删除用户组邀请
"""
try:
col('user').update({'cid': cid}, {'$pull': {'invite': {'group_id': group_id}}})
return 'OK', 200
except Exception as e:
print(e)
return '用户组邀请数据库删除失败', 500
def bindUserGroup(sid, group_id):
"""
绑定用户以及用户组
"""
try:
col('link').insert_one({'sid': sid, 'group_id': group_id})
return 'OK', 200
except Exception as e:
print(e)
return '用户与用户组绑定数据库插入失败', 102
def unbindUserGroup(sid, group_id):
"""
解绑用户以及用户组
"""
try:
col('link').remove({'sid': sid, 'group_id': group_id})
return 'OK', 200
except Exception as e:
print(e)
return '用户与用户组解绑数据库删除失败', 501
def updateAvatar(cid, img_id):
"""
用户更新头像
"""
try:
col('user').update({'cid': cid}, {'$set': {'avatar': img_id}})
return 'OK', 200
except Exception as e:
print(e)
return '头像数据库更新失败', 401
def updateBg(cid, img_id):
"""
用户更新背景图片
"""
try:
col('user').update({'cid': cid}, {'$set': {'setting.bg': img_id}})
return 'OK', 200
except Exception as e:
print(e)
return '背景图数据库更新失败', 402
def insertGroup(group_info):
"""
用户创建用户组
"""
try:
col('group').insert_one(group_info)
return 'OK', 200
except Exception as e:
print(e)
return '用户组数据库插入失败', 103
def findGroup(group_id):
"""
查询用户组信息,附带用户组中用户信息
"""
try:
groupinfo = col('group').aggregate([
{
'$match': {
'group_id': group_id
}
},
{
'$lookup': {
'from': 'link',
'localField': 'group_id',
'foreignField': 'group_id',
'as': 'user_list'
}
},
{
'$lookup': {
'from': 'user',
'localField': 'cid',
'foreignField': 'cid',
'as': 'user_list'
}
},
{
'$project': {
'_id': 0,
'user_list._id': 0,
'user_list.invite_list': 0,
'user_list.cid':0,
'user_list.pwd':0,
'user_list.setting':0,
'user_list.last_update': 0,
}
}
])
return groupinfo, 200
except Exception as e:
print(e)
return '用户组信息数据库查询失败', 302
def deleteGroup(group_id):
"""
解散用户组
"""
try:
col('group').remove({'group_id': group_id})
return 'OK', 200
except Exception as e:
print(e)
return '解散用户组数据库删除失败', 504
def addLog(group_id, log):
"""
向用户组中添加操作记录
"""
try:
col('group').update({'group_id': group_id}, {'$push': {'log_list': log}})
return 'OK', 200
except Exception as e:
print(e)
return '操作记录数据库插入失败', 104
def groupInsertAdmin(cid, group_id):
"""
向管理组中添加管理
"""
try:
col('group').update({'group_id': group_id}, {'$push': {'admin_list': cid}})
return 'OK', 200
except Exception as e:
print(e)
return '用户组侧加入管理数据库插入失败', 106
def groupDeleteAdmin(cid, group_id):
"""
从管理组中删除管理
"""
try:
col('group').update({'group_id': group_id}, {'$pull': {'admin_list': cid}})
return 'OK', 200
except Exception as e:
print(e)
return '用户组侧移除管理数据库删除失败', 503
def userInsertCrouse(crouse):
"""
用户添加自定义课程
"""
try:
col('user_crouse').insert_one(crouse)
return 'OK', 200
except Exception as e:
print(e)
return '用户自定义课程数据库插入失败', 107
def userDeleteCrouse(crouse_id):
"""
用户删除自定义课程
加入是否自定义的判断,防止用户改前端导致删除非定义课程
"""
try:
col('user_crouse').remove({'crouse_id': crouse_id, 'is_personal': False})
return 'OK', 200
except Exception as e:
print(e)
return '用户自定义课程数据库删除失败', 505
def userDeleteAllCrouse(sid):
"""
用户删除所有课程(刷新课表用)
不删除自定义课程
"""
try:
col('user_crouse').remove({'sid': sid, 'is_personal': False})
return 'OK', 200
except Exception as e:
print(e)
return '用户所有课程数据库删除失败', 502
def userInsertAllCrouse(crouses):
"""
用户批量添加课表(刷新课表用)
"""
try:
col('user_crouse').insert_many(crouses)
return 'OK', 200
except Exception as e:
print(e)
return '用户所有课程数据库插入失败', 108
def insertRank(cid, nick, count, time):
"""
向排名表里增加或者覆写数据
"""
try:
col('rank').update({"cid": cid}, {'$setOnInsert': {"nick": nick}, '$set': {
"count": count, "time": time}} , {'upsert': 'true'})
# col('rank').insert_one({"count":count,"time":time,"nick":nick})
return 'OK', 200
except Exception as e:
# 失败
return '排名表数据库插入失败', 109
def findRank():
"""
获取所有排名信息
"""
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)
return {'time_rank': time_rank, 'count_rank': count_rank}, 200
except Exception as e:
print(e)
return '排名表数据库查询失败', 303
def findUserCrouse(sid):
"""
获取所有用户课程
"""
crouse_list = []
try:
for i in col('user_crouse').find({'sid': sid}, {'_id': 0}):
crouse_list.append(i)
return crouse_list, 200
except Exception as e:
print(e)
return '用户课程数据库查询失败', 304
def groupInsertCrouse(crouse):
"""
用户组添加课程
"""
try:
col('group_crouse').insert_one(crouse)
return 'OK', 200
except Exception as e:
print(e)
return '用户组课程数据库插入失败', 110
def groupDeleteCrouse(crouse_id):
"""
用户组删除课程
"""
try:
col('group_crouse').remove({'crouse_id': crouse_id})
return 'OK', 200
except Exception as e:
print(e)
return '用户组课程数据库删除失败', 506
def findGroupCrouse(group_id):
"""
获取所有指定用户组课程
"""
crouse_list = []
try:
for i in col('group_crouse').find({'group_id': group_id}):
crouse_list.append(i)
return crouse_list, 200
except Exception as e:
print(e)
return '用户组课程数据库查询失败', 305
def insertLog(log):
"""
插入操作记录
"""
try:
col('log').insert_one(log)
return 'OK', 200
except Exception as e:
print(e)
return '系统操作记录数据库插入失败', 111
def findLog(has_read_page):
"""
查询操作记录默认50条分割
"""
skip = 50 * (has_read_page - 1)
log_list = []
try:
for i in col('log').find({}).limit(50).skip(skip):
log_list.append(i)
return log_list, 200
except Exception as e:
print(e)
return '系统操作记录数据库查询失败', 306