def post(self): """ @@@ ## 显示没有处理过的申诉 只显示没有处理过的申诉信息 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | 管理员的token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | start_after | true | string | 偏移游标 | ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| > 返回申诉的列表(20个) @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() token = req.get('token') start_after = req.get('start_after') admin_name = verify_admin_token(token) if admin_name == None: return {'success': False, 'message': '请以管理员身份操作'}, 403 reports = [] ref = db.collection('report').where(u'status', u'==', 0) #未处理的状态都是0 start_after = db.collection('report').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).stream() else: ref = ref.limit(20).stream() for report in ref: report_id = report.id report = report.to_dict() author_id = report['author_id'] author = db.collection('author').document(author_id).get() if not author.exists: return {'success': False, 'message': 'Author不存在'}, 403 else: author = author.to_dict() author['id'] = author_id report['author'] = author report.pop('author_id') report['report_id'] = report_id reports.append(report) return {'success': True, 'data': reports}, 200
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 cancel_bind_author(author_id, username): author_ref = db.collection('author').document(author_id) user_ref = db.collection('user').document(username) author = author_ref.get() user = user_ref.get() if 'bind_user' in author.to_dict(): author_ref.update({u'bind_user': firestore.DELETE_FIELD}) if 'bind_author' in user.to_dict(): user_ref.update({u'bind_author': firestore.DELETE_FIELD})
def post(self): """ @@@ ## 同意用户的申诉请求 **管理员同意用户的申请请求** ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | 管理员的token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | report_id | false | string | 要处理的申诉id | ### return 无data @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument('report_id', type=str, required=True) req = parser.parse_args() token = req.get('token') report_id = req.get('report_id') admin_name = verify_admin_token(token) if admin_name == None: return {'success': False, 'message': '请以管理员身份操作'}, 403 report_ref = db.collection('report').document(report_id) report = report_ref.get() report_dict = report.to_dict() if report_dict['status'] == 1: return {'success': True, 'message': '该申诉信息已经处理'}, 200 else: report_ref.update({u'status': 1}) author_id = report_dict['author_id'] 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': '作者未被认领'}, 200 #获得该作者绑定的用户名 username = author.to_dict()['bind_user'] cancel_bind_author(author_id, username) return {'success': True, 'message': '处理申诉信息成功'}, 200
def post(self): """ @@@ ## 显示所有用户的列表 ### header args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | token | false | string | 管理员的token | ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | start_after | true | string | 偏移游标 | ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| > data:用户列表 @@@ """ parser = RequestParser() parser.add_argument('token', type=str, required=True, location='headers') parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() token = req.get('token') start_after = req.get('start_after') admin_name = verify_admin_token(token) if admin_name == None: return {'success': False, 'message': '请以管理员身份操作'}, 403 users_list = [] user_ref = db.collection('user') start_after = db.collection('user').document(start_after).get() if start_after.exists: users = user_ref.start_after(start_after).limit(20).stream() else: users = user_ref.limit(20).stream() for user in users: user = user.to_dict() user.pop('password') users_list.append(user) return {'success': True, 'data': users_list}, 200
def get(self, author_id): """ @@@ # 根据ID获取作者关系图谱 # args 无 # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | 列表 | 有合作关系的作者,仅保留前20位 | 相比与一般的作者数据,返回地数据中增加了weight,表示合作篇数 @@@ """ ref = db.collection('paper').where(u'authors', u'array_contains', author_id) weight = {} ref = ref.limit(100).get() for paper in ref: paper = paper.to_dict() authors = paper['authors'] for author in authors: if author == author_id: continue if author in weight: weight[author] += 1 else: weight[author] = 1 weight = sorted(weight.items(), key=lambda x: x[1], reverse=True) auhtors = [] for author in weight[0:10]: doc = db.collection('author').document(author[0]).get() if doc.exists: doc = doc.to_dict() doc['id'] = author[0] doc['weight'] = author[1] else: doc = { 'name': author[0], 'weight': author[1], } auhtors.append(doc) return { 'success': True, 'data': auhtors, }
def get(self): """ @@@ ## 获取排序的论文列表 ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | order_by | false | string | 排序字段 | | start_after | true | string | 偏移游标 | 排序字段可选:n_citation, year, id ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 排好序的的论文列表 | @@@ """ parser = RequestParser() parser.add_argument("order_by", type=str, location="args", required=True) parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() order_by = req.get("order_by") start_after = req.get("start_after") papers = [] if order_by == "id": ref = db.collection('paper') else: ref = db.collection('paper').order_by(order_by, direction=desc) start_after = db.collection('paper').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() for paper in ref: a_id = paper.id paper = paper.to_dict() paper['id'] = a_id get_venue(paper) get_authors(paper['authors']) papers.append(paper) return {'success': True, 'data': papers}
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 get(self): """ @@@ ## 根据DOI获取论文 ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | doi | false | string | doi号 | ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 一般情况只会有一个或0个 | @@@ """ parser = RequestParser() parser.add_argument("doi", type=str, location="args", required=True) req = parser.parse_args() doi = req.get("doi") ref = db.collection('paper').where(u'doi', u'==', doi).limit(1).get() papers = [] for paper in ref: p_id = paper.id paper = paper.to_dict() paper['id'] = p_id get_venue(paper) get_authors(paper['authors']) papers.append(paper) return {'success': True, 'data': papers}
def get(self, author_id): """ @@@ # 根据ID获取作者 # args 无 # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | id | false | string | id | | name | false | string | 姓名 | | orgs | ture | string | 所属机构 | | h_index | true | int | H 指数 | | n_pubs | ture | int | 论文数(与论文数据库不完全匹配) | | n_citation | ture | int | 被引量 | | avatar | ture | str | 头像链接 | @@@ """ auhtor = db.collection('author').document(author_id).get() if auhtor.exists: auhtor = auhtor.to_dict() auhtor['id'] = author_id auhtor['avatar'] = get_avatar(author_id) return {'success': True, 'data': auhtor} else: return {'success': False, 'message': '作者不存在'}, 404
def get(self): """ @@@ # 获取排序的作者列表 # args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | order_by | false | string | 排序字段 | | start_after | true | string | 偏移游标 | 排序字段可选:h_index,n_pubs,n_citation, id,orgs(一般不用) # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 排好序的的作者列表 | @@@ """ parser = RequestParser() parser.add_argument("order_by", type=str, location="args", required=True) parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() order_by = req.get("order_by") start_after = req.get("start_after") authors = [] if order_by == "id": ref = db.collection('author') else: ref = db.collection('author').order_by(order_by, direction=desc) start_after = db.collection('author').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() for author in ref: a_id = author.id author = author.to_dict() author['id'] = a_id authors.append(author) return {'success': True, 'data': authors}
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): """ @@@ ## 用户注册 ### args 参数位于body | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | username | false | string | 用户名,不能重复 | | password | false | string | 密码 | | email | false | string | 密码 | | name | true | string | 真实姓名 | | introduction | true | string | 自我介绍 | | location | true | string | 用户位置 | ### return - #### data > 返回注册的用户信息 @@@ """ parser = RequestParser() parser.add_argument("username", type=str, required=True) parser.add_argument("password", type=str, required=True) parser.add_argument("email", type=str, required=True) 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() username = req.get("username") users = db.collection('user') doc = users.document(username).get() if doc.exists: return{ 'success': False, 'message': '用户名已存在'} password = req.get("password") name = req.get("name") introduction = req.get("introduction") location = req.get("location") email = req.get("email") pwhash = generate_password_hash( password, method='pbkdf2:sha1', salt_length=8) data = { 'username': username, 'password': pwhash, 'email': email, 'name': name, 'introduction': introduction, 'location': location, 'activate': False, } users.document(username).set(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 get(self, author_id): """ @@@ # 获取该作者的项目 # args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | start_after | true | string | 偏移游标 | # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | | @@@ """ parser = RequestParser() parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() start_after = req.get("start_after") ref = db.collection('fund').where(u'author_id', u'==', author_id) funds = [] start_after = db.collection('fund').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() for fund in ref: p_id = fund.id fund = fund.to_dict() fund['id'] = p_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) return {'success': True, 'data': funds}
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 get(self): """ @@@ ## 根据出版物获取论文 ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | venue | false | string | venue id | | start_after | true | string | 偏移游标 | ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 该出版物下属的论文 | @@@ """ parser = RequestParser() parser.add_argument("venue", type=str, location="args", required=True) parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() venue = req.get("venue") start_after = req.get("start_after") ref = db.collection('paper').where(u'venue', u'==', venue) start_after = db.collection('paper').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() papers = [] for paper in ref: p_id = paper.id paper = paper.to_dict() paper['id'] = p_id get_venue(paper) get_authors(paper['authors']) papers.append(paper) return {'success': True, 'data': papers}
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 get(self, author_id): """ @@@ # 获取该作者的论文 # args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | start_after | true | string | 偏移游标 | # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | | @@@ """ parser = RequestParser() parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() start_after = req.get("start_after") ref = db.collection('paper').where(u'authors', u'array_contains', author_id) papers = [] start_after = db.collection('paper').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() for paper in ref: p_id = paper.id paper = paper.to_dict() paper['id'] = p_id get_venue(paper) get_authors(paper['authors']) papers.append(paper) return {'success': True, 'data': papers}
def post(self): """ @@@ ## 发送验证邮件 ### args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | username | false | string | 用户名 | | email | false | string | 发送邮箱 | | url | false | string | 邮件内包含的链接 | 示例: url为https://gugoo.fewings.xyz/#/auth 则邮件内链接为:https://gugoo.fewings.xyz/#/auth?authkey=xxxxxxxxxx ### return 无data @@@ """ parser = RequestParser() parser.add_argument('username', type=str, required=True) parser.add_argument("email", type=str, required=True) parser.add_argument("url", type=str, required=True) req = parser.parse_args() username = req.get('username') users = db.collection('user') user = users.document(username).get() if not user.exists: return{ 'success': False, 'message': '用户名不存在'}, 403 email = req.get('email') user_email = user.to_dict()['email'] if user_email != email: return{ 'success': False, 'message': '邮箱地址不正确'}, 403 url = req.get('url') msg = Message() msg.add_recipient(email) url += ('?authkey=' + create_authkey(email, username)) msg.subject = '咕鸽学术帐号电子邮件验证' msg.html = f'''<p>尊敬的{username}:您好!</p> <p>请点击以下链接验证您的电子邮件地址,以继续您的操作。</p> <p><a href='{url}'>{url}</a></p> <p>链接两小时内有效。</p> <p>如果未曾要求验证该地址,您可以忽略此电子邮件。</p> <p>此致</p> <p>咕鸽学术团队敬上</p>''' msg.send(mail) return{ 'success': True, 'message': '发送成功'}
def get(self): """ @@@ # 根据机构获取作者 # args | 参数名 | 是否可选 | type | remark | |--------|--------|--------|--------| | org | false | string | 机构名称 | | start_after | true | string | 偏移游标 | # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 属于该机构的作者 | @@@ """ parser = RequestParser() parser.add_argument("org", type=str, location="args", required=True) parser.add_argument("start_after", type=str, location="args", required=False) req = parser.parse_args() org = req.get("org") start_after = req.get("start_after") authors = [] ref = db.collection('author').where(u'orgs', u'==', org) start_after = db.collection('author').document(start_after).get() if start_after.exists: ref = ref.start_after(start_after).limit(20).get() else: ref = ref.limit(20).get() for author in ref: a_id = author.id author = author.to_dict() author['id'] = a_id authors.append(author) return {'success': True, 'data': authors}
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 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 订阅的作者列表 @@@ """ 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 get(self, field): """ @@@ # 根据领域获取作者(该接口最多返回前40个作者) # args 无(url传参) # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 属于该领域的作者 | @@@ """ paper_ids = querycl.query('paperK', 'keywords', terms=field, limit=40) authors_ref = [] papers_ref = [] for id in paper_ids: papers_ref.append(db.collection('paper').document(id)) papers_ref = db.get_all(papers_ref) for paper in papers_ref: paper = paper.to_dict() if 'authors' in paper and len(paper['authors']) > 0: authors_ref.append( db.collection('author').document(paper['authors'][0])) authors_ref = db.get_all(authors_ref) authors = [] for author in authors_ref: a_id = author.id author = author.to_dict() if author != None: author['id'] = a_id authors.append(author) return { 'success': True, 'data': authors, }
def get(self, fund_id): """ @@@ ## 根据ID获取论文 ### args 无 ### return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | id | false | string | id | | title | false | string | 标题 | | author | false | json | 项目作者 | | abstract | false | string | 摘要 | | desc | ture | string | 描述 | | start_year | ture | int | 开始年份 | | end_year | ture | int | 结束年份 | | end_date | ture | string | 结束日期 | | src | ture | string | 数据来源 | | type | ture | string | 项目类型 | @@@ """ 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') return {'success': True, 'data': fund} else: return {'success': False, 'message': '项目不存在'}, 404
def get_authors(authors: list): authors_ref = [] for i in range(min(10, len(authors))): authors_ref.append(db.collection('author').document(authors[i])) authors_ref = db.get_all(authors_ref) authors_temp = {} for author in authors_ref: a_id = author.id author = author.to_dict() if author != None: author['id'] = a_id else: author = {'name': a_id} authors_temp[a_id] = author for i in range(min(10, len(authors))): authors[i] = authors_temp[authors[i]]
def get_venue(paper: dict): if 'venue' in paper and isinstance(paper['venue'], str): if paper['venue'].isalnum(): venue = db.collection('venue').document(paper['venue']).get() if venue.exists: v_id = paper['venue'] paper['venue'] = venue.to_dict() paper['venue']['id'] = v_id else: name = paper['venue'] paper['venue'] = {'name': name} else: name = paper['venue'] paper['venue'] = {'name': name} elif 'venue' in paper: paper.pop('venue')
def get(self): """ @@@ # 搜索作者 # args | 参数名 | 是否可选 | 类型 | 备注 | |--------|--------|--------|--------| | words | false | string | 检索关键词 | | offset | true | int | 偏移量 | # return - #### data > | 字段 | 可能不存在 | 类型 | 备注 | |--------|--------|--------|--------| | \ | false | list | 检索得到作者 | @@@ """ parser = RequestParser() parser.add_argument("words", type=str, location="args", required=True) parser.add_argument("offset", type=int, location="args", required=False) req = parser.parse_args() words = req.get("words") if words == '' or words.isspace(): return {'success': True, 'data': []} offset = req.get("offset") author_ids = querycl.query("author", "name", terms=words, offset=offset, limit=20) authors_ref = [] for id in author_ids: author = db.collection('author').document(id) authors_ref.append(author) authors_ref = db.get_all(authors_ref) authors = [] for author in authors_ref: id = author.id author = author.to_dict() author['id'] = id authors.append(author) return {'success': True, 'data': authors}