topicreply.py 26.5 KB
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
# coding=utf8

from __future__ import unicode_literals, absolute_import, print_function

from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django.utils.html import escape
from gm_types.gaia import DOCTOR_TYPE
from gm_types.gaia import PROBLEM_FLAG_CHOICES
from gm_types.gaia import TOPIC_TYPE
from gm_types.gaia import USER_TYPE
from gm_types.mimas import TRACTATE_SORT_TYPE
from gm_upload import IMG_TYPE
from gm_upload import ImgUrlField

from talos.libs.datetime_utils import get_humanize_datetime
from talos.libs.datetime_utils import get_timestamp
from talos.libs.datetime_utils import get_timestamp_or_none
from talos.models.diary.diary import Diary
from talos.models.topic.topic import Problem
from talos.rpc import CODES
from talos.rpc import gen
from talos.rpc import logging_exception
from talos.services.doctor import DoctorService
from talos.services.hospital import HospitalService
from talos.services.user import UserService


class TopicReply(models.Model):
    """
        回复的内容
    """

    class Meta:
        verbose_name = u'011.话题回复'
        verbose_name_plural = u'011. 话题回复'
        db_table = 'api_topicreply'
        app_label = 'talos'

    diary = models.ForeignKey(Diary, verbose_name=u"关联的日记本", null=True, blank=True, default=None)
    problem = models.ForeignKey(Problem, verbose_name=u"话题", null=True, blank=True, default=None)
    user_id = models.IntegerField(verbose_name=u'用户外键id')
    doctor_id = models.CharField(max_length=100, null=True, blank=True, verbose_name=u'医生外键id')
    hospital_id = models.CharField(max_length=100, null=True, blank=True, verbose_name=u'关联的医院外键id')

    replied_topic = models.ForeignKey("self", default=None, null=True, blank=True, verbose_name=u"被回复的信息")
    commented_reply = models.ForeignKey('self', related_name=u'comments', verbose_name=u'被评论的回复', null=True,
                                        blank=True, default=None)
    content = models.TextField(verbose_name=u"回复内容")
    image = ImgUrlField(img_type=IMG_TYPE.TOPICREPLY, max_length=100, default="", verbose_name=u"图片", null=True,
                        blank=True)
    audio = models.TextField(default="", verbose_name=u"音频", null=True, blank=True)
    is_new = models.BooleanField(default=True, verbose_name=u"是否新的回复", db_index=True)
    is_spam = models.BooleanField(verbose_name=u'是否为疑似广告', default=False, db_index=True)

    like_num = models.IntegerField(default=0, verbose_name=u"赞的数目")
    dislike_num = models.IntegerField(default=0, verbose_name=u"踩的数目")

    # FIXME: reply_date重命名成reply_time,避免混淆含义
    reply_date = models.DateTimeField(verbose_name=u"回复时间", db_index=True, default=timezone.now)
    is_topic_append = models.BooleanField(verbose_name=u'是否是更新', default=False, db_index=True)
    is_online = models.BooleanField(default=True, help_text=u"是否可以上线", verbose_name=u"上线")
    is_fake = models.BooleanField(default=False, verbose_name=u"是否是灌水数据")

    def is_voted(self, user, reply):
        # TODO CR
        from .topicreplyvote import TopicReplyVote
        """
        NOTE:
            对一级评论进行点赞
        """
        is_voted = TopicReplyVote.objects.filter(user_id=user.id, topic_reply=reply).exists() if user else False
        return is_voted

    def get_reply_info(self):
        data = {
            'id': self.id,
            'uid': self.user_id,
            'nickname': self.user.nickname,
            'content': escape(self.content),
            'reply_time': get_timestamp_or_none(self.reply_date),
            'images': [],
            'is_online': self.is_online,
            'can_reply': not self.replied_topic,
        }

        return data

    def get_reply_info_for_doctor(self):
        data = {
            'topic_id': self.problem.id,
            'reply_date': get_timestamp_or_none(self.reply_date),
            'like_num': self.like_num,
            'my_content': escape(self.content),
            "old_content": self.replied_topic.content if self.replied_topic_id else (
                self.problem.ask or self.problem.answer)
        }

        return data

    def __unicode__(self):
        return self.content

    def reply_data_after_create(self):
        if self.user_id:
            portrait = self.user.portrait
        else:
            portrait = u''

        user_type = USER_TYPE.NORMAL
        user_id = self.user_id
        if self.doctor_id:
            d = DoctorService.get_doctor_from_doctor_id(self.doctor_id)

            if d.doctor_type == DOCTOR_TYPE.OFFICER:
                user_type = USER_TYPE.OFFICER
                user_id = d.hospital_id
            elif d.doctor_type == DOCTOR_TYPE.DOCTOR:
                user_type = USER_TYPE.EXPERT
                user_id = self.doctor_id

        reply_data = {
            'reply_id': self.id,
            'user_type': user_type,
            'user_id': user_id,
            'user_nickname': escape(self.user.nickname) or u'昵称未设置',
            'user_portrait': portrait,
            'content': escape(self.content),
            'reply_date': get_humanize_datetime(self.reply_date),
            'reply_timestamp': get_timestamp(self.reply_date),
            'comments': [],
            'reply_count': self.comments.count(),
            'vote_count': self.like_num,
        }
        return reply_data

    def comment_data_after_create(self):
        if self.replied_topic and self.replied_topic.user_id == self.user_id:
            at_nickname = u''
        elif self.replied_topic:
            at_nickname = self.replied_topic.user.nickname
        else:
            at_nickname = u''

        user_type = 0
        if self.doctor_id:
            d = DoctorService.get_doctor_from_doctor_id(self.doctor_id)

            if d.doctor_type == DOCTOR_TYPE.OFFICER:
                user_type = 2
            elif d.doctor_type == DOCTOR_TYPE.DOCTOR:
                user_type = 1

        data = {
            'comment_id': self.id,
            'nickname': escape(self.user.nickname) or u'昵称未设置',
            'reply_id': self.replied_topic and self.replied_topic.id or '',
            'at_nickname': escape(at_nickname),
            'at_user_id': self.replied_topic.user_id if self.replied_topic else 0,
            'at_user_type': user_type,
            'comment_user_id': self.user_id,
            'comment_user_type': '',
            'content': escape(self.content),
            'reply_count': self.comments.count(),
            'vote_count': self.like_num,
        }
        return data

    def is_elite(self):
        """
        判断是否是热门回复
        """
        return self.like_num >= settings.ELITE_LIKE_NUM and True or False

    def get_reply_detail(self, user_id=None):
        from .replyheadline import ReplyHeadline    # 避免循环引用

        if self.user_id:
            city_name = self.user.city_name
            portrait = self.user.portrait
        else:
            city_name = u''
            portrait = u''

        user_type = 0
        doctor_info = UserService.get_doctor_from_user_id(self.user_id)
        if doctor_info is not None and doctor_info.is_online:
            doctor = doctor_info
            if doctor.doctor_type == DOCTOR_TYPE.OFFICER:
                user_type = USER_TYPE.OFFICER
                hospital_id = doctor_info.hospital_id
            else:
                user_type = USER_TYPE.EXPERT
                hospital_id = None
        else:
            doctor = None
            hospital_id = None

        hospital = HospitalService.get_hospital_from_doctor_id(self.doctor_id)
        if hospital is not None:
            hospital_name_full = hospital.name or u''
        else:
            hospital_name_full = u''
        hospital_name = hospital_name_full[:14] if len(hospital_name_full) <= 14 else hospital_name_full[:14] + '...'

        reply_data = {
            'user': {
                'user_id': self.user_id,
                'user_name': self.user.nickname,
                'is_doctor': True if doctor else False,
                'is_hospital': True if hospital_id is not None else False,
                'doctor_id': doctor.id if doctor else '',
                'doctor_name': doctor and doctor.name,
                'hospital_id': hospital_id if hospital_id is not None else '',
                'portrait': portrait,
                'user_type': user_type,
                'membership_level': self.user.membership_level,
            },
            'is_private': bool(user_id) and user_id == self.user_id,
            'doctor_portrait': DoctorService.get_doctor_from_doctor_id(self.doctor_id).portrait if self.doctor_id else '',
            'hospital_name': hospital_name,

            'is_recommend_doctor': False,
            'recommend_doctor_title': '',

            'favored': False,
            'images': [],
            'is_star_user': False,
            'city': city_name,
            'is_topic_append': self.is_topic_append,
            'reply': {
                'reply_id': self.id,
                'content': escape(self.content),
                'favor_amount': self.like_num,
                'reply_time': get_timestamp_or_none(self.reply_date),
                'is_voted': bool(user_id) and self.topicreplyvote_set.filter(user_id=user_id, topic_reply=self).exists(),
                'image': self.image,
                'is_new': self.is_new,
                'replied_topic_id': self.replied_topic and self.replied_topic.id or '',
                'reply_date': get_timestamp_or_none(self.reply_date),
                'commented_reply_id': (
                    self.replied_topic and self.commented_reply and
                    self.commented_reply.id or ''
                ),
                'like_num': self.like_num,
                'is_best_reply': self.is_best_reply(),
                'is_recommand': ReplyHeadline.is_recommend(self.id),    # TODO CR 修改这个函数
                'is_elite': self.is_elite(),
                'comments_num': self.comments.count(),
            }
        }
        try:
            reply_data['diary'], reply_data['topic'], reply_data['answer'] = None, None, None
            if self.problem:
                topic_info = self.get_reply_topic_info()
                reply_data['topic'] = topic_info

            if self.diary:
                diary_info = self.get_reply_diary_info()
                reply_data['diary'] = diary_info

            author_id = self.problem and self.problem.user_id or self.diary.user_id
            if self.user_id == author_id:
                reply_data['user_titles'] = [{'title_type': 0, 'title_text': u'楼主'}, ]
            else:
                reply_data['user_titles'] = []

            if self.is_topic_append:
                # 增加术后第几天的展示
                if self.problem.operation_time:
                    interval = self.reply_date.date() - self.problem.operation_time
                    if interval.days >= 0:
                        reply_data['interval'] = u'{}'.format(interval.days + 1)
                    else:
                        reply_data['interval'] = u'以前'
                else:
                    reply_data['interval'] = u''
        except:
            logging_exception()

        return reply_data

    def get_reply_topic_info(self):
        """
        NOTE:
            获取回复相关的帖子topic信息
        :return:
        """
        topic_info = {
            'id': self.problem_id,
            'title': (
                self.replied_topic and self.replied_topic.id and
                escape(self.replied_topic.content) or escape(self.problem.answer)
            ),
            'problem_ask': escape(self.problem.ask),
            'problem_answer': escape(self.problem.answer),
            'is_deleted': (
                self.problem.flag == PROBLEM_FLAG_CHOICES.MARK_DELETED or
                not self.problem.is_online
            ),
            'user': {
                'id': self.problem.user_id,
                'nickname': self.replied_topic and self.replied_topic.user.nickname or self.user.nickname
            },
        }
        if self.problem.topic_type == TOPIC_TYPE.WEIXIN_NUM:
            topic_info['title'] = self.problem.get_title()
        return topic_info

    def get_reply_diary_info(self):
        """
        NOTE:
            获取回复相关的日记本信息
            其中 显示的title做特殊处理
            title = 'tag'+'日记'
        :return:
        """
        title = escape(self.diary.title)

        tags_new_era = self.diary.tags_new_era
        if tags_new_era:
            title = tags_new_era[0]['name'] + u'日记'

        diary_info = {
            'id': self.diary.id,
            'title': title,
            'is_deleted': not self.diary.is_online,
            'user': {
                'id': self.diary.user_id,
                'nickname': UserService.get_user_by_user_id(self.diary.user_id).nickname,
            },
        }

        return diary_info

    def comment_data(self):
        if self.replied_topic:
            if self.replied_topic.user_id == self.user_id:
                at_nickname = u''
                at_doctor = None
                at_hospital_id = None
            else:
                if self.replied_topic.user_id is not None:
                    replied_doctor_info = UserService.get_doctor_from_user_id(self.replied_topic.user_id)
                    at_doctor = replied_doctor_info
                    if at_doctor and at_doctor.is_online and at_doctor.doctor_type == DOCTOR_TYPE.OFFICER:
                        at_hospital_id = at_doctor.hospital_id
                    else:
                        at_hospital_id = None
                else:
                    at_doctor = None
                    at_hospital_id = None

                if self.replied_topic.doctor_id:
                    u = UserService.get_user_id_by_doctor_id(self.replied_topic.doctor_id)
                    at_nickname = u and UserService.get_user_by_user_id(u).nickname or u'昵称未设置'
                else:
                    at_nickname = self.replied_topic.user.nickname or u'昵称未设置'
        else:
            at_nickname = u'昵称未设置'
            at_doctor = None
            at_hospital_id = None

        if self.user_id:
            portrait = self.user.portrait
        else:
            portrait = u''

        doctor_info = UserService.get_doctor_from_user_id(self.user_id)
        if doctor_info is not None and doctor_info.is_online:
            doctor = doctor_info
            if doctor.doctor_type == DOCTOR_TYPE.OFFICER:
                hospital_id = doctor.hospital_id
            else:
                hospital_id = None
        else:
            doctor = None
            hospital_id = None

        data = {
            'type': 0,
            'content': escape(self.content),
            'at_nickname': at_nickname,
            'at_doctor_id': at_doctor.id if at_doctor else '',
            'at_hospital_id': at_hospital_id if at_hospital_id is not None else '',
            'user_portrait': portrait,
            'at_user_id': self.replied_topic.user_id if self.replied_topic else '',
            'nickname': '',
            'comment_id': self.id,
            'comment_time': self.reply_date.strftime('%m.%d'),
            'comment_expert_id': doctor.id if doctor else '',
            'at_expert_id': at_doctor.id if at_doctor else '',
            'user_id': self.user_id,
            'user_is_doctor': True if doctor else False,
            'user_is_hospital': True if hospital_id is not None else False,
            'doctor_id': doctor.id if doctor else '',
            'hospital_id': hospital_id if hospital_id is not None else '',
            'membership_level': self.user.membership_level,
            'is_best_reply': self.is_best_reply(),
        }

        if self.doctor_id is not None:
            d = DoctorService.get_doctor_from_doctor_id(self.doctor_id)
            data['nickname'] = d.name
            if d.user != settings.SUOZHANG_UID:
                data['nickname'] += u' 医生'
        else:
            data['nickname'] = self.user.nickname or u'昵称未设置'

        return data

    @classmethod
    def get_by_id(cls, id):
        try:
            return TopicReply.objects.get(pk=id)
        except:
            return None

    def is_best_reply(self):
        """
            是否是优质问答
        """
        return self.replyheadline_set.filter(is_online=True).exists()

    def update_reply_num(self, is_online=False):
        real_replynum = 1 + TopicReply.objects.filter(commented_reply=self).count()
        if self.problem:
            if is_online:
                self.problem.reply_num += real_replynum
                self.problem.update_reply_num(is_online=True, reply_num=real_replynum)
            else:
                self.problem.reply_num -= real_replynum
                self.problem.update_reply_num(is_online=False, reply_num=real_replynum)

            if self.problem.reply_num < 0:
                return False
            self.problem.save()

        elif self.diary:
            if is_online:
                self.diary.reply_num += real_replynum
            else:
                self.diary.reply_num -= real_replynum

            if self.diary.reply_num < 0:
                return False
            self.diary.save()

        return True

    @classmethod
    def get_replies_info(cls, user, replies, start_num=0, count=10, comment_id=None, new_sub_comment_count=False,
                         is_xcx=False):
        from talos.manager import comment_data_by_reply_ids, get_reply_details_by_reply_ids
        result = []
        need_add_reply_id = None
        if comment_id:
            top_comment = TopicReply.objects.filter(id=comment_id, commented_reply_id__isnull=True).first()
            replies = list(replies)
            if not top_comment:
                reply = TopicReply.objects.get(id=comment_id)
                replies = [item for item in replies if item.id != reply.commented_reply.id]
                replies = [reply.commented_reply] + replies
                if is_xcx and start_num == 0:
                    need_add_reply_id = reply.commented_reply_id
            else:
                replies = [item for item in replies if item.id != top_comment.id]
                replies = [top_comment] + replies
        _replies = replies[start_num: start_num + count]
        reply_ids = [r.id for r in _replies]
        comments_ids = list(TopicReply.objects.filter(Q(commented_reply__id__in=reply_ids) &
                                                      Q(is_online=True))
                            .values_list('id', flat=True))
        comments_data = comment_data_by_reply_ids(comments_ids)
        topicreplys_data = get_reply_details_by_reply_ids(reply_ids, user.id)
        for reply in _replies:
            replies_data = None
            for _reply in topicreplys_data:
                if _reply['reply']['reply_id'] == reply.id:
                    replies_data = _reply
                    break
            if replies_data is None:
                continue

            reply_info = replies_data['reply']
            if reply.replyheadline_set.count() == 0:
                replies_data['reply']['is_recommand'] = False
            else:
                replies_data['reply']['is_recommand'] = True

            replies_data['reply']['is_elite'] = (1 if reply_info['favor_amount'] >= settings.ELITE_LIKE_NUM else 0)

            replies_data['comments'] = []

            if new_sub_comment_count:
                _reply_comments_ids = list(reply.comments.filter(
                    is_online=True
                ).order_by('-id').values_list('id', flat=True))[:2]  # 取最新两条
            else:
                _reply_comments_ids = list(reply.comments.filter(
                    is_online=True
                ).values_list('id', flat=True))

            second_top_comment = None
            for c in comments_data:
                if c['comment_id'] in _reply_comments_ids:
                    replies_data['comments'].append(c)

                if comment_id and not second_top_comment and c['comment_id'] == int(comment_id):
                    second_top_comment = c

            # 将二级评论 置顶
            if need_add_reply_id and need_add_reply_id == reply.id and second_top_comment:
                replies_data['comments'] = [item for item in replies_data['comments']
                                            if item['comment_id'] != int(comment_id)]
                replies_data['comments'].insert(0, second_top_comment)

            result.append(replies_data)
        return result

    @classmethod
    def get_replies_list_info(
            cls, user, query, start_num=0, count=10, comment_id=None, new_sub_comment_count=False
    ):
        """
        NOTE:
            首先获取热门评论数目
            如果不够,则继续获取最新评论
            获取 日记本 帖子 评论列表信息
        :param user:
        :param start_num:
        :param count:
        :param new_sub_comment_count: 子评论展示个数控制, 7130 以及评论下展示两个子评论
        :return:
        """
        recommand_reply = Q(like_num__gte=settings.ELITE_LIKE_NUM)
        recommand_reply &= query
        replies = TopicReply.objects.filter(recommand_reply)
        hot_count = replies.count()
        if comment_id:
            myreplies = TopicReply.objects.filter(id=comment_id)
            replies = myreplies | replies

        result = []
        if hot_count > start_num:
            replies = replies.order_by('-like_num', '-reply_date')
            result = cls.get_replies_info(
                user, replies, start_num=start_num, count=count,
                comment_id=comment_id, new_sub_comment_count=new_sub_comment_count
            )

        if len(result) < count:
            if start_num < hot_count:
                start_num = 0
            else:
                start_num = start_num - len(result) - hot_count

            count -= len(result)
            last_query = Q(like_num__lt=settings.ELITE_LIKE_NUM)
            last_query &= query
            last_replies = cls.objects.filter(last_query).order_by('-id').exclude(recommand_reply)
            last_result = cls.get_replies_info(
                user, last_replies,
                start_num=start_num,
                count=count,
                comment_id=comment_id,
                new_sub_comment_count=new_sub_comment_count,
            )
            result.extend(last_result)

        return result

    @classmethod
    def reply_detail_diary(cls, user, reply_id, start_num=0, count=10):
        from talos.manager.topic_reply import comment_data_by_reply_ids

        try:
            reply = TopicReply.objects.get(pk=reply_id)
        except TopicReply.DoesNotExist:
            return gen(CODES.TOPIC_NOT_FOUND)
        topic = reply.problem

        result = {
            'comments': [],
            'topic_id': topic.id,
            'title': topic.diary.title,
            'comment_count': topic.topicreply_set.count(),
            'reply_id': reply.id,
            'is_private': False if not user else user.id == reply.user_id,
            'doctor_id': '',
            'doctor_portrait': '',
            'doctor_name': '',
            'hospital_name': '',
            'is_recommend_doctor': False,
            'recommend_doctor_title': '',
            'favored': False,
            'favor_amount': topic.vote_amount,
            'content': topic.answer,
            'images': [i.get_image_data() for i in topic.images.all()],
            'user_id': reply.user_id,
            'user_nickname': reply.user.nickname,
            'user_portrait': reply.user.portrait,
            'is_star_user': False,
            'reply_date': get_timestamp_or_none(topic.created_time),
            'city': reply.user.city_name,
            'is_topic_append': True,
        }

        doctor_reply_sql = """
            (case when api_topicreply.doctor_id !='' then 1 else 0 end)
        """
        comments = topic.topicreply_set.extra(
            select={'doctor_reply': doctor_reply_sql})
        comments = comments.extra(order_by=['-doctor_reply', 'id'])
        comments = comments[start_num: start_num + count]
        _comments_ids = [c.id for c in comments]
        result['comments'] = comment_data_by_reply_ids(_comments_ids)

        return result

    @property
    def user(self):
        if hasattr(self, '_user_obj'):
            return self._user_obj

        self._user_obj = UserService.get_user_by_user_id(self.user_id)
        return self._user_obj

    @property
    def doctor(self):
        return DoctorService.get_doctor_from_doctor_id(self.doctor_id)

    @property
    def hospital(self):
        return HospitalService.get_hospital_from_hospital_id(self.hospital_id)

    def get_reply_for_mine(self):
        return {
            'reply': {
                'replied_topic_id': self.replied_topic_id or '',
                'content': escape(self.content),
                'reply_id': self.id,
                'commented_reply_id': self.replied_topic_id and self.commented_reply_id or '',
                'reply_date': get_timestamp_or_none(self.reply_date),
                'image': self.image,
                'is_new': self.is_new,
                'commented_content': self.replied_topic.content if self.replied_topic else None,
                'commented_user_id': self.replied_topic.user_id if self.replied_topic else None,
            },
            'user': {
                'doctor_name': '',
                'user_name': '',
                'doctor_id': '',
                'user_id': self.user_id,
                'user_portrait': '',
                'doctor_portrait': '',
            },
            'topic': {
                'user': {
                    'nickname': '',
                    'id': self.problem.user_id,
                    'portrait': '',
                },
                'id': self.problem_id,
                'problem_ask': escape(self.problem.ask),
                'title': self.replied_topic_id and escape(self.replied_topic.content) or escape(self.problem.answer),
                'is_deleted': self.problem.flag == PROBLEM_FLAG_CHOICES.MARK_DELETED or not self.problem.is_online,
            } if self.problem_id else None,
            'diary': {
                'user': {
                    'nickname': '',
                    'id': self.diary.user_id,
                    'portrait': '',
                },
                'id': self.diary_id,
                'title': escape(self.diary.title),
                'is_deleted': not self.diary.is_online,
            } if self.diary_id else None,
            'answer': None,
        }