Clean old baskets

29 views
Skip to first unread message

Gosti

unread,
Mar 31, 2021, 12:05:49 PMMar 31
to django...@googlegroups.com
Hi all,

We have too many basket records which causes long database queries.
I need to clean those and I wonder if there is any Oscar internal
mechanisms that would make "submitted" baskets deletion not recommended.

Those baskets are linked to order records via a PK on Order model that
is allowed to be null.
Additionally, Order model seems to have whole basket data copied during
OrderCreator.place_orded method: mainly basket lines => order lines.
So I guess it's not a problem but I need to be sure and I didn't find
anything about that in documentation.

Does anyone can confirm that I can delete submitted baskets without any
damaged?
Or if it's not possible, can you point out the code line(s) in Oscar
sources that could need it?

Below is my Django manager command sources.
Note that we implemented a field "date_updated" in Basket model for
other reasons (internal basket validity feature).

Best
Yannick


```
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.conf import settings
from django.utils.timezone import now
from oscar.core.loading import get_model

Basket = get_model("basket", "Basket")


class Command(BaseCommand):

    VALIDITY_DURATION = settings.BASKET_EXPIRATION_TIME_LAPSE

    @property
    def validity_deadline(self):
        return now() - timedelta(seconds=self.VALIDITY_DURATION)

    @property
    def expired_baskets(self):
        baskets =
Basket.objects.filter(date_updated__lt=self.validity_deadline)
        # Remove next line if submitted baskets can be deleted
        baskets = baskets.exclude(status=Basket.SUBMITTED)
        return baskets

    def handle(self, *args, **options):
        returned = self.expired_baskets.delete()
        nb = returned[1]["basket.Basket"] if returned[0] else 0
        print(f"{nb} baskets deleted")

```

Reply all
Reply to author
Forward
0 new messages