def test_import_add_and_obsolete_units(project0_nongnu, import_tps, site_users): from pytest_pootle.factories import StoreDBFactory, UnitDBFactory tp = import_tps user = site_users["user"] store = StoreDBFactory(translation_project=tp) unit = UnitDBFactory(store=store, state=TRANSLATED) filestore = create_store( store.pootle_path, "0", [(unit.source_f + " REPLACED", unit.target_f + " REPLACED", False)]) import_file(SimpleUploadedFile("import_add_and_obsolete.po", str(filestore), "text/x-gettext-translation"), user) allow_add_and_obsolete = ((tp.project.checkstyle == 'terminology' or tp.is_template_project) and check_user_permission(user, 'administrate', tp.directory)) if allow_add_and_obsolete: assert Unit.objects.filter(store=store, state=OBSOLETE).count() == 1 assert store.units.filter(state=TRANSLATED).count() == 1 unit_source = store.units[0].unit_source assert unit_source.created_with == SubmissionTypes.UPLOAD assert unit_source.created_by == user assert store.units[0].change.changed_with == SubmissionTypes.UPLOAD assert store.units[0].change.submitted_by == user assert Unit.objects.filter(store=store).count() == 2 else: assert store.units.all().count() == 1
def test_import_to_empty(project0_nongnu, import_tps, site_users): from pytest_pootle.factories import StoreDBFactory tp = import_tps user = site_users["user"] store = StoreDBFactory(translation_project=tp, name="import_to_empty.po") filestore = create_store(store.pootle_path, "0", [("Unit Source", "Unit Target", False)]) import_file(SimpleUploadedFile(store.name, str(filestore), "text/x-gettext-translation"), user) allow_add_and_obsolete = ((tp.project.checkstyle == 'terminology' or tp.is_template_project) and check_user_permission(user, 'administrate', tp.directory)) if allow_add_and_obsolete: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 1 unit_source = store.units[0].unit_source assert unit_source.created_with == SubmissionTypes.UPLOAD assert unit_source.created_by == user assert store.units[0].change.changed_with == SubmissionTypes.UPLOAD assert store.units[0].change.submitted_by == user else: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 0
def handle(self, *args, **options): user = None if options["user"] is not None: User = get_user_model() try: user = User.objects.get(username=options["user"]) except User.DoesNotExist: raise CommandError("Unrecognised user: %s" % options["user"]) for filename in args: if is_zipfile(filename): with ZipFile(filename, "r") as zf: for path in zf.namelist(): with zf.open(path, "r") as f: if path.endswith("/"): # is a directory continue try: import_file(f, user=user) except Exception as e: self.stderr.write("Warning: %s" % (e)) else: with open(filename, "r") as f: try: import_file(f, user=user) except Exception as e: raise CommandError(e)
def _import_file(file_name, file_dir=TEST_PO_DIR, content_type="text/x-gettext-translation", user=None): with open(os.path.join(file_dir, file_name), "r") as f: import_file( SimpleUploadedFile(file_name, f.read(), content_type), user=user)
def _import_file(file_name, file_dir=TEST_PO_DIR, content_type="text/x-gettext-translation", user=None): with open(os.path.join(file_dir, file_name), "r") as f: import_file(SimpleUploadedFile(file_name, f.read(), content_type), user=user)
def _import_file(file_name, file_dir=None, content_type="text/x-gettext-translation", user=None): if not file_dir: file_dir = os.path.join(os.path.dirname(pytest_pootle.__file__), "data/po/tutorial/en") with open(os.path.join(file_dir, file_name), "r") as f: import_file(SimpleUploadedFile(file_name, f.read(), content_type), user=user)
def _import_file(file_name, file_dir=None, content_type="text/x-gettext-translation", user=None): if not file_dir: file_dir = os.path.join( os.path.dirname(pytest_pootle.__file__), "data/po/tutorial/en") with open(os.path.join(file_dir, file_name), "r") as f: import_file( SimpleUploadedFile(file_name, f.read(), content_type), user=user)
def test_import_new_file(project0_nongnu, import_tps, site_users): tp = import_tps user = site_users["user"] store_pootle_path = tp.pootle_path + "import_new_file.po" filestore = create_store(store_pootle_path, "0", [("Unit Source", "Unit Target", False)]) # New store can't be created via current import command. This test will # need to be adjusted if we allow to create new stores via import command. from import_export.exceptions import FileImportError with pytest.raises(FileImportError): import_file(SimpleUploadedFile("import_new_file.po", str(filestore), "text/x-gettext-translation"), user)
def handle(self, *args, **options): for filename in args: if is_zipfile(filename): with ZipFile(filename, "r") as zf: for path in zf.namelist(): with zf.open(path, "r") as f: if path.endswith("/"): # is a directory continue try: import_file(f) except Exception as e: self.stderr.write("Warning: %s" % (e)) else: with open(filename, "r") as f: try: import_file(f) except Exception as e: raise CommandError(e)
def test_import_to_empty(import_tps, site_users): from pytest_pootle.factories import StoreFactory tp = import_tps user = site_users["user"] store = StoreFactory(translation_project=tp, name="import_to_empty.po") filestore = create_store(store.pootle_path, "0", [("Unit Source", "Unit Target")]) import_file( SimpleUploadedFile(store.name, str(filestore), "text/x-gettext-translation"), user) allow_add_and_obsolete = ( (tp.project.checkstyle == 'terminology' or tp.is_template_project) and check_user_permission(user, 'administrate', tp.directory)) if allow_add_and_obsolete: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 1 else: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 0
def test_import_to_empty(import_tps, site_users): from pytest_pootle.factories import StoreFactory tp = import_tps user = site_users["user"] store = StoreFactory(translation_project=tp, name="import_to_empty.po") filestore = create_store(store.pootle_path, "0", [("Unit Source", "Unit Target")]) import_file(SimpleUploadedFile(store.name, str(filestore), "text/x-gettext-translation"), user) allow_add_and_obsolete = ((tp.project.checkstyle == 'terminology' or tp.is_template_project) and check_user_permission(user, 'administrate', tp.directory)) if allow_add_and_obsolete: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 1 else: assert tp.stores.get(pootle_path=store.pootle_path).units.count() == 0
def handle(self, **options): user = None if options["user"] is not None: User = get_user_model() try: user = User.objects.get(username=options["user"]) self.stdout.write( 'User %s will be set as author of the import.' % user.username) except User.DoesNotExist: raise CommandError("Unrecognised user: %s" % options["user"]) start = datetime.datetime.now() for filename in options['file']: self.stdout.write('Importing %s...' % filename) if not os.path.isfile(filename): raise CommandError("No such file '%s'" % filename) if is_zipfile(filename): with ZipFile(filename, "r") as zf: for path in zf.namelist(): with zf.open(path, "r") as f: if path.endswith("/"): # is a directory continue try: import_file(f, user=user) except Exception as e: self.stderr.write("Warning: %s" % (e)) else: with open(filename, "r") as f: try: import_file(f, user=user) except Exception as e: raise CommandError(e) end = datetime.datetime.now() self.stdout.write('All done in %s.' % (end - start))