コード例 #1
0
    def prepare(self):
        self.data = self._transform_request_file()
        if self.data is None:
            return False

        context = self.importer_cls.get_importer_context(
            self.request,
            shop=get_shop(self.request),
            language=self.lang,
        )
        self.importer = self.importer_cls(self.data, context)
        self.importer.process_data()

        if self.request.method == "POST":
            # check if mapping was done
            for field in self.importer.unmatched_fields:
                key = "remap[%s]" % field
                vals = self.request.POST.getlist(key)
                if len(vals):
                    self.importer.manually_match(field, vals[0])
            self.importer.do_remap()

        self.settings_form = ImportSettingsForm(
            data=self.request.POST if self.request.POST else None)
        if self.settings_form.is_bound:
            self.settings_form.is_valid()
        return True
コード例 #2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        file_importer = FileImporter(
            importer=self.request.GET["importer"],
            import_mode=ImportMode.CREATE_UPDATE,
            file_name=self.request.GET["n"],
            language=self.request.GET.get("lang"),
            shop_id=get_shop(self.request),
            supplier_id=get_supplier(self.request),
        )
        file_importer.prepare()

        settings_form = ImportSettingsForm(
            data=self.request.POST if self.request.POST else None)
        if settings_form.is_bound:
            settings_form.is_valid()

        context["data"] = file_importer.data
        context["importer"] = file_importer.importer
        context["form"] = settings_form
        context[
            "model_fields"] = file_importer.importer.get_fields_for_mapping()
        context["visible_rows"] = file_importer.data.rows[1:5]
        return context
コード例 #3
0
ファイル: import_views.py プロジェクト: ruqaiya/shuup
    def prepare(self):
        self.data = self._transform_request_file()
        if self.data is None:
            return False

        self.importer = self.importer_cls(self.data, get_shop(self.request), self.lang)
        self.importer.process_data()

        if self.request.method == "POST":
            # check if mapping was done
            for field in self.importer.unmatched_fields:
                key = "remap[%s]" % field
                vals = self.request.POST.getlist(key)
                if len(vals):
                    self.importer.manually_match(field, vals[0])
            self.importer.do_remap()

        self.settings_form = ImportSettingsForm(data=self.request.POST if self.request.POST else None)
        if self.settings_form.is_bound:
            self.settings_form.is_valid()
        return True
コード例 #4
0
class ImportProcessView(TemplateView):
    template_name = "shuup/importer/admin/import_process.jinja"
    importer = None

    def dispatch(self, request, *args, **kwargs):
        self.importer_cls = get_importer(request.GET.get("importer"))
        self.model_str = request.GET.get("importer")
        self.lang = request.GET.get("lang")
        self.supplier = get_supplier(request)
        return super(ImportProcessView, self).dispatch(request, *args,
                                                       **kwargs)

    def _transform_request_file(self):
        try:
            filename = get_import_file_path(self.request.GET.get("n"))
            if not os.path.isfile(filename):
                raise ValueError(
                    _("%s is not a file.") % self.request.GET.get("n"))
        except:
            raise Problem(_("File is missing."))
        try:
            mode = "xls"
            if filename.endswith("xlsx"):
                mode = "xlsx"
            if filename.endswith("csv"):
                mode = "csv"
            if self.importer_cls.custom_file_transformer:
                return self.importer_cls.transform_file(mode, filename)
            return transform_file(mode, filename)
        except (Exception, RuntimeError) as e:
            messages.error(self.request, e)

    def prepare(self):
        self.data = self._transform_request_file()
        if self.data is None:
            return False

        context = self.importer_cls.get_importer_context(
            self.request,
            shop=get_shop(self.request),
            language=self.lang,
        )
        self.importer = self.importer_cls(self.data, context)
        self.importer.process_data()

        if self.request.method == "POST":
            # check if mapping was done
            for field in self.importer.unmatched_fields:
                key = "remap[%s]" % field
                vals = self.request.POST.getlist(key)
                if len(vals):
                    self.importer.manually_match(field, vals[0])
            self.importer.do_remap()

        self.settings_form = ImportSettingsForm(
            data=self.request.POST if self.request.POST else None)
        if self.settings_form.is_bound:
            self.settings_form.is_valid()
        return True

    def post(self, request, *args, **kwargs):
        prepared = self.prepare()
        if not prepared:
            return redirect(reverse("shuup_admin:importer.import"))
        try:
            with atomic():
                self.importer.do_import(
                    self.settings_form.cleaned_data["import_mode"])
        except Exception:
            logger.exception("Error! Failed to import data.")
            messages.error(request, _("Failed to import the file."))
            return redirect(reverse("shuup_admin:importer.import"))

        self.template_name = "shuup/importer/admin/import_process_complete.jinja"
        return self.render_to_response(self.get_context_data(**kwargs))

    def get_context_data(self, **kwargs):
        context = super(ImportProcessView, self).get_context_data(**kwargs)
        context["data"] = self.data
        context["importer"] = self.importer
        context["form"] = self.settings_form
        context["model_fields"] = self.importer.get_fields_for_mapping()
        context["visible_rows"] = self.data.rows[1:5]
        return context

    def get(self, request, *args, **kwargs):
        prepared = self.prepare()
        if not prepared:
            return redirect(reverse("shuup_admin:importer.import"))
        return self.render_to_response(self.get_context_data(**kwargs))
