(self, instance: Dict, with_valid=True)
| 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 | |
| 101 | class HistoricalConversationSerializer(serializers.Serializer): |
no test coverage detected