jumpserver/apps/orgs/mixins.py

58 lines
1.6 KiB
Python
Raw Normal View History

2018-07-12 16:00:35 +00:00
# -*- coding: utf-8 -*-
#
from django.db import models
2018-07-13 07:05:46 +00:00
from django.shortcuts import redirect
from django.contrib.auth import get_user_model
2018-07-12 16:00:35 +00:00
from common.utils import get_logger
from .utils import get_current_org, get_model_by_db_table
logger = get_logger(__file__)
2018-07-13 07:05:46 +00:00
__all__ = ['OrgManager', 'OrgViewGenericMixin', 'OrgModelMixin']
2018-07-12 16:00:35 +00:00
2018-07-13 07:05:46 +00:00
class OrgManager(models.Manager):
2018-07-12 16:00:35 +00:00
def get_queryset(self):
2018-07-13 16:47:21 +00:00
print("GET CURR")
2018-07-12 16:00:35 +00:00
current_org = get_current_org()
kwargs = {}
2018-07-13 07:05:46 +00:00
print("Get queryset ")
print(current_org)
2018-07-12 16:00:35 +00:00
if not current_org:
2018-07-13 16:47:21 +00:00
return super().get_queryset().filter(**kwargs)
2018-07-12 16:00:35 +00:00
kwargs['id'] = None
2018-07-13 07:05:46 +00:00
elif current_org.is_real():
2018-07-12 16:00:35 +00:00
kwargs['org'] = current_org
elif current_org.is_default():
kwargs['org'] = None
2018-07-13 16:47:21 +00:00
queryset = super().get_queryset().filter(**kwargs)
2018-07-12 16:00:35 +00:00
print(kwargs)
2018-07-13 16:47:21 +00:00
print(queryset)
return queryset
2018-07-12 16:00:35 +00:00
class OrgModelMixin(models.Model):
org = models.ForeignKey('orgs.Organization', on_delete=models.PROTECT, null=True)
objects = OrgManager()
2018-07-13 16:47:21 +00:00
def save(self, *args, **kwargs):
2018-07-12 16:00:35 +00:00
current_org = get_current_org()
if current_org and current_org.is_real():
self.org = current_org
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):
current_org = get_current_org()
if not current_org:
return redirect('orgs:switch-a-org')
return super().dispatch(request, *args, **kwargs)