MCPcopy Index your code
hub / github.com/1Panel-dev/MaxKB / vote

Method vote

apps/chat/serializers/chat_record.py:51–98  ·  view source on GitHub ↗
(self, instance: Dict, with_valid=True)

Source from the content-addressed store, hash-verified

49
50 @transaction.atomic
51 def vote(self, instance: Dict, with_valid=True):
52 if with_valid:
53 self.is_valid(raise_exception=True)
54 VoteRequest(data=instance).is_valid(raise_exception=True)
55 rlock = RedisLock()
56 if not rlock.try_lock(self.data.get('chat_record_id')):
57 raise AppApiException(500,
58 gettext(
59 "Voting on the current session minutes, please do not send repeated requests"))
60 try:
61 chat_record_details_model = QuerySet(ChatRecord).get(id=self.data.get('chat_record_id'),
62 chat_id=self.data.get('chat_id'))
63 if chat_record_details_model is None:
64 raise AppApiException(500, gettext("Non-existent conversation chat_record_id"))
65 vote_status = instance.get("vote_status")
66
67 # 未投票状态,可以进行投票
68 if chat_record_details_model.vote_status == VoteChoices.UN_VOTE:
69 # 投票时获取字段
70 vote_reason = instance.get("vote_reason")
71 vote_other_content = instance.get("vote_other_content") or ''
72
73 if vote_status == VoteChoices.STAR:
74 # 点赞
75 chat_record_details_model.vote_status = VoteChoices.STAR
76 chat_record_details_model.vote_reason = vote_reason
77 chat_record_details_model.vote_other_content = vote_other_content
78 elif vote_status == VoteChoices.TRAMPLE:
79 # 点踩
80 chat_record_details_model.vote_status = VoteChoices.TRAMPLE
81 chat_record_details_model.vote_reason = vote_reason
82 chat_record_details_model.vote_other_content = vote_other_content
83
84 chat_record_details_model.save()
85 # 已投票状态
86 else:
87 if vote_status == VoteChoices.UN_VOTE:
88 # 取消点赞
89 chat_record_details_model.vote_status = VoteChoices.UN_VOTE
90 chat_record_details_model.vote_reason = None
91 chat_record_details_model.vote_other_content = ''
92 chat_record_details_model.save()
93 else:
94 raise AppApiException(500, gettext("Already voted, please cancel first and then vote again"))
95 finally:
96 rlock.un_lock(self.data.get('chat_record_id'))
97 ChatCountSerializer(data={'chat_id': self.data.get('chat_id')}).update_chat()
98 return True
99
100
101class HistoricalConversationSerializer(serializers.Serializer):

Callers 1

putMethod · 0.80

Calls 10

try_lockMethod · 0.95
un_lockMethod · 0.95
RedisLockClass · 0.90
AppApiExceptionClass · 0.90
ChatCountSerializerClass · 0.90
VoteRequestClass · 0.85
update_chatMethod · 0.80
is_validMethod · 0.45
getMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected