def save(self, *args, **kwargs): if not self.site_code: from corehq.apps.locations.util import generate_site_code self.site_code = generate_site_code( self.name, Location.site_codes_for_domain(self.domain)) return super(Location, self).save(*args, **kwargs)
def save(self, *args, **kwargs): if not self.site_code: from corehq.apps.locations.util import generate_site_code self.site_code = generate_site_code( self.name, Location.site_codes_for_domain(self.domain) ) return super(Location, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.last_modified = datetime.now() # lazy migration for site_code if not self.site_code: from corehq.apps.locations.util import generate_site_code self.site_code = generate_site_code( self.name, Location.site_codes_for_domain(self.domain)) return super(Location, self).save(*args, **kwargs)
def save(self, *args, **kwargs): self.last_modified = datetime.now() # lazy migration for site_code if not self.site_code: from corehq.apps.locations.util import generate_site_code self.site_code = generate_site_code( self.name, Location.site_codes_for_domain(self.domain) ) return super(Location, self).save(*args, **kwargs)
def handle(self, *args, **options): self.stdout.write("Populating site codes...\n") site_codes_by_domain = {} relevant_ids = set([r['id'] for r in Location.get_db().view( 'locations/by_type', reduce=False, ).all()]) to_save = [] for loc in iter_docs(Location.get_db(), relevant_ids): if not loc['site_code']: # triggering the safe will cause this to get populated self.stdout.write("Updating location %s\n" % loc['name']) if loc['domain'] not in site_codes_by_domain: site_codes_by_domain[loc['domain']] = list( Location.site_codes_for_domain(loc['domain']) ) loc['site_code'] = generate_site_code( loc['name'], site_codes_by_domain[loc['domain']] ) site_codes_by_domain[loc['domain']].append(loc['site_code']) to_save.append(loc) if len(to_save) > 500: Location.get_db().bulk_save(to_save) to_save = [] if to_save: Location.get_db().bulk_save(to_save)