本文整理汇总了Python中temba.flows.models.Flow类的典型用法代码示例。如果您正苦于以下问题:Python Flow类的具体用法?Python Flow怎么用?Python Flow使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Flow类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: advance_stuck_runs
def advance_stuck_runs(apps, schema_editor):
# this data migration is not forward-compatible
from temba.flows.models import Flow, FlowStep, FlowRun, RuleSet
from temba.msgs.models import Msg
flows = Flow.objects.filter(flow_type="F", version_number=5)
if flows:
print "%d version 5 flows" % len(flows)
for flow in flows:
# looking for flows that start with a passive ruleset
ruleset = RuleSet.objects.filter(uuid=flow.entry_uuid, flow=flow).first()
if ruleset and not ruleset.is_pause():
# now see if there are any active steps at our current flow
steps = FlowStep.objects.filter(
run__is_active=True, step_uuid=ruleset.uuid, rule_value=None, left_on=None
).select_related("contact")
if steps:
print "\nAdvancing %d steps for %s:%s" % (len(steps), flow.org.name, flow.name)
for idx, step in enumerate(steps):
if (idx + 1) % 100 == 0:
print "\n\n *** Step %d of %d\n\n" % (idx + 1, len(steps))
# force them to be handled again
msg = Msg(contact=step.contact, text="", id=0)
Flow.handle_destination(ruleset, step, step.run, msg)
示例2: update_destination_no_check
def update_destination_no_check(self, flow, node, destination, rule=None): # pragma: no cover
""" Update the destination without doing a cycle check """
# look up our destination, we need this in order to set the correct destination_type
destination_type = ACTION_SET
action_destination = Flow.get_node(flow, destination, destination_type)
if not action_destination:
destination_type = RULE_SET
ruleset_destination = Flow.get_node(flow, destination, destination_type)
self.assertTrue(ruleset_destination, "Unable to find new destination with uuid: %s" % destination)
actionset = ActionSet.get(flow, node)
if actionset:
actionset.destination = destination
actionset.destination_type = destination_type
actionset.save()
ruleset = RuleSet.get(flow, node)
if ruleset:
rules = ruleset.get_rules()
for r in rules:
if r.uuid == rule:
r.destination = destination
r.destination_type = destination_type
ruleset.set_rules(rules)
ruleset.save()
else:
self.fail("Couldn't find node with uuid: %s" % node)
示例3: send
def send(self, message, contact=None):
if not contact:
contact = self.contact
if contact.is_test:
Contact.set_simulation(True)
incoming = self.create_msg(direction=INCOMING, contact=contact, text=message)
Flow.find_and_handle(incoming)
return Msg.all_messages.filter(response_to=incoming).order_by('pk').first()
示例4: test_flow_event
def test_flow_event(self):
self.setupChannel()
org = self.channel.org
org.save()
from temba.flows.models import ActionSet, WebhookAction, Flow
flow = self.create_flow()
# replace our uuid of 4 with the right thing
actionset = ActionSet.objects.get(x=4)
actionset.set_actions_dict([WebhookAction(org.get_webhook_url()).as_json()])
actionset.save()
with patch('requests.Session.send') as mock:
# run a user through this flow
flow.start([], [self.joe])
# have joe reply with mauve, which will put him in the other category that triggers the API Action
sms = self.create_msg(contact=self.joe, direction='I', status='H', text="Mauve")
mock.return_value = MockResponse(200, "{}")
Flow.find_and_handle(sms)
# should have one event created
event = WebHookEvent.objects.get()
self.assertEquals('C', event.status)
self.assertEquals(1, event.try_count)
self.assertFalse(event.next_attempt)
result = WebHookResult.objects.get()
self.assertStringContains("successfully", result.message)
self.assertEquals(200, result.status_code)
self.assertTrue(mock.called)
args = mock.call_args_list[0][0]
prepared_request = args[0]
self.assertStringContains(self.channel.org.get_webhook_url(), prepared_request.url)
data = parse_qs(prepared_request.body)
self.assertEquals(self.channel.pk, int(data['channel'][0]))
self.assertEquals(actionset.uuid, data['step'][0])
self.assertEquals(flow.pk, int(data['flow'][0]))
self.assertEquals(self.joe.uuid, data['contact'][0])
self.assertEquals(unicode(self.joe.get_urn('tel')), data['urn'][0])
values = json.loads(data['values'][0])
self.assertEquals('Other', values[0]['category']['base'])
self.assertEquals('color', values[0]['label'])
self.assertEquals('Mauve', values[0]['text'])
self.assertTrue(values[0]['time'])
self.assertTrue(data['time'])
示例5: send
def send(self, message, contact=None):
if not contact:
contact = self.contact
if contact.is_test:
Contact.set_simulation(True)
incoming = self.create_msg(direction=INCOMING, contact=contact, text=message)
# evaluate the inbound message against our triggers first
from temba.triggers.models import Trigger
if not Trigger.find_and_handle(incoming):
Flow.find_and_handle(incoming)
return Msg.objects.filter(response_to=incoming).order_by('pk').first()
示例6: send_message
def send_message(
self,
flow,
message,
restart_participants=False,
contact=None,
initiate_flow=False,
assert_reply=True,
assert_handle=True,
):
"""
Starts the flow, sends the message, returns the reply
"""
if not contact:
contact = self.contact
try:
if contact.is_test:
Contact.set_simulation(True)
incoming = self.create_msg(
direction=INCOMING, contact=contact, contact_urn=contact.get_urn(), text=message
)
# start the flow
if initiate_flow:
flow.start(
groups=[], contacts=[contact], restart_participants=restart_participants, start_msg=incoming
)
else:
flow.start(groups=[], contacts=[contact], restart_participants=restart_participants)
(handled, msgs) = Flow.find_and_handle(incoming)
Msg.mark_handled(incoming)
if assert_handle:
self.assertTrue(handled, "'%s' did not handle message as expected" % flow.name)
else:
self.assertFalse(handled, "'%s' handled message, was supposed to ignore" % flow.name)
# our message should have gotten a reply
if assert_reply:
replies = Msg.objects.filter(response_to=incoming).order_by("pk")
self.assertGreaterEqual(len(replies), 1)
if len(replies) == 1:
self.assertEqual(contact, replies.first().contact)
return replies.first().text
# if it's more than one, send back a list of replies
return [reply.text for reply in replies]
else:
# assert we got no reply
replies = Msg.objects.filter(response_to=incoming).order_by("pk")
self.assertFalse(replies)
return None
finally:
Contact.set_simulation(False)
示例7: migrate_flows
def migrate_flows(min_version=None): # pragma: no cover
to_version = min_version or get_current_export_version()
# get all flows below the min version
old_versions = Flow.get_versions_before(to_version)
flows_to_migrate = Flow.objects.filter(is_active=True, version_number__in=old_versions)
flow_ids = list(flows_to_migrate.values_list("id", flat=True))
total = len(flow_ids)
if not total:
print("All flows up to date")
return True
print("Found %d flows to migrate to %s..." % (len(flow_ids), to_version))
num_updated = 0
errored = []
for id_batch in chunk_list(flow_ids, 1000):
for flow in Flow.objects.filter(id__in=id_batch):
try:
flow.ensure_current_version(min_version=to_version)
num_updated += 1
except Exception:
print("Unable to migrate flow '%s' (#%d)" % (flow.name, flow.id))
errored.append(flow)
print(" > Flows migrated: %d of %d (%d errored)" % (num_updated, total, len(errored)))
if errored:
print(" > Errored flows: %s" % (", ".join([str(e.id) for e in errored])))
return len(errored) == 0
示例8: test_export_import
def test_export_import(self):
# tweak our current channel to be twitter so we can create a channel-based trigger
Channel.objects.filter(id=self.channel.id).update(channel_type=TWITTER)
flow = self.create_flow()
group = self.create_group("Trigger Group", [])
# create a trigger on this flow for the follow actions but only on some groups
trigger = Trigger.objects.create(org=self.org, flow=flow, trigger_type=Trigger.TYPE_FOLLOW, channel=self.channel,
created_by=self.admin, modified_by=self.admin)
trigger.groups.add(group)
# export everything
export = Flow.export_definitions([flow])
# remove our trigger
Trigger.objects.all().delete()
# and reimport them.. trigger should be recreated
self.org.import_app(export, self.admin)
trigger = Trigger.objects.get()
self.assertEqual(trigger.trigger_type, Trigger.TYPE_FOLLOW)
self.assertEqual(trigger.flow, flow)
self.assertEqual(trigger.channel, self.channel)
self.assertEqual(list(trigger.groups.all()), [group])
示例9: create_flow
def create_flow(self):
start = int(time.time()*1000) % 1000000
definition = dict(action_sets=[dict(uuid=uuid(start + 1), x=1, y=1, destination=uuid(start + 5),
actions=[dict(type='reply', msg='What is your favorite color?')]),
dict(uuid=uuid(start + 2), x=2, y=2, destination=None,
actions=[dict(type='reply', msg='I love orange too!')]),
dict(uuid=uuid(start + 3), x=3, y=3, destination=None,
actions=[dict(type='reply', msg='Blue is sad. :(')]),
dict(uuid=uuid(start + 4), x=4, y=4, destination=None,
actions=[dict(type='reply', msg='That is a funny color.')])
],
rule_sets=[dict(uuid=uuid(start + 5), x=5, y=5,
label='color',
response_type='C',
rules=[
dict(uuid=uuid(start + 12), destination=uuid(start + 2), test=dict(type='contains', test='orange'), category="Orange"),
dict(uuid=uuid(start + 13), destination=uuid(start + 3), test=dict(type='contains', test='blue'), category="Blue"),
dict(uuid=uuid(start + 14), destination=uuid(start + 4), test=dict(type='true'), category="Other"),
dict(uuid=uuid(start + 15), test=dict(type='true'), category="Nothing")]) # test case with no destination
],
entry=uuid(start + 1))
flow = Flow.create(self.org, self.admin, "Color Flow")
flow.update(definition)
return flow
示例10: start_call
def start_call(self, call, to, from_, status_callback):
if not settings.SEND_CALLS:
raise ValueError("SEND_CALLS set to False, skipping call start")
channel = call.channel
Contact.get_or_create(channel.org, URN.from_tel(to), channel)
# Verboice differs from Twilio in that they expect the first block of twiml up front
payload = str(Flow.handle_call(call))
# now we can post that to verboice
url = "%s?%s" % (self.endpoint, urlencode(dict(channel=self.verboice_channel, address=to)))
response = requests.post(url, data=payload, auth=self.auth).json()
if "call_id" not in response:
call.status = IVRCall.FAILED
call.save()
raise IVRException(_("Verboice connection failed."))
# store the verboice call id in our IVRCall
call.external_id = response["call_id"]
# the call was successfully sent to the IVR provider
call.status = IVRCall.WIRED
call.save()
示例11: create_message_event
def create_message_event(
cls, org, user, campaign, relative_to, offset, unit, message, delivery_hour=-1, base_language=None
):
if campaign.org != org:
raise ValueError("Org mismatch")
if relative_to.value_type != Value.TYPE_DATETIME:
raise ValueError(
f"Contact fields for CampaignEvents must have a datetime type, got {relative_to.value_type}."
)
if isinstance(message, str):
base_language = org.primary_language.iso_code if org.primary_language else "base"
message = {base_language: message}
flow = Flow.create_single_message(org, user, message, base_language)
return cls.objects.create(
campaign=campaign,
relative_to=relative_to,
offset=offset,
unit=unit,
event_type=cls.TYPE_MESSAGE,
message=message,
flow=flow,
delivery_hour=delivery_hour,
created_by=user,
modified_by=user,
)
示例12: __init__
def __init__(self, user, *args, **kwargs):
flows = Flow.get_triggerable_flows(user.get_org())
super().__init__(user, flows, *args, **kwargs)
self.fields["channel"].queryset = Channel.objects.filter(
is_active=True, org=self.user.get_org(), schemes__overlap=list(ContactURN.SCHEMES_SUPPORTING_REFERRALS)
)
示例13: pre_save
def pre_save(self, request, obj):
org = self.user.get_org()
# if it's before, negate the offset
if self.cleaned_data["direction"] == "B":
obj.offset = -obj.offset
if self.cleaned_data["unit"] == "H" or self.cleaned_data["unit"] == "M": # pragma: needs cover
obj.delivery_hour = -1
# if its a message flow, set that accordingly
if self.cleaned_data["event_type"] == CampaignEvent.TYPE_MESSAGE:
if self.instance.id:
base_language = self.instance.flow.base_language
else:
base_language = org.primary_language.iso_code if org.primary_language else "base"
translations = {}
for language in self.languages:
iso_code = language.language["iso_code"]
translations[iso_code] = self.cleaned_data.get(iso_code, "")
if not obj.flow_id or not obj.flow.is_active or not obj.flow.is_system:
obj.flow = Flow.create_single_message(org, request.user, translations, base_language=base_language)
else:
# set our single message on our flow
obj.flow.update_single_message_flow(translations, base_language)
obj.message = translations
obj.full_clean()
# otherwise, it's an event that runs an existing flow
else:
obj.flow = Flow.objects.get(org=org, id=self.cleaned_data["flow_to_start"])
示例14: create_message_event
def create_message_event(cls, org, user, campaign, relative_to, offset, unit, message, delivery_hour=-1):
if campaign.org != org: # pragma: no cover
raise ValueError("Org mismatch")
flow = Flow.create_single_message(org, user, message)
return cls.objects.create(campaign=campaign, relative_to=relative_to, offset=offset, unit=unit,
event_type=MESSAGE_EVENT, message=message, flow=flow, delivery_hour=delivery_hour,
created_by=user, modified_by=user)
示例15: create_flow
def create_flow(self, uuid_start=None, **kwargs):
if 'org' not in kwargs:
kwargs['org'] = self.org
if 'user' not in kwargs:
kwargs['user'] = self.user
if 'name' not in kwargs:
kwargs['name'] = "Color Flow"
flow = Flow.create(**kwargs)
flow.update(self.create_flow_definition(uuid_start))
return Flow.objects.get(pk=flow.pk)