def import_products(domain, importer): from corehq.apps.products.views import ProductFieldsView results = {'errors': [], 'messages': []} to_save = [] product_count = 0 seen_product_ids = set() custom_data_validator = ProductFieldsView.get_validator(domain) for row in importer.worksheet: try: p = Product.from_excel(row, custom_data_validator) except Exception, e: results['errors'].append( _(u'Failed to import product {name}: {ex}'.format( name=row['name'] or '', ex=e, )) ) continue importer.add_progress() if not p: # skip if no product is found (or the row is blank) continue if not p.domain: # if product doesn't have domain, use from context p.domain = domain elif p.domain != domain: # don't let user import against another domains products results['errors'].append( _(u"Product {product_name} belongs to another domain and was not updated").format( product_name=p.name ) ) continue if p.code and p.code in seen_product_ids: results['errors'].append(_( u"Product {product_name} could not be imported \ due to duplicated product ids in the excel \ file" ).format( product_name=p.name )) continue elif p.code: seen_product_ids.add(p.code) product_count += 1 to_save.append(p) if len(to_save) > 500: Product.get_db().bulk_save(to_save) for couch_product in to_save: couch_product.sync_to_sql() to_save = []
from __future__ import absolute_import from __future__ import unicode_literals from django.conf.urls import url from corehq.apps.products.views import ( ProductListView, FetchProductListView, NewProductView, EditProductView, UploadProductView, ProductImportStatusView, ProductFieldsView, product_importer_job_poll, download_products, archive_product, unarchive_product, ) settings_urls = [ url(r'^$', ProductListView.as_view(), name=ProductListView.urlname), url(r'^fields/$', ProductFieldsView.as_view(), name=ProductFieldsView.urlname), url(r'^list/$', FetchProductListView.as_view(), name=FetchProductListView.urlname), url(r'^new/$', NewProductView.as_view(), name=NewProductView.urlname), url(r'^upload/$', UploadProductView.as_view(), name=UploadProductView.urlname), url(r'^upload/status/(?P<download_id>(?:dl-)?[0-9a-fA-Z]{25,32})/$', ProductImportStatusView.as_view(), name=ProductImportStatusView.urlname), url(r'^upload/poll/(?P<download_id>(?:dl-)?[0-9a-fA-Z]{25,32})/$', product_importer_job_poll, name='product_importer_job_poll'), url(r'^download/$', download_products, name='product_export'), url(r'^(?P<prod_id>[\w-]+)/$', EditProductView.as_view(), name=EditProductView.urlname), url(r'^archive/(?P<prod_id>[\w-]+)/$', archive_product, name='archive_product'), url(r'^unarchive/(?P<prod_id>[\w-]+)/$', unarchive_product, name='unarchive_product'), ]
def import_products(domain, importer): from corehq.apps.products.views import ProductFieldsView results = {'errors': [], 'messages': []} to_save = [] product_count = 0 seen_codes = set() program_ids = [program._id for program in Program.by_domain(domain)] codes = { row['code']: row['product_id'] for row in SQLProduct.objects.filter(domain=domain, is_archived=False).values('code', 'product_id') } custom_data_validator = ProductFieldsView.get_validator(domain) for row in importer.worksheet: try: p = Product.from_excel(row, custom_data_validator) except Exception, e: results['errors'].append( _(u'Failed to import product {name}: {ex}'.format( name=row['name'] or '', ex=e, )) ) continue importer.add_progress() if not p: # skip if no product is found (or the row is blank) continue if not p.domain: # if product doesn't have domain, use from context p.domain = domain elif p.domain != domain: # don't let user import against another domains products results['errors'].append( _(u"Product {product_name} belongs to another domain and was not updated").format( product_name=p.name ) ) continue if p.code: if (p.code in codes and codes[p.code] != p.get_id) or (p.code in seen_codes): results['errors'].append(_( u"Product {product_name} could not be imported \ since its product ID is already assigned to another product" ).format( product_name=p.name )) continue if p.code not in codes: seen_codes.add(p.code) if p.program_id and p.program_id not in program_ids: results['errors'].append(_( u"Product {product_name} references a program that doesn't exist: {program_id}" ).format( product_name=p.name, program_id=p.program_id )) continue product_count += 1 to_save.append(p) if len(to_save) > 500: Product.bulk_save(to_save) for couch_product in to_save: couch_product.sync_to_sql() to_save = []
FetchProductListView, NewProductView, EditProductView, UploadProductView, ProductImportStatusView, ProductFieldsView, product_importer_job_poll, download_products, archive_product, unarchive_product, ) settings_urls = [ url(r'^$', ProductListView.as_view(), name=ProductListView.urlname), url(r'^fields/$', ProductFieldsView.as_view(), name=ProductFieldsView.urlname), url(r'^list/$', FetchProductListView.as_view(), name=FetchProductListView.urlname), url(r'^new/$', NewProductView.as_view(), name=NewProductView.urlname), url(r'^upload/$', UploadProductView.as_view(), name=UploadProductView.urlname), url(r'^upload/status/(?P<download_id>[0-9a-fA-Z]{25,32})/$', ProductImportStatusView.as_view(), name=ProductImportStatusView.urlname), url(r'^upload/poll/(?P<download_id>[0-9a-fA-Z]{25,32})/$', product_importer_job_poll, name='product_importer_job_poll'), url(r'^download/$', download_products, name='product_export'),
def import_products(domain, importer): from corehq.apps.products.views import ProductFieldsView results = {'errors': [], 'messages': []} to_save = [] product_count = 0 seen_codes = set() program_ids = [program._id for program in Program.by_domain(domain)] codes = { row['code']: row['product_id'] for row in SQLProduct.objects.filter( domain=domain, is_archived=False).values('code', 'product_id') } custom_data_validator = ProductFieldsView.get_validator(domain) for row in importer.worksheet: try: p = Product.from_excel(row, custom_data_validator) except Exception as e: results['errors'].append( _(u'Failed to import product {name}: {ex}'.format( name=row['name'] or '', ex=e, ))) continue importer.add_progress() if not p: # skip if no product is found (or the row is blank) continue if not p.domain: # if product doesn't have domain, use from context p.domain = domain elif p.domain != domain: # don't let user import against another domains products results['errors'].append( _(u"Product {product_name} belongs to another domain and was not updated" ).format(product_name=p.name)) continue if p.code: if (p.code in codes and codes[p.code] != p.get_id) or (p.code in seen_codes): results['errors'].append( _(u"Product {product_name} could not be imported \ since its product ID is already assigned to another product" ).format(product_name=p.name)) continue if p.code not in codes: seen_codes.add(p.code) if p.program_id and p.program_id not in program_ids: results['errors'].append( _(u"Product {product_name} references a program that doesn't exist: {program_id}" ).format(product_name=p.name, program_id=p.program_id)) continue product_count += 1 to_save.append(p) if len(to_save) > 500: Product.bulk_save(to_save) for couch_product in to_save: couch_product.sync_to_sql() to_save = [] if to_save: Product.bulk_save(to_save) for couch_product in to_save: couch_product.sync_to_sql() if product_count: results['messages'].insert( 0, _('Successfully updated {number_of_products} products with {errors} ' 'errors.').format(number_of_products=product_count, errors=len(results['errors']))) return results