jumpserver/apps/orgs/mixins.py

104 lines
3.0 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-25 09:21:13 +00:00
from django.db.models import Q
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
2018-07-27 08:21:55 +00:00
from .utils import current_org, set_current_org, set_to_root_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__ = [
2018-07-27 08:21:55 +00:00
'OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin', 'OrgModelForm',
'RootOrgViewMixin',
2018-07-14 16:55:05 +00:00
]
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):
queryset = super(OrgManager, self).get_queryset()
2018-07-12 16:00:35 +00:00
kwargs = {}
2018-07-20 05:25:50 +00:00
if not hasattr(tl, 'times'):
tl.times = 0
2018-08-06 09:16:52 +00:00
# logger.debug("[{}]>>>>>>>>>> Get query set".format(tl.times))
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-25 09:21:13 +00:00
queryset = queryset.filter(Q(org_id="") | Q(org_id__isnull=True))
2018-07-16 06:47:06 +00:00
queryset = queryset.filter(**kwargs)
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'
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):
org_id = models.CharField(max_length=36, null=True, blank=True, default=None)
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-25 09:21:13 +00:00
return super().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
2018-07-27 08:21:55 +00:00
class RootOrgViewMixin:
def dispatch(self, request, *args, **kwargs):
set_to_root_org()
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()