# Path: lametro/models.py
# class LAMetroSubject(models.Model):
# 
#     CLASSIFICATION_CHOICES = [
#         ('bill_type_exact', 'Board Report Type'),
#         ('lines_and_ways_exact', 'Lines / Ways'),
#         ('phase_exact', 'Phase'),
#         ('project_exact', 'Project'),
#         ('metro_location_exact', 'Metro Location'),
#         ('geo_admin_location_exact', 'Geographic / Administrative Location'),
#         ('significant_date_exact', 'Significant Date'),
#         ('motion_by_exact', 'Motion By'),
#         ('topics_exact', 'Subject'),
#         ('plan_program_policy_exact', 'Plan, Program, or Policy')
#     ]
# 
#     class Meta:
#         unique_together = ['guid', 'name']
# 
#     name = models.CharField(max_length=256, unique=True)
#     guid = models.CharField(max_length=256, blank=True, null=True)
#     classification = models.CharField(
#         max_length=256,
#         default='topics_exact',
#         choices=CLASSIFICATION_CHOICES
#     )
# 
#     def __str__(self):
#         if self.guid is not None:
#             return '{0} ({1})'.format(self.name, self.guid)
# 
#         else:
#             return self.name
# 
# Path: lametro/smartlogic.py
# class SmartLogic(object):
#     BASE_URL = 'https://cloud.smartlogic.com/'
#     SERVICE_URL = 'svc/0dcee7c7-1667-4164-81e5-c16e46f2f74c/ses/v1.2/CombinedModel/'
# 
#     def __init__(self, api_key):
#         self.api_key = api_key
#         self._token = self.token()['access_token']
# 
#     @property
#     def auth_headers(self):
#         return {
#             'Content-Type': 'application/x-www-form-urlencoded',
#             'Authorization': 'Bearer {}'.format(self._token)
#         }
# 
#     def endpoint(self, method, route, **kwargs):
#         url = self.BASE_URL + route
# 
#         try:
#             response = getattr(requests, method)(url, **kwargs)
#         except HTTPError as e:
#             raise
# 
#         try:
#             return response.json()
#         except json.JSONDecodeError:
#             return response
# 
#     def token(self):
#         data = {'grant_type': 'apikey', 'key': self.api_key}
#         return self.endpoint('post', 'token', data=data)
# 
#     def terms(self, params):
#         return self.endpoint(
#             'post',
#             self.SERVICE_URL + 'terms.json',
#             params=params,
#             headers=self.auth_headers
#         )


from collections import ChainMap
from itertools import chain
from django.core.management.base import BaseCommand
from django.conf import settings
from django.db.utils import IntegrityError
from legistar.bills import LegistarAPIBillScraper
from opencivicdata.legislative.models import Bill
from lametro.models import LAMetroSubject
from lametro.smartlogic import SmartLogic
        # uses the FILTER key (and dictionaries can't contain duplicate keys).
        params = [('FILTER', 'CL={}'.format(cls)) for cls in classes]
        params.append(('FILTER', 'AT=System: Legistar'))

        response = self.smartlogic.terms(params)

        yield from (t['term']['name'] for t in response['terms'])


class Command(BaseCommand, ClassificationMixin):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

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

    def handle(self, *args, **options):
        current_topics = set(chain(*Bill.objects.values_list('subject', flat=True)))

        # Delete topics no longer associated with any bills.
        deleted, _ = LAMetroSubject.objects.exclude(name__in=current_topics).delete()

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

        # Create LAMetroSubject instances for all existing topics. Subjects are
        # unique on name. Ignore conflicts so we can bulk create instances
        # without querying for or introducing duplicates.

# Path: lametro/models.py
# class LAMetroSubject(models.Model):
# 
#     CLASSIFICATION_CHOICES = [
#         ('bill_type_exact', 'Board Report Type'),
#         ('lines_and_ways_exact', 'Lines / Ways'),
#         ('phase_exact', 'Phase'),
#         ('project_exact', 'Project'),
#         ('metro_location_exact', 'Metro Location'),
#         ('geo_admin_location_exact', 'Geographic / Administrative Location'),
#         ('significant_date_exact', 'Significant Date'),
#         ('motion_by_exact', 'Motion By'),
#         ('topics_exact', 'Subject'),
#         ('plan_program_policy_exact', 'Plan, Program, or Policy')
#     ]
# 
#     class Meta:
#         unique_together = ['guid', 'name']
# 
#     name = models.CharField(max_length=256, unique=True)
#     guid = models.CharField(max_length=256, blank=True, null=True)
#     classification = models.CharField(
#         max_length=256,
#         default='topics_exact',
#         choices=CLASSIFICATION_CHOICES
#     )
# 
#     def __str__(self):
#         if self.guid is not None:
#             return '{0} ({1})'.format(self.name, self.guid)
# 
#         else:
#             return self.name
# 
# Path: lametro/smartlogic.py
# class SmartLogic(object):
#     BASE_URL = 'https://cloud.smartlogic.com/'
#     SERVICE_URL = 'svc/0dcee7c7-1667-4164-81e5-c16e46f2f74c/ses/v1.2/CombinedModel/'
# 
#     def __init__(self, api_key):
#         self.api_key = api_key
#         self._token = self.token()['access_token']
# 
#     @property
#     def auth_headers(self):
#         return {
#             'Content-Type': 'application/x-www-form-urlencoded',
#             'Authorization': 'Bearer {}'.format(self._token)
#         }
# 
#     def endpoint(self, method, route, **kwargs):
#         url = self.BASE_URL + route
# 
#         try:
#             response = getattr(requests, method)(url, **kwargs)
#         except HTTPError as e:
#             raise
# 
#         try:
#             return response.json()
#         except json.JSONDecodeError:
#             return response
# 
#     def token(self):
#         data = {'grant_type': 'apikey', 'key': self.api_key}
#         return self.endpoint('post', 'token', data=data)
# 
#     def terms(self, params):
#         return self.endpoint(
#             'post',
#             self.SERVICE_URL + 'terms.json',
#             params=params,
#             headers=self.auth_headers
#         )


# Path: lametro/management/commands/refresh_guid.py
from collections import ChainMap
from itertools import chain
from django.core.management.base import BaseCommand
from django.conf import settings
from django.db.utils import IntegrityError
from legistar.bills import LegistarAPIBillScraper
from opencivicdata.legislative.models import Bill
from lametro.models import LAMetroSubject
from lametro.smartlogic import SmartLogic

        # uses the FILTER key (and dictionaries can't contain duplicate keys).
        params = [('FILTER', 'CL={}'.format(cls)) for cls in classes]
        params.append(('FILTER', 'AT=System: Legistar'))

        response = self.smartlogic.terms(params)

        yield from (t['term']['name'] for t in response['terms'])


class Command(BaseCommand, ClassificationMixin):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

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

    def handle(self, *args, **options):
        current_topics = set(chain(*Bill.objects.values_list('subject', flat=True)))

        # Delete topics no longer associated with any bills.
        deleted, _ = LAMetroSubject.objects.exclude(name__in=current_topics).delete()

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

        # Create LAMetroSubject instances for all existing topics. Subjects are
        # unique on name. Ignore conflicts so we can bulk create instances
        # without querying for or introducing duplicates.

        LAMetroSubject.objects.bulk_create([