def init_field_registry(): """ Searches for module called 'python_coded_fields' in each app. If there is such module and it has 'PYTHON_CODED_FIELDS' list attribute in it then try to add each field from this list to PYTHON_CODED_FIELDS_REGISTRY. Additionally updates choice values of DocumentField.python_coded_field model. :return: """ logging.info('Going to register Python-coded document fields from all Django apps...') plugins = collect_plugins_in_apps('python_coded_fields', 'PYTHON_CODED_FIELDS') # type: Dict[str, List[PythonCodedField]] for app_name, fields in plugins.items(): try: fields = list(fields) except TypeError: raise TypeError('{0}.python_coded_fields.PYTHON_CODED_FIELDS is not iterable'.format(app_name)) i = -1 for field in fields: i += 1 try: PYTHON_CODED_FIELDS_REGISTRY[field.code] = field except AttributeError: raise AttributeError('{0}.python_coded_fields.PYTHON_CODED_FIELDS[{1}] is something wrong' .format(app_name, i)) print('Registered python-coded document field: {0} ({1})'.format(field.title, field.code)) from apps.document.models import DocumentField for f in DocumentField._meta.fields: if f.name == 'python_coded_field': f.choices = list((k, PYTHON_CODED_FIELDS_REGISTRY[k].title or k) for k in sorted(PYTHON_CODED_FIELDS_REGISTRY)) break
def _register_app_dump_models(plugin_attr_name: str, dst_collection: Dict[Type[Model], Callable]): app_dump_models = collect_plugins_in_apps(STR_APP_DUMP_MODELS, plugin_attr_name) for app_name, models in app_dump_models.items(): try: models = dict(models) dst_collection.update(models) except Exception as e: logging.error(f'Unable to register app dump models from app {app_name}.\n' 'Check {app_name}.app_dump_models.{plugin_attr_name}', exc_info=e)
def _register_app_dump_models(plugin_attr_name: str, dst_collection: Dict[Type[Model], Callable]): app_dump_models = collect_plugins_in_apps(STR_APP_DUMP_MODELS, plugin_attr_name) for app_name, models in app_dump_models.items(): try: models = dict(models) dst_collection.update(models) except Exception as e: logging.error(render_error('Unable to register app dump models from app {0}.\n' 'Check {0}.app_dump_models.{1}' .format(app_name, plugin_attr_name), caused_by=e))