我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用django.conf.settings.PROJECT_ROOT。
def test_publish_select_external_xlsform(self): view = XFormViewSet.as_view({ 'post': 'create' }) path = os.path.join( settings.PROJECT_ROOT, "apps", "api", "tests", "fixtures", "select_one_external.xlsx") with open(path) as xls_file: meta_count = MetaData.objects.count() post_data = {'xls_file': xls_file} request = self.factory.post('/', data=post_data, **self.extra) response = view(request) self.assertEqual(response.status_code, 201) self.assertEqual(meta_count + 1, MetaData.objects.count()) xform = self.user.xforms.all()[0] metadata = xform.metadata_set.all()[0] self.assertEqual('itemsets.csv', metadata.data_value) self.assertTrue(OwnerRole.user_has_role(self.user, xform)) self.assertTrue(OwnerRole.user_has_role(self.user, metadata)) self.assertEquals("owner", response.data['users'][0]['role'])
def test_publish_invalid_xls_form_no_choices(self): view = XFormViewSet.as_view({ 'post': 'create' }) path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.no_choices.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.post('/', data=post_data, **self.extra) response = view(request) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) error_msg = ( 'There should be a choices sheet in this xlsform. Please ' 'ensure that the choices sheet name is all in small caps.') self.assertEqual(response.data.get('text'), error_msg)
def test_update_xform_xls_file_with_version_set(self): self._publish_xls_form_to_project() form_id = self.xform.pk self.assertIsNotNone(self.xform.version) view = XFormViewSet.as_view({ 'patch': 'partial_update', }) path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation_version.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.patch('/', data=post_data, **self.extra) response = view(request, pk=form_id) self.assertEqual(response.status_code, 200) self.xform.reload() # diff versions self.assertEquals(self.xform.version, u"212121211") self.assertEquals(form_id, self.xform.pk)
def test_date_created_override(self): """ Test that passing a date_created_override when creating and instance will set our date as the date_created """ xml_file_path = os.path.join( settings.PROJECT_ROOT, "apps", "logger", "fixtures", "tutorial", "instances", "tutorial_2012-06-27_11-27-53.xml") xml_file = django_file( xml_file_path, field_name="xml_file", content_type="text/xml") media_files = [] date_created = datetime.strptime("2013-01-01 12:00:00", "%Y-%m-%d %H:%M:%S") instance = create_instance( self.user.username, xml_file, media_files, date_created_override=date_created) self.assertIsNotNone(instance) self.assertEqual(instance.date_created.strftime("%Y-%m-%d %H:%M:%S"), date_created.strftime("%Y-%m-%d %H:%M:%S"))
def main(): if settings.DEBUG: pass try: from affiliation.models import AffiliationItem, Store from affiliation.decathlon.tools import decathlon_db_processing except ImportError: sys.stdout.write("Imports failed.\n") else: store = Store.objects.get_store("Decathlon") log_path = settings.PROJECT_ROOT + "/decathlon_db_log.txt" errors = decathlon_db_processing(AffiliationItem, store, log_path) if len(errors) != 0: sys.stdout.writelines(errors)
def handle_noargs(self, **options): fixtures_dir = os.path.join(settings.PROJECT_ROOT, 'demo', 'fixtures') fixture_file = os.path.join(fixtures_dir, 'demo.json') image_src_dir = os.path.join(fixtures_dir, 'images') image_dest_dir = os.path.join(settings.MEDIA_ROOT, 'original_images') call_command('loaddata', fixture_file, verbosity=0) if not os.path.isdir(image_dest_dir): os.makedirs(image_dest_dir) for filename in os.listdir(image_src_dir): shutil.copy(os.path.join(image_src_dir, filename), image_dest_dir)
def handle_noargs(self, **options): fixtures_dir = os.path.join(settings.PROJECT_ROOT, 'demo', 'fixtures') catalogue_dir = os.path.join(fixtures_dir, 'catalogue') products_file = os.path.join(catalogue_dir, 'child_products.json') orders_file = os.path.join(catalogue_dir, 'orders.json') image_src = os.path.join(catalogue_dir, 'images.tar.gz') csv_files = [ os.path.join(catalogue_dir, 'books.computers-in-fiction.csv'), os.path.join(catalogue_dir, 'books.essential.csv'), os.path.join(catalogue_dir, 'books.hacking.csv'), ] json_files = [ os.path.join(fixtures_dir, 'pages.json'), os.path.join(fixtures_dir, 'auth.json'), os.path.join(fixtures_dir, 'ranges.json'), os.path.join(fixtures_dir, 'offers.json'), ] call_command('loaddata', products_file, verbosity=0) call_command('oscar_import_catalogue', *csv_files, verbosity=0) call_command('oscar_import_catalogue_images', image_src, verbosity=0) call_command('oscar_populate_countries', verbosity=0) call_command('loaddata', *json_files, verbosity=0) call_command('loaddata', orders_file, verbosity=0)
def create_product_image(product, placeholder_dir): placeholder_root = os.path.join(settings.PROJECT_ROOT, placeholder_dir) img_path = '%s/%s' % (placeholder_dir, random.choice(os.listdir(placeholder_root))) image = ProductImage( product=product, image=File(open(img_path, 'rb'))).save() return image
def test_import(list): file = os.path.join(settings.PROJECT_ROOT, "squads", list) with open(file, encoding="utf8") as raw_data: data = json.load(raw_data) import_squad(data, "Jim Loves U-wings")
def get_manage_shell_cmd(cmd, *args, **kw): ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" settings_module = os.environ[ENVIRONMENT_VARIABLE] kw_expanded = "" for k, v in kw.items(): kw_expanded += "--%s=%s" % (k, v) args_expanded = " ".join(args) cmd = "%s/manage.py %s --settings=%s %s %s" % (settings.PROJECT_ROOT, cmd, settings_module, kw_expanded, args_expanded) if os.environ.get("VIRTUAL_ENV"): cmd = ". %s/bin/activate && %s" % (os.environ["VIRTUAL_ENV"], cmd) return cmd
def setUp(self): super(TestMetaDataViewSet, self).setUp() self.view = MetaDataViewSet.as_view({ 'delete': 'destroy', 'get': 'retrieve', 'post': 'create' }) self._publish_xls_form_to_project() self.data_value = "screenshot.png" self.fixture_dir = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation" ) self.path = os.path.join(self.fixture_dir, self.data_value)
def test_publish_xlsform(self): view = XFormViewSet.as_view({ 'post': 'create' }) data = { 'owner': 'http://testserver/api/v1/users/bob', 'public': False, 'public_data': False, 'description': u'transportation_2011_07_25', 'downloadable': True, 'allows_sms': False, 'encrypted': False, 'sms_id_string': u'transportation_2011_07_25', 'id_string': u'transportation_2011_07_25', 'title': u'transportation_2011_07_25', 'bamboo_dataset': u'' } path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.post('/', data=post_data, **self.extra) response = view(request) self.assertEqual(response.status_code, 201) xform = self.user.xforms.all()[0] data.update({ 'url': 'http://testserver/api/v1/forms/%s' % xform.pk }) self.assertDictContainsSubset(data, response.data) self.assertTrue(OwnerRole.user_has_role(self.user, xform)) self.assertEquals("owner", response.data['users'][0]['role'])
def test_publish_invalid_xls_form(self): view = XFormViewSet.as_view({ 'post': 'create' }) path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.bad_id.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.post('/', data=post_data, **self.extra) response = view(request) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) error_msg = '[row : 5] Question or group with no name.' self.assertEqual(response.data.get('text'), error_msg)
def test_csv_import(self): self._publish_xls_form_to_project() view = XFormViewSet.as_view({'post': 'csv_import'}) csv_import = open(os.path.join(settings.PROJECT_ROOT, 'libs', 'utils', 'tests', 'fixtures', 'good.csv')) post_data = {'csv_file': csv_import} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=self.xform.id) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Last-Modified'), None) self.assertEqual(response.data.get('additions'), 9) self.assertEqual(response.data.get('updates'), 0)
def test_csv_import_fail(self): self._publish_xls_form_to_project() view = XFormViewSet.as_view({'post': 'csv_import'}) csv_import = open(os.path.join(settings.PROJECT_ROOT, 'libs', 'utils', 'tests', 'fixtures', 'bad.csv')) post_data = {'csv_file': csv_import} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=self.xform.id) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) self.assertIsNotNone(response.data.get('error'))
def test_csv_import_fail_invalid_field_post(self): """Test that invalid post returns 400 with the error in json respone""" self._publish_xls_form_to_project() view = XFormViewSet.as_view({'post': 'csv_import'}) csv_import = open(os.path.join(settings.PROJECT_ROOT, 'libs', 'utils', 'tests', 'fixtures', 'bad.csv')) post_data = {'wrong_file_field': csv_import} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=self.xform.id) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) self.assertIsNotNone(response.data.get('error'))
def test_update_xform_xls_bad_file(self): self._publish_xls_form_to_project() self.assertIsNotNone(self.xform.version) version = self.xform.version form_id = self.xform.pk view = XFormViewSet.as_view({ 'patch': 'partial_update', }) path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.bad_id.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.patch('/', data=post_data, **self.extra) response = view(request, pk=form_id) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) self.xform.reload() new_version = self.xform.version # fails to update the form self.assertEquals(version, new_version) self.assertEquals(form_id, self.xform.pk)
def test_update_xform_xls_file_with_submissions(self): self._publish_xls_form_to_project() self._make_submissions() self.assertIsNotNone(self.xform.version) version = self.xform.version form_id = self.xform.pk view = XFormViewSet.as_view({ 'patch': 'partial_update', }) path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation_updated.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.patch('/', data=post_data, **self.extra) response = view(request, pk=form_id) self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) self.assertEquals(response.data, u"Cannot update the xls file in " u"a form that has submissions") self.xform.reload() new_version = self.xform.version # diff versions self.assertEquals(version, new_version) self.assertEquals(form_id, self.xform.pk)
def _load_metadata(self, xform=None): data_value = "screenshot.png" data_type = 'media' fixture_dir = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation" ) path = os.path.join(fixture_dir, data_value) xform = xform or self.xform self._add_form_metadata(xform, data_type, data_value, path)
def _main_fixture_path(instance_name): return os.path.join(settings.PROJECT_ROOT, 'apps', 'main', 'tests', 'fixtures', 'transportation', 'instances_w_uuid', instance_name, instance_name + '.xml')
def viewer_fixture_path(*args): return os.path.join(settings.PROJECT_ROOT, 'apps', 'viewer', 'tests', 'fixtures', *args)
def setUp(self): super(TestBackupTools, self).setUp() self._publish_xls_file_and_set_xform( os.path.join( settings.PROJECT_ROOT, "apps", "logger", "fixtures", "test_forms", "tutorial.xls"))
def setUp(self): TestBase.setUp(self) self._publish_xls_file( os.path.join( settings.PROJECT_ROOT, "apps", "logger", "fixtures", "test_forms", "tutorial.xls"))
def _publish_xls_form_to_project(self, publish_data={}, merge=True, public=False): if not hasattr(self, 'project'): self._project_create() elif self.project.created_by != self.user: self._project_create() view = ProjectViewSet.as_view({ 'post': 'forms' }) project_id = self.project.pk if merge: data = { 'owner': 'http://testserver/api/v1/users/%s' % self.project.organization.username, 'public': False, 'public_data': False, 'description': u'transportation_2011_07_25', 'downloadable': True, 'allows_sms': False, 'encrypted': False, 'sms_id_string': u'transportation_2011_07_25', 'id_string': u'transportation_2011_07_25', 'title': u'transportation_2011_07_25', 'bamboo_dataset': u'' } data.update(publish_data) else: data = publish_data path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.xls") with open(path) as xls_file: post_data = {'xls_file': xls_file} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=project_id) self.assertEqual(response.status_code, 201) self.xform = XForm.objects.all().order_by('pk').reverse()[0] data.update({ 'url': 'http://testserver/api/v1/forms/%s' % (self.xform.pk) }) # Input was a private so change to public if project public if public: data['public_data'] = data['public'] = True self.assertDictContainsSubset(data, response.data) self.form_data = response.data
def test_form_format(self): self._publish_xls_form_to_project() view = XFormViewSet.as_view({ 'get': 'form' }) formid = self.xform.pk data = { "name": "transportation", "title": "transportation_2011_07_25", "default_language": "default", "id_string": "transportation_2011_07_25", "type": "survey", } request = self.factory.get('/', **self.extra) # test for unsupported format response = view(request, pk=formid, format='csvzip') self.assertEqual(response.status_code, 400) self.assertEqual(response.get('Last-Modified'), None) # test for supported formats response = view(request, pk=formid, format='json') self.assertEqual(response.status_code, 200) self.assertNotEqual(response.get('Last-Modified'), None) self.assertDictContainsSubset(data, response.data) response = view(request, pk=formid, format='xml') self.assertEqual(response.status_code, 200) self.assertNotEqual(response.get('Last-Modified'), None) response_doc = minidom.parseString(response.data) response = view(request, pk=formid, format='xls') self.assertEqual(response.status_code, 200) self.assertNotEqual(response.get('Last-Modified'), None) xml_path = os.path.join( settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures", "transportation", "transportation.xml") with open(xml_path) as xml_file: expected_doc = minidom.parse(xml_file) model_node = [ n for n in response_doc.getElementsByTagName("h:head")[0].childNodes if n.nodeType == Node.ELEMENT_NODE and n.tagName == "model"][0] # check for UUID and remove uuid_nodes = [ node for node in model_node.childNodes if node.nodeType == Node.ELEMENT_NODE and node.getAttribute("nodeset") == "/transportation/formhub/uuid"] self.assertEqual(len(uuid_nodes), 1) uuid_node = uuid_nodes[0] uuid_node.setAttribute("calculate", "''") # check content without UUID response_xml = response_doc.toxml().replace( self.xform.version, u"201411120717") self.assertEqual(response_xml, expected_doc.toxml())