Example #1
0
    def get_model(self,*args,**kwargs):
        '''
        Returns a functional Django model based on current data
        Attention: this method does not take are of the model cache, admin, DB etc,
        this needs to be done separately.
        '''
        #if hasattr(self,'_model'):
        #    return self._model
        
        # Get all associated fields into a list ready for dict()
        #fields = [(f.name, f.get_django_field()) for f in self._fields.all()]

        # Use the create_model function defined above
        #self._model= model_factory(self)

        # Do not return anything, if model is not saved yet
        if not self.id:
            return None

        _app_label = getattr(self,'app',None).lower() or DYNAMO_DEFAULT_APP.lower()
        _model_name = self.name.encode('ascii')
        
        try:
            # try to get the model from the model cache
            m=app_cache.app_models[_app_label][_model_name]
        except KeyError:
            # Model has never been created before, just continue with the process
            pass
        except:
            # Any other error should be raised
            raise
        else:
            # if model was found, check the hash to identify changes
            if m.hash==self.get_hash():
                # return cached model, since nothing has changed
                return m
            else:
                # we need to clear the model cache here, otherwise
                # the metaclass ModelBase will not generate it, but get
                # the old one from the cache, when we run the type(...) statement
                remove_from_model_cache(_app_label,_model_name)

        # Create the model from sratch
        
        class Meta:
            app_label = _app_label
            verbose_name = self.verbose_name
            unique_together=self.unique_together
        attrs=self.django_fields
        attrs.update({
            'Meta': Meta,
            '__module__': getattr(self,'module',DYNAMO_DEFAULT_MODULE),
            '__unicode__': lambda s: '%s' % self.name,
            'hash': self.get_hash(),
            'admin': self.admin
            })
        return type(_model_name, (models.Model,), attrs)
Example #2
0
def field_post_delete(sender, **kwargs):
    '''
    A signal handler to run any pre_save activities and trigger the built-in
    pre_save signals
    1. Update the database with regard to the deleted field
    2. Trigger the post delete signal
    '''

    MetaField=sender
    meta_field=kwargs['instance']
    try:
        # Regenerate our MetaModel, which may have changed
        DynamicModel = meta_field.meta_model.get_model(regenerate=True, notify_changes=True)
        utils.unregister_in_admin(admin_site, DynamicModel)
        utils.remove_from_model_cache(DynamicModel._meta.app_label, DynamicModel.__name__)

        # Delete field in the database
        if DYNAMO_DELETE_FIELDS:
            # TODO: no delete field function available yet
            pass

        post_field_update.send(sender=Dynamic_model,field=meta_field)
    except:
        pass