from django import db from django.db import models from django.conf import settings from django.db.models.aggregates import Count from django.db.models.base import ModelBase from django.db.utils import DEFAULT_DB_ALIAS from django.utils.translation import gettext_lazy as _ from pingcap_sso.user_behavior_track.models import TrackEvent class PortraitKeyword(models.Model): ORIGIN_WEBSITE_META = 0 ORIGIN_ASKTUG = 1 ORIGIN_STAFF_CUSTOM = 2 ORIGIN_CHOICES = ( (ORIGIN_WEBSITE_META, _('website meta')), (ORIGIN_ASKTUG, _('AskTUG')), (ORIGIN_STAFF_CUSTOM, _('staff custom')), ) name = models.CharField(max_length=128, unique=True) preffered_keyword = models.ForeignKey( 'self', on_delete=models.DO_NOTHING, blank=True, null=True ) origin = models.SmallIntegerField(default=ORIGIN_WEBSITE_META, choices=ORIGIN_CHOICES) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) creator = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, blank=True, null=True ) class Meta: db_table = 'pingcap_portrait_keywords' def __str__(self) -> str: return f'{self.id} - {self.name}' class PortraitKeywordUser(models.Model): keyword = models.ForeignKey( 'pingcap_sso.PortraitKeyword', on_delete=models.DO_NOTHING ) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING ) count = models.IntegerField(default=0) read_count = models.IntegerField(default=0) ask_count = models.IntegerField(default=0) reply_count = models.IntegerField(default=0) score = models.IntegerField(default=0) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) creator = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name='created_portrait_keyword_user_relation', blank=True, null=True ) description = models.CharField(max_length=255) class Meta: db_table = 'pingcap_portrait_keyword_users' unique_together = ('keyword', 'user') class PortraitKeywordUserHistory(models.Model): ACTION_READ = 0 ACTION_ASK = 1 ACTION_REPLY = 2 ACTION_CHOICES = ( (ACTION_READ, _('read')), (ACTION_ASK, _('ask')), (ACTION_REPLY, _('reply')) ) keyword = models.ForeignKey( 'pingcap_sso.PortraitKeyword', on_delete=models.DO_NOTHING, related_name='user_histories' ) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name='keyword_histories' ) action = models.SmallIntegerField(default=ACTION_READ, choices=ACTION_CHOICES) track_event = models.ForeignKey( 'user_behavior_track.TrackEvent', on_delete=models.DO_NOTHING, blank=True, null=True ) asktug_topic_id = models.IntegerField(blank=True, null=True) asktug_post_id = models.IntegerField(blank=True, null=True) event_time = models.DateTimeField(blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: db_table = 'pingcap_portrait_keyword_users_histories' unique_together = ('keyword', 'user', 'action', 'track_event', 'asktug_topic_id', 'asktug_post_id') class PortraitTrackURL(models.Model): url = models.URLField(unique=True) keywords = models.ManyToManyField('pingcap_sso.PortraitKeyword') extract_meta_keywords = models.CharField(max_length=255, blank=True, null=True) updated_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: db_table = 'pingcap_portrait_track_urls'