from django.core.management.base import BaseCommand
from django.conf import settings
from legistar.bills import LegistarAPIBillScraper
from lametro.models import SubjectGuid, Subject


class Command(BaseCommand):
    def handle(self, *args, **options):

        scraper = LegistarAPIBillScraper()
        scraper.BASE_URL = 'https://webapi.legistar.com/v1/metrotest'
        scraper.retry_attempts = 0
        scraper.requests_per_minute = 0

        all_topics = scraper.topics()

        # Delete topics not currently in use
        current_topics = Subject.objects.values_list('subject', flat=True)
        deleted, _ = SubjectGuid.objects.exclude(name__in=current_topics).delete()

        self.stdout.write('Removed {0} stale topics'.format(deleted))

        total_created = 0
        for topic in all_topics:
            subject, created = SubjectGuid.objects.get_or_create(
                name=topic['IndexName'],
                guid=topic['api_metadata']
            )
            if created:
                total_created += 1

        self.stdout.write('{0} topics created'.format(total_created))

@@ -78,16 +78,60 @@
 ettings%0A
+from django.db.utils import IntegrityError%0A%0A
 from leg
@@ -402,12 +402,8 @@
 etro
-test
 '%0A  
@@ -812,24 +812,74 @@
 created = 0%0A
+        total_updated = 0%0A        total_noop = 0%0A%0A
         for 
@@ -899,16 +899,37 @@
 topics:%0A
+            try:%0A    
         
@@ -986,16 +986,20 @@
 create(%0A
+    
         
@@ -1047,16 +1047,20 @@
         
+    
 guid=top
@@ -1090,16 +1090,20 @@
         
+  
+  
 )%0A      
@@ -1112,119 +1112,888 @@
     
-if created:%0A                total_created += 1%0A%0A        self.stdout.write('%7B0%7D topics created'.format(total_cre
+except IntegrityError as e:%0A                # This exception will be raised if get_or_create tries to create%0A                # a SubjectGuid with a name that already exists. The Legistar%0A                # API should not contain duplicates, i.e., the GUID has changed.%0A                # Update the GUID on the existing topic.%0A                subject = SubjectGuid.objects.get(name=topic%5B'IndexName'%5D)%0A                subject.guid = topic%5B'api_metadata'%5D%0A                subject.save()%0A                total_updated += 1%0A            else:%0A                if created:%0A                    total_created += 1%0A                else:%0A                    total_noop += 1%0A%0A        self.stdout.write('Created %7B0%7D new topics'.format(total_created))%0A        self.stdout.write('Updated %7B0%7D existing topics'.format(total_updated))%0A        self.stdout.write(%22No-op'ed %7B0%7D topics%22.format(total_upd
 ated

Update API path, handle updated SubjectGuid