class SalesforceModel(models.Model, metaclass=SalesforceModelBase): """ Abstract model class for Salesforce objects. """ # pylint:disable=invalid-name _salesforce_object = 'standard' # If both managers are specified in this class, they should be different objects, # otherwse the name of the base manager must be passed to the mataclass before # the name of default manager, but that order is randomized by Python <= 3.5.x base_manager = manager.SalesforceManager( ) # type: manager.SalesforceManager[_T] objects = manager.SalesforceManager( ) # type: manager.SalesforceManager[_T] class Meta: abstract = True base_manager_name = 'base_manager' if not DJANGO_20_PLUS: manager_inheritance_from_future = True # Name of primary key 'Id' can be easily changed to 'id' # by "settings.SF_PK='id'". id = SalesforceAutoField(primary_key=True, name=SF_PK, db_column='Id', verbose_name='ID', auto_created=True)
def add_to_class(cls, name, value): if name == '_meta': sf_custom = False if hasattr(value.meta, 'custom'): sf_custom = value.meta.custom delattr(value.meta, 'custom') super(SalesforceModelBase, cls).add_to_class(name, value) setattr(cls._meta, 'sf_custom', sf_custom) else: if type(value) is models.manager.Manager: # TODO use args: obj._constructor_args = (args, kwargs) value = manager.SalesforceManager() super(SalesforceModelBase, cls).add_to_class(name, value)
class SalesforceModel(models.Model): """ Abstract model class for Salesforce objects. """ __metaclass__ = SalesforceModelBase _base_manager = objects = manager.SalesforceManager() _salesforce_object = True class Meta: managed = False abstract = True Id = fields.SalesforceAutoField(primary_key=True)
class SalesforceModel(with_metaclass(SalesforceModelBase, models.Model)): """ Abstract model class for Salesforce objects that can be saved to other db. (It is not a subclass of salesforce.models.SalesforceModel. That is not a big problem if we don't check inheritance but only the '_salesforce_object' attribute or if we use only this or only the original implementation.) """ # pylint:disable=invalid-name _salesforce_object = 'extended' objects = manager.SalesforceManager() class Meta: # pylint:disable=duplicate-code abstract = True base_manager_name = 'objects' if not DJANGO_20_PLUS: manager_inheritance_from_future = True id = SfCharAutoField(primary_key=True, name=SF_PK, db_column='Id', verbose_name='ID', auto_created=True) def save(self, force_insert=False, force_update=False, using=None, update_fields=None): using = using or router.db_for_write(self.__class__, instance=self) if self.pk is None and not force_update and not is_sf_database(using): self.pk = get_sf_alt_pk() super(SalesforceModel, self).save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields) if not isinstance(self.pk, string_types): raise ValueError("The primary key value is not assigned correctly") if DJANGO_30_PLUS: def _do_insert(self, manager, using, fields, returning_fields, raw): # the check "is_sf_database(using)" is used for something unexpected if self.pk and not is_sf_database(using): returning_fields = [] return super(SalesforceModel, self)._do_insert(manager, using, fields, returning_fields, raw)
def add_to_class(cls, name, value): if name == '_meta': sf_custom = False if hasattr(value.meta, 'custom'): sf_custom = value.meta.custom delattr(value.meta, 'custom') super(SalesforceModelBase, cls).add_to_class(name, value) setattr(cls._meta, 'sf_custom', sf_custom) else: if type(value) is models.manager.Manager: # this is or better migrations because: obj._constructor_args = (args, kwargs) _constructor_args = value._constructor_args value = manager.SalesforceManager() value._constructor_args = _constructor_args super(SalesforceModelBase, cls).add_to_class(name, value)
class SalesforceModel(with_metaclass(SalesforceModelBase, models.Model)): """ Abstract model class for Salesforce objects. """ _base_manager = objects = manager.SalesforceManager() _salesforce_object = True class Meta: managed = False abstract = True # Name of primary key 'Id' can be easily changed to 'id' # by "settings.SF_PK='id'". Id = fields.SalesforceAutoField(primary_key=True, name=SF_PK, db_column='Id')
def add_to_class(cls, name, value): # pylint:disable=protected-access if name == '_meta': sf_custom = False if hasattr(value.meta, 'custom'): sf_custom = value.meta.custom delattr(value.meta, 'custom') super(SalesforceModelBase, cls).add_to_class(name, value) # pylint: disable=no-value-for-parameter setattr(cls._meta, 'sf_custom', sf_custom) else: if type(value) is models.manager.Manager: # pylint:disable=unidiomatic-typecheck # this is for better migrations because: obj._constructor_args = (args, kwargs) _constructor_args = value._constructor_args value = manager.SalesforceManager() value._constructor_args = _constructor_args super(SalesforceModelBase, cls).add_to_class(name, value) # pylint: disable=no-value-for-parameter
class SalesforceModel(with_metaclass(SalesforceModelBase, models.Model)): """ Abstract model class for Salesforce objects. """ # pylint:disable=invalid-name _salesforce_object = True objects = manager.SalesforceManager() class Meta: abstract = True base_manager_name = 'objects' if not DJANGO_20_PLUS: manager_inheritance_from_future = True # Name of primary key 'Id' can be easily changed to 'id' # by "settings.SF_PK='id'". id = SalesforceAutoField(primary_key=True, name=SF_PK, db_column='Id', verbose_name='ID', auto_created=True)