Esempio n. 1
0
 def to_python(self, value):
     """
         Resolve the model from the class name and id
     """
     simple_dict = super(ModelPickledObjectField, self).to_python(value)
     if simple_dict:
         return resolve_model(simple_dict['class_name']).objects.get(id=simple_dict['id'])
     return simple_dict
Esempio n. 2
0
 def to_python(self, value):
     """
         Resolve the model from the class name and id
     """
     simple_dict = super(ModelPickledObjectField, self).to_python(value)
     if simple_dict:
         return resolve_model(
             simple_dict['class_name']).objects.get(id=simple_dict['id'])
     return simple_dict
def resolve_dynamic_model_class(base_class, class_name=None, scope=None):
    """
        Finds the dynamic model class if it already exists
    :param base_class: The base class
    :param class_name: Precomputed class_name if it exists, otherwise based on the scope and base_class
    :param scope: another model instance containing an id to use to scope this class
    :return:
    """
    computed_name = class_name or get_dynamic_model_class_name(base_class, scope.id if isinstance(scope, models.Model) else scope or '')
    # Return the model class if already created
    return resolve_model('main.%s' % computed_name)
Esempio n. 4
0
 def class_scope(self):
     """
         Resolve the actual model class, since it's non-trivial to store in the database
     :return:
     """
     if not self.scope:
         return None
     model = resolve_model('main.{0}'.format(self.scope))
     if not model:
         raise Exception("Could not resolve model: " + 'main.{0}'.format(self.scope))
     return model
 def class_scope(self):
     """
         Resolve the actual model class, since it's non-trivial to store in the database
     :return:
     """
     if not self.scope:
         return None
     model = resolve_model('main.{0}'.format(self.scope))
     if not model:
         raise Exception("Could not resolve model: " +
                         'main.{0}'.format(self.scope))
     return model
def resolve_dynamic_model_class(base_class, class_name=None, scope=None):
    """
        Finds the dynamic model class if it already exists
    :param base_class: The base class
    :param class_name: Precomputed class_name if it exists, otherwise based on the scope and base_class
    :param scope: another model instance containing an id to use to scope this class
    :return:
    """
    computed_name = class_name or get_dynamic_model_class_name(
        base_class,
        scope.id if isinstance(scope, models.Model) else scope or '')
    # Return the model class if already created
    return resolve_model('main.%s' % computed_name)
    def clazz(self):
        """
            Reconsitutes the class from the class_name
        """
        try:
            clazz = resolve_module_attr(self.class_path)
        except AttributeError:
            # Dynamic subclasses can't resolve using a module path
            clazz = resolve_model(self.model_path)

        if not clazz:
            raise Exception("No class could be resolved for %s or %s" % (self.class_path, self.model_path))
        return clazz
Esempio n. 8
0
 def model_from_class_name_and_pk(self, model_dict):
     try:
         model_class = self.__class__._model_cache.get(
             model_dict['class_name'], None)
         if not model_class:
             model_class = self.__class__._model_cache[
                 model_dict['class_name']] = resolve_model(
                     model_dict['class_name'])
         return model_class.objects.get(pk=model_dict['pk'])
     except:
         logger.warn(
             "pk %s of model class %s not found. This should not happen unless the model is being deleted"
             % (model_dict['pk'], model_dict['class_name']))
         return None
Esempio n. 9
0
 def class_scope(self):
     """
         Resolve the actual model class, since it's non-trivial to store in the database
     :return:
     """
     return resolve_model('footprint.main.{0}'.format(self.scope))
Esempio n. 10
0
 def class_scope(self):
     """
         Resolve the actual model class, since it's non-trivial to store in the database
     :return:
     """
     return resolve_model('footprint.main.{0}'.format(self.scope))
Esempio n. 11
0
 def model_from_class_name_and_pk(self, model_dict):
     try:
         model_class = self.__class__._model_cache.get(model_dict['class_name'], None)
         if not model_class:
             model_class = self.__class__._model_cache[model_dict['class_name']] = resolve_model(model_dict['class_name'])
         return model_class.objects.get(pk=model_dict['pk'])
     except:
         logger.warn("pk %s of model class %s not found. This should not happen unless the model is being deleted" % (model_dict['pk'], model_dict['class_name']))
         return None
