Files
hossein-por-shop/backend/account/admin.py
T
2025-02-17 00:11:50 +03:30

98 lines
3.6 KiB
Python

from django.contrib import admin
from .models import *
from unfold.admin import ModelAdmin, TabularInline
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from import_export.admin import ImportExportModelAdmin
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
from unfold.contrib.forms.widgets import ArrayWidget, WysiwygWidget
from django.contrib.postgres.fields import ArrayField
from django.contrib.auth.models import Group
from unfold.forms import AdminPasswordChangeForm
from unfold.forms import AdminPasswordChangeForm, UserChangeForm, UserCreationForm
from utils.admin import ModelAdmin
class UserAddressInLine(TabularInline):
model = UserAddressModel
extra = 0
tab = True
verbose_name = 'ادرس کاربر'
verbose_name_plural = 'ادرس های کاربر'
@admin.register(User)
class UserAdmin(BaseUserAdmin, ModelAdmin, ImportExportModelAdmin):
form = UserChangeForm
add_form = UserCreationForm
change_password_form = AdminPasswordChangeForm
filter_horizontal = []
ordering = []
inlines = [UserAddressInLine]
list_filter = ['is_superuser']
search_fields = ['phone', 'first_name', 'last_name', 'email']
list_display = ['full_name_display', 'phone', 'email', 'is_superuser', 'gender', 'birth_date']
# readonly_fields = ['phone', 'email', 'otp_expiry', 'otp_hash', 'date_joined', 'profile_photo']
exclude = ('otp_hash', 'otp_expiry', 'is_active', 'is_staff', 'password', 'last_login',)
import_form_class = ImportForm
export_form_class = ExportForm
fieldsets = (
('اطلاعات شخصی', {'fields': ('first_name', 'last_name', 'profile_photo', 'password', 'gender', 'birth_date'),}),
('اطلاعات ارتباطی', {'fields': ('phone', 'email'),}),
('دسترسی های وبسایت', {'fields': ('is_superuser', 'video_uploader'),}),
)
empty_value_display = 'ثبت نشده'
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('phone', 'password1', 'password2'),
}),
)
compressed_fields = True
warn_unsaved_form = True
formfield_overrides = {
ArrayField: {
"widget": ArrayWidget,
}
}
def full_name_display(self, obj):
return obj.full_name
full_name_display.short_description = 'نام و نام خانوادگی'
admin.site.unregister(Group)
from django.contrib import admin
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken, OutstandingToken
admin.site.unregister(BlacklistedToken)
admin.site.unregister(OutstandingToken)
@admin.register(UserAddressModel)
class AddressAdmin(ModelAdmin, ImportExportModelAdmin):
import_form_class = ImportForm
export_form_class = ExportForm
search_fields = ['address', 'name', 'city', 'province']
list_display = ['user', 'name', 'address_display', 'postal_code', 'city', 'province', 'for_me', 'is_main']
#readonly_fields = ['user', 'name', 'address', 'postal_code', 'phone', 'city', 'province', 'for_me']
compressed_fields = True
warn_unsaved_form = True
formfield_overrides = {
models.TextField: {
"widget": WysiwygWidget,
},
ArrayField: {
"widget": ArrayWidget,
}
}
def address_display(self, obj):
return obj.address[0:35] + '...'
address_display.short_description = 'ادرس'
@admin.register(PushSubscription)
class PushSubscription(ModelAdmin, ImportExportModelAdmin):
import_form_class = ImportForm
export_form_class = ExportForm
compressed_fields = True
warn_unsaved_form = True