jumpserver/apps/orgs/mixins.py

101 lines
2.9 KiB
Python
Raw Normal View History

2018-07-12 16:00:35 +00:00
# -*- coding: utf-8 -*-
#
from threading import local
2018-07-12 16:00:35 +00:00
from django.db import models
2018-07-13 07:05:46 +00:00
from django.shortcuts import redirect
2018-07-14 16:55:05 +00:00
import warnings
from django.forms import ModelForm
from django.http.response import HttpResponseForbidden
2018-07-12 16:00:35 +00:00
from common.utils import get_logger
from .utils import current_org, set_current_org
2018-07-19 11:24:29 +00:00
from .models import Organization
2018-07-12 16:00:35 +00:00
logger = get_logger(__file__)
2018-07-20 05:25:50 +00:00
tl = local()
2018-07-12 16:00:35 +00:00
2018-07-14 16:55:05 +00:00
__all__ = [
'OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin', 'OrgModelForm'
]
2018-07-12 16:00:35 +00:00
2018-07-13 07:05:46 +00:00
class OrgManager(models.Manager):
2018-07-20 05:25:50 +00:00
2018-07-12 16:00:35 +00:00
def get_queryset(self):
kwargs = {}
2018-07-20 05:25:50 +00:00
if not hasattr(tl, 'times'):
tl.times = 0
2018-07-12 16:00:35 +00:00
2018-07-20 05:25:50 +00:00
print("[{}]>>>>>>>>>> Get query set".format(tl.times))
print(current_org)
2018-07-12 16:00:35 +00:00
if not current_org:
2018-07-15 10:39:11 +00:00
kwargs['id'] = None
2018-07-13 07:05:46 +00:00
elif current_org.is_real():
2018-07-20 02:54:16 +00:00
kwargs['org_id'] = current_org.id
2018-07-12 16:00:35 +00:00
elif current_org.is_default():
2018-07-20 02:54:16 +00:00
kwargs['org_id'] = None
2018-07-16 06:47:06 +00:00
queryset = super(OrgManager, self).get_queryset()
queryset = queryset.filter(**kwargs)
# print(kwargs)
2018-07-19 11:24:29 +00:00
# print(queryset.query)
2018-07-20 05:25:50 +00:00
tl.times += 1
2018-07-13 16:47:21 +00:00
return queryset
2018-07-12 16:00:35 +00:00
2018-07-20 04:15:45 +00:00
def all(self):
if not current_org:
msg = 'You can `objects.set_current_org(org).all()` then run it'
warnings.warn(msg)
return self
else:
return super(OrgManager, self).all()
2018-07-14 16:55:05 +00:00
def set_current_org(self, org):
2018-07-19 11:24:29 +00:00
if isinstance(org, str):
org = Organization.objects.get(name=org)
2018-07-14 16:55:05 +00:00
set_current_org(org)
return self
2018-07-12 16:00:35 +00:00
class OrgModelMixin(models.Model):
2018-07-20 02:54:16 +00:00
org_id = models.CharField(max_length=36, null=True)
2018-07-12 16:00:35 +00:00
objects = OrgManager()
2018-07-13 16:47:21 +00:00
def save(self, *args, **kwargs):
2018-07-12 16:00:35 +00:00
if current_org and current_org.is_real():
2018-07-20 02:54:16 +00:00
self.org_id = current_org.id
2018-07-13 16:47:21 +00:00
return super(OrgModelMixin, self).save(*args, **kwargs)
2018-07-12 16:00:35 +00:00
class Meta:
abstract = True
2018-07-13 07:05:46 +00:00
class OrgViewGenericMixin:
def dispatch(self, request, *args, **kwargs):
print("Current org: {}".format(current_org))
2018-07-13 07:05:46 +00:00
if not current_org:
return redirect('orgs:switch-a-org')
if not current_org.can_admin_by(request.user):
print("{} cannot admin {}".format(request.user, current_org))
if request.user.is_org_admin:
print("Is org admin")
return redirect('orgs:switch-a-org')
return HttpResponseForbidden()
else:
print(current_org.can_admin_by(request.user))
2018-07-13 07:05:46 +00:00
return super().dispatch(request, *args, **kwargs)
2018-07-14 16:55:05 +00:00
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
model = field.queryset.model
field.queryset = model.objects.all()