[Bugfix] 修复唯一认证

This commit is contained in:
ibuler
2018-08-16 16:32:49 +08:00
parent 2ac5786ba1
commit 670c8a6d0b
5 changed files with 70 additions and 45 deletions

View File

@@ -3,12 +3,15 @@
from werkzeug.local import Local
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import Q
from django.shortcuts import redirect
from django.forms import ModelForm
from django.http.response import HttpResponseForbidden
from django.core.exceptions import ValidationError
from common.utils import get_logger, is_uuid
from common.utils import get_logger
from .utils import current_org, set_current_org, set_to_root_org
from .models import Organization
@@ -26,15 +29,15 @@ class OrgManager(models.Manager):
def get_queryset(self):
queryset = super(OrgManager, self).get_queryset()
kwargs = {}
if not hasattr(tl, 'times'):
tl.times = 0
# if not hasattr(tl, 'times'):
# tl.times = 0
# logger.debug("[{}]>>>>>>>>>> Get query set".format(tl.times))
if not current_org:
kwargs['id'] = None
elif current_org.is_real():
kwargs['org_id'] = current_org.id
elif current_org.is_default():
queryset = queryset.filter(Q(org_id="") | Q(org_id__isnull=True))
queryset = queryset.filter(org_id="")
queryset = queryset.filter(**kwargs)
tl.times += 1
return queryset
@@ -73,7 +76,7 @@ class OrgManager(models.Manager):
class OrgModelMixin(models.Model):
org_id = models.CharField(max_length=36, null=True, blank=True, default=None)
org_id = models.CharField(max_length=36, blank=True, default='', verbose_name=_("Organization"))
objects = OrgManager()
sep = '@'
@@ -120,6 +123,25 @@ class OrgModelMixin(models.Model):
else:
return name
def validate_unique(self, exclude=None):
"""
Check unique constraints on the model and raise ValidationError if any
failed.
"""
self.org_id = current_org.id if current_org.is_real() else ''
if exclude and 'org_id' in exclude:
exclude.remove('org_id')
unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
errors = self._perform_unique_checks(unique_checks)
date_errors = self._perform_date_checks(date_checks)
for k, v in date_errors.items():
errors.setdefault(k, []).extend(v)
if errors:
raise ValidationError(errors)
class Meta:
abstract = True
@@ -157,4 +179,3 @@ class OrgModelForm(ModelForm):
continue
model = field.queryset.model
field.queryset = model.objects.all()