api contact
This commit is contained in:
1 parent
0b56766f96
commit
04d2bc957f
14 files changed
+595
-1
No files matched your search
@@ -0,0 +1,44 @@
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import PhoneContactApiKey
|
||||
|
||||
|
||||
class PhoneContactApiKeyForm(forms.ModelForm):
|
||||
new_key = forms.CharField(
|
||||
label="کلید جدید",
|
||||
required=False,
|
||||
min_length=32,
|
||||
max_length=256,
|
||||
strip=False,
|
||||
widget=forms.PasswordInput(render_value=False),
|
||||
help_text="برای ایجاد، کلید تصادفی حداقل ۳۲ نویسهای وارد کنید. هنگام ویرایش، خالی بگذارید تا کلید فعلی حفظ شود.",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = PhoneContactApiKey
|
||||
fields = ("is_active",)
|
||||
|
||||
def clean_new_key(self):
|
||||
value = self.cleaned_data["new_key"]
|
||||
if not value and not self.instance.pk:
|
||||
raise forms.ValidationError("برای ایجاد کلید، مقدار جدید لازم است.")
|
||||
return value
|
||||
|
||||
|
||||
@admin.register(PhoneContactApiKey)
|
||||
class PhoneContactApiKeyAdmin(admin.ModelAdmin):
|
||||
form = PhoneContactApiKeyForm
|
||||
fields = ("new_key", "is_active", "updated_at")
|
||||
readonly_fields = ("updated_at",)
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return super().has_add_permission(request) and not PhoneContactApiKey.objects.exists()
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
if form.cleaned_data["new_key"]:
|
||||
obj.set_key(form.cleaned_data["new_key"])
|
||||
super().save_model(request, obj, form, change)
|
||||
Reference in new issue
Block a user