mirror of
https://github.com/bugsink/bugsink.git
synced 2025-12-21 13:00:13 -06:00
when you count, it's usually because there are many, so this extra complication is probaly going to be required
27 lines
722 B
Python
27 lines
722 B
Python
from django.apps import apps
|
|
from django.utils import timezone
|
|
|
|
from snappea.decorators import shared_task
|
|
|
|
from bugsink.transaction import durable_atomic, immediate_atomic
|
|
from .models import CachedModelCount
|
|
|
|
|
|
@shared_task
|
|
def count_model(app_label, model_name):
|
|
ModelClass = apps.get_model(app_label, model_name)
|
|
|
|
# separate transaction for the expensive counting
|
|
with durable_atomic():
|
|
count = ModelClass.objects.count()
|
|
|
|
with immediate_atomic():
|
|
CachedModelCount.objects.update_or_create(
|
|
app_label=app_label,
|
|
model_name=model_name,
|
|
defaults={
|
|
'count': count,
|
|
'last_updated': timezone.now(),
|
|
},
|
|
)
|