def mk_loctype(name, code, allowed_parents, administrative, shares_cases, view_descendants, pk): if allowed_parents and allowed_parents[0]: parent = sql_loc_types[allowed_parents[0]] else: parent = None cleaned_code = unicode_slug(code) if cleaned_code != code: err = _('Location type code "{code}" is invalid. No spaces or ' 'special characters are allowed. It has been replaced ' 'with "{new_code}".') messages.warning(request, err.format(code=code, new_code=cleaned_code)) try: loc_type = LocationType.objects.get(domain=self.domain, pk=pk) except LocationType.DoesNotExist: loc_type = LocationType(domain=self.domain) loc_type.name = name loc_type.code = cleaned_code loc_type.administrative = administrative loc_type.parent_type = parent loc_type.shares_cases = shares_cases loc_type.view_descendants = view_descendants loc_type.save() sql_loc_types[name] = loc_type
def save(self, *args, **kwargs): if not self.code: from corehq.apps.commtrack.util import unicode_slug self.code = unicode_slug(self.name) self._populate_stock_levels() return super(LocationType, self).save(*args, **kwargs)
def handle(self, app_id, path, **options): # setup directory if not os.path.exists(path): os.mkdir(path) app = Application.get(app_id) for module_index, module in enumerate(app.get_modules()): module_dir_name = '{index} - {name}'.format(index=module_index, name=unicode_slug(module.default_name())) module_dir = os.path.join(path, module_dir_name) if not os.path.exists(module_dir): os.mkdir(module_dir) for form_index, form in enumerate(module.get_forms()): form_name = ('{index} - {name}.xml'.format(index=form_index, name=unicode_slug(form.default_name()))) form_path = os.path.join(module_dir, form_name) with open(form_path, 'w') as f: f.write(form.source.encode('utf-8')) print('wrote {}'.format(form_path))
def test_unicode_slug(self): test_cases = ( ('normal', 'normal'), ('unicode', 'unicode'), (' with\n\t whitespace ', 'with-whitespace'), ('speçial çháracters', 'special-characters'), ('हिंदी', 'hindii'), ) for input, output in test_cases: self.assertEqual(output, unicode_slug(input))
def test_unicode_slug(self): test_cases = ( ('normal', 'normal'), (u'unicode', 'unicode'), (' with\n\t whitespace ', 'with-whitespace'), (u'speçial çháracters', 'special-characters'), (u'हिंदी', 'hindii'), ) for input, output in test_cases: self.assertEqual(output, unicode_slug(input))
def test_unicode_slug(self): test_cases = ( ("normal", "normal"), (u"unicode", "unicode"), (" with\n\t whitespace ", "with-whitespace"), (u"speçial çháracters", "special-characters"), (u"हिंदी", "hindii"), ) for input, output in test_cases: self.assertEqual(output, unicode_slug(input))
def mk_loctype(loctype): loctype['allowed_parents'] = [p or '' for p in loctype['allowed_parents']] cleaned_code = unicode_slug(loctype['code']) if cleaned_code != loctype['code']: err = _( 'Location type code "{code}" is invalid. No spaces or special characters are allowed. ' 'It has been replaced with "{new_code}".' ) messages.warning(request, err.format(code=loctype['code'], new_code=cleaned_code)) loctype['code'] = cleaned_code return LocationType(**loctype)
def handle(self, *args, **options): # todo: would be nice if this worked off remote servers too if len(args) != 2: raise CommandError('Usage: %s\n%s' % (self.args, self.help)) app_id, path = args # setup directory if not os.path.exists(path): os.mkdir(path) app = Application.get(app_id) for module_index, module in enumerate(app.get_modules()): module_dir_name = '{index} - {name}'.format(index=module_index, name=unicode_slug(module.default_name())) module_dir = os.path.join(path, module_dir_name) if not os.path.exists(module_dir): os.mkdir(module_dir) for form_index, form in enumerate(module.get_forms()): form_name = ('{index} - {name}.xml'.format(index=form_index, name=unicode_slug(form.default_name()))) form_path = os.path.join(module_dir, form_name) with open(form_path, 'w') as f: f.write(form.source.encode('utf-8')) print 'wrote {}'.format(form_path)
def _ordered_types(self, hierarchy): from corehq.apps.commtrack.util import unicode_slug def _gen(hierarchy): next_types = set([None]) while next_types: next_type = next_types.pop() if next_type is not None: yield next_type new_children = hierarchy.get(next_type, []) for child in new_children: next_types.add(child) return [unicode_slug(t) for t in _gen(hierarchy)]
def handle(self, *args, **options): # todo: would be nice if this worked off remote servers too if len(args) != 2: raise CommandError('Usage: %s\n%s' % (self.args, self.help)) app_id, path = args # setup directory if not os.path.exists(path): os.mkdir(path) app = Application.get(app_id) for module_index, module in enumerate(app.get_modules()): module_dir_name = '{index} - {name}'.format( index=module_index, name=unicode_slug(module.default_name())) module_dir = os.path.join(path, module_dir_name) if not os.path.exists(module_dir): os.mkdir(module_dir) for form_index, form in enumerate(module.get_forms()): form_name = ('{index} - {name}.xml'.format( index=form_index, name=unicode_slug(form.default_name()))) form_path = os.path.join(module_dir, form_name) with open(form_path, 'w') as f: f.write(form.source.encode('utf-8')) print('wrote {}'.format(form_path))
def save(self, *args, **kwargs): from .tasks import sync_administrative_status if not self.code: from corehq.apps.commtrack.util import unicode_slug self.code = unicode_slug(self.name) if not self.commtrack_enabled: self.administrative = True self._populate_stock_levels() is_not_first_save = self.pk is not None saved = super(LocationType, self).save(*args, **kwargs) if is_not_first_save and self._administrative_old != self.administrative: sync_administrative_status.delay(self) self._administrative_old = self.administrative return saved
def save(self, *args, **kwargs): if not self.code: from corehq.apps.commtrack.util import unicode_slug self.code = unicode_slug(self.name) if not self.commtrack_enabled: self.administrative = True config = stock_level_config_for_domain(self.domain, self.commtrack_enabled) if config: self._populate_stock_levels(config) is_not_first_save = self.pk is not None super(LocationType, self).save(*args, **kwargs) if is_not_first_save: self.sync_administrative_status()
def save(self, *args, **kwargs): if not self.code: from corehq.apps.commtrack.util import unicode_slug self.code = unicode_slug(self.name) if not self.commtrack_enabled: self.administrative = True config = stock_level_config_for_domain(self.domain, self.commtrack_enabled) if config: self._populate_stock_levels(config) is_not_first_save = self.pk is not None saved = super(LocationType, self).save(*args, **kwargs) if is_not_first_save: self.sync_administrative_status() return saved
def mk_loctype(name, parent_type, administrative, shares_cases, view_descendants, pk, code, **kwargs): parent = sql_loc_types[parent_type] if parent_type else None loc_type = None if not _is_fake_pk(pk): try: loc_type = LocationType.objects.get(domain=self.domain, pk=pk) except LocationType.DoesNotExist: pass if loc_type is None: loc_type = LocationType(domain=self.domain) loc_type.name = name loc_type.administrative = administrative loc_type.parent_type = parent loc_type.shares_cases = shares_cases loc_type.view_descendants = view_descendants loc_type.code = unicode_slug(code) sql_loc_types[pk] = loc_type loc_type.save()
def save(self, *args, **kwargs): from .tasks import update_location_users if not self.code: from corehq.apps.commtrack.util import unicode_slug self.code = unicode_slug(self.name) if not self.commtrack_enabled: self.administrative = True config = stock_level_config_for_domain(self.domain, self.commtrack_enabled) if config: self._populate_stock_levels(config) is_not_first_save = self.pk is not None saved = super(LocationType, self).save(*args, **kwargs) if is_not_first_save: self.sync_administrative_status() if self._has_user_old != self.has_user: update_location_users.delay(self) return saved
def wrap(cls, obj): from corehq.apps.commtrack.util import unicode_slug if not obj.get('code'): obj['code'] = unicode_slug(obj['name']) return super(LocationType, cls).wrap(obj)
def location_type_lookup(location_type): return type_to_slug_mapping.get(location_type, unicode_slug(location_type))