# Path: capitolweb/legislators/models.py
# class CongressPerson(models.Model):
#     def __str__(self):
#         return self.official_full
#     bioguide_id = models.CharField(max_length=7, primary_key=True)
#     first = models.CharField(max_length=25)
#     middle = models.CharField(max_length=25)
#     last = models.CharField(max_length=25)
#     suffix = models.CharField(max_length=5)
#     nickname = models.CharField(max_length=25)
#     official_full = models.CharField(max_length=50)
#     birthday = models.DateField(default=date(1776, 7, 4))
#     gender = models.CharField(max_length=1, choices=(('M', "Male"), ('F', 'Female')))
#     religion = models.CharField(max_length=30)
# 
#     @property
#     def image_lg(self):
#         return "https://theunitedstates.io/images/congress/450x550/{}.jpg".format(self.bioguide_id)
# 
#     @property
#     def image_sm(self):
#         return "https://theunitedstates.io/images/congress/225x275/{}.jpg".format(self.bioguide_id)
# 
#     class Meta:
#         ordering = ('official_full',)
# 
# class ExternalId(models.Model):
#     person = models.ForeignKey(CongressPerson, on_delete=models.CASCADE, related_name='external_ids')
#     type = models.CharField(max_length=50)
#     value = models.CharField(max_length=50)
# 
# def get_current_legislators():
#     """
#     Filter legislators based on terms
#     :param state: 2 letter code for state
#     :param party: full name of a party
#     :param term_type: 'sen' or 'rep' for senate or house
#     :return:
#     """
#     results = CongressPerson.objects.filter(terms__start_date__lte=date.today(), terms__end_date__gte=date.today())
#     return results


from .models import CongressPerson, ExternalId, get_current_legislators
from rest_framework.decorators import api_view
from django.http import JsonResponse
from legislators.serializers import CongressPersonSerializer, CongressPersonShortSerializer
import logging
        if params.get('term_type') in ['sen', 'rep']:
            people = people.filter(terms__type=params.get('term_type'))
    if 'name' in params:
        logger.info("search by name: " + params.get('name'))
        people = people.filter(official_full=params.get('name'))

    if 'last_name' in params:
        logger.info("search by last_name")
        people = people.filter(last=params.get('last_name'))

    if 'gender' in params:
        logger.info("search by gender")
        people = people.filter(gender=params.get('gender'))

    if 'religion' in params:
        logger.info("search by religion")
        people = people.filter(religion=params.get('religion'))

    serializer = CongressPersonSerializer(people, many=True)
    return JsonResponse(serializer.data, safe=False)


def find_by_id(request, person_id):
    """
    Find by the database id
    :param request: 
    :param person_id: the db id
    :return:  a single congress person
    """
    logger.info("Request: {}".format(person_id))

# Path: capitolweb/legislators/models.py
# class CongressPerson(models.Model):
#     def __str__(self):
#         return self.official_full
#     bioguide_id = models.CharField(max_length=7, primary_key=True)
#     first = models.CharField(max_length=25)
#     middle = models.CharField(max_length=25)
#     last = models.CharField(max_length=25)
#     suffix = models.CharField(max_length=5)
#     nickname = models.CharField(max_length=25)
#     official_full = models.CharField(max_length=50)
#     birthday = models.DateField(default=date(1776, 7, 4))
#     gender = models.CharField(max_length=1, choices=(('M', "Male"), ('F', 'Female')))
#     religion = models.CharField(max_length=30)
# 
#     @property
#     def image_lg(self):
#         return "https://theunitedstates.io/images/congress/450x550/{}.jpg".format(self.bioguide_id)
# 
#     @property
#     def image_sm(self):
#         return "https://theunitedstates.io/images/congress/225x275/{}.jpg".format(self.bioguide_id)
# 
#     class Meta:
#         ordering = ('official_full',)
# 
# class ExternalId(models.Model):
#     person = models.ForeignKey(CongressPerson, on_delete=models.CASCADE, related_name='external_ids')
#     type = models.CharField(max_length=50)
#     value = models.CharField(max_length=50)
# 
# def get_current_legislators():
#     """
#     Filter legislators based on terms
#     :param state: 2 letter code for state
#     :param party: full name of a party
#     :param term_type: 'sen' or 'rep' for senate or house
#     :return:
#     """
#     results = CongressPerson.objects.filter(terms__start_date__lte=date.today(), terms__end_date__gte=date.today())
#     return results


# Path: capitolweb/legislators/views.py
from .models import CongressPerson, ExternalId, get_current_legislators
from rest_framework.decorators import api_view
from django.http import JsonResponse
from legislators.serializers import CongressPersonSerializer, CongressPersonShortSerializer
import logging

        if params.get('term_type') in ['sen', 'rep']:
            people = people.filter(terms__type=params.get('term_type'))
    if 'name' in params:
        logger.info("search by name: " + params.get('name'))
        people = people.filter(official_full=params.get('name'))

    if 'last_name' in params:
        logger.info("search by last_name")
        people = people.filter(last=params.get('last_name'))

    if 'gender' in params:
        logger.info("search by gender")
        people = people.filter(gender=params.get('gender'))

    if 'religion' in params:
        logger.info("search by religion")
        people = people.filter(religion=params.get('religion'))

    serializer = CongressPersonSerializer(people, many=True)
    return JsonResponse(serializer.data, safe=False)


def find_by_id(request, person_id):
    """
    Find by the database id
    :param request: 
    :param person_id: the db id
    :return:  a single congress person
    """
    logger.info("Request: {}".format(person_id))

    person = CongressPerson.objects.prefetch_related('terms').get(pk=person_id)