def delete(self): # you cannot delete the default program if self.default: raise Exception(_('You cannot delete the default program')) default = Program.default_for_domain(self.domain) products = Product.by_program_id( self.domain, self._id, wrap=True ) to_save = [] for product in products: product['program_id'] = default._id to_save.append(product) # break up saving in case there are many products if len(to_save) > 500: Product.bulk_save(to_save) to_save = [] Product.bulk_save(to_save) # bulk update sqlproducts SQLProduct.objects.filter(program_id=self._id).update(program_id=default._id) return super(Program, self).delete()
def delete(self): # you cannot delete the default program if self.default: raise Exception(_('You cannot delete the default program')) default = Program.default_for_domain(self.domain) sql_products = SQLProduct.objects.filter(domain=self.domain, program_id=self.get_id) to_save = [] for product in sql_products.couch_products(): product['program_id'] = default._id to_save.append(product) # break up saving in case there are many products if len(to_save) > 500: Product.bulk_save(to_save) to_save = [] Product.bulk_save(to_save) # bulk update sqlproducts sql_products.update(program_id=default._id) super(Program, self).delete() self.clear_caches(self.domain)
def delete(self): # you cannot delete the default program if self.default: raise Exception(_('You cannot delete the default program')) default = Program.default_for_domain(self.domain) products = Product.by_program_id(self.domain, self._id, wrap=True) to_save = [] for product in products: product['program_id'] = default._id to_save.append(product) # break up saving in case there are many products if len(to_save) > 500: Product.bulk_save(to_save) to_save = [] Product.bulk_save(to_save) # bulk update sqlproducts SQLProduct.objects.filter(program_id=self._id).update( program_id=default._id) return super(Program, self).delete()
def handle(self, *args, **options): self.stdout.write("Processing products...\n") relevant_ids = set([ r['id'] for r in Product.get_db().view( 'commtrack/products', reduce=False, ).all() ]) to_save = [] for product in iter_docs(Product.get_db(), relevant_ids): if 'last_modified' not in product or not product['last_modified']: product['last_modified'] = datetime.utcnow().isoformat() to_save.append(product) if len(to_save) > 500: Product.bulk_save(to_save) to_save = [] if to_save: Product.bulk_save(to_save) self.stdout.write("Processing programs...\n") relevant_ids = set([ r['id'] for r in Program.get_db().view( 'commtrack/programs', reduce=False, ).all() ]) to_save = [] for program in iter_docs(Program.get_db(), relevant_ids): if 'last_modified' not in program or not program['last_modified']: program['last_modified'] = datetime.utcnow().isoformat() to_save.append(program) if len(to_save) > 500: Program.get_db().bulk_save(to_save) to_save = [] if to_save: Program.get_db().bulk_save(to_save)
def handle(self, *args, **options): self.stdout.write("Processing products...\n") relevant_ids = set([r["id"] for r in Product.get_db().view("commtrack/products", reduce=False).all()]) to_save = [] for product in iter_docs(Product.get_db(), relevant_ids): if "last_modified" not in product or not product["last_modified"]: product["last_modified"] = datetime.utcnow().isoformat() to_save.append(product) if len(to_save) > 500: Product.bulk_save(to_save) to_save = [] if to_save: Product.bulk_save(to_save) self.stdout.write("Processing programs...\n") relevant_ids = set([r["id"] for r in Program.get_db().view("commtrack/programs", reduce=False).all()]) to_save = [] for program in iter_docs(Program.get_db(), relevant_ids): if "last_modified" not in program or not program["last_modified"]: program["last_modified"] = datetime.utcnow().isoformat() to_save.append(program) if len(to_save) > 500: Program.get_db().bulk_save(to_save) to_save = [] if to_save: Program.get_db().bulk_save(to_save)
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 = []
).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
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
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