def _contributions_form_submissions(self): count = XForm.objects.count() path = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'forms', 'contributions') form_path = os.path.join(path, 'contributions.xml') f = open(form_path) xml_file = ContentFile(f.read()) f.close() xml_file.name = 'contributions.xml' self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) instances_path = os.path.join(path, 'instances') for uuid in os.listdir(instances_path): s_path = os.path.join(instances_path, uuid, 'submission.xml') create_instance(self.user.username, open(s_path), []) self.assertEqual(self.xform.instances.count(), 6)
def _contributions_form_submissions(self): count = XForm.objects.count() path = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'forms', 'contributions') form_path = os.path.join(path, 'contributions.xml') with open(form_path, encoding='utf-8') as f: xml_file = ContentFile(f.read()) xml_file.name = 'contributions.xml' project = get_user_default_project(self.user) self.xform = publish_xml_form(xml_file, self.user, project) self.assertTrue(XForm.objects.count() > count) instances_path = os.path.join(path, 'instances') for uuid in os.listdir(instances_path): s_path = os.path.join(instances_path, uuid, 'submission.xml') create_instance(self.user.username, open(s_path, 'rb'), []) self.assertEqual(self.xform.instances.count(), 6)
def _contributions_form_submissions(self): count = XForm.objects.count() path = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'forms', 'contributions') form_path = os.path.join(path, 'contributions.xml') f = open(form_path) xml_file = ContentFile(f.read()) f.close() xml_file.name = 'contributions.xml' self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) instances_path = os.path.join(path, 'instances') for uuid in os.listdir(instances_path): s_path = os.path.join(instances_path, uuid, 'submission.xml') create_instance(self.user.username, open(s_path), []) self.assertEqual(self.xform.instances.count(), 6)
def test_publish_xml_xlsform_download(self): count = XForm.objects.count() path = os.path.join( self.this_directory, '..', '..', 'api', 'tests', 'fixtures', 'forms', 'contributions', 'contributions.xml') f = open(path) xml_file = ContentFile(f.read()) f.close() xml_file.name = 'contributions.xml' self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) response = self.client.get(reverse(download_xlsform, kwargs={ 'username': self.user.username, 'id_string': 'contributions' }), follow=True) self.assertContains(response, 'No XLS file for your form ')
def test_publish_xml_xlsform_download(self): count = XForm.objects.count() path = os.path.join( self.this_directory, "..", "..", "api", "tests", "fixtures", "forms", "contributions", "contributions.xml" ) f = open(path) xml_file = ContentFile(f.read()) f.close() xml_file.name = "contributions.xml" self.xform = publish_xml_form(xml_file, self.user) self.assertTrue(XForm.objects.count() > count) response = self.client.get( reverse(download_xlsform, kwargs={"username": self.user.username, "id_string": "contributions"}), follow=True, ) self.assertContains(response, "No XLS file for your form ")
def publish(self): return publish_xml_form(self.xml_file, self.user)
def publish(self, user, id_string=None, created_by=None): """ Publish XLSForm. """ if self.is_valid(): # If a text (csv) representation of the xlsform is present, # this will save the file and pass it instead of the 'xls_file' # field. cleaned_xls_file = None if 'text_xls_form' in self.cleaned_data\ and self.cleaned_data['text_xls_form'].strip(): csv_data = self.cleaned_data['text_xls_form'] # assigning the filename to a random string (quick fix) import random rand_name = "uploaded_form_%s.csv" % ''.join( random.sample("abcdefghijklmnopqrstuvwxyz0123456789", 6)) cleaned_xls_file = \ default_storage.save( upload_to(None, rand_name, user.username), ContentFile(csv_data)) if 'xls_file' in self.cleaned_data and\ self.cleaned_data['xls_file']: cleaned_xls_file = self.cleaned_data['xls_file'] if 'floip_file' in self.cleaned_data and\ self.cleaned_data['floip_file']: cleaned_xls_file = self.cleaned_data['floip_file'] cleaned_url = (self.cleaned_data['xls_url'].strip() or self.cleaned_data['dropbox_xls_url'] or self.cleaned_data['csv_url']) if cleaned_url: cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) name, extension = os.path.splitext(cleaned_xls_file) if extension not in VALID_FILE_EXTENSIONS and name: response = requests.get(cleaned_url) if response.headers.get('content-type') in \ VALID_XLSFORM_CONTENT_TYPES and \ response.status_code < 400: cleaned_xls_file = get_filename(response) cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urllib2.urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) project = self.cleaned_data['project'] if project is None: project = get_user_default_project(user) else: project = self._project cleaned_xml_file = self.cleaned_data['xml_file'] if cleaned_xml_file: return publish_xml_form(cleaned_xml_file, user, project, id_string, created_by or user) if cleaned_xls_file is None: raise forms.ValidationError( _(u"XLSForm not provided, expecting either of these" " params: 'xml_file', 'xls_file', 'xls_url', 'csv_url'," " 'dropbox_xls_url', 'text_xls_form', 'floip_file'")) # publish the xls return publish_xls_form(cleaned_xls_file, user, project, id_string, created_by or user)
def publish_xform(self): return publish_xml_form(self.xml_file, self.user)
def publish(self, user, id_string=None, created_by=None): """ Publish XLSForm. """ if self.is_valid(): # If a text (csv) representation of the xlsform is present, # this will save the file and pass it instead of the 'xls_file' # field. cleaned_xls_file = None if 'text_xls_form' in self.cleaned_data\ and self.cleaned_data['text_xls_form'].strip(): csv_data = self.cleaned_data['text_xls_form'] # assigning the filename to a random string (quick fix) import random rand_name = "uploaded_form_%s.csv" % ''.join( random.sample("abcdefghijklmnopqrstuvwxyz0123456789", 6)) cleaned_xls_file = \ default_storage.save( upload_to(None, rand_name, user.username), ContentFile(csv_data.encode())) if 'xls_file' in self.cleaned_data and\ self.cleaned_data['xls_file']: cleaned_xls_file = self.cleaned_data['xls_file'] if 'floip_file' in self.cleaned_data and\ self.cleaned_data['floip_file']: cleaned_xls_file = self.cleaned_data['floip_file'] cleaned_url = ( self.cleaned_data['xls_url'].strip() or self.cleaned_data['dropbox_xls_url'] or self.cleaned_data['csv_url']) if cleaned_url: cleaned_xls_file = urlparse(cleaned_url) cleaned_xls_file = \ '_'.join(cleaned_xls_file.path.split('/')[-2:]) name, extension = os.path.splitext(cleaned_xls_file) if extension not in VALID_FILE_EXTENSIONS and name: response = requests.get(cleaned_url) if response.headers.get('content-type') in \ VALID_XLSFORM_CONTENT_TYPES and \ response.status_code < 400: cleaned_xls_file = get_filename(response) cleaned_xls_file = \ upload_to(None, cleaned_xls_file, user.username) self.validate(cleaned_url) xls_data = ContentFile(urlopen(cleaned_url).read()) cleaned_xls_file = \ default_storage.save(cleaned_xls_file, xls_data) project = self.cleaned_data['project'] if project is None: project = get_user_default_project(user) else: project = self._project cleaned_xml_file = self.cleaned_data['xml_file'] if cleaned_xml_file: return publish_xml_form(cleaned_xml_file, user, project, id_string, created_by or user) if cleaned_xls_file is None: raise forms.ValidationError( _(u"XLSForm not provided, expecting either of these" " params: 'xml_file', 'xls_file', 'xls_url', 'csv_url'," " 'dropbox_xls_url', 'text_xls_form', 'floip_file'")) # publish the xls return publish_xls_form(cleaned_xls_file, user, project, id_string, created_by or user)