Esempio n. 12
0
    def run_footprint_init(self, *args, **options):

        if not settings.CELERY_ALWAYS_EAGER:
            raise Exception('This command must run with settings.CELERY_ALWAYS_EQUAL = True. '
                            'Add --settings=footprint.settings_init to the command line.')

        db_entity_keys = options.get('db_entity_keys').split(',') if options.get('db_entity_keys') else None
        # Replace so we can use options as kwargs
        options['db_entity_keys'] = db_entity_keys
        config_entity_keys = options.get('config_entity_keys').split(',') if options.get('config_entity_keys') else None
        # Replace so we can use options as kwargs
        options['config_entity_keys'] = config_entity_keys
        if not options.get('run_analysis'):
            AnalysisModule._no_post_save_task_run_global = True
        limit_to_classes = map(
            lambda cls: resolve_model('main.%s' % cls), (options.get('class').split(',') if options.get('class') else [])
        )
        options['limit_to_classes'] = limit_to_classes

        # Perforance testing
        if options.get('memory'):
            ConfigEntity.init_heapy()
            ConfigEntity.start_heapy_diagnosis()

        # Delete all ConfigEntity intances so they can be recreated.
        # This will cascade delete related models, but it doesn't delete
        # BuiltForms and other independent models
        if options.get('recreate'):
            for cls in filter_classes(limit_to_classes):
                cls.objects.all().delete()

        # Delete deleted config_entities
        if options.get('recycle'):
            for cls in filter_classes(limit_to_classes):
                cls.objects.filter(deleted=True).delete()
        if options.get('delete_clones') or options.get('delete_scenario_clones'):
            # Delete clones and uploads
            for cls in filter_classes(limit_to_classes):
                all_config_entities = cls.objects.all()
                for config_entity in all_config_entities:
                    if options.get('delete_clones'):
                        db_entities = map(
                            lambda db_entity_interest: db_entity_interest.db_entity,
                            DbEntityInterest.objects.filter(
                                    config_entity=config_entity,
                                    db_entity__origin_instance__isnull=False)
                        ) +\
                        filter(
                            lambda db_entity: db_entity.feature_class_configuration and \
                                              (isinstance(db_entity.feature_class_configuration, dict) or
                                               db_entity.feature_class_configuration.generated),
                            config_entity.computed_db_entities())

                        layers_to_remove = Layer.objects.filter(layer_libraries__config_entity__in=[config_entity], db_entity_interest__db_entity__key__in=map(lambda db_entity: db_entity.key, db_entities))
                        for layer in layers_to_remove:
                             # Drop the layer_selection classes
                            layer_selection_class = get_or_create_layer_selection_class_for_layer(layer)
                            drop_tables_for_dynamic_classes(
                                layer_selection_class,
                                layer_selection_class.features.field.rel.through
                            )
                        layers_to_remove.delete()

                        for layer in Layer.objects.all():
                            try:
                                layer.db_entity_interest.db_entity
                            except:
                                # orphan
                                try:
                                    # Drop the layer_selection classes
                                    layer_selection_class = get_or_create_layer_selection_class_for_layer(layer)
                                    drop_tables_for_dynamic_classes(
                                        layer_selection_class,
                                        layer_selection_class.features.field.rel.through
                                    )
                                    layer.delete()
                                except:
                                    pass
                        # DbEntities
                        for db_entity in db_entities:
                            feature_class = None
                            try:
                                feature_class = FeatureClassCreator(config_entity, db_entity).dynamic_model_class()
                            except Exception, e:
                                logger.warn("No feature class for db_entity %s could be created. Exception: %s" % (db_entity.name, e.message))
                            DeleteImportProcessor().drop_data(config_entity, db_entity)
                            db_entity.delete()

                if issubclass(cls, Scenario):
                    cloned_config_entities = cls.objects.filter(origin_instance__isnull=False)

                    # ConfigEntities and their schemas
                    if options.get('delete_clones') or options.get('delete_scenario_clones'):
                        for config_entity in cloned_config_entities:
                            PGNamespace.objects.drop_schema(config_entity.schema())
                            for db_entity in config_entity.owned_db_entities():
                                db_entity.delete()
                        cloned_config_entities.delete()

                if options.get('delete_clones') and False:
                    for built_form_set in BuiltFormSet.objects.all():
                        built_form_set.built_forms.remove(*built_form_set.built_forms.filter(origin_instance__isnull=False))
                    # BuiltForms
                    BuiltForm.objects.filter(origin_instance__isnull=False).delete()
                    # Orphaned BuiltForm assets (only an issue when corrupt saves have happened)
                    BuildingAttributeSet.objects.annotate(
                        num_buildings=Count('building'), num_buildingtypes=Count('buildingtype'), num_placetypes=Count('building')).filter(
                        num_buildings=0, num_buildingtypes=0, num_placetypes=0).delete()
                    Medium.objects.annotate(num_built_form_sets=Count('builtform')).filter(num_built_form_sets=0, key__startswith='built_form').delete()
                    BuiltFormExample.objects.annotate(num_built_form_sets=Count('builtform')).filter(num_built_form_sets=0).delete()