Skip to content

Commit

Permalink
feat(mangas, playlists): add signals and methods to update stats when…
Browse files Browse the repository at this point in the history
… user changes their mangalist
  • Loading branch information
tyronejosee committed Jun 25, 2024
1 parent f86a216 commit e151d06
Show file tree
Hide file tree
Showing 16 changed files with 1,058 additions and 30 deletions.
6 changes: 3 additions & 3 deletions apps/animes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,19 @@ def __str__(self):
return str(self.name)

def calculate_score(self, user_score):
# Calculate the new score based on the existing score and the user's score
# Calculate new score based on the existing score and the user's score
if self.members >= 2:
self.score = (self.score + user_score) / self.members
else:
self.score = user_score

def calculate_ranked(self):
# Calculate the ranking of the anime based on its score compared to all other animes
# Calculate ranking of anime based on its score compared to all other animes
all_animes = Anime.objects.all().order_by("-score")
self.ranked = list(all_animes).index(self) + 1

def calculate_popularity(self):
# Calculate the popularity of the anime based on the number of members who have it in their list
# Calculate popularity of anime based on number of members who have it in list
all_animes = Anime.objects.all().order_by("-members")
self.popularity = list(all_animes).index(self) + 1

Expand Down
5 changes: 0 additions & 5 deletions apps/mangas/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ class MangaAdmin(ImportExportModelAdmin, BaseAdmin):
readonly_fields = [
"pk",
"slug",
"score",
"ranked",
"popularity",
"members",
"favorites",
"created_at",
"updated_at",
]
Expand Down
17 changes: 17 additions & 0 deletions apps/mangas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ def save(self, *args, **kwargs):
def __str__(self):
return str(self.name)

def calculate_score(self, user_score):
# Calculate score based on the existing score and the user's score
if self.members >= 2:
self.score = (self.score + user_score) / self.members
else:
self.score = user_score

def calculate_ranked(self):
# Calculate ranking of manga based on its score compared to all other mangas
all_mangas = Manga.objects.all().order_by("-score")
self.ranked = list(all_mangas).index(self) + 1

def calculate_popularity(self):
# Calculate popularity of manga based on number of members who have it in list
all_mangas = Manga.objects.all().order_by("-members")
self.popularity = list(all_mangas).index(self) + 1


class MangaStats(BaseModel):
"""Model definition for MangaStats."""
Expand Down
79 changes: 57 additions & 22 deletions apps/playlists/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

@receiver(post_save, sender=AnimeListItem)
def update_anime_on_save(sender, instance, created, **kwargs):
"""Signal update statistics when a user updates their animelist."""

def update_statistics():
try:
"""Signal update stats when user changes their animelist."""
try:
with transaction.atomic():
anime = instance.anime_id
user_score = instance.score

Expand All @@ -32,31 +31,67 @@ def update_statistics():
anime.favorites = 0

# TODO: Fix so that when the user updates, calculate_score is not executed again
# user_update = instance.updated_at
# old_instance = AnimeListItem.objects.get(pk=instance.pk)
# if user_update != old_instance.updated_at:
# anime.calculate_score(user_score)

anime.calculate_score(user_score)
anime.calculate_ranked()
anime.calculate_popularity()

# Update all fields
anime.save(
update_fields=["members", "favorites", "score", "ranked", "popularity"]
update_fields=[
"members",
"favorites",
"score",
"ranked",
"popularity",
]
)
except Exception as e:
logger.error(f"ANIMELIST ERROR: {e}")

transaction.on_commit(update_statistics)
except Exception as e:
logger.error(f"ANIMELIST ERROR: {e}")


@receiver(post_save, sender=MangaListItem)
def update_manga_on_save(sender, instance, **kwargs):
"""Signal update statistics when a user updates their mangalist."""
manga = instance.manga_id

if instance.is_favorite:
manga.members += 1
manga.favorites += 1
manga.save()
else:
manga.members -= 1
manga.favorites -= 1

manga.save()
def update_manga_on_save(sender, instance, created, **kwargs):
"""Signal update stats when user changes their mangalist."""
try:
with transaction.atomic():
manga = instance.manga_id
user_score = instance.score

if created:
manga.members += 1

if instance.is_favorite:
manga.favorites += 1
else:
manga.favorites -= 1

if manga.favorites < 0:
manga.favorites = 0

# TODO: Fix so that when the user updates, calculate_score is not executed again
# user_update = instance.updated_at
# old_instance = mangaListItem.objects.get(pk=instance.pk)
# if user_update != old_instance.updated_at:
# manga.calculate_score(user_score)

manga.calculate_score(user_score)
manga.calculate_ranked()
manga.calculate_popularity()

# Update all fields
manga.save(
update_fields=[
"members",
"favorites",
"score",
"ranked",
"popularity",
]
)
except Exception as e:
logger.error(f"MANGALIST ERROR: {e}")
Loading

0 comments on commit e151d06

Please sign in to comment.