from pupa.core import db


def people_by_jurisdiction(jurisdiction_id):
    """ Find all people by a jurisdiction """
    people_ids = db.memberships.find({
        "jurisdiction_id": jurisdiction_id,
    }).distinct('person_id')

    if None in people_ids:
        people_ids.remove(None)
    return people_ids


def orgs_by_jurisdiction(jurisdiction_id):
    """ Find all orgs by a jurisdiction """
    org_ids = db.organizations.find({
        "jurisdiction_id": jurisdiction_id,
    }).distinct('_id')

    if None in org_ids:
        org_ids.remove(None)
    return org_ids


def people_by_name(name, people_ids=None):
    """ Find all people by their name. Optional people_ids _id constraint """
    spec = {"$or": [
        { "name": name },
        { "other_names.name": name }
    ]}
    if people_ids is not None:

        # This isn't a raw if conditional, since you could pass
        # an empty list.
        spec["_id"] = {"$in": people_ids}
    return db.people.find(spec)


def orgs_by_name(name, org_ids=None):
    """ Find all orgs their name. Optional org_ids _id constraint """

    spec = {"$or": [
        { "name": name },
        { "other_names.name": name },
        { "identifiers.identifier": name },
    ]}

    if org_ids is not None:
        # This isn't a raw if conditional, since you could pass
        # an empty list.
        spec["_id"] = {"$in": org_ids}
    return db.organizations.find(spec)


def people_by_jurisdiction_and_name(jurisdiction_id, name):
    people_ids = people_by_jurisdiction(jurisdiction_id)
    people = people_by_name(name, people_ids=people_ids)
    return people


def orgs_by_jurisdiction_and_name(jurisdiction_id, name):
    org_ids = orgs_by_jurisdiction(jurisdiction_id)
    orgs = orgs_by_name(name, org_ids=org_ids)
    return people

@@ -606,32 +606,42 @@
  people_ids=None
+, **kwargs
 ):%0A    %22%22%22 Find 
@@ -708,32 +708,62 @@
  %22%22%22%0A    spec = 
+kwargs.copy()%0A    spec.update(
 %7B%22$or%22: %5B%0A      
@@ -825,16 +825,17 @@
 %7D%0A    %5D%7D
+)
 %0A    if 
@@ -1059,16 +1059,26 @@
 ids=None
+, **kwargs
 ):%0A    %22
@@ -1142,17 +1142,16 @@
 int %22%22%22%0A
-%0A
     spec
@@ -1153,16 +1153,47 @@
  spec = 
+kwargs.copy()%0A%0A    spec.update(
 %7B%22$or%22: 
@@ -1308,16 +1308,17 @@
 ,%0A    %5D%7D
+)
 %0A%0A    if
@@ -1561,24 +1561,34 @@
 ion_id, name
+, **kwargs
 ):%0A    peopl
@@ -1686,24 +1686,34 @@
 s=people_ids
+, **kwargs
 )%0A    return
@@ -1777,16 +1777,26 @@
 id, name
+, **kwargs
 ):%0A    o
@@ -1887,16 +1887,26 @@
 =org_ids
+, **kwargs
 )%0A    re

allow extra query params