#wzc: 评论数据模型定义
from django.conf import settings
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from blog.models import Article


class Comment(models.Model):
    """wzc: 评论模型，存储用户对文章的评论信息"""
    body = models.TextField('正文', max_length=300)
    creation_time = models.DateTimeField(_('creation time'), default=now)
    last_modify_time = models.DateTimeField(_('last modify time'), default=now)
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name=_('author'),
        on_delete=models.CASCADE)
    article = models.ForeignKey(
        Article,
        verbose_name=_('article'),
        on_delete=models.CASCADE)
    parent_comment = models.ForeignKey(
        'self',
        verbose_name=_('parent comment'),
        blank=True,
        null=True,
        on_delete=models.CASCADE,
        help_text="wzc: 父评论，用于实现评论回复功能")
    is_enable = models.BooleanField(
        _('enable'),
        default=False,
        blank=False,
        null=False,
        help_text="wzc: 评论是否启用（审核通过）")

    class Meta:
        ordering = ['-id']
        verbose_name = _('comment')
        verbose_name_plural = verbose_name
        get_latest_by = 'id'

    def __str__(self):
        """wzc: 返回评论正文作为字符串表示"""
        return self.body[:50]  #wzc: 只返回前50个字符，避免过长