def do_transfers_extras(): # retrieves the reference to the api object that is going # to be used for the updating of prices operation api = util.get_api() # tries to retrieve the origin value from the provided set # of fields and in case it's not defined re-renders the template origin = quorum.get_field("origin", None, cast = int) if not origin: return flask.render_template( "extra/transfers.html.tpl", link = "extras", error = "No origin defined" ) # tries to retrieve the transfers file from the current # form in case it's not available renders the current # template with an error message transfers_file = quorum.get_field("transfers_file", None) if transfers_file == None or not transfers_file.filename: return flask.render_template( "extra/transfers.html.tpl", link = "extras", error = "No file defined" ) # creates a temporary file path for the storage of the file # and then saves it into that directory fd, file_path = tempfile.mkstemp() transfers_file.save(file_path) # creates the file object that is going to be used in the # reading of the csv file (underlying object) file = open(file_path, "rb") try: data = file.read() finally: file.close() # constructs the bytes based buffer object from the data that # has just been loaded from the file buffer = quorum.legacy.BytesIO(data) # creates the maps that are going to be used to cache the # resolution processes for both the stores and the merchandise stores_map = dict() merchandise_map = dict() # creates the map that is going to hold the complete state # to be used in the process of the various transfers (context) state = dict() def get_transfer(): return state.get("transfer", None) def new_transfer(target_id, workflow_state = 6): flush_transfer() transfer = dict( origin = dict( object_id = origin ), destination = dict( object_id = target_id ), transfer_lines = [], _parameters = dict( target_workflow_state = workflow_state ) ) state["transfer"] = transfer return transfer def flush_transfer(): transfer = get_transfer() state["transfer"] = None if not transfer: return payload = dict(transfer = transfer) transfer = api.create_transfer(payload) transfer_id = transfer["object_id"] quorum.debug( "Created stock transfer '%d'" % transfer_id ) return transfer def add_transfer_line(merchandise_id, quantity = 1): transfer = get_transfer() if not transfer: raise quorum.OperationalError( "No transfer in context" ) lines = transfer["transfer_lines"] line = dict( quantity = quantity, merchandise = dict( object_id = merchandise_id ) ) lines.append(line) def get_store_id(store_code): object_id = stores_map.get(store_code, None) if object_id: return object_id kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "store_code:equals:%s" % store_code ] } try: stores = api.list_stores(**kwargs) except: stores = [] if stores: object_id = stores[0]["object_id"] stores_map[store_code] = object_id return object_id def get_merchandise_id(company_product_code): # tries to retrieve the object id of the merchandise from the # cache and in case it succeeds returns it immediately object_id = merchandise_map.get(company_product_code, None) if object_id: return object_id # creates the map containing the (filter) keyword arguments that # are going to be send to the list merchandise operation kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "company_product_code:equals:%s" % company_product_code ] } # runs the list merchandise operation in order to try to find a # merchandise entity for the requested (unique) product code in # case there's at least one merchandise its object id is used try: merchandise = api.list_merchandise(**kwargs) except: merchandise = [] if merchandise: object_id = merchandise[0]["object_id"] # updates the (cache) map for the merchandise with the reference # new object id to company product code reference and then returns # the object id of the merchandise to the caller method merchandise_map[company_product_code] = object_id return object_id def callback(line, header = None): code, quantity, _date, _time = line[:4] code = code.strip() quantity = quantity.strip() quantity = int(quantity) is_store = len(code) < 4 if is_store: store_id = get_store_id(code) else: merchandise_id = get_merchandise_id(code) if is_store: if store_id: new_transfer(store_id) else: flush_transfer() elif merchandise_id: try: add_transfer_line( merchandise_id, quantity = quantity ) except: pass try: # start the csv import operation that is going to import the # various lines of the csv in the buffer and for each of them # call the function passed as callback util.csv_import(buffer, callback, delimiter = ";") flush_transfer() finally: # closes the temporary file descriptor and removes the temporary # file (avoiding any memory leaks) os.close(fd) os.remove(file_path) # redirects the user back to the transfers list page with a success # message indicating that everything went ok return flask.redirect( flask.url_for( "transfers_extras", message = "Transfers file processed with success" ) )
def do_transfers_extras(): # retrieves the reference to the API object that is going # to be used for the updating of prices operation api = util.get_api() # tries to retrieve the origin value from the provided set # of fields and in case it's not defined re-renders the template origin = quorum.get_field("origin", None, cast = int) if not origin: return flask.render_template( "extra/transfers.html.tpl", link = "extras", error = "No origin defined" ) # tries to retrieve the transfers file from the current # form in case it's not available renders the current # template with an error message transfers_file = quorum.get_field("transfers_file", None) if transfers_file == None or not transfers_file.filename: return flask.render_template( "extra/transfers.html.tpl", link = "extras", error = "No file defined" ) # creates a temporary file path for the storage of the file # and then saves it into that directory fd, file_path = tempfile.mkstemp() transfers_file.save(file_path) # creates the file object that is going to be used in the # reading of the CSV file (underlying object) file = open(file_path, "rb") try: data = file.read() finally: file.close() # constructs the bytes based buffer object from the data that # has just been loaded from the file buffer = quorum.legacy.BytesIO(data) # creates the maps that are going to be used to cache the # resolution processes for both the stores and the merchandise stores_map = dict() merchandise_map = dict() # creates the map that is going to hold the complete state # to be used in the process of the various transfers (context) state = dict() def get_transfer(): return state.get("transfer", None) def new_transfer(target_id, workflow_state = 6): flush_transfer() transfer = dict( origin = dict( object_id = origin ), destination = dict( object_id = target_id ), transfer_lines = [], _parameters = dict( target_workflow_state = workflow_state ) ) state["transfer"] = transfer return transfer def flush_transfer(): transfer = get_transfer() state["transfer"] = None if not transfer: return payload = dict(transfer = transfer) transfer = api.create_transfer(payload) transfer_id = transfer["object_id"] quorum.debug( "Created stock transfer '%d'" % transfer_id ) return transfer def add_transfer_line(merchandise_id, quantity = 1): transfer = get_transfer() if not transfer: raise quorum.OperationalError( "No transfer in context" ) lines = transfer["transfer_lines"] line = dict( quantity = quantity, merchandise = dict( object_id = merchandise_id ) ) lines.append(line) def get_store_id(store_code): object_id = stores_map.get(store_code, None) if object_id: return object_id kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "store_code:equals:%s" % store_code ] } try: stores = api.list_stores(**kwargs) except Exception: stores = [] if stores: object_id = stores[0]["object_id"] stores_map[store_code] = object_id return object_id def get_merchandise_id(company_product_code): # tries to retrieve the object id of the merchandise from the # cache and in case it succeeds returns it immediately object_id = merchandise_map.get(company_product_code, None) if object_id: return object_id # creates the map containing the (filter) keyword arguments that # are going to be send to the list merchandise operation kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "company_product_code:equals:%s" % company_product_code ] } # runs the list merchandise operation in order to try to find a # merchandise entity for the requested (unique) product code in # case there's at least one merchandise its object id is used try: merchandise = api.list_merchandise(**kwargs) except Exception: merchandise = [] if merchandise: object_id = merchandise[0]["object_id"] # updates the (cache) map for the merchandise with the reference # new object id to company product code reference and then returns # the object id of the merchandise to the caller method merchandise_map[company_product_code] = object_id return object_id def callback(line, header = None): code, quantity, _date, _time = line[:4] code = code.strip() quantity = quantity.strip() quantity = int(quantity) is_store = len(code) < 4 if is_store: store_id = get_store_id(code) else: merchandise_id = get_merchandise_id(code) if is_store: if store_id: new_transfer(store_id) else: flush_transfer() elif merchandise_id: try: add_transfer_line( merchandise_id, quantity = quantity ) except Exception: pass try: # start the CSV import operation that is going to import the # various lines of the CSV in the buffer and for each of them # call the function passed as callback util.csv_import(buffer, callback, delimiter = ";") flush_transfer() finally: # closes the temporary file descriptor and removes the temporary # file (avoiding any memory leaks) os.close(fd) os.remove(file_path) # redirects the user back to the transfers list page with a success # message indicating that everything went ok return flask.redirect( flask.url_for( "transfers_extras", message = "Transfers file processed with success" ) )
def do_metadata_extras(): # retrieves the reference to the api object that is going # to be used for the updating of prices operation api = util.get_api() # tries to retrieve the metadata file from the current # form in case it's not available renders the current # template with an error message metadata_file = quorum.get_field("metadata_file", None) if metadata_file == None or not metadata_file.filename: return flask.render_template( "extra/metadata.html.tpl", link = "extras", error = "No file defined" ) # retrieves the value of the custom field that control if # the importing will be performed using a dynamic approach # meaning that no static values will be retrieved and instead # the header will be used for dynamic retrieval custom = quorum.get_field("custom", False, cast = bool) # check if the csv file to uploaded is separated by the comma # character or if instead it used the semicolon comma = quorum.get_field("comma", False, cast = bool) # creates a temporary file path for the storage of the file # and then saves it into that directory fd, file_path = tempfile.mkstemp() metadata_file.save(file_path) # creates the file object that is going to be used in the # reading of the csv file (underlying object) file = open(file_path, "rb") try: data = file.read() finally: file.close() # constructs the bytes based buffer object from the data that # has just been loaded from the file buffer = quorum.legacy.BytesIO(data) def callback(line, header = None): if custom: # creates a zip of tuples with the header to line value association # and uses them to build a proper dictionary zipped = zip(header, line) update = dict(zipped) # iterates over the complete set of items in the map of values # and update the update map with the sanitized value for name, value in update.items(): update[name] = value.strip() # tries to retrieve the base identifier of the entity # this value is going to be used as the basis for identification base = update.pop("code", None) base = update.pop("company_product_code", base) base = update.pop("object_id", base) base = update.pop("base", base) # tries to retrieve some of the base entity values # if their found they are properly poped out name = update.pop("name", None) description = update.pop("description", None) else: # unpacks the current "metadata" line into its components as # expected by the specification base,\ name,\ _retail_price,\ characteristics,\ material,\ category,\ collection,\ brand,\ description,\ order = line[:10] # normalizes the various values that have been extracted from the line # so they are properly represented for importing characteristics = [value.strip() for value in characteristics.split(";") if value.strip()] material = [value.strip() for value in material.split(";") if value.strip()] category = [value.strip() for value in category.split(";") if value.strip()] collection = [value.strip() for value in collection.split(";") if value.strip()] brand = brand or None order = order or None # verifies and strips the various possible string values so that they # represent a valid not trailed value if name: name = name.strip() if brand: brand = brand.strip() if description: description = description.strip() if order: order = int(order.strip()) # creates the update dictionary that is going to be used in the updating # of the "product" metadata update = dict( characteristics = characteristics, material = material, category = category, collection = collection, brand = brand, order = order ) # tries to "cast" the base value as an integer and in case # it's possible assumes that this value is the object identifier try: object_id = int(base) except: object_id = None # in case no object id was retrieved from the base name value # a secondary strategy is used, so that the merchandise database # is searched using the base string value as the company product code if not object_id: # creates the keyword arguments map so that the the merchandise # with the provided company product code is retrieved kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "company_product_code:equals:%s" % base ] } # runs the list merchandise operation in order to try to find a # merchandise entity for the requested (unique) product code in # case there's at least one merchandise its object id is used try: merchandise = api.list_merchandise(**kwargs) except: merchandise = [] if merchandise: object_id = merchandise[0]["object_id"] # in case no object id was retrieved must skip the current loop # with a proper information message (as expected) if not object_id: quorum.info("Skipping, could not resolve object id for '%s'" % base) return # prints a logging message about the updating of the metadata for # the entity with the current object id quorum.debug("Setting metadata for entity '%d'" % object_id) # retrieves the reference to the entity so that it's possible to # retrieve the currently defined metadata for it (to be updated) entity = api.get_entity(object_id) metadata = entity.get("metadata", {}) or {} # updates the metadata dictionary with the new values that are going # to be used for the updating of the entity, note that the previous # metadata values are leveraged and not overwritten with this strategy metadata.update(update) # creates the model structure to be updated and then runs the # proper execution of the metadata import model = dict(metadata = metadata) if name: model["name"] = name if description: model["description"] = description api.update_entity(object_id, payload = dict(root_entity = model)) try: # start the csv import operation that is going to import the # various lines of the csv in the buffer and for each of them # call the function passed as callback util.csv_import( buffer, callback, header = True, delimiter = "," if comma else ";", quoting = True ) finally: # closes the temporary file descriptor and removes the temporary # file (avoiding any memory leaks) os.close(fd) os.remove(file_path) # redirects the user back to the metadata list page with a success # message indicating that everything went ok return flask.redirect( flask.url_for( "metadata_extras", message = "Metadata file processed with success" ) )
def do_metadata_extras(): # retrieves the reference to the API object that is going # to be used for the updating of prices operation api = util.get_api() # tries to retrieve the metadata file from the current # form in case it's not available renders the current # template with an error message metadata_file = quorum.get_field("metadata_file", None) if metadata_file == None or not metadata_file.filename: return flask.render_template( "extra/metadata.html.tpl", link = "extras", error = "No file defined" ) # retrieves the value of the custom field that control if # the importing will be performed using a dynamic approach # meaning that no static values will be retrieved and instead # the header will be used for dynamic retrieval custom = quorum.get_field("custom", False, cast = bool) # check if the CSV file to uploaded is separated by the comma # character or if instead it used the semicolon comma = quorum.get_field("comma", False, cast = bool) # creates a temporary file path for the storage of the file # and then saves it into that directory fd, file_path = tempfile.mkstemp() metadata_file.save(file_path) # creates the file object that is going to be used in the # reading of the CSV file (underlying object) file = open(file_path, "rb") try: data = file.read() finally: file.close() # constructs the bytes based buffer object from the data that # has just been loaded from the file buffer = quorum.legacy.BytesIO(data) def callback(line, header = None): # in case the custom metadata mode is enabled then a special work # model is set where all of the columns are going to be used dynamically # for the update of the metadata map of the object if custom: # creates a zip of tuples with the header to line value association # and uses them to build a proper dictionary zipped = zip(header, line) update = dict(zipped) # iterates over the complete set of items in the map of values # and updates the update map with the sanitized value for name, value in update.items(): update[name] = value.strip() # tries to retrieve the base identifier of the entity # this value is going to be used as the basis for identification base = update.pop("code", None) base = update.pop("company_product_code", base) base = update.pop("object_id", base) base = update.pop("base", base) # tries to retrieve some of the base entity values # if their found they are properly popped out name = update.pop("name", None) description = update.pop("description", None) upc = update.pop("upc", None) ean = update.pop("ean", None) # otherwise this is a "normal" update and the "typical" metadata # fields are the one to be updated else: # unpacks the current "metadata" line into its components as # expected by the specification base,\ name,\ _retail_price,\ compare_price,\ discount,\ characteristics,\ material,\ category,\ collection,\ brand,\ season,\ gender,\ description,\ order,\ discountable,\ sku_field,\ upc,\ ean = line[:18] # verifies if the initials part of the CSV line exists and # if that's the case processes it properly if len(line) > 20: initials, initials_min, initials_max = line[18:21] else: initials, initials_min, initials_max = "", "", "" # normalizes the various values that have been extracted from the line # so they are properly represented for importing name = name or None compare_price = (compare_price and compare_price.strip()) or None discount = (discount and discount.strip()) or None characteristics = [value.strip() for value in characteristics.split(";") if value.strip()] material = [value.strip() for value in material.split(";") if value.strip()] category = [value.strip() for value in category.split(";") if value.strip()] collection = [value.strip() for value in collection.split(";") if value.strip()] brand = brand or None season = season or None gender = gender or None description = description or None order = (order and order.strip()) or None discountable = discountable or None sku_field = sku_field or None upc = upc or None ean = ean or None initials = initials or None initials_min = initials_min or None initials_max = initials_max or None # verifies and strips the various possible string values so that they # represent a valid not trailed value if name: name = name.strip() if compare_price: compare_price = float(compare_price) if brand: brand = brand.strip() if season: season = season.strip() if gender: gender = gender.strip() if description: description = description.strip() if order: order = int(order) if discountable: discountable = discountable == "1" if sku_field: sku_field = sku_field.strip() if discount: discount = float(discount) if upc: upc = upc.strip() if ean: ean = ean.strip() if initials: initials = initials == "1" if initials_min: initials_min = int(initials_min) if initials_max: initials_max = int(initials_max) # creates the map that is going to hold the complete set of features # and populates the features according to their existence features = dict() if initials: features["initials"] = dict(min = initials_min, max = initials_max) # creates the update dictionary that is going to be used in the updating # of the "product" metadata (this is considered to be a delta dictionary) update = dict( compare_price = compare_price, discount = discount, characteristics = characteristics, features = features, material = material, category = category, collection = collection, brand = brand, season = season, gender = gender, order = order, discountable = discountable, sku_field = sku_field ) # tries to "cast" the base value as an integer and in case # it's possible assumes that this value is the object identifier try: object_id = int(base) except Exception: object_id = None # in case no object id was retrieved from the base name value # a secondary strategy is used, so that the merchandise database # is searched using the base string value as the company product code if not object_id: # creates the keyword arguments map so that the the merchandise # with the provided company product code is retrieved kwargs = { "start_record" : 0, "number_records" : 1, "filters[]" : [ "company_product_code:equals:%s" % base ] } # runs the list merchandise operation in order to try to find a # merchandise entity for the requested (unique) product code in # case there's at least one merchandise its object id is used try: merchandise = api.list_merchandise(**kwargs) except Exception: merchandise = [] if merchandise: object_id = merchandise[0]["object_id"] # in case no object id was retrieved must skip the current loop # with a proper information message (as expected) if not object_id: quorum.info("Skipping, could not resolve Object ID for '%s'" % base) return # prints a logging message about the updating of the metadata for # the entity with the current object id quorum.debug("Setting metadata for entity '%d'" % object_id) # retrieves the reference to the entity so that it's possible to # retrieve the currently defined metadata for it (to be updated) entity = api.get_entity(object_id) metadata = entity.get("metadata", {}) or {} # updates the metadata dictionary with the new values that are going # to be used for the updating of the entity, note that the previous # metadata values are leveraged and not overwritten with this strategy metadata.update(update) # creates the model structure to be updated and then runs the # proper execution of the metadata import model = dict(metadata = metadata) if name: model["name"] = name if description: model["description"] = description if upc: model["upc"] = upc if ean: model["ean"] = ean api.update_entity(object_id, payload = dict(root_entity = model)) try: # start the CSV import operation that is going to import the # various lines of the CSV in the buffer and for each of them # call the function passed as callback util.csv_import( buffer, callback, header = True, delimiter = "," if comma else ";", quoting = True ) finally: # closes the temporary file descriptor and removes the temporary # file (avoiding any memory leaks) os.close(fd) os.remove(file_path) # redirects the user back to the metadata list page with a success # message indicating that everything went ok return flask.redirect( flask.url_for( "metadata_extras", message = "Metadata file processed with success" ) )