1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from django.conf import settings
from gm_rpcd.all import bind
from gm_types.doris import CARD_TYPE
from gm_types.mimas import GRABBING_PLATFORM
from qa.models import Answer, AnswerTag, AnswerTagV3
from talos.models.tractate import Tractate, TractateTag, TractateTagV3
from talos.services.tractate import TractateService
def get_author_id_by_business_id(content_id, content_type=CARD_TYPE.USERTOPIC):
"""
根据内容,获取作者
:param content_id:
:param content_type:
:return: user_id, related_tag_ids
"""
if not content_id:
return None, [], []
if content_type == CARD_TYPE.USERTOPIC:
try:
content = Tractate.objects.get(id=content_id)
except Tractate.DoesNotExist:
return None
elif content_type == CARD_TYPE.ANSWER:
try:
content = Answer.objects.get(id=content_id)
except Tractate.DoesNotExist:
return None
return content.user_id
def get_answers_by_author_id(author_id, start_num=0, count=10):
"""
获取作者的回答
:param author_id:
:param start_num:
:param count:
:return:
"""
kyc_answers = Answer.objects.using(settings.SLAVE_DB_NAME).filter(
user=author_id, is_online=True, platform=GRABBING_PLATFORM.KYC, question__is_online=True,
).order_by('-create_time')
if count:
kyc_answers = kyc_answers[start_num: start_num + count]
answer_ids = [answer.id for answer in kyc_answers]
return {
'ids': answer_ids,
}
@bind('mimas/get_content_ids')
def get_content_ids(business_id, content_type, start_num=0, count=10, answer_start_num=0, only_kyc_answers=False):
"""
获取内容id和类型
:param business_id:
:param content_type:
:param start_num:
:param answer_start_num:
:param count:
:param only_kyc_answers:
:return:
"""
result = {
"tractate_ids": [],
'answer_ids': [],
'author_id': None,
}
if not all([business_id, content_type]):
return result
business_id = int(business_id)
author_id = get_author_id_by_business_id(business_id, content_type)
if not author_id:
return result
result['author_id'] = author_id
if start_num == 0:
if content_type == CARD_TYPE.USERTOPIC:
result['tractate_ids'].insert(0, business_id)
elif content_type == CARD_TYPE.ANSWER:
result['answer_ids'].insert(0, business_id)
if only_kyc_answers:
kyc_answers = get_answers_by_author_id(
author_id, count=0,
)
result['answer_ids'].extend(kyc_answers.get('ids', []))
return result
tractates = TractateService.get_mark_tractate_ids(
author_id, business_id, start_num=start_num, count=count
)
tractate_ids = tractates.get('ids', [])
result['tractate_ids'].extend(tractate_ids)
if len(result['tractate_ids']) < count:
kyc_answers = get_answers_by_author_id(
author_id, count=count-len(result['tractate_ids']), start_num=answer_start_num
)
result['answer_ids'].extend(kyc_answers.get('ids', []))
return result