Fixes deletion of lists

This commit is contained in:
Mouse Reeve 2022-01-07 13:30:11 -08:00
parent 0580b66c3b
commit 8a8ce0c0d4
2 changed files with 17 additions and 9 deletions

View file

@ -13,6 +13,9 @@ class ListsStream(RedisStore):
def stream_id(self, user): # pylint: disable=no-self-use def stream_id(self, user): # pylint: disable=no-self-use
"""the redis key for this user's instance of this stream""" """the redis key for this user's instance of this stream"""
if isinstance(user, int):
# allows the function to take an int or an obj
return f"{user}-lists"
return f"{user.id}-lists" return f"{user.id}-lists"
def get_rank(self, obj): # pylint: disable=no-self-use def get_rank(self, obj): # pylint: disable=no-self-use
@ -119,7 +122,7 @@ def add_list_on_create(sender, instance, created, *args, **kwargs):
transaction.on_commit(lambda: add_list_on_create_command(instance.id)) transaction.on_commit(lambda: add_list_on_create_command(instance.id))
@receiver(signals.pre_delete, sender=models.List) @receiver(signals.post_delete, sender=models.List)
# pylint: disable=unused-argument # pylint: disable=unused-argument
def remove_list_on_delete(sender, instance, *args, **kwargs): def remove_list_on_delete(sender, instance, *args, **kwargs):
"""remove deleted lists to streams""" """remove deleted lists to streams"""
@ -214,15 +217,15 @@ def populate_lists_task(user_id):
@app.task(queue=MEDIUM) @app.task(queue=MEDIUM)
def remove_list_task(list_ids): def remove_list_task(list_id):
"""remove a list from any stream it might be in""" """remove a list from any stream it might be in"""
# this can take an id or a list of ids stores = models.User.objects.filter(local=True, is_active=True).values_list(
if not isinstance(list_ids, list): "id", flat=True
list_ids = [list_ids] )
lists = models.List.objects.filter(id__in=list_ids)
for book_list in lists: # delete for every store
ListsStream().remove_object_from_related_stores(book_list) stores = [ListsStream().stream_id(idx) for idx in stores]
ListsStream().remove_object_from_related_stores(list_id, stores=stores)
@app.task(queue=HIGH) @app.task(queue=HIGH)

View file

@ -39,10 +39,15 @@ class RedisStore(ABC):
def remove_object_from_related_stores(self, obj, stores=None): def remove_object_from_related_stores(self, obj, stores=None):
"""remove an object from all stores""" """remove an object from all stores"""
# if the stoers are provided, the object can just be an id
if stores and isinstance(obj, int):
obj_id = obj
else:
obj_id = obj.id
stores = self.get_stores_for_object(obj) if stores is None else stores stores = self.get_stores_for_object(obj) if stores is None else stores
pipeline = r.pipeline() pipeline = r.pipeline()
for store in stores: for store in stores:
pipeline.zrem(store, -1, obj.id) pipeline.zrem(store, -1, obj_id)
pipeline.execute() pipeline.execute()
def bulk_add_objects_to_store(self, objs, store): def bulk_add_objects_to_store(self, objs, store):