def render(self, data, *args, **kwargs): charset = 'utf-8' root_node = 'xforms' xmlns = "http://openrosa.org/xforms/xformsList" if 'detail' in data.keys(): stream = StringIO() xml = SimplerXMLGenerator(stream, charset) xml.startDocument() xml.startElement(root_node, {'xmlns': xmlns}) for key, value in six.iteritems(data): xml.startElement(key, {}) xml.characters(smart_text(value)) xml.endElement(key) xml.endElement(root_node) xml.endDocument() return stream.getvalue() else: json = self.transform_to_xform_json(data) survey = create_survey_element_from_dict(json) xml = survey.xml() fix_languages(xml) xml = xml.toxml() xml = self.insert_version_attribute(xml, data.get('id_string'), data.get('version')) xml = self.insert_uuid_bind(xml, data.get('id_string')) return xml
def test_mysql_cache_migration_alias(self): out = StringIO() call_command('mysql_cache_migration', 'default', stdout=out) output = out.getvalue() num_run_sqls = (len(output.split('RunSQL')) - 1) assert num_run_sqls == 1
def test_spelling(self): status = StringIO() with TemporaryDirectory() as OUT_DIR: with tmp_list_append(sys.argv, 'spelling'): try: app = Sphinx( srcdir=DOCS_DIR, confdir=DOCS_DIR, outdir=OUT_DIR, doctreedir=OUT_DIR, buildername="spelling", warningiserror=True, status=status, confoverrides={ 'extensions': [ 'djangocms', 'sphinx.ext.intersphinx', 'sphinxcontrib.spelling' ] } ) app.build() self.assertEqual(app.statuscode, 0, status.getvalue()) except SphinxWarning: # while normally harmless, causes a test failure pass except: print(status.getvalue()) raise
def test_uninstall_plugins_with_plugin(self): out = StringIO() placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 1) management.call_command('cms', 'uninstall', 'plugins', PLUGIN, interactive=False, stdout=out) self.assertEqual(out.getvalue(), "1 'TextPlugin' plugins uninstalled\n") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 0)
def test_list_apphooks(self): out = StringIO() create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK) self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1) command = cms.Command() command.stdout = out command.handle("list", "apphooks", interactive=False) self.assertEqual(out.getvalue(), "SampleApp\n")
def test_uninstall_apphooks_with_apphook(self): out = StringIO() create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK) self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1) command = cms.Command() command.stdout = out command.handle("uninstall", "apphooks", APPHOOK, interactive=False) self.assertEqual(out.getvalue(), "1 'SampleApp' apphooks uninstalled\n") self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 0)
def test_mysql_full(self): out = StringIO() call_command("dbparams", stdout=out, skip_checks=True) output = out.getvalue() assert ( output == "--defaults-file=/tmp/defaults.cnf --user=ausername " "--password=apassword --host=ahost.example.com --port=12345 " "--ssl-ca=/tmp/mysql.cert mydatabase" )
def test_dsn_full(self): out = StringIO() err = StringIO() call_command("dbparams", "default", dsn=True, stdout=out, stderr=err, skip_checks=True) output = out.getvalue() assert output == "F=/tmp/defaults.cnf,u=ausername,p=apassword,h=ahost.example.com," "P=12345,D=mydatabase" errors = err.getvalue() assert "SSL params can't be" in errors
def test_uninstall_plugins_with_plugin(self): out = StringIO() placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 1) command = cms.Command() command.stdout = out command.handle("uninstall", "plugins", PLUGIN, interactive=False) self.assertEqual(out.getvalue(), "1 'TextPlugin' plugins uninstalled\n") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 0)
def test_dsn_socket(self): out = StringIO() err = StringIO() call_command('dbparams', dsn=True, stdout=out, stderr=err) output = out.getvalue() self.assertEqual(output, 'S=/etc/mydb.sock') errors = err.getvalue() self.assertEqual(errors, "")
def test_mysql_full(self): out = StringIO() call_command('dbparams', stdout=out) output = out.getvalue() self.assertEqual( output, "--defaults-file=/tmp/defaults.cnf --user=ausername " "--password=apassword --host=ahost.example.com --port=12345 " "--ssl-ca=/tmp/mysql.cert mydatabase" )
def test_list_apphooks(self): out = StringIO() apps = ["cms", "menus", "sekizai", "cms.test_utils.project.sampleapp"] with SettingsOverride(INSTALLED_APPS=apps): create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK) self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1) command = cms.Command() command.stdout = out command.handle("list", "apphooks", interactive=False) self.assertEqual(out.getvalue(), "SampleApp\n")
def _exception_traceback(exc_info): """ Return a string containing a traceback message for the given exc_info tuple (as returned by sys.exc_info()). """ # Get a traceback message. excout = StringIO() exc_type, exc_val, exc_tb = exc_info traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) return excout.getvalue()
def test_dsn_socket(self): out = StringIO() err = StringIO() call_command("dbparams", dsn=True, stdout=out, stderr=err, skip_checks=True) output = out.getvalue() assert output == "S=/etc/mydb.sock" errors = err.getvalue() assert errors == ""
def run_it(*args, **kwargs): run_args = ['fix_datetime_columns'] run_args.extend(args) out = StringIO() run_kwargs = {'stdout': out, 'skip_checks': True} run_kwargs.update(kwargs) call_command(*run_args, **run_kwargs) return out.getvalue()
def test_uninstall_apphooks_without_apphook(self): with apphooks(): out = StringIO() management.call_command( "cms", "uninstall", "apphooks", APPHOOK, interactive=False, stdout=out, ) self.assertEqual(out.getvalue(), "no 'SampleApp' apphooks found\n")
def test_uninstall_plugins_with_plugin(self): out = StringIO() apps = ["cms", "menus", "sekizai", "cms.test_utils.project.sampleapp"] with SettingsOverride(INSTALLED_APPS=apps): placeholder = Placeholder.objects.create(slot="test") add_plugin(placeholder, TextPlugin, "en", body="en body") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 1) command = cms.Command() command.stdout = out command.handle("uninstall", "plugins", PLUGIN, interactive=False) self.assertEqual(out.getvalue(), "1 'TextPlugin' plugins uninstalled\n") self.assertEqual(CMSPlugin.objects.filter(plugin_type=PLUGIN).count(), 0)
def test_dsn_full(self): out = StringIO() err = StringIO() call_command('dbparams', 'default', dsn=True, stdout=out, stderr=err) output = out.getvalue() self.assertEqual( output, "F=/tmp/defaults.cnf,u=ausername,p=apassword,h=ahost.example.com," "P=12345,D=mydatabase" ) errors = err.getvalue() self.assertIn("SSL params can't be", errors)
def render(self, data, accepted_media_type=None, renderer_context=None): if data is None: return '' view = (renderer_context.get("view") if renderer_context else None) self.item_tag_name = getattr(view, "item_tag_name", self.item_tag_name) self.root_tag_name = getattr(view, "root_tag_name", self.root_tag_name) stream = StringIO() xml = SimplerXMLGenerator(stream, self.charset) xml.startDocument() root_tag_name = (getattr(data, "xml_tag", None) or self.root_tag_name) self._to_xml(xml, data, root_tag_name) xml.endDocument() return stream.getvalue()
def test_list_apphooks(self): with apphooks(SampleApp): out = StringIO() create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK) self.assertEqual(Page.objects.filter(application_urls=APPHOOK).count(), 1) management.call_command( "cms", "list", "apphooks", interactive=False, stdout=out, ) self.assertEqual(out.getvalue(), "SampleApp (draft)\n")
def test_cull_mysql_caches_named_cache(self): cache.set('key', 'value', 0.1) time.sleep(0.2) assert self.table_count() == 1 out = StringIO() call_command('cull_mysql_caches', 'default', verbosity=1, stdout=out) output = out.getvalue() assert ( output.strip() == "Deleting from cache 'default'... 1 entries deleted." ) assert self.table_count() == 0
def test_fix_tree(self): create_page("home", "nav_playground.html", "en") page1 = create_page("page", "nav_playground.html", "en") page1.depth = 3 page1.numchild = 4 page1.path = "00100010" page1.save() out = StringIO() management.call_command('cms', 'fix-tree', interactive=False, stdout=out) self.assertEqual(out.getvalue(), 'fixing page tree\nfixing plugin tree\nall done\n') page1 = page1.reload() self.assertEqual(page1.path, "0002") self.assertEqual(page1.depth, 1) self.assertEqual(page1.numchild, 0)
def test_copy_from_non_existing_lang(self): """ If an existing title in the target language has plugins in a placeholder and the command is called with *force-copy*, the plugins are copied on top of the existing one """ site = 1 out = StringIO() command = cms.Command() command.stdout = out command.handle("copy-lang", "de", "fr", "verbose") text = out.getvalue() page_count = Page.objects.on_site(site).drafts().count() + 1 for idx in range(1, page_count): self.assertTrue(text.find("Skipping page page%d, language de not defined" % idx) > -1)
def test_fix_tree(self): create_page("home", "nav_playground.html", "en") page1 = create_page("page", "nav_playground.html", "en") page1.depth = 3 page1.numchild = 4 page1.path = "00100010" page1.save() out = StringIO() command = cms.Command() command.stdout = out command.handle("fix-tree", interactive=False) self.assertEqual(out.getvalue(), 'fixing page treefixing plugin treeall done') page1 = page1.reload() self.assertEqual(page1.path, "0002") self.assertEqual(page1.depth, 1) self.assertEqual(page1.numchild, 0)
def test_copy_from_non_existing_lang(self): """ If an existing title in the target language has plugins in a placeholder and the command is called with *force-copy*, the plugins are copied on top of the existing one """ site = 1 out = StringIO() management.call_command( 'cms', 'copy', 'lang', '--from-lang=de', '--to-lang=fr', verbosity=3, interactive=False, stdout=out ) text = out.getvalue() page_count = Page.objects.on_site(site).drafts().count() + 1 for idx in range(1, page_count): self.assertTrue("Skipping page page%d, language de not defined" % idx in text)
def render(self, data, accepted_media_type=None, renderer_context=None): if data is None: return '' stream = StringIO() xml = SimplerXMLGenerator(stream, self.charset) xml.startDocument() if isinstance(data, (list, tuple)): self._render_list(data, xml, renderer_context) elif isinstance(data, dict): self._render_dict(data, xml, renderer_context) xml.endDocument() return stream.getvalue()
def test_mysql_cache_migration(self): out = StringIO() call_command('mysql_cache_migration', stdout=out) output = out.getvalue() # Lint it with captured_stdout() as stderr: errors = check_code(output) self.assertEqual( errors, 0, "Encountered {} errors whilst trying to lint the mysql cache " "migration.\nMigration:\n\n{}\n\nLint errors:\n\n{}" .format(errors, output, stderr.getvalue()) ) # Dynamic import and check migration_mod = imp.new_module('0001_add_cache_tables') six.exec_(output, migration_mod.__dict__) self.assertTrue(hasattr(migration_mod, 'Migration')) migration = migration_mod.Migration self.assertTrue(hasattr(migration, 'dependencies')) self.assertTrue(hasattr(migration, 'operations')) # Since they all have the same table name, there should only be one # operation self.assertEqual(len(migration.operations), 1) # Now run the migration forwards and backwards to check it works operation = migration.operations[0] self.drop_table() self.assertTableNotExists(self.table_name) state = ProjectState() new_state = state.clone() with connection.schema_editor() as editor: operation.database_forwards("django_mysql_tests", editor, state, new_state) self.assertTableExists(self.table_name) new_state = state.clone() with connection.schema_editor() as editor: operation.database_backwards("django_mysql_tests", editor, new_state, state) self.assertTableNotExists(self.table_name) self.create_table()
def test_html(self): status = StringIO() with TemporaryDirectory() as OUT_DIR: app = Sphinx( srcdir=DOCS_DIR, confdir=DOCS_DIR, outdir=OUT_DIR, doctreedir=OUT_DIR, buildername="html", warningiserror=True, status=status, ) try: app.build() except: print(status.getvalue()) raise
def test_html(self): nullout = StringIO() with TemporaryDirectory() as OUT_DIR: app = Sphinx( DOCS_DIR, DOCS_DIR, OUT_DIR, OUT_DIR, "html", warningiserror=True, status=nullout, ) try: app.build() except: print(nullout.getvalue()) raise
def render(self, data, accepted_media_type=None, renderer_context=None): """ Renders `data` into serialized XML. """ if data is None: return '' stream = StringIO() xml = self.generator_class(stream, self.charset) xml.startDocument() xml.startElement("root", {}) self._to_xml(xml, data) xml.endElement("root") xml.endDocument() return stream.getvalue()
def test_copy_langs_no_content(self): """ Various checks here: * page structure is copied * no plugin is copied """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() out = StringIO() management.call_command('cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', '--skip-content', interactive=False, stdout=out) pages = Page.objects.on_site(site).drafts() for page in pages: self.assertEqual(set((u'en', u'de')), set(page.get_languages())) # These asserts that no orphaned plugin exists self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins) self.assertEqual( CMSPlugin.objects.filter(language='en').count(), number_start_plugins) self.assertEqual(CMSPlugin.objects.filter(language='de').count(), 0) root_page = Page.objects.on_site(site).get_home() root_plugins = CMSPlugin.objects.filter( placeholder=root_page.placeholders.get(slot="body")) first_plugin_en, _ = root_plugins.get( language='en', parent=None).get_plugin_instance() first_plugin_de = None with self.assertRaises(CMSPlugin.DoesNotExist): first_plugin_de, _ = root_plugins.get( language='de', parent=None).get_plugin_instance() self.assertIsNone(first_plugin_de) stack_plugins = CMSPlugin.objects.filter( placeholder=StaticPlaceholder.objects.order_by('?')[0].draft) stack_text_en, _ = stack_plugins.get( language='en', plugin_type='TextPlugin').get_plugin_instance() with self.assertRaises(CMSPlugin.DoesNotExist): stack_text_de, _ = stack_plugins.get( language='de', plugin_type='TextPlugin').get_plugin_instance()
def encode(self, o, *args, **kwargs): self.level = 0 self.doIndent = False stream = StringIO() self.xml = XMLGenerator(stream, settings.DEFAULT_CHARSET) self.xml.startDocument() self.startElement("rsp") self.__encode(o, *args, **kwargs) self.endElement("rsp") self.xml.endDocument() self.xml = None return stream.getvalue()
def setUp(self): self._input = StringIO('<?xml version="1.0" encoding="utf-8"?>' '<root>' '<field_a>121.0</field_a>' '<field_b>dasd</field_b>' '<field_c></field_c>' '<field_d>2011-12-25 12:45:00</field_d>' '</root>') self._data = { 'field_a': 121, 'field_b': 'dasd', 'field_c': None, 'field_d': datetime.datetime(2011, 12, 25, 12, 45, 00) } self._complex_data_input = StringIO( '<?xml version="1.0" encoding="utf-8"?>' '<root>' '<creation_date>2011-12-25 12:45:00</creation_date>' '<sub_data_list>' '<listitem><sub_id>1</sub_id><sub_name>first</sub_name></listitem>' '<listitem><sub_id>2</sub_id><sub_name>second</sub_name></listitem>' '</sub_data_list>' '<name>name</name>' '</root>') self._complex_data = { "creation_date": datetime.datetime(2011, 12, 25, 12, 45, 00), "name": "name", "sub_data_list": [{ "sub_id": 1, "sub_name": "first" }, { "sub_id": 2, "sub_name": "second" }] }
def output_results(profiler, options, stdout): """Generate the profiler output in the desired format. Implemented as a separate function so it can be run as an exit handler (because management commands often call exit() directly, bypassing the rest of the profile command's handle() method).""" profiler.create_stats() if not options['sort']: if not which('dot'): stdout.write('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation') return if not which('gprof2dot.py'): stdout.write('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler') return return_code, output = run_gprof2dot(profiler) if return_code: stdout.write('gprof2dot/dot exited with {}'.format(return_code)) return path = options['path'] with open(path, 'wb') as pdf_file: pdf_file.write(output) stdout.write('Wrote call graph to {}'.format(path)) else: sort = options['sort'] if sort == 'file': # Work around bug on Python versions >= 2.7.4 sort = 'fil' out = StringIO() stats = pstats.Stats(profiler, stream=out) with mock.patch('pstats.func_strip_path') as mock_func_strip_path: mock_func_strip_path.side_effect = func_strip_path stats.strip_dirs() restrictions = [] if options['pattern']: restrictions.append(options['pattern']) if options['fraction']: restrictions.append(float(options['fraction'])) elif options['max_calls']: restrictions.append(int(options['max_calls'])) elif not options['pattern']: restrictions.append(.2) stats.sort_stats(sort).print_stats(*restrictions) if options['path']: path = options['path'] with open(path, 'w') as text_file: text_file.write(out.getvalue()) stdout.write('Wrote profiling statistics to {}'.format(path)) else: stdout.write(out.getvalue())
def test_mysql_cache_migration(self): out = StringIO() call_command('mysql_cache_migration', stdout=out) output = out.getvalue() # Lint it self.flake8dir.make_example_py(output) result = self.flake8dir.run_flake8() assert result.out_lines == [] # Dynamic import and check migration_mod = imp.new_module('0001_add_cache_tables') six.exec_(output, migration_mod.__dict__) assert hasattr(migration_mod, 'Migration') migration = migration_mod.Migration assert hasattr(migration, 'dependencies') assert hasattr(migration, 'operations') # Since they all have the same table name, there should only be one # operation assert len(migration.operations) == 1 # Now run the migration forwards and backwards to check it works operation = migration.operations[0] assert not self.table_exists(self.table_name) state = ProjectState() new_state = state.clone() with connection.schema_editor() as editor: operation.database_forwards("testapp", editor, state, new_state) assert self.table_exists(self.table_name) new_state = state.clone() with connection.schema_editor() as editor: operation.database_backwards("testapp", editor, new_state, state) assert not self.table_exists(self.table_name)
def test_copy_bad_languages(self): out = StringIO() with self.assertRaises(CommandError) as command_error: management.call_command('cms', 'copy', 'lang', '--from-lang=it', '--to-lang=fr', interactive=False, stdout=out) self.assertEqual( str(command_error.exception), 'Both languages have to be present in settings.LANGUAGES and settings.CMS_LANGUAGES' )
def _http_get(service, url, *args, **kwargs): if expect_git_blob_url: self.assertEqual( url, 'https://api3.codebasehq.com/myproj/myrepo/blob/123') else: self.assertEqual( url, 'https://api3.codebasehq.com/myproj/myrepo/blob/123/' 'myfile') if file_exists: return b'{}', {} else: raise HTTPError(url, 404, '', {}, StringIO())
def __init__(self): """Initialize the parsed file information.""" self.origFile = None self.newFile = None self.origInfo = None self.newInfo = None self.origChangesetId = None self.binary = False self.deleted = False self.moved = False self.copied = False self.insert_count = 0 self.delete_count = 0 self._data_io = StringIO() self._data = None
def test_dsn_socket(self): out = StringIO() err = StringIO() call_command('dbparams', dsn=True, stdout=out, stderr=err, skip_checks=True) output = out.getvalue() assert output == 'S=/etc/mydb.sock' errors = err.getvalue() assert errors == ""
def test_fix_tree_regression_5641(self): # ref: https://github.com/divio/django-cms/issues/5641 alpha = create_page("Alpha", "nav_playground.html", "en", published=True) beta = create_page("Beta", "nav_playground.html", "en", published=False) gamma = create_page("Gamma", "nav_playground.html", "en", published=False) delta = create_page("Delta", "nav_playground.html", "en", published=True) theta = create_page("Theta", "nav_playground.html", "en", published=True) beta.move_page(alpha.node, position='last-child') gamma.move_page(beta.node, position='last-child') delta.move_page(gamma.node, position='last-child') theta.move_page(delta.node, position='last-child') out = StringIO() management.call_command('cms', 'fix-tree', interactive=False, stdout=out) tree = [ (alpha, '0001'), (beta, '00010001'), (gamma, '000100010001'), (delta, '0001000100010001'), (theta, '00010001000100010001'), ] for page, path in tree: self.assertEqual(page.node.path, path)
def test_copy_site_safe(self): """ Check that copy of languages on one site does not interfere with other sites """ site_other = 1 site_active = 2 origina_site1_langs = {} number_start_plugins = CMSPlugin.objects.all().count() site_obj = Site.objects.create(domain="sample2.com", name="sample2.com", pk=site_active) for page in Page.objects.on_site(1).drafts(): origina_site1_langs[page.pk] = set(page.get_languages()) p1 = create_page('page1', published=True, in_navigation=True, language='de', template='nav_playground.html', site=site_obj) create_page('page4', published=True, in_navigation=True, language='de', template='nav_playground.html', site=site_obj) create_page('page2', published=True, in_navigation=True, parent=p1, language='de', template='nav_playground.html', site=site_obj) for page in Page.objects.on_site(site_active).drafts(): self._fill_page_body(page, 'de') number_site2_plugins = CMSPlugin.objects.all().count() - number_start_plugins out = StringIO() management.call_command( 'cms', 'copy', 'lang', '--from-lang=de', '--to-lang=fr', '--site=%s' % site_active, interactive=False, stdout=out ) for page in Page.objects.on_site(site_other).drafts(): self.assertEqual(origina_site1_langs[page.pk], set(page.get_languages())) for page in Page.objects.on_site(site_active).drafts(): self.assertEqual(set(('de', 'fr')), set(page.get_languages())) # plugins for site 1 self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins) # plugins for site 2 de self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_site2_plugins) # plugins for site 2 fr self.assertEqual(CMSPlugin.objects.filter(language='fr').count(), number_site2_plugins) # global number of plugins self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins + number_site2_plugins*2)
def test_copy_langs(self): """ Various checks here: * plugins are exactly doubled, half per language with no orphaned plugin * the bottom-most plugins in the nesting chain maintain the same position and the same content * the top-most plugin are of the same type """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() out = StringIO() management.call_command( 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out ) pages = Page.objects.on_site(site).drafts() for page in pages: self.assertEqual(set((u'en', u'de')), set(page.get_languages())) # These asserts that no orphaned plugin exists self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins*2) self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins) self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins) root_page = Page.objects.on_site(site).get_home() root_plugins = CMSPlugin.objects.filter(placeholder=root_page.placeholders.get(slot="body")) first_plugin_en, _ = root_plugins.get(language='en', parent=None).get_plugin_instance() first_plugin_de, _ = root_plugins.get(language='de', parent=None).get_plugin_instance() self.assertEqual(first_plugin_en.plugin_type, first_plugin_de.plugin_type) link_en, _ = root_plugins.get(language='en', plugin_type='LinkPlugin').get_plugin_instance() link_de, _ = root_plugins.get(language='de', plugin_type='LinkPlugin').get_plugin_instance() self.assertEqual(link_en.url, link_de.url) self.assertEqual(link_en.get_position_in_placeholder(), link_de.get_position_in_placeholder()) stack_plugins = CMSPlugin.objects.filter(placeholder=StaticPlaceholder.objects.order_by('?')[0].draft) stack_text_en, _ = stack_plugins.get(language='en', plugin_type='TextPlugin').get_plugin_instance() stack_text_de, _ = stack_plugins.get(language='de', plugin_type='TextPlugin').get_plugin_instance() self.assertEqual(stack_text_en.plugin_type, stack_text_de.plugin_type) self.assertEqual(stack_text_en.body, stack_text_de.body)
def _migrate_extension_models(self, ext_class): """Perform database migrations for an extension's models. This will call out to Django Evolution to handle the migrations. Args: ext_class (djblets.extensions.extension.Extension): The class for the extension to migrate. """ if django_evolution is None: # Django Evolution isn't installed. Extensions with evolutions # are not supported. return try: stream = StringIO() call_command('evolve', verbosity=0, interactive=False, execute=True, stdout=stream, stderr=stream) output = stream.getvalue() if output: logging.info('Evolved extension models for %s: %s', ext_class.id, stream.read()) stream.close() except CommandError as e: # Something went wrong while running django-evolution, so # grab the output. We can't raise right away because we # still need to put stdout back the way it was output = stream.getvalue() stream.close() logging.error('Error evolving extension models: %s: %s', e, output, exc_info=1) load_error = self._store_load_error(ext_class.id, output) raise InstallExtensionError(six.text_type(e), load_error)
def test_dsn_full(self): out = StringIO() err = StringIO() call_command('dbparams', 'default', dsn=True, stdout=out, stderr=err, skip_checks=True) output = out.getvalue() assert ( output == "F=/tmp/defaults.cnf,u=ausername,p=apassword,h=ahost.example.com," "P=12345,D=mydatabase" ) errors = err.getvalue() assert "SSL params can't be" in errors
def _cache_iter_large_data(data, key): """Iterate through large data that was fetched from the cache. This will unpickle the large data previously fetched through _cache_fetch_large_data, and yield each object to the caller. """ fp = StringIO(data) try: # Unpickle all the items we're expecting from the cached data. # # There will only be one item in the case of old-style cache data. while True: try: yield pickle.load(fp) except EOFError: return except Exception as e: logging.warning('Unpickle error for cache key "%s": %s.' % (key, e)) raise
def process_response(self, request, response): if self.profile_parameter is not None: if self.error: return text_response(response, self.error) self.profiler.create_stats() mode = self.profile_parameter if mode == 'file': # Work around bug on Python versions >= 2.7.4 mode = 'fil' if not mode: if not which('dot'): return text_response(response, _('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation')) if not which('gprof2dot.py'): return text_response(response, _('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler')) return_code, output = run_gprof2dot(self.profiler) if return_code: raise Exception(_('gprof2dot.py exited with {return_code}').format(return_code=return_code)) set_content(response, output) response['Content-Type'] = 'application/pdf' return response elif mode == 'help': return text_response(response, ProfilerMiddleware.__doc__) else: out = StringIO() stats = pstats.Stats(self.profiler, stream=out) with mock.patch('pstats.func_strip_path') as mock_func_strip_path: mock_func_strip_path.side_effect = func_strip_path stats.strip_dirs() restrictions = [] if self.pattern_parameter is not None: restrictions.append(self.pattern_parameter) if self.fraction_parameter is not None: restrictions.append(float(self.fraction_parameter)) elif self.max_calls_parameter is not None: restrictions.append(int(self.max_calls_parameter)) elif self.pattern_parameter is None: restrictions.append(.2) stats.sort_stats(mode).print_stats(*restrictions) return text_response(response, out.getvalue()) return response
def arch_month_mbox(request, list_name, year, month_name): """ The messages must be rebuilt before being added to the mbox file, including headers and the textual content, making sure to escape email addresses. """ return HttpResponse("Not implemented yet.", content_type="text/plain", status=500) mlist = get_list_by_name(list_name, request.get_host()) month = month_name_to_num(month_name) year = int(year) begin_date = datetime.datetime(year, month, 1) if month != 12: end_month = month + 1 else: end_month = 1 end_date = datetime.datetime(year, end_month, 1) messages = Email.objects.filter( mailinglist=mlist, date__gte=begin_date, date__lte=end_date ).order_by("date") mboxfile, mboxfilepath = tempfile.mkstemp( prefix="hyperkitty-", suffix=".mbox.gz") os.close(mboxfile) mbox = mailbox.mbox(mboxfilepath) for msg in messages: mbox.add(msg.full) mbox.close() content = StringIO() zipped_content = gzip.GzipFile(fileobj=content) with gzip.GzipFile(fileobj=content, mode="wb") as zipped_content: with open(mboxfilepath, "rb") as mboxfile: zipped_content.write(mboxfile.read()) response = HttpResponse(content.getvalue()) content.close() response['Content-Type'] = "application/mbox+gz" response['Content-Disposition'] = 'attachment; filename=%d-%s.txt.gz' \ % (year, month_name) response['Content-Length'] = len(response.content) os.remove(mboxfilepath) return response
def test_copy_filled_placeholder(self): """ If an existing title in the target language has plugins in a placeholder that placeholder is skipped """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() # create an empty title language root_page = Page.objects.on_site(site).get_home() create_title("de", "root page de", root_page) ph = root_page.placeholders.get(slot="body") add_plugin(ph, "TextPlugin", "de", body="Hello World") out = StringIO() management.call_command( 'cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out ) self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins) # one placeholder (with 7 plugins) is skipped, so the difference must be 6 self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins-6)
def test_copy_filled_placeholder_force_copy(self): """ If an existing title in the target language has plugins in a placeholder and the command is called with *force-copy*, the plugins are copied on top of the existing one """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() # create an empty title language root_page = Page.objects.on_site(site).get_home() create_title("de", "root page de", root_page) ph = root_page.placeholders.get(slot="body") add_plugin(ph, "TextPlugin", "de", body="Hello World") root_plugins = CMSPlugin.objects.filter(placeholder=ph) text_de_orig, _ = root_plugins.get( language='de', plugin_type='TextPlugin').get_plugin_instance() out = StringIO() management.call_command('cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', '--force', interactive=False, stdout=out) CMSPlugin.objects.filter(placeholder=root_page.placeholders.get( slot="body")) self.assertEqual( CMSPlugin.objects.filter(language='en').count(), number_start_plugins) # we have an existing plugin in one placeholder, so we have one more self.assertEqual( CMSPlugin.objects.filter(language='de').count(), number_start_plugins + 1)
def test_copy_filled_placeholder(self): """ If an existing title in the target language has plugins in a placeholder that placeholder is skipped """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() # create an empty title language root_page = Page.objects.on_site(site).get_home() create_title("de", "root page de", root_page) ph = root_page.placeholders.get(slot="body") add_plugin(ph, "TextPlugin", "de", body="Hello World") out = StringIO() command = cms.Command() command.stdout = out command.handle("copy-lang", "en", "de") self.assertEqual(CMSPlugin.objects.filter(language='en').count(), number_start_plugins) # one placeholder (with 7 plugins) is skipped, so the difference must be 6 self.assertEqual(CMSPlugin.objects.filter(language='de').count(), number_start_plugins-6)
def read_solarsimulator_plot_file(filename, position): """Read a datafile from a solarsimulator measurement and return the content of the voltage column and the selected current column. :param filename: full path to the solarsimulator measurement data file :param position: the position of the cell the currents of which should be read. :type filename: str :type position: str :return: all voltages in Volt, then all currents in Ampere :rtype: list of float, list of float :raises PlotError: if something wents wrong with interpreting the file (I/O, unparseble data) """ try: datafile_content = StringIO(open(filename).read()) except IOError: raise PlotError("Data file could not be read.") for line in datafile_content: if line.startswith("# Positions:"): positions = line.partition(":")[2].split() break else: positions = [] try: column = positions.index(position) + 1 except ValueError: raise PlotError("Cell position not found in the datafile.") datafile_content.seek(0) try: return numpy.loadtxt(datafile_content, usecols=(0, column), unpack=True) except ValueError: raise PlotError("Data file format was invalid.")
def test_copy_existing_title(self): """ Even if a title already exists the copy is successfull, the original title remains untouched """ site = 1 number_start_plugins = CMSPlugin.objects.all().count() # create an empty title language root_page = Page.objects.on_site(site).get_home() create_title("de", "root page de", root_page) out = StringIO() management.call_command('cms', 'copy', 'lang', '--from-lang=en', '--to-lang=de', interactive=False, stdout=out) pages = Page.objects.on_site(site).drafts() for page in pages: self.assertEqual(set((u'en', u'de')), set(page.get_languages())) # Original Title untouched self.assertEqual("root page de", Page.objects.on_site(site).get_home().get_title("de")) # Plugins still copied self.assertEqual(CMSPlugin.objects.all().count(), number_start_plugins * 2) self.assertEqual( CMSPlugin.objects.filter(language='en').count(), number_start_plugins) self.assertEqual( CMSPlugin.objects.filter(language='de').count(), number_start_plugins)
def _cache_store_large_data(cache, key, data, expiration, compress_large_data): # We store large data in the cache broken into chunks that are 1M in size. # To do this easily, we first pickle the data and compress it with zlib. # This gives us a string which can be chunked easily. These are then stored # individually in the cache as single-element lists (so the cache backend # doesn't try to convert binary data to utf8). The number of chunks needed # is stored in the cache under the unadorned key file = StringIO() pickler = pickle.Pickler(file) pickler.dump(data) data = file.getvalue() if compress_large_data: data = zlib.compress(data) i = 0 while len(data) > CACHE_CHUNK_SIZE: chunk = data[0:CACHE_CHUNK_SIZE] data = data[CACHE_CHUNK_SIZE:] cache.set(make_cache_key('%s-%d' % (key, i)), [chunk], expiration) i += 1 cache.set(make_cache_key('%s-%d' % (key, i)), [data], expiration) cache.set(make_cache_key(key), '%d' % (i + 1), expiration)
def test_uninstall_apphooks_with_apphook(self): create_page('Hello Title', "nav_playground.html", "en", apphook=APPHOOK) create_page('Hello 2', "nav_playground.html", "en", apphook=APPHOOK_NAMESPACE, apphook_namespace='any') self.assertEqual( Page.objects.filter(application_urls=APPHOOK).count(), 1) self.assertEqual( Page.objects.filter(application_urls=APPHOOK_NAMESPACE, application_namespace='any').count(), 1) command = cms.Command() out = StringIO() command.stdout = out command.handle("uninstall", "apphooks", APPHOOK, interactive=False) self.assertEqual(out.getvalue(), "1 'SampleApp' apphooks uninstalled\n") self.assertEqual( Page.objects.filter(application_urls=APPHOOK).count(), 0) out = StringIO() command.stdout = out command.handle("uninstall", "apphooks", APPHOOK_NAMESPACE, interactive=False) self.assertEqual(out.getvalue(), "1 'NamespacedApp' apphooks uninstalled\n") self.assertEqual( Page.objects.filter(application_urls=APPHOOK_NAMESPACE).count(), 0) self.assertEqual( Page.objects.filter(application_namespace='any').count(), 0)
def _http_get(service, url, *args, **kwargs): if http_status == 200: return payload, {} else: raise HTTPError(url, http_status, '', {}, StringIO(payload))
def test_copy_sites(self): """ Various checks here: * plugins are exactly doubled, half per site with no orphaned plugin * the bottom-most plugins in the nesting chain maintain the same position and the same content * the top-most plugin are of the same type """ site_1_pk = 1 site_2 = Site.objects.create(name='site 2') site_2_pk = site_2.pk phs = [] for page in Page.objects.on_site(site_1_pk).drafts(): phs.extend(page.placeholders.values_list('pk', flat=True)) number_start_plugins = CMSPlugin.objects.filter( placeholder__in=phs).count() out = StringIO() management.call_command('cms', 'copy', 'site', '--from-site=%s' % site_1_pk, '--to-site=%s' % site_2_pk, stdout=out) for page in Page.objects.on_site(site_1_pk).drafts(): page.publish('en') for page in Page.objects.on_site(site_2_pk).drafts(): page.publish('en') pages_1 = list(Page.objects.on_site(site_1_pk).drafts()) pages_2 = list(Page.objects.on_site(site_2_pk).drafts()) for index, page in enumerate(pages_1): self.assertEqual(page.get_title('en'), pages_2[index].get_title('en')) self.assertEqual(page.depth, pages_2[index].depth) phs_1 = [] phs_2 = [] for page in Page.objects.on_site(site_1_pk).drafts(): phs_1.extend(page.placeholders.values_list('pk', flat=True)) for page in Page.objects.on_site(site_2_pk).drafts(): phs_2.extend(page.placeholders.values_list('pk', flat=True)) # These asserts that no orphaned plugin exists self.assertEqual( CMSPlugin.objects.filter(placeholder__in=phs_1).count(), number_start_plugins) self.assertEqual( CMSPlugin.objects.filter(placeholder__in=phs_2).count(), number_start_plugins) root_page_1 = Page.objects.on_site(site_1_pk).get_home(site_1_pk) root_page_2 = Page.objects.on_site(site_2_pk).get_home(site_2_pk) root_plugins_1 = CMSPlugin.objects.filter( placeholder=root_page_1.placeholders.get(slot="body")) root_plugins_2 = CMSPlugin.objects.filter( placeholder=root_page_2.placeholders.get(slot="body")) first_plugin_1, _ = root_plugins_1.get( language='en', parent=None).get_plugin_instance() first_plugin_2, _ = root_plugins_2.get( language='en', parent=None).get_plugin_instance() self.assertEqual(first_plugin_1.plugin_type, first_plugin_2.plugin_type) link_1, _ = root_plugins_1.get( language='en', plugin_type='LinkPlugin').get_plugin_instance() link_2, _ = root_plugins_2.get( language='en', plugin_type='LinkPlugin').get_plugin_instance() self.assertEqual(link_1.url, link_2.url) self.assertEqual(link_1.get_position_in_placeholder(), link_2.get_position_in_placeholder())
def test_mysql_cache_migration_no_mysql_caches(self): err = StringIO() call_command('mysql_cache_migration', stderr=err) assert "No MySQLCache instances in CACHES" in err.getvalue()
def test_mysql_cache_migration_non_existent(self): out = StringIO() with pytest.raises(CommandError): call_command('mysql_cache_migration', 'nonexistent', stdout=out)
def test_mysql_socket(self): out = StringIO() call_command('dbparams', stdout=out, skip_checks=True) output = out.getvalue() assert output == "--socket=/etc/mydb.sock"
def __init__(self, std='out', buffer=None): self.std = std self.buffer = buffer or StringIO()