def post(self): """ @@@ ## 申诉被冒领的作者 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | author_id | false | string | 认领的作者id | | description | false | string | 申诉理由 | ### return - #### data > 返回创建的申诉 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('author_id', type=str, required=True) parser.add_argument('description', type=str, required=True) req = parser.parse_args() token = req.get('token') author_id = req.get('author_id') description = req.get('description') username = verify_token(token) if username == None: return{ 'success': False, 'message': 'token无效'}, 403 author_ref = db.collection('author').document(author_id) author = author_ref.get() if not author.exists: return{ 'success': False, 'message': '作者不存在'}, 403 if not 'bind_user' in author.to_dict(): return{ 'success': False, 'message': '作者未被认领'}, 403 data = { 'author_id': author_id, 'username': username, 'description': description, 'status': 0, } t = time.time() r_id = str(int(round(t * 1000))) db.collection('report').document(r_id).set(data) data['author'] = author.to_dict() return{ 'success': True, 'data': data}
def post(self): """ @@@ ## 用户修改密码 ## header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | | ### args 参数位于body | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | old_password | false | string | 旧密码 | | new_password | false | string | 新密码 | ### return 无data @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument("old_password", type=str, required=True) parser.add_argument("new_password", type=str, required=True) req = parser.parse_args() token = req.get('token') old_password = req.get('old_password') new_password = req.get('new_password') username = verify_token(token) if username == None: return{ 'success': False, 'message': 'token无效'}, 403 user_ref = db.collection('user').document(username) user = user_ref.get().to_dict() pwhash = user['password'] if check_password_hash(pwhash, old_password): new_pwhash = generate_password_hash( new_password, method='pbkdf2:sha1', salt_length=8) user_ref.update({'password': new_pwhash}) return{ 'success': True, 'message': '密码修改成功'} else: return{ 'success': False, 'message': '旧密码不正确'}, 403
def post(self): """ @@@ ## 用户修改个人信息 ## header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | | ### args 参数位于body | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | name | true | string | 真实姓名 | | introduction | true | string | 自我介绍 | | location | true | string | 用户位置 | ### return - #### data > 返回用户信息 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument("name", type=str, required=False) parser.add_argument("introduction", type=str, required=False) parser.add_argument("location", type=str, required=False) req = parser.parse_args() token = req.get('token') username = verify_token(token) if username == None: return{ 'success': False, 'message': 'token无效'}, 403 name = req.get("name") introduction = req.get("introduction") location = req.get("location") user_ref = db.collection('user').document(username) if name != None: user_ref.update({u'name': name}) if introduction != None: user_ref.update({u'introduction': introduction}) if location != None: user_ref.update({u'location': location}) data = user_ref.get().to_dict() data.pop('password') return{ 'success': True, 'data': data}
def verify_admin_token(token): admin_name = verify_token(token) if admin_name == None: return None # 查询一下admin表里面是否有该用户 #admin/admin_users/username admin = db.collection('admin') admin_users = admin.document('admin_users').get() admins = admin_users.to_dict()['username'] if admin_name in admins: return admin_name else: return None
def post(self): """ @@@ ## 显示订阅的项目 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### return data 订阅的项目 @@@ """ print("显示订阅的项目") parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') req = parser.parse_args() token = req.get('token') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'}, 403 fund_ids = [] ref = db.collection('subscribe').where(u'username', u'==', username).get() for fund in ref: if 'fund_id' in fund.to_dict(): fund_ids.append(fund.to_dict()['fund_id']) print(fund_ids) funds = [] print(fund_ids) for fund_id in fund_ids: fund = db.collection('fund').document(fund_id).get() if fund.exists: fund = fund.to_dict() fund['id'] = fund_id author = db.collection('author').document( fund['author_id']).get().to_dict() author['id'] = fund['author_id'] fund['author'] = author fund.pop('author_id') funds.append(fund) print(funds) print("aaaaaaaaaaaaaaaaaaaaaaaaaaaa") return {'success': True, 'data': funds}, 200
def post(self): """ @@@ ## 订阅科研人员 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | author_id | false | string | 订阅的作者id | ### return data @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('author_id', type=str, required=True) req = parser.parse_args() token = req.get('token') author_id = req.get('author_id') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'}, 403 author_ref = db.collection('author').document(author_id) author = author_ref.get() if not author.exists: return {'success': False, 'message': '作者不存在'}, 403 data = {'username': username, 'author_id': author_id} subscribes = db.collection('subscribe').where(u'username', u'==', username).where( u'author_id', u'==', author_id).get() for subscribe in subscribes: if subscribe.to_dict()['author_id'] == author_id: return {'success': False, 'message': '您已订阅该作者'}, 403 db.collection('subscribe').add(data) return {'success': True, 'data': data}
def post(self): """ @@@ ## 是否订阅项目 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | fund_id | false | string | 订阅的项目id | ### return 是否订阅项目 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('fund_id', type=str, required=True) req = parser.parse_args() token = req.get('token') fund_id = req.get('fund_id') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'} fund_ref = db.collection('fund').document(fund_id) fund = fund_ref.get() if not fund.exists: return {'success': False, 'message': '项目不存在'} subscribes = db.collection('subscribe').where(u'username', u'==', username).where( u'fund_id', u'==', fund_id).get() for subscribe in subscribes: if subscribe.to_dict()['fund_id'] == fund_id: return {'success': True, 'message': '您已订阅该项目'} return {'success': False, 'data': '您未订阅该项目'}
def post(self): """ @@@ ## 显示订阅的论文 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### return data 订阅的论文 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') req = parser.parse_args() token = req.get('token') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'}, 403 paper_ids = [] ref = db.collection('subscribe').where(u'username', u'==', username).get() for paper in ref: if 'paper_id' in paper.to_dict(): paper_ids.append(paper.to_dict()['paper_id']) print(paper_ids) papers = [] for paper_id in paper_ids: paper = db.collection('paper').document(paper_id).get() if paper.exists: paper = paper.to_dict() paper['id'] = paper_id get_venue(paper) get_authors(paper['authors']) papers.append(paper) return {'success': True, 'data': papers}, 200
def post(self): """ @@@ ## 取消订阅项目 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | fund_id | false | string | 订阅的项目id | ### return 无data @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('fund_id', type=str, required=True) req = parser.parse_args() token = req.get('token') fund_id = req.get('fund_id') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'}, 403 subscribes = db.collection('subscribe').where(u'username', u'==', username).where( u'fund_id', u'==', fund_id).get() for subscribe in subscribes: db.collection('subscribe').document(subscribe.id).delete() return {'success': True, 'message': '取消订阅成功'} return {'success': False, 'message': '您未订阅该项目'}, 403
def post(self): """ @@@ ## 显示订阅的科研人员 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | token | ### return data 订阅的作者列表 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') req = parser.parse_args() token = req.get('token') username = verify_token(token) if username == None: return {'success': False, 'message': 'token无效'}, 403 author_ids = [] ref = db.collection('subscribe').where(u'username', u'==', username).get() for author in ref: if 'author_id' in author.to_dict(): author_ids.append(author.to_dict()['author_id']) authors = [] for author_id in author_ids: author = db.collection('author').document(author_id).get() if author.exists: author = author.to_dict() author['id'] = author_id authors.append(author) return {'success': True, 'data': authors}, 200
def post(self): """ @@@ ## 认领作者 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | author_id | false | string | 认领的作者id | ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | token | false | string | 用于验证身份的token | @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('author_id', type=str, required=True) req = parser.parse_args() token = req.get('token') author_id = req.get('author_id') username = verify_token(token) if username == None: return{ 'success': False, 'message': 'token无效'}, 403 author_ref = db.collection('author').document(author_id) user_ref = db.collection('user').document(username) user = user_ref.get() author = author_ref.get() if user.to_dict()['activate'] == False: return{ 'success': False, 'message': '用户未激活'}, 403 if not author.exists: return{ 'success': False, 'message': '作者不存在'}, 403 if 'bind_user' in author.to_dict(): return{ 'success': False, 'message': '作者已被认领'}, 403 authors = db.collection('author').where( 'bind_user', '==', username).get() for author in authors: db.collection('author').document( author.id).update({u'bind_user': delete_field}) author_ref.update({u'bind_user': username}) user_ref.update({u'bind_author': author_id}) return{ 'success': True, 'message': '认领成功'}