Esempio n. 1
0
    def handle_stocks(self, fields, sess):
        """
        Handle stocks for product

        If stock qty has been given, expect that a supplier with stock management must be available.
        """
        row = sess.row
        supplier = row.get("supplier")
        if not supplier:
            raise ImporterError(
                _("Please add supplier to row before importing stock quantities."
                  ))
        # shuup only has 1 supplier support now so get first
        obj = sess.importer.resolve_object(Supplier, supplier)
        if not obj:
            raise ImporterError(
                _("No supplier found, please check the supplier exists."))
        if not obj.stock_managed:
            raise ImporterError(
                _("This supplier doesn't handle stocks, please set Stock Managed on."
                  ))

        for qty_field in self.aliases["qty"]:
            val = row.get(qty_field, None)
            if val is not None:
                StockAdjustment(product=sess.instance, delta=val)
                break
Esempio n. 2
0
    def handle_stocks(self, fields, sess):
        """
        Handle stocks for product

        If stock qty has been given, expect that a supplier with stock management must be available.
        """
        # convert all keys to lowercase
        row = {k.lower(): v for k, v in sess.row.items()}

        # check if row even has these fields we are requiring
        field_found = False
        for qty_field in self.aliases["qty"]:
            if qty_field in row:
                field_found = True
                break

        if not field_found:  # no need to process this as qty was not available
            return

        supplier_id = row.get("supplier")
        if not supplier_id:
            raise ImporterError(_("Please add supplier to row before importing stock quantities."))
        # shuup only has 1 supplier support now so get first
        supplier = sess.importer.resolve_object(Supplier, supplier_id)
        if not supplier:
            raise ImporterError(_("No supplier found, please check the supplier exists."))
        if not supplier.stock_managed:
            raise ImporterError(_("This supplier doesn't handle stocks, please set Stock Managed on."))

        for qty_field in self.aliases["qty"]:
            qty_field = qty_field.lower()
            val = row.get(qty_field, None)
            if val is not None:
                supplier.adjust_stock(sess.instance.pk, val)
                break
Esempio n. 3
0
    def import_file(self):
        if self.data is None:
            raise ImporterError(_("The file doesn't contain data."))

        try:
            self.importer.do_import(self.import_mode)
        except Exception as exc:
            LOGGER.exception("Failed to run importer.")
            raise ImporterError(
                _("Failed to import data: {}.").format(str(exc)))
Esempio n. 4
0
    def prepare(self):
        self.data = self._transform_request_file()

        if self.data is None:
            raise ImporterError(_("The file doesn't contain data."))

        context = self.importer_cls.get_importer_context(
            request=None,
            shop=self.shop,
            language=self.language,
            supplier=self.supplier,
            user=self.user)
        self.importer = self.importer_cls(self.data, context)
        self.importer.process_data()

        # check if mapping was done
        if self.mapping:
            for field in self.importer.unmatched_fields:
                values = self.mapping.get(field)
                if len(values):
                    self.importer.manually_match(field, values[0])

                self.importer.do_remap()

        return True
Esempio n. 5
0
    def save(self):
        if hasattr(self.instance, "shop"):
            self.instance.shop = self.shop
        try:
            self.instance.save()
        except Exception as e:
            self.importer.other_log_messages.append(
                _("Row import failed (%s).") % e.message)
            raise ImporterError("Row import failed (cannot save).",
                                code="save-failed")

        self._handle_deferred()
        self._handle_postsave_objects()
Esempio n. 6
0
    def _transform_request_file(self):
        try:
            filename = get_import_file_path(self.file_name)
            if not os.path.isfile(filename):
                raise ImporterError(
                    _("{file_name} is not a valid file.").format(
                        file_name=self.file_name))
        except Exception:
            raise ImporterError(_("The 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:
            raise ImporterError(str(e))
Esempio n. 7
0
    def save(self):
        try:
            self.instance._meta.get_field('shop')
            self.instance.shop = self.shop
        except FieldDoesNotExist:
            pass

        try:
            self.instance.save()
        except Exception as e:
            msg = e.message if hasattr(e, "message") else e
            self.importer.other_log_messages.append(_("Row import failed (%s).") % msg)
            raise ImporterError("Row import failed (cannot save).", code="save-failed")

        self._handle_deferred()
        self._handle_postsave_objects()