-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve favicon loading request now fetching first svgs
- Loading branch information
1 parent
296631e
commit 3122163
Showing
2 changed files
with
48 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
from django.utils import timezone | ||
from django.db.models.signals import pre_save | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from .tasks.fetch_favicon import fetch_favicon | ||
from .models import Project | ||
|
||
DELAY_REFRESH_FAVICON_SECONDS = 10 # Every 10 seconds | ||
DELAY_REFRESH_FAVICON_SECONDS = 30 # Every 30 seconds | ||
|
||
@receiver(pre_save, sender=Project) | ||
def post_save_fetch_favicon(sender, instance=None, **kwargs): | ||
@receiver(post_save, sender=Project) | ||
def post_save_fetch_favicon(sender, instance, created, **kwargs): | ||
favicon_need_refresh = False | ||
|
||
# Fetch the existing instance from the database if it exists | ||
if instance.pk: | ||
try: | ||
old_instance = Project.objects.get(pk=instance.pk) | ||
# Check if the favicon value has changed | ||
if old_instance.favicon != instance.favicon: | ||
instance.favicon_task_status = 'SUCCESS' | ||
instance.favicon_last_edited = timezone.now() | ||
except Project.DoesNotExist: | ||
# If the object does not exist in the database, it's a new object | ||
favicon_need_refresh = True | ||
else: | ||
try: | ||
old_instance = Project.objects.get(pk=instance.pk) | ||
# Check if the favicon value has changed | ||
if old_instance.favicon != instance.favicon: | ||
instance.favicon_task_status = 'SUCCESS' | ||
instance.favicon_last_edited = timezone.now() | ||
except Project.DoesNotExist: | ||
# If the object does not exist in the database, it's a new object | ||
favicon_need_refresh = True | ||
|
||
if created: | ||
# If instance.pk is None, it's a new object | ||
favicon_need_refresh = True | ||
|
||
if instance.favicon_task_status == 'UNKNOWN': | ||
favicon_need_refresh = True | ||
if (timezone.now() - instance.favicon_last_edited).seconds > DELAY_REFRESH_FAVICON_SECONDS and instance.favicon_task_status != 'PENDING': | ||
|
||
if (timezone.now() - instance.favicon_last_edited).seconds > DELAY_REFRESH_FAVICON_SECONDS: | ||
favicon_need_refresh = True | ||
|
||
if favicon_need_refresh: | ||
instance.favicon_task_status = 'PENDING' | ||
instance.favicon_last_edited = timezone.now() | ||
fetch_favicon.delay(instance.pk, instance.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters