def _add_form_schedules(self): self.form_1.schedule = FormSchedule( expires=120, starts=-5, visits=[ ScheduleVisit(due=5, expires=4, starts=-5), ScheduleVisit(due=10, expires=9), ScheduleVisit(starts=5, expires=100, repeats=True, increment=15) ] ) self.form_2.schedule = FormSchedule( allow_unscheduled=True, visits=[ ScheduleVisit(due=7, expires=4), ScheduleVisit(due=15) ] ) self.form_3.schedule = FormSchedule( visits=[ ScheduleVisit(due=9, expires=1), ScheduleVisit(due=11) ] )
def get_schedule_context(form): from corehq.apps.app_manager.models import SchedulePhase schedule_context = {} module = form.get_module() if not form.schedule: # Forms created before the scheduler module existed don't have this property # so we need to add it so everything works. form.schedule = FormSchedule(enabled=False) schedule_context.update({ 'all_schedule_phase_anchors': [phase.anchor for phase in module.get_schedule_phases()], 'schedule_form_id': form.schedule_form_id, }) if module.has_schedule: phase = form.get_phase() if phase is not None: schedule_context.update({'schedule_phase': phase}) else: schedule_context.update( {'schedule_phase': SchedulePhase(anchor='')}) return schedule_context
def test_multiple_modules(self): self._apply_schedule_phases() other_module = self.app.get_module(2) other_module.has_schedule = True scheduled_form = other_module.get_form(0) scheduled_form.schedule = FormSchedule( visits=[ScheduleVisit( due=9), ScheduleVisit(due=11)]) other_module.forms.append(copy.copy(scheduled_form)) other_module.schedule_phases = [ SchedulePhase( anchor='case_property', forms=[SchedulePhaseForm(form_id=scheduled_form.unique_id)]) ] expected_fixture = u""" <partial> <fixture id="schedule:m2:p1:f0"> <schedule expires="" allow_unscheduled="False"> <visit id="1" due="9" repeats="False"/> <visit id="2" due="11" repeats="False"/> </schedule> </fixture> </partial> """ suite = self.app.create_suite() self.assertXmlPartialEqual(expected_fixture, suite, './fixture[@id="schedule:m2:p1:f0"]') self.assertXmlHasXpath(suite, './fixture[@id="schedule:m1:p1:f0"]')
def edit_visit_schedule(request, domain, app_id, form_unique_id): app = get_app(domain, app_id) form = app.get_form(form_unique_id) module = form.get_module() json_loads = json.loads(request.POST.get('schedule')) enabled = json_loads.pop('enabled') anchor = json_loads.pop('anchor') schedule_form_id = json_loads.pop('schedule_form_id') if enabled: try: phase, is_new_phase = module.get_or_create_schedule_phase(anchor=anchor) except ScheduleError as e: return HttpResponseBadRequest(six.text_type(e)) form.schedule_form_id = schedule_form_id form.schedule = FormSchedule.wrap(json_loads) phase.add_form(form) else: try: form.disable_schedule() except ScheduleError: pass response_json = {} app.save(response_json) return json_response(response_json)
def edit_visit_schedule(request, domain, app_id, module_id, form_id): app = get_app(domain, app_id) module = app.get_module(module_id) form = module.get_form(form_id) json_loads = json.loads(request.POST.get('schedule')) enabled = json_loads.pop('enabled') anchor = json_loads.pop('anchor') schedule_form_id = json_loads.pop('schedule_form_id') if enabled: try: phase, is_new_phase = module.get_or_create_schedule_phase(anchor=anchor) except ScheduleError as e: return HttpResponseBadRequest(unicode(e)) form.schedule_form_id = schedule_form_id form.schedule = FormSchedule.wrap(json_loads) phase.add_form(form) else: try: form.disable_schedule() except ScheduleError: pass response_json = {} app.save(response_json) return json_response(response_json)
def get_visit_scheduler_module_and_form_for_test(): form = AdvancedForm(schedule=FormSchedule( unique_id='form-unique-id-1', schedule_form_id='form1', enabled=True, visits=[ ScheduleVisit( due=1, starts=-1, expires=1, repeats=False, increment=None), ScheduleVisit( due=7, starts=-2, expires=3, repeats=False, increment=None), ScheduleVisit(due=None, starts=None, expires=None, repeats=True, increment=14), ], )) module = AdvancedModule( schedule_phases=[ SchedulePhase(anchor='edd', forms=[]), SchedulePhase(anchor='add', forms=[SchedulePhaseForm(form_id=form.unique_id)]), ], forms=[form], ) return module, form
def test_schedule(self): self.form.actions.load_update_cases.append( LoadUpdateAction(case_type=self.module.case_type, case_tag='load_1', case_properties={'question1': '/data/question1'})) self.module.has_schedule = True self.form.schedule = FormSchedule(anchor='edd') xml = self.get_xml('schedule').format( form_id=self.form.schedule_form_id, form_index=1) self.assertXmlEqual(xml, self.form.render_xform())
def test_schedule_index(self): self.module.has_schedule = True form = self.app.new_form(0, 'New Form', lang='en') form.source = self.get_xml('original_form', override_path=('data', )) form.actions.load_update_cases.append( LoadUpdateAction(case_type=self.module.case_type, case_tag='load_1', case_properties={'question1': '/data/question1'})) form.schedule = FormSchedule(anchor='edd') xml = self.get_xml('schedule').format(form_id=form.schedule_form_id, form_index=2) self.assertXmlEqual(xml, form.render_xform())
def test_schedule(self): app = Application.wrap(self.get_json('suite-advanced')) mod = app.get_module(1) mod.has_schedule = True f1 = mod.get_form(0) f2 = mod.get_form(1) f3 = mod.get_form(2) f1.schedule = FormSchedule(anchor='edd', expires=120, post_schedule_increment=15, visits=[ ScheduleVisit(due=5, late_window=4), ScheduleVisit(due=10, late_window=9), ScheduleVisit(due=20, late_window=5) ]) f2.schedule = FormSchedule(anchor='dob', visits=[ ScheduleVisit(due=7, late_window=4), ScheduleVisit(due=15) ]) f3.schedule = FormSchedule(anchor='dob', visits=[ ScheduleVisit(due=9, late_window=1), ScheduleVisit(due=11) ]) mod.case_details.short.columns.append( DetailColumn( header={'en': 'Next due'}, model='case', field='schedule:nextdue', format='plain', )) suite = app.create_suite() self.assertXmlPartialEqual(self.get_xml('schedule-fixture'), suite, './fixture') self.assertXmlPartialEqual(self.get_xml('schedule-entry'), suite, "./detail[@id='m1_case_short']")
def _add_scheduler_to_form(self, form, module, form_abreviation): # (this mimics the behavior in app_manager.views.schedules.edit_visit_schedule() # A Form.source is required to retreive scheduler properties form.source = self.get_xml('very_simple_form') phase, _ = module.get_or_create_schedule_phase(anchor='date-opened') form.schedule_form_id = form_abreviation form.schedule = FormSchedule(starts=5, expires=None, visits=[ ScheduleVisit(due=7, expires=5, starts=-2), ]) phase.add_form(form)