本文整理汇总了Python中corehq.apps.smsforms.models.XFormsSession类的典型用法代码示例。如果您正苦于以下问题:Python XFormsSession类的具体用法?Python XFormsSession怎么用?Python XFormsSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XFormsSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sync_from_creation
def test_sync_from_creation(self):
properties = _arbitrary_session_properties()
couch_session = XFormsSession(**properties)
couch_session.save()
sql_session = SQLXFormsSession.objects.get(couch_id=couch_session._id)
for prop, value in properties.items():
self.assertEqual(getattr(sql_session, prop), value)
# make sure we didn't do any excess saves
self.assertTrue(XFormsSession.get_db().get_rev(couch_session._id).startswith('1-'))
示例2: _get_case
def _get_case(session):
session = XFormsSession.get(session.get_id)
self.assertTrue(session.submission_id)
instance = XFormInstance.get(session.submission_id)
case_id = instance.xpath("form/case/@case_id")
self.assertTrue(case_id)
return CommCareCase.get(case_id)
示例3: test_get_all_open_sessions_already_ended
def test_get_all_open_sessions_already_ended(self):
domain = uuid.uuid4().hex
contact = uuid.uuid4().hex
_make_session(
domain=domain,
connection_id=contact,
end_time=datetime.utcnow(),
session_type=XFORMS_SESSION_SMS,
)
self.assertEqual(0, len(XFormsSession.get_all_open_sms_sessions(domain, contact)))
示例4: test_get_all_open_sessions_contact_mismatch
def test_get_all_open_sessions_contact_mismatch(self):
domain = uuid.uuid4().hex
contact = uuid.uuid4().hex
_make_session(
domain=domain,
connection_id='wrong',
end_time=None,
session_type=XFORMS_SESSION_SMS,
)
self.assertEqual(0, len(XFormsSession.get_all_open_sms_sessions(domain, contact)))
示例5: test_get_and_close_all_open_sessions
def test_get_and_close_all_open_sessions(self):
domain = uuid.uuid4().hex
contact = uuid.uuid4().hex
for i in range(3):
_make_session(
domain=domain,
connection_id=contact,
end_time=None,
session_type=XFORMS_SESSION_SMS,
)
couch_sessions = XFormsSession.get_all_open_sms_sessions(domain, contact)
sql_sessions = SQLXFormsSession.get_all_open_sms_sessions(domain, contact)
self.assertEqual(3, len(couch_sessions))
self.assertEqual(3, len(sql_sessions))
self.assertEqual(set([x._id for x in couch_sessions]), set([x.couch_id for x in sql_sessions]))
SQLXFormsSession.close_all_open_sms_sessions(domain, contact)
self.assertEqual(0, len(XFormsSession.get_all_open_sms_sessions(domain, contact)))
self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
示例6: form_session_handler
def form_session_handler(v, text, msg=None):
"""
The form session handler will use the inbound text to answer the next question
in the open XformsSession for the associated contact. If no session is open,
the handler passes. If multiple sessions are open, they are all closed and an
error message is displayed to the user.
"""
sessions = XFormsSession.get_all_open_sms_sessions(v.domain, v.owner_id)
if len(sessions) > 1:
# If there are multiple sessions, there's no way for us to know which one this message
# belongs to. So we should inform the user that there was an error and to try to restart
# the survey.
for session in sessions:
session.end(False)
session.save()
send_sms_to_verified_number(v, "An error has occurred. Please try restarting the survey.")
return True
session = sessions[0] if len(sessions) == 1 else None
if session is not None:
if msg is not None:
msg.workflow = session.workflow
msg.reminder_id = session.reminder_id
msg.xforms_session_couch_id = session._id
msg.save()
# If there's an open session, treat the inbound text as the answer to the next question
try:
resp = current_question(session.session_id)
event = resp.event
valid, text, error_msg = validate_answer(event, text)
if valid:
responses = _get_responses(v.domain, v.owner_id, text, yield_responses=True)
if has_invalid_response(responses):
if msg:
mark_as_invalid_response(msg)
text_responses = _responses_to_text(responses)
if len(text_responses) > 0:
response_text = format_message_list(text_responses)
send_sms_to_verified_number(v, response_text, workflow=session.workflow, reminder_id=session.reminder_id, xforms_session_couch_id=session._id)
else:
if msg:
mark_as_invalid_response(msg)
send_sms_to_verified_number(v, error_msg + event.text_prompt, workflow=session.workflow, reminder_id=session.reminder_id, xforms_session_couch_id=session._id)
except Exception:
# Catch any touchforms errors
msg_id = msg._id if msg is not None else ""
logging.exception("Exception in form_session_handler for message id %s." % msg_id)
send_sms_to_verified_number(v, "An error has occurred. Please try again later. If the problem persists, try restarting the survey.")
return True
else:
return False
示例7: test_sync_from_update
def test_sync_from_update(self):
properties = _arbitrary_session_properties()
couch_session = XFormsSession(**properties)
couch_session.save()
sql_session = SQLXFormsSession.objects.get(couch_id=couch_session._id)
for prop, value in properties.items():
self.assertEqual(getattr(sql_session, prop), value)
previous_count = SQLXFormsSession.objects.count()
updated_properties = _arbitrary_session_properties()
for attr, val in updated_properties.items():
couch_session[attr] = val
couch_session.save()
# make sure nothing new was created
self.assertEqual(previous_count, SQLXFormsSession.objects.count())
# check updated props in the sql model
sql_session = SQLXFormsSession.objects.get(pk=sql_session.pk)
for prop, value in updated_properties.items():
self.assertEqual(getattr(sql_session, prop), value)
示例8: test_get_single_open_session
def test_get_single_open_session(self):
properties = _arbitrary_session_properties(
end_time=None,
session_type=XFORMS_SESSION_SMS,
)
couch_session = XFormsSession(**properties)
couch_session.save()
(mult, session) = get_single_open_session_or_close_multiple(
couch_session.domain, couch_session.connection_id
)
self.assertEqual(False, mult)
self.assertEqual(couch_session._id, session._id)
[couch_session_back] = XFormsSession.get_all_open_sms_sessions(
couch_session.domain, couch_session.connection_id
)
[sql_session] = SQLXFormsSession.get_all_open_sms_sessions(
couch_session.domain, couch_session.connection_id
)
self.assertEqual(couch_session._id, couch_session_back._id)
self.assertEqual(couch_session._id, sql_session.couch_id)
示例9: sms_keyword_handler
def sms_keyword_handler(v, text, msg):
text = text.strip()
if text == "":
return False
sessions = XFormsSession.get_all_open_sms_sessions(v.domain, v.owner_id)
text_words = text.upper().split()
if text.startswith("#"):
return handle_global_keywords(v, text, msg, text_words, sessions)
else:
return handle_domain_keywords(v, text, msg, text_words, sessions)
示例10: handle_sms_form_complete
def handle_sms_form_complete(sender, session_id, form, **kwargs):
from corehq.apps.smsforms.models import XFormsSession
session = XFormsSession.latest_by_session_id(session_id)
if session:
# i don't know if app_id is the id of the overall app or the id of the specific build of the app
# the thing i want to pass in is the id of the overall app
resp = spoof_submission(get_submit_url(session.domain, session.app_id), form, hqsubmission=False)
xform_id = resp["X-CommCareHQ-FormID"]
session.end(completed=True)
session.submission_id = xform_id
session.save()
示例11: handle_sms_form_complete
def handle_sms_form_complete(sender, session_id, form, **kwargs):
from corehq.apps.smsforms.models import XFormsSession
session = XFormsSession.by_session_id(session_id)
if session:
resp = submit_form_locally(form, session.domain, app_id=session.app_id)
xform_id = resp['X-CommCareHQ-FormID']
session.end(completed=True)
session.submission_id = xform_id
session.save()
xform = XFormInstance.get(xform_id)
xform.survey_incentive = session.survey_incentive
xform.save()
示例12: handle
def handle(self, *args, **options):
db = XFormsSession.get_db()
session_ids = [row['id'] for row in db.view("smsforms/sessions_by_touchforms_id")]
errors = []
for session_doc in iter_docs(db, session_ids):
try:
# Handle the old touchforms session id convention where it was
# always an int
session_id = session_doc.get("session_id", None)
if isinstance(session_id, int):
session_doc["session_id"] = str(session_id)
couch_session = XFormsSession.wrap(session_doc)
sync_sql_session_from_couch_session(couch_session)
except Exception as e:
logging.exception('problem migrating session {}: {}'.format(session_doc['_id'], e))
errors.append(session_doc['_id'])
print 'migrated {} couch sessions. there are now {} in sql'.format(
len(session_ids) - len(errors), SQLXFormsSession.objects.count()
)
if errors:
print 'errors: {}'.format(', '.join(errors))
示例13: handle
def handle(self, *args, **options):
sessions = XFormsSession.view(
"smsforms/open_sms_sessions_by_connection",
include_docs=True
).all()
for session in sessions:
try:
get_raw_instance(session.session_id)
except InvalidSessionIdException:
print "Closing %s %s" % (session.domain, session._id)
session.end(False)
session.save()
except Exception as e:
print "An unexpected error occurred when processing %s %s" % (session.domain, session._id)
print e
示例14: test_get_single_open_session_close_multiple
def test_get_single_open_session_close_multiple(self):
domain = uuid.uuid4().hex
contact = uuid.uuid4().hex
for i in range(3):
_make_session(
domain=domain,
connection_id=contact,
end_time=None,
session_type=XFORMS_SESSION_SMS,
)
(mult, session) = get_single_open_session_or_close_multiple(domain, contact)
self.assertEqual(True, mult)
self.assertEqual(None, session)
self.assertEqual(0, len(XFormsSession.get_all_open_sms_sessions(domain, contact)))
self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
示例15: test_basic_form_playing
def test_basic_form_playing(self):
# load the app
with open(os.path.join(os.path.dirname(__file__), "data", "demo_app.json")) as f:
app_json = json.loads(f.read())
app = import_app(app_json, self.domain)
# start form session
session, responses = start_session(self.domain, self.contact, app,
app.get_module(0),
app.get_module(0).get_form(0))
[answer] = responses
self.assertEqual("what is your name?", answer)
# check state of model
self.assertEqual(session.start_time, session.modified_time)
self.assertEqual("http://www.commcarehq.org/tests/smsforms", session.form_xmlns)
self.assertFalse(session.end_time)
self.assertEqual(False, session.completed)
self.assertEqual(self.domain, session.domain)
self.assertEqual(self.contact.get_id, session.user_id)
self.assertEqual(app.get_id, session.app_id)
self.assertFalse(session.submission_id)
# play through the form, checking answers
q_and_a(self, "sms contact", "how old are you, sms contact?", self.domain)
q_and_a(self, "29", "what is your gender? 1:male, 2:female", self.domain)
q_and_a(self, "2", "thanks for submitting!", self.domain)
# check the instance
session = XFormsSession.get(session.get_id)
self.assertTrue(session.submission_id)
instance = XFormInstance.get(session.submission_id)
self.assertEqual("sms contact", instance.xpath("form/name"))
self.assertEqual("29", instance.xpath("form/age"))
self.assertEqual("f", instance.xpath("form/gender"))
self.assertEqual(self.domain, instance.domain)