def import_setup(todo_setup): app_path = Path(__file__).resolve().parent.parent filepath = Path(app_path, "tests/data/csv_import_data.csv") with filepath.open(mode="r", encoding="utf-8-sig") as fileobj: importer = CSVImporter() results = importer.upsert(fileobj, as_string_obj=True) assert results return {"results": results}
def import_csv(request) -> HttpResponse: """Import a specifically formatted CSV into stored tasks. """ ctx = {} if request.method == "POST": filepath = request.FILES.get('csvfile') importer = CSVImporter() results = importer.upsert(filepath) ctx["results"] = results return render(request, "todo/import_csv.html", context=ctx)
def handle(self, *args: Any, **options: Any) -> None: # Need a file to proceed # Нужен файл для продолжения if not options.get("file"): print("Sorry, we need a filename to work from.") sys.exit(1) filepath = Path(options["file"]) if not filepath.exists(): print(f"Sorry, couldn't find file: {filepath}") sys.exit(1) # Encoding "utf-8-sig" means "ignore byte order mark (BOM), which Excel inserts when saving CSVs." # Кодировка "utf-8-sig" означает "игнорировать метку порядка байтов (BOM), которую Excel вставляет при сохранении CSV". with filepath.open(mode="r", encoding="utf-8-sig") as fileobj: importer = CSVImporter() results = importer.upsert(fileobj, as_string_obj=True) # Report successes, failures and summaries # Сообщать об успехах, неудачах и резюмировать print() if results["upserts"]: for upsert_msg in results["upserts"]: print(upsert_msg) # Stored errors has the form: # Сохраненные ошибки имеют вид: # self.errors = [{3: ["Incorrect foo", "Non-existent bar"]}, {7: [...]}] if results["errors"]: for error_dict in results["errors"]: for k, error_list in error_dict.items(): print(f"\nSkipped CSV row {k}:") for msg in error_list: print(f"- {msg}") print() if results["summaries"]: for summary_msg in results["summaries"]: print(summary_msg)
def import_csv(request) -> HttpResponse: """Import a specifically formatted CSV into stored tasks. """ ctx = {"results": None} if request.method == "POST": filepath = request.FILES.get("csvfile") if not filepath: messages.error(request, "You must supply a CSV file to import.") return redirect(reverse("todo:import_csv")) importer = CSVImporter() results = importer.upsert(filepath) if results: ctx["results"] = results else: messages.error(request, "Could not parse provided CSV file.") return redirect(reverse("todo:import_csv")) return render(request, "todo/import_csv.html", context=ctx)
def handle(self, *args: Any, **options: Any) -> None: # Need a file to proceed if not options.get("file"): print("Sorry, we need a filename to work from.") sys.exit(1) filepath = Path(options["file"]) if not filepath.exists(): print(f"Sorry, couldn't find file: {filepath}") sys.exit(1) # Encoding "utf-8-sig" means "ignore byte order mark (BOM), which Excel inserts when saving CSVs." with filepath.open(mode="r", encoding="utf-8-sig") as fileobj: importer = CSVImporter() results = importer.upsert(fileobj, as_string_obj=True) # Report successes, failures and summaries print() if results["upserts"]: for upsert_msg in results["upserts"]: print(upsert_msg) # Stored errors has the form: # self.errors = [{3: ["Incorrect foo", "Non-existent bar"]}, {7: [...]}] if results["errors"]: for error_dict in results["errors"]: for k, error_list in error_dict.items(): print(f"\nSkipped CSV row {k}:") for msg in error_list: print(f"- {msg}") print() if results["summaries"]: for summary_msg in results["summaries"]: print(summary_msg)