Skip to content

Commit

Permalink
remove un-necessary @classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jul 22, 2024
1 parent 95d907e commit 8733bac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
36 changes: 16 additions & 20 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions recipes/VideoBotsStats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down

0 comments on commit 8733bac

Please sign in to comment.