mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Import DFC product images
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "private_address_check"
|
||||
require "private_address_check/tcpsocket_ext"
|
||||
|
||||
class SuppliedProductBuilder < DfcBuilder
|
||||
def self.supplied_product(variant)
|
||||
id = urls.enterprise_supplied_product_url(
|
||||
@@ -66,7 +69,8 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
description: supplied_product.description,
|
||||
price: 0, # will be in DFC Offer
|
||||
supplier_id: supplier.id,
|
||||
primary_taxon_id: taxon(supplied_product).id
|
||||
primary_taxon_id: taxon(supplied_product).id,
|
||||
image: image(supplied_product),
|
||||
).tap do |product|
|
||||
QuantitativeValueBuilder.apply(supplied_product.quantity, product)
|
||||
product.ensure_standard_variant
|
||||
@@ -96,5 +100,20 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
Spree::Taxon.find_by(dfc_id:) || Spree::Taxon.first
|
||||
end
|
||||
|
||||
def self.image(supplied_product)
|
||||
url = URI.parse(supplied_product.image)
|
||||
filename = File.basename(supplied_product.image)
|
||||
|
||||
Spree::Image.new.tap do |image|
|
||||
PrivateAddressCheck.only_public_connections do
|
||||
image.attachment.attach(io: url.open, filename:)
|
||||
end
|
||||
end
|
||||
rescue StandardError
|
||||
# Any URL parsing or network error shouldn't impact the product import
|
||||
# at all. Maybe we'll add UX for error handling later.
|
||||
nil
|
||||
end
|
||||
|
||||
private_class_method :product_type, :taxon
|
||||
end
|
||||
|
||||
@@ -5,19 +5,22 @@ module DfcProvider
|
||||
attr_accessor :spree_product_id, :spree_product_uri, :image
|
||||
|
||||
def initialize(
|
||||
semantic_id, spree_product_id: nil, spree_product_uri: nil, image_url: nil, **properties
|
||||
semantic_id, spree_product_id: nil, spree_product_uri: nil,
|
||||
image: nil, image_url: nil, **properties
|
||||
)
|
||||
super(semantic_id, **properties)
|
||||
|
||||
self.spree_product_id = spree_product_id
|
||||
self.spree_product_uri = spree_product_uri
|
||||
self.image = image_url
|
||||
self.image = image || image_url
|
||||
|
||||
# This is now replaced by spree_product_uri, keeping it for backward compatibility
|
||||
register_ofn_property("spree_product_id")
|
||||
register_ofn_property("spree_product_uri")
|
||||
# Temporary solution, will be replaced by "dfc_b:image" in future version of the DFC connector
|
||||
register_ofn_property("image")
|
||||
registerSemanticProperty("dfc-b:image", &method(:image))
|
||||
.valueSetter = method("image=")
|
||||
end
|
||||
|
||||
def register_ofn_property(name)
|
||||
|
||||
@@ -86,7 +86,7 @@ RSpec.describe SuppliedProductBuilder do
|
||||
|
||||
describe ".import_product" do
|
||||
let(:supplied_product) do
|
||||
DataFoodConsortium::Connector::SuppliedProduct.new(
|
||||
DfcProvider::SuppliedProduct.new(
|
||||
"https://example.net/tomato",
|
||||
name: "Tomato",
|
||||
description: "Awesome tomato",
|
||||
@@ -95,6 +95,7 @@ RSpec.describe SuppliedProductBuilder do
|
||||
value: 2,
|
||||
),
|
||||
productType: product_type,
|
||||
image: "https://cd.net/tomato.png?v=5",
|
||||
)
|
||||
end
|
||||
let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE }
|
||||
@@ -106,6 +107,13 @@ RSpec.describe SuppliedProductBuilder do
|
||||
)
|
||||
}
|
||||
|
||||
before do
|
||||
stub_request(:get, "https://cd.net/tomato.png?v=5").to_return(
|
||||
status: 200,
|
||||
body: black_logo_path.read,
|
||||
)
|
||||
end
|
||||
|
||||
it "creates a new Spree::Product" do
|
||||
product = builder.import_product(supplied_product, supplier)
|
||||
|
||||
@@ -113,6 +121,9 @@ RSpec.describe SuppliedProductBuilder do
|
||||
expect(product.name).to eq("Tomato")
|
||||
expect(product.description).to eq("Awesome tomato")
|
||||
expect(product.variant_unit).to eq("weight")
|
||||
expect(product.image).to be_present
|
||||
expect(product.image.attachment).to be_attached
|
||||
expect(product.image.url(:product)).to match /^http.*tomato\.png/
|
||||
end
|
||||
|
||||
describe "taxon" do
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -63,5 +63,10 @@ RSpec.describe "DFC Product Import" do
|
||||
}
|
||||
|
||||
expect(page).to have_content "Importing a DFC product catalog"
|
||||
|
||||
product = Spree::Product.last
|
||||
|
||||
expect(product.variants[0].semantic_links).to be_present
|
||||
expect(product.image).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user