[Update] 修改一些逻辑

This commit is contained in:
ibuler
2018-07-14 00:47:21 +08:00
parent 7412bdcba7
commit 28e47f33c1
30 changed files with 167 additions and 185 deletions

View File

@@ -15,62 +15,35 @@ __all__ = ['OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin']
class OrgManager(models.Manager):
def get_queryset(self):
print("GET CURR")
current_org = get_current_org()
user_model = get_user_model()
kwargs = {}
print("Get queryset ")
print(self.model)
print(current_org)
if not current_org:
return super().get_queryset().filter(**kwargs)
kwargs['id'] = None
elif issubclass(self.model, user_model):
kwargs['orgs'] = current_org
elif current_org.is_real():
kwargs['org'] = current_org
elif current_org.is_default():
kwargs['org'] = None
queryset = super().get_queryset().filter(**kwargs)
print(kwargs)
return super().get_queryset().filter(**kwargs)
print(queryset)
return queryset
class OrgModelMixin(models.Model):
org = models.ForeignKey('orgs.Organization', on_delete=models.PROTECT, null=True)
objects = OrgManager()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
def save(self, *args, **kwargs):
current_org = get_current_org()
if current_org and current_org.is_real():
kwargs = {'org': current_org}
base_qs = base_qs.filter(**kwargs)
else:
logger.warn(
'Attempting to update %s instance "%s" without a current tenant '
'set. This may cause issues in a partitioned environment. '
'Recommend calling set_current_org() before performing this '
'operation.', self._meta.model.__name__, self
)
return super()._do_update(base_qs, using, pk_val, values, update_fields, forced_update)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
user_model = get_user_model()
current_org = get_current_org()
if current_org and not current_org.is_real():
self.org = current_org
instance = super().save(
force_insert=force_insert, force_update=force_update,
using=using, update_fields=update_fields
)
if isinstance(instance, user_model):
instance.orgs.add(current_org)
return instance
return super(OrgModelMixin, self).save(*args, **kwargs)
class Meta:
abstract = True