def save_form_data(self, attachment): """The entry point for saving form data from an attachment. returns True on success and false on fail.""" xml_file_name = attachment.filepath f = open(xml_file_name, "r") # should match XMLNS xmlns, version = self.get_xmlns_from_instance(f) # If there is a special way to route this form, based on the xmlns # then do so here. # czue: this is probably not the most appropriate place for this logic # but it keeps us from having to parse the xml multiple times. process(attachment, xmlns, version) try: formdefmodel = FormDefModel.objects.get(domain=attachment.submission.domain, target_namespace=xmlns, version=version) except FormDefModel.DoesNotExist: raise self.XFormError("XMLNS %s could not be matched to any registered formdefmodel in %s." % (xmlns, attachment.submission.domain)) if formdefmodel.xsd_file_location is None: raise self.XFormError("Schema for form %s could not be found on the file system." % formdefmodel[0].id) formdef = self.get_formdef_from_schema_file(formdefmodel.xsd_file_location) self.formdef = formdef f.seek(0,0) status = self._save_form_data_matching_formdef(f, formdef, formdefmodel, attachment) f.close() return status
def testRouting(self): """Tests the creation of a form group from a single form.""" # nothing registered. assure that nothing happens self.assertEqual(0, counter) xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(0, counter) xmlrouter.process(attachment, dec_xmlns, 0) self.assertEqual(0, counter) xmlrouter.process(attachment, unused_xmlns, 0) self.assertEqual(0, counter) # register the incrementer and the decrementer xmlrouter.register(inc_xmlns, increment) xmlrouter.register(dec_xmlns, decrement) # make sure we are incrementing correctly xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(1, counter) xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(2, counter) # and decrementing correctly xmlrouter.process(attachment, dec_xmlns, 0) self.assertEqual(1, counter) # and ignoring what should be ignored xmlrouter.process(attachment, unused_xmlns, 0) self.assertEqual(1, counter) # reregistering should not "double up" xmlrouter.register(inc_xmlns, increment) xmlrouter.register(inc_xmlns, increment) xmlrouter.register(inc_xmlns, increment) xmlrouter.register(dec_xmlns, decrement) xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(2, counter) xmlrouter.process(attachment, dec_xmlns, 0) self.assertEqual(1, counter) # but registering with a second method should xmlrouter.register(inc_xmlns, increment_again) xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(3, counter) xmlrouter.process(attachment, inc_xmlns, 0) self.assertEqual(5, counter) xmlrouter.process(attachment, dec_xmlns, 0) self.assertEqual(4, counter)