block user in django admin
This commit is contained in:
1 parent
089db0afa4
commit
fe7a10cb61
19 files changed
+574
-11
No files matched your search
@@ -1,5 +1,7 @@
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.db import transaction
|
||||
from django.core.exceptions import ValidationError
|
||||
from apps.core.models import PersianTextNormalizationMixin, TimeStampedModel
|
||||
|
||||
|
||||
@@ -51,6 +53,34 @@ class CustomerProfile(PersianTextNormalizationMixin, TimeStampedModel):
|
||||
groups = models.ManyToManyField(ContactGroup, blank=True, related_name="contacts")
|
||||
province = models.CharField(max_length=100, blank=True)
|
||||
city = models.CharField(max_length=100, blank=True)
|
||||
login_blocked = models.BooleanField("مسدود کردن ورود مشتری / مخاطب", default=False)
|
||||
login_block_reason = models.TextField(
|
||||
"دلیل مسدودی ورود", blank=True,
|
||||
help_text="این متن به مشتری یا مخاطب نمایش داده میشود. ورود کارکنان و کارشناسان تحت تأثیر نیست.",
|
||||
)
|
||||
login_access_version = models.PositiveIntegerField(default=0, editable=False)
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if self.login_blocked and not self.login_block_reason.strip():
|
||||
raise ValidationError({"login_block_reason": "برای مسدود کردن ورود، دلیل مسدودی را بنویسید."})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# Keep old sessions revoked even if the administrator later unblocks access.
|
||||
with transaction.atomic():
|
||||
previous = type(self).objects.select_for_update().filter(pk=self.pk).first()
|
||||
if previous is None and self.login_blocked:
|
||||
self.login_access_version = 1
|
||||
if previous is not None:
|
||||
self.login_access_version = previous.login_access_version
|
||||
fields = kwargs.get("update_fields")
|
||||
changes_access = fields is None or "login_blocked" in fields
|
||||
if changes_access and self.login_blocked and not previous.login_blocked:
|
||||
self.login_access_version += 1
|
||||
if fields is not None:
|
||||
kwargs["update_fields"] = set(fields) | {"login_access_version"}
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self): return (f"{self.first_name} {self.last_name}".strip() or self.user.mobile or self.user.username)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user