Commit dfbcfe2c authored by Lynn Foster's avatar Lynn Foster
Browse files

added api search for agent plans, processes, commitments

parent d2ab5bbd
......@@ -1240,6 +1240,28 @@ query ($token: String) {
}
}
query($token: String) {
viewer(token: $token) {
agent(id:106) {
name
searchAgentCommitments(searchString:"Fruit") {
id
note
}
searchAgentPlans(searchString:"Fruit", isFinished: false) {
id
name
note
}
searchAgentProcesses(searchString:"fruit") {
id
name
note
}
}
}
}
query($token: String) {
viewer(token: $token) {
organizationTypes {
......
......@@ -46,11 +46,19 @@ class Agent(graphene.Interface):
agent_processes = graphene.List(lambda: types.Process,
is_finished=graphene.Boolean())
search_agent_processes = graphene.List(lambda: types.Process,
search_string=graphene.String(),
is_finished=graphene.Boolean())
agent_plans = graphene.List(lambda: types.Plan,
is_finished=graphene.Boolean(),
year=graphene.Int(),
month=graphene.Int())
search_agent_plans = graphene.List(lambda: types.Plan,
search_string=graphene.String(),
is_finished=graphene.Boolean())
agent_economic_events = graphene.List(lambda: types.EconomicEvent,
latest_number_of_days=graphene.Int(),
request_distribution=graphene.Boolean(),
......@@ -61,6 +69,10 @@ class Agent(graphene.Interface):
agent_commitments = graphene.List(lambda: types.Commitment,
latest_number_of_days=graphene.Int())
search_agent_commitments = graphene.List(lambda: types.Commitment,
search_string=graphene.String(),
is_finished=graphene.Boolean())
agent_relationships = graphene.List(AgentRelationship,
role_id=graphene.Int(),
category=AgentRelationshipCategory())
......@@ -158,6 +170,16 @@ class Agent(graphene.Interface):
return agent_processes
return None
def resolve_search_agent_processes(self, args, context, info):
agent = _load_identified_agent(self)
finished = args.get('is_finished', None)
search_string = args.get('search_string', "")
if search_string == "":
raise ValidationError("A search string is required.")
if agent:
return agent.search_processes(search_string=search_string, finished=finished)
return None
# if an organization, this returns plans from that context
# if a person, this returns plans the person has worked on
def resolve_agent_plans(self, args, context, info):
......@@ -182,6 +204,16 @@ class Agent(graphene.Interface):
return plans
return None
def resolve_search_agent_plans(self, args, context, info):
agent = _load_identified_agent(self)
finished = args.get('is_finished', None)
search_string = args.get('search_string', "")
if search_string == "":
raise ValidationError("A search string is required.")
if agent:
return agent.search_plans(search_string=search_string, finished=finished)
return None
# returns events where an agent is a provider, receiver, or scope agent, excluding exchange related events
def resolve_agent_economic_events(self, args, context, info):
agent = _load_identified_agent(self)
......@@ -219,6 +251,16 @@ class Agent(graphene.Interface):
return commits
return None
def resolve_search_agent_commitments(self, args, context, info):
agent = _load_identified_agent(self)
finished = args.get('is_finished', None)
search_string = args.get('search_string', "")
if search_string == "":
raise ValidationError("A search string is required.")
if agent:
return agent.search_commitments(search_string=search_string, finished=finished)
return None
def resolve_agent_relationships(self, args, context, info):
agent = _load_identified_agent(self)
cat = args.get('category')
......
......@@ -917,6 +917,36 @@ class EconomicAgent(models.Model):
def involved_in_commitments(self):
return Commitment.objects.filter(Q(from_agent=self)|Q(to_agent=self)|Q(context_agent=self))
 
def active_commitments(self):
return [p for p in self.involved_in_commitments() if p.finished==False]
def finished_commitments(self):
return [p for p in self.involved_in_commitments() if p.finished==True]
# primitive search
def search_commitments(self, search_string, finished=None, also_search_children=True):
if also_search_children:
agents = self.with_all_sub_agents() #includes self
else: #not tested
agents = []
agents.append(self)
commits = []
for agent in agents:
if finished==True:
commits.extend(list(agent.finished_commitments()))
elif finished==False:
commits.extend(list(agent.active_commitments()))
else:
commits.extend(list(agent.involved_in_commitments()))
answer = []
strings = search_string.lower().split(" ")
for com in commits:
for string in strings:
if com.description:
if string in com.description.lower():
answer.append(com)
return list(set(answer))
def user(self):
users = self.users.filter(user__is_active=True)
if users:
......@@ -976,6 +1006,31 @@ class EconomicAgent(models.Model):
else:
return self.context_processes()
 
# primitive search
def search_processes(self, search_string, finished=None, also_search_children=True):
if also_search_children:
agents = self.with_all_sub_agents() #includes self
else: #not tested
agents = []
agents.append(self)
procs = []
for agent in agents:
if finished==True:
procs.extend(list(agent.finished_processes()))
elif finished==False:
procs.extend(list(agent.active_processes()))
else:
procs.extend(list(agent.all_processes()))
answer = []
strings = search_string.lower().split(" ")
for proc in procs:
for string in strings:
if string in proc.name.lower():
answer.append(proc)
elif string in proc.notes.lower():
answer.append(proc)
return list(set(answer))
def all_plans(self):
procs = self.all_processes()
plans = []
......@@ -1005,6 +1060,33 @@ class EconomicAgent(models.Model):
open_plans.append(plan)
return open_plans
 
# primitive search
def search_plans(self, search_string, finished=None, also_search_children=True):
if also_search_children:
agents = self.with_all_sub_agents() #includes self
else: #not tested
agents = []
agents.append(self)
plans = []
for agent in agents:
if finished==True:
plans.extend(list(agent.finished_plans()))
elif finished==False:
plans.extend(list(agent.unfinished_plans()))
else:
plans.extend(list(agent.all_plans()))
answer = []
strings = search_string.lower().split(" ")
for plan in plans:
for string in strings:
if plan.name:
if string in plan.name.lower():
answer.append(plan)
if plan.description:
if string in plan.description.lower():
answer.append(plan)
return list(set(answer))
def resources_created(self):
creations = []
for p in self.all_processes():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment