From 8733bac40d0ab54133b424b4abf344bd1a1d8fca Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Tue, 23 Jul 2024 01:45:26 +0530 Subject: [PATCH] remove un-necessary @classmethod --- bots/models.py | 36 ++++++++++++++++-------------------- recipes/VideoBotsStats.py | 4 +--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/bots/models.py b/bots/models.py index c67adb5ff..9073a0f13 100644 --- a/bots/models.py +++ b/bots/models.py @@ -7,7 +7,7 @@ from django.contrib import admin from django.contrib.auth import get_user_model from django.db import models, transaction -from django.db.models import Q, IntegerChoices +from django.db.models import Q, IntegerChoices, QuerySet from django.utils import timezone from django.utils.text import Truncator from phonenumber_field.modelfields import PhoneNumberField @@ -877,7 +877,7 @@ class ConvoState(models.IntegerChoices): class ConversationQuerySet(models.QuerySet): def get_unique_users(self) -> models.QuerySet["Conversation"]: """Get unique conversations""" - return self.distinct(*Conversation.ID_COLUMNS()) + return self.distinct(*Conversation.ID_COLUMNS) def to_df(self, tz=pytz.timezone(settings.TIME_ZONE)) -> "pd.DataFrame": import pandas as pd @@ -1088,6 +1088,15 @@ class Conversation(models.Model): objects = ConversationQuerySet.as_manager() + ID_COLUMNS = [ + "fb_page_id", + "ig_account_id", + "slack_user_id", + "web_user_id", + "wa_phone_number", + "twilio_phone_number", + ] + class Meta: unique_together = [ ("slack_channel_id", "slack_user_id", "slack_team_id"), @@ -1110,20 +1119,9 @@ class Meta: def __str__(self): return f"{self.get_display_name()} <> {self.bot_integration}" - @classmethod - def ID_COLUMNS(cls): - return [ - "fb_page_id", - "ig_account_id", - "slack_user_id", - "web_user_id", - "wa_phone_number", - "twilio_phone_number", - ] - def get_id(self): self_id = None - for col in self.ID_COLUMNS(): + for col in self.ID_COLUMNS: if getattr(self, col): self_id = getattr(self, col) break @@ -1176,9 +1174,9 @@ def api_integration_id(self) -> str: class MessageQuerySet(models.QuerySet): - def get_unique_users(self) -> "MessageQuerySet": + def get_unique_users(self) -> QuerySet["Message"]: """Get unique users""" - return self.distinct(*Message.CONVO_ID_COLUMNS()) + return self.distinct(*Message.CONVO_ID_COLUMNS) def to_df(self, tz=pytz.timezone(settings.TIME_ZONE)) -> "pd.DataFrame": import pandas as pd @@ -1395,6 +1393,8 @@ class Message(models.Model): objects = MessageQuerySet.as_manager() + CONVO_ID_COLUMNS = [f"conversation__{col}" for col in Conversation.ID_COLUMNS] + class Meta: ordering = ("-created_at",) get_latest_by = "created_at" @@ -1412,10 +1412,6 @@ def __str__(self): def local_lang(self): return Truncator(self.display_content).words(30) - @classmethod - def CONVO_ID_COLUMNS(cls): - return [f"conversation__{col}" for col in Conversation.ID_COLUMNS()] - class MessageAttachment(models.Model): message = models.ForeignKey( diff --git a/recipes/VideoBotsStats.py b/recipes/VideoBotsStats.py index 74b33ff2b..7cf47659d 100644 --- a/recipes/VideoBotsStats.py +++ b/recipes/VideoBotsStats.py @@ -476,9 +476,7 @@ def calculate_stats_binned_by_time(*, bi, start_date, end_date, factor, trunc_fn .annotate(Convos=Count("conversation_id", distinct=True)) .annotate( Senders=Count( - Concat( - *Message.CONVO_ID_COLUMNS(), - ), + Concat(*Message.CONVO_ID_COLUMNS), distinct=True, ) )