from django.core.management.base import BaseCommand, CommandError
from django.db.models import F, Q
from django.utils import timezone
import json
import logging
import requests
from cityhallmonitor.models import Matter, MatterSponsor


logger = logging.getLogger(__name__)

_legistar_matter_sponsors_url = \
    'http://webapi.legistar.com/v1/chicago/Matters/%(matter_id)s/Sponsors'


class Command(BaseCommand):
    help = 'Get sponsors for updated matters from the Chicago Legistar API.'

    def add_arguments(self, parser):
        parser.add_argument('matter_id', nargs='?', help='Matter ID')

    def fetch(self, matter):
        """Fetch sponsors for matter"""
        url = _legistar_matter_sponsors_url % { 'matter_id': matter.id }
        logger.debug('Downloading %s' % url)
        
        headers = {'Accept': 'text/json'}
        r = requests.get(url, headers=headers)
        if not r.ok:
            raise Exception('Error downloading %s [%d]' \
                % (url, r.status_code))
        for i, item in enumerate(r.json()):
            try:
                if item['MatterSponsorNameId']:
                    r = MatterSponsor.from_json(item)
                    r.save()
            except Exception as e:
                logger.info(item)
                raise e
                
        matter.sponsors_obtained_at = timezone.now()
        matter.save()

    def handle(self, *args, **options):
        logger.info('matter_id=%(matter_id)s' % options)

        try:
            matter_id = options['matter_id']
            if matter_id:
                logger.info(
                    'Fetching sponsors for matter ID %s', matter_id)
                matter = Matter.objects.get(id=matter_id)
                self.fetch(matter)
            else:
                logger.info(
                    'Fetching all updated matter sponsors.')
            
                for matter in Matter.objects.filter(
                        Q(sponsors_obtained_at=None)
                        | Q(sponsors_obtained_at__lte=F('updated_at'))):
                    self.fetch(matter)
        except Exception as e:
            logger.exception(str(e))
        
        logger.info('Done\n')
         

@@ -517,24 +517,109 @@
 f, parser):%0A
+        parser.add_argument('--limit', type=int, help='Process up to LIMIT matters')%0A
         pars
@@ -1336,21 +1336,27 @@
 er.info(
+repr(
 item)
+)
 %0A       
@@ -1526,16 +1526,33 @@
 r.info('
+limit=%25(limit)s, 
 matter_i
@@ -1873,16 +1873,243 @@
   else:%0A
+                qs = Matter.objects.filter(%0A                        Q(sponsors_obtained_at=None)%0A                        %7C Q(sponsors_obtained_at__lte=F('updated_at')))%0A                %0A                if options%5B'limit'%5D:%0A    
         
@@ -2141,32 +2141,36 @@
                 
+    
 'Fetching all up
@@ -2163,19 +2163,25 @@
 etching 
-all
+%25(limit)s
  updated
@@ -2198,18 +2198,27 @@
 onsors.'
-)%0A
+,%0A         
         
@@ -2213,33 +2213,48 @@
                 
-%0A
+   options)%0A    
                 
@@ -2267,39 +2267,87 @@
 tter in 
-Matter.objects.filter(%0A
+qs%5B:options%5B'limit'%5D%5D:%0A                        self.fetch(matter)%0A     
         
@@ -2339,32 +2339,40 @@
 )%0A              
+  else:%0A
                Q
@@ -2374,110 +2374,133 @@
     
-Q(sponsors_obtained_at=None)%0A                        %7C Q(sponsors_obtained_at__lte=F('updated_at'))):%0A
+     logger.info(%0A                        'Fetching all updated matter sponsors.')%0A                    for matter in qs:%0A    
     
@@ -2526,32 +2526,57 @@
 f.fetch(matter)%0A
+                        %0A
         except E

Add --limit option to pull_sponsors