コード例 #5
0
ファイル: import_views.py プロジェクト: ruqaiya/shuup
class ImportProcessView(TemplateView):
    template_name = "shuup/importer/admin/import_process.jinja"
    importer = None

    def dispatch(self, request, *args, **kwargs):
        self.importer_cls = get_importer(request.GET.get("importer"))
        self.model_str = request.GET.get("importer")
        self.lang = request.GET.get("lang")
        return super(ImportProcessView, self).dispatch(request, *args, **kwargs)

    def _transform_request_file(self):
        try:
            filename = get_import_file_path(self.request.GET.get("n"))
            if not os.path.isfile(filename):
                raise ValueError(_("%s is not a file") % self.request.GET.get("n"))
        except:
            raise Problem(_("File missing."))
        try:
            mode = "xls"
            if filename.endswith("xlsx"):
                mode = "xlsx"
            if filename.endswith("csv"):
                mode = "csv"
            return transform_file(mode, filename)
        except (Exception, RuntimeError) as e:
            messages.error(self.request, e)

    def prepare(self):
        self.data = self._transform_request_file()
        if self.data is None:
            return False

        self.importer = self.importer_cls(self.data, get_shop(self.request), self.lang)
        self.importer.process_data()

        if self.request.method == "POST":
            # check if mapping was done
            for field in self.importer.unmatched_fields:
                key = "remap[%s]" % field
                vals = self.request.POST.getlist(key)
                if len(vals):
                    self.importer.manually_match(field, vals[0])
            self.importer.do_remap()

        self.settings_form = ImportSettingsForm(data=self.request.POST if self.request.POST else None)
        if self.settings_form.is_bound:
            self.settings_form.is_valid()
        return True

    def post(self, request, *args, **kwargs):
        prepared = self.prepare()
        if not prepared:
            return redirect(reverse("shuup_admin:importer.import"))
        try:
            with atomic():
                self.importer.do_import(self.settings_form.cleaned_data["import_mode"])
        except Exception:
            logger.exception("Failed to import data")
            messages.error(request, _("Failed to import the file."))
            return redirect(reverse("shuup_admin:importer.import"))

        self.template_name = "shuup/importer/admin/import_process_complete.jinja"
        return self.render_to_response(self.get_context_data(**kwargs))

    def get_context_data(self, **kwargs):
        context = super(ImportProcessView, self).get_context_data(**kwargs)
        context["data"] = self.data
        context["importer"] = self.importer
        context["form"] = self.settings_form
        context["model_fields"] = self.importer.get_fields_for_mapping()
        context["visible_rows"] = self.data.rows[1:5]
        return context

    def get(self, request, *args, **kwargs):
        prepared = self.prepare()
        if not prepared:
            return redirect(reverse("shuup_admin:importer.import"))
        return self.render_to_response(self.get_context_data(**kwargs))