From 6acd3b0c7c58532d8bb7dc1fa37cc0e4b813b91d Mon Sep 17 00:00:00 2001 From: Forest Gregg Date: Wed, 20 Dec 2023 13:37:26 -0500 Subject: [PATCH] robust --- councilmatic_core/signals/handlers.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/councilmatic_core/signals/handlers.py b/councilmatic_core/signals/handlers.py index e12dcc1..45004ac 100644 --- a/councilmatic_core/signals/handlers.py +++ b/councilmatic_core/signals/handlers.py @@ -66,10 +66,26 @@ def create_councilmatic_event(sender, instance, created, **kwargs): @receiver(post_save, sender=OCDBill) def create_councilmatic_bill(sender, instance, created, **kwargs): + # the save_base also triggers a signal, so we check that + # we have richer kwargs indicative of a normal save + if "raw" not in kwargs: + return + if created: slug = slugify(instance.identifier) - cb = CouncilmaticBill(bill=instance, slug=slug) + try: + obj = CouncilmaticBill.objects.get(slug=slug) + except CouncilmaticBill.DoesNotExist: + cb = CouncilmaticBill(bill=instance, slug=slug) + else: + # we should onlly be in this path if + # there is a new bill that has the same slug + assert obj.bill != instance + + ocd_part = instance.id.rsplit("-", 1)[-1] + long_slug = slug + "-" + ocd_part + cb = CouncilmaticBill(bill=instance, slug=long_slug) else: cb = instance.councilmatic_bill