[Update] 修改用户view

This commit is contained in:
ibuler
2018-07-15 00:55:05 +08:00
parent 28e47f33c1
commit 5648dcd7e7
24 changed files with 138 additions and 100 deletions

View File

@@ -2,18 +2,26 @@
#
from django.db import models
from django.shortcuts import redirect
import warnings
from django.contrib.auth import get_user_model
from django.forms import ModelForm
from common.utils import get_logger
from .utils import get_current_org, get_model_by_db_table
from .utils import get_current_org, get_model_by_db_table, set_current_org
logger = get_logger(__file__)
__all__ = ['OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin']
__all__ = [
'OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin', 'OrgModelForm'
]
class OrgManager(models.Manager):
def __init__(self, *args, **kwargs):
print("INit manager")
super().__init__(*args, **kwargs)
def get_queryset(self):
print("GET CURR")
current_org = get_current_org()
@@ -22,9 +30,9 @@ class OrgManager(models.Manager):
print("Get queryset ")
print(current_org)
print(self.model)
if not current_org:
return super().get_queryset().filter(**kwargs)
kwargs['id'] = None
pass
elif current_org.is_real():
kwargs['org'] = current_org
elif current_org.is_default():
@@ -34,6 +42,19 @@ class OrgManager(models.Manager):
print(queryset)
return queryset
def all(self):
current_org = get_current_org()
if not current_org:
msg = 'You should `objects.set_current_org(org).all()` then run it'
warnings.warn(msg)
return self
else:
return super().all()
def set_current_org(self, org):
set_current_org(org)
return self
class OrgModelMixin(models.Model):
org = models.ForeignKey('orgs.Organization', on_delete=models.PROTECT, null=True)
@@ -55,3 +76,17 @@ class OrgViewGenericMixin:
if not current_org:
return redirect('orgs:switch-a-org')
return super().dispatch(request, *args, **kwargs)
class OrgModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'initial' not in kwargs:
return
for name, field in self.fields.items():
if not hasattr(field, 'queryset'):
continue
print(field)
model = field.queryset.model
field.queryset = model.objects.all()