permission
This commit is contained in:
+30
-13
@@ -1,3 +1,4 @@
|
|||||||
|
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken, OutstandingToken
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .models import *
|
from .models import *
|
||||||
from unfold.admin import TabularInline
|
from unfold.admin import TabularInline
|
||||||
@@ -23,26 +24,34 @@ class UserAddressInLine(TabularInline):
|
|||||||
verbose_name = 'ادرس کاربر'
|
verbose_name = 'ادرس کاربر'
|
||||||
verbose_name_plural = 'ادرس های کاربر'
|
verbose_name_plural = 'ادرس های کاربر'
|
||||||
|
|
||||||
|
|
||||||
@admin.register(User)
|
@admin.register(User)
|
||||||
class UserAdmin(BaseUserAdmin, ModelAdmin, ImportExportModelAdmin):
|
class UserAdmin(BaseUserAdmin, ModelAdmin, ImportExportModelAdmin):
|
||||||
form = UserChangeForm
|
form = UserChangeForm
|
||||||
add_form = UserCreationForm
|
add_form = UserCreationForm
|
||||||
change_password_form = AdminPasswordChangeForm
|
change_password_form = AdminPasswordChangeForm
|
||||||
filter_horizontal = []
|
filter_horizontal = ('groups', 'user_permissions',)
|
||||||
|
|
||||||
ordering = []
|
ordering = []
|
||||||
inlines = [UserAddressInLine]
|
inlines = [UserAddressInLine]
|
||||||
list_filter = ['is_superuser']
|
list_filter = ['is_superuser']
|
||||||
search_fields = ['phone', 'first_name', 'last_name', 'email']
|
search_fields = ['phone', 'first_name', 'last_name', 'email']
|
||||||
list_display = ['full_name_display', 'phone', 'email', 'is_superuser', 'gender', 'birth_date']
|
list_display = ['full_name_display', 'phone',
|
||||||
|
'email', 'is_superuser', 'gender', 'birth_date']
|
||||||
# readonly_fields = ['phone', 'email', 'otp_expiry', 'otp_hash', 'date_joined', 'profile_photo']
|
# readonly_fields = ['phone', 'email', 'otp_expiry', 'otp_hash', 'date_joined', 'profile_photo']
|
||||||
|
|
||||||
exclude = ('otp_hash', 'otp_expiry', 'is_active', 'is_staff', 'password', 'last_login',)
|
exclude = ('otp_hash', 'otp_expiry', 'is_active',
|
||||||
|
'password', 'last_login',)
|
||||||
import_form_class = ImportForm
|
import_form_class = ImportForm
|
||||||
export_form_class = ExportForm
|
export_form_class = ExportForm
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
('اطلاعات شخصی', {'fields': ('first_name', 'last_name', 'profile_photo', 'password', 'gender', 'birth_date'),}),
|
('اطلاعات شخصی', {'fields': ('first_name', 'last_name',
|
||||||
|
'profile_photo', 'gender', 'birth_date'), }),
|
||||||
('اطلاعات ارتباطی', {'fields': ('phone', 'email'), }),
|
('اطلاعات ارتباطی', {'fields': ('phone', 'email'), }),
|
||||||
('دسترسی های وبسایت', {'fields': ('is_superuser', 'video_uploader'),}),
|
('دسترسی های وبسایت', {
|
||||||
|
'fields': ('is_superuser', 'is_staff', 'video_uploader'), }),
|
||||||
|
('گروه ها و مجوزها', {'fields': ('groups', 'user_permissions',)}),
|
||||||
|
|
||||||
)
|
)
|
||||||
empty_value_display = 'ثبت نشده'
|
empty_value_display = 'ثبت نشده'
|
||||||
add_fieldsets = (
|
add_fieldsets = (
|
||||||
@@ -50,8 +59,15 @@ class UserAdmin(BaseUserAdmin, ModelAdmin, ImportExportModelAdmin):
|
|||||||
'classes': ('wide',),
|
'classes': ('wide',),
|
||||||
'fields': ('phone', 'password1', 'password2'),
|
'fields': ('phone', 'password1', 'password2'),
|
||||||
}),
|
}),
|
||||||
|
('دسترسی ها', {
|
||||||
|
'fields': ('groups', 'user_permissions', 'is_superuser', 'is_staff', 'video_uploader'),
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def display_groups(self, obj):
|
||||||
|
"""Display user's groups in the list view"""
|
||||||
|
return ", ".join([group.name for group in obj.groups.all()]) or "هیچ گروهی"
|
||||||
|
display_groups.short_description = 'گروه ها'
|
||||||
|
|
||||||
compressed_fields = True
|
compressed_fields = True
|
||||||
warn_unsaved_form = True
|
warn_unsaved_form = True
|
||||||
@@ -67,9 +83,8 @@ class UserAdmin(BaseUserAdmin, ModelAdmin, ImportExportModelAdmin):
|
|||||||
|
|
||||||
full_name_display.short_description = 'نام و نام خانوادگی'
|
full_name_display.short_description = 'نام و نام خانوادگی'
|
||||||
|
|
||||||
admin.site.unregister(Group)
|
|
||||||
from django.contrib import admin
|
# admin.site.unregister(Group)
|
||||||
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken, OutstandingToken
|
|
||||||
admin.site.unregister(BlacklistedToken)
|
admin.site.unregister(BlacklistedToken)
|
||||||
admin.site.unregister(OutstandingToken)
|
admin.site.unregister(OutstandingToken)
|
||||||
|
|
||||||
@@ -79,7 +94,8 @@ class AddressAdmin(ModelAdmin, ImportExportModelAdmin):
|
|||||||
import_form_class = ImportForm
|
import_form_class = ImportForm
|
||||||
export_form_class = ExportForm
|
export_form_class = ExportForm
|
||||||
search_fields = ['address', 'name', 'city', 'province']
|
search_fields = ['address', 'name', 'city', 'province']
|
||||||
list_display = ['user', 'name', 'address_display', 'postal_code', 'city', 'province', 'for_me', 'is_main']
|
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']
|
# readonly_fields = ['user', 'name', 'address', 'postal_code', 'phone', 'city', 'province', 'for_me']
|
||||||
compressed_fields = True
|
compressed_fields = True
|
||||||
warn_unsaved_form = True
|
warn_unsaved_form = True
|
||||||
@@ -91,6 +107,7 @@ class AddressAdmin(ModelAdmin, ImportExportModelAdmin):
|
|||||||
"widget": ArrayWidget,
|
"widget": ArrayWidget,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def address_display(self, obj):
|
def address_display(self, obj):
|
||||||
return obj.address[0:35] + '...'
|
return obj.address[0:35] + '...'
|
||||||
address_display.short_description = 'ادرس'
|
address_display.short_description = 'ادرس'
|
||||||
@@ -104,7 +121,6 @@ class PushSubscription(ModelAdmin, ImportExportModelAdmin):
|
|||||||
warn_unsaved_form = True
|
warn_unsaved_form = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@admin.register(SecurityBreachAttemptModel)
|
@admin.register(SecurityBreachAttemptModel)
|
||||||
class SecurityBreachAttemptAdmin(ModelAdmin, ImportExportModelAdmin):
|
class SecurityBreachAttemptAdmin(ModelAdmin, ImportExportModelAdmin):
|
||||||
import_form_class = ImportForm
|
import_form_class = ImportForm
|
||||||
@@ -112,7 +128,8 @@ class SecurityBreachAttemptAdmin(ModelAdmin, ImportExportModelAdmin):
|
|||||||
compressed_fields = True
|
compressed_fields = True
|
||||||
warn_unsaved_form = True
|
warn_unsaved_form = True
|
||||||
change_form_template = 'loction_chagne_form.html'
|
change_form_template = 'loction_chagne_form.html'
|
||||||
list_display = ['ip_address', 'country', 'region_name', 'city', 'zip_code', 'isp', 'created_at', 'trys', 'display_viewd']
|
list_display = ['ip_address', 'country', 'region_name', 'city',
|
||||||
|
'zip_code', 'isp', 'created_at', 'trys', 'display_viewd']
|
||||||
|
|
||||||
def change_view(self, request, object_id, form_url='', extra_context=None):
|
def change_view(self, request, object_id, form_url='', extra_context=None):
|
||||||
extra_context = extra_context or {}
|
extra_context = extra_context or {}
|
||||||
@@ -120,7 +137,8 @@ class SecurityBreachAttemptAdmin(ModelAdmin, ImportExportModelAdmin):
|
|||||||
|
|
||||||
if obj and obj.lat and obj.lon:
|
if obj and obj.lat and obj.lon:
|
||||||
m = Map(location=[obj.lat, obj.lon], zoom_start=10)
|
m = Map(location=[obj.lat, obj.lon], zoom_start=10)
|
||||||
Marker([obj.lat, obj.lon], popup=f"Location: {obj.ip_address}").add_to(m)
|
Marker([obj.lat, obj.lon],
|
||||||
|
popup=f"Location: {obj.ip_address}").add_to(m)
|
||||||
map_html = m._repr_html_()
|
map_html = m._repr_html_()
|
||||||
extra_context['map_html'] = map_html
|
extra_context['map_html'] = map_html
|
||||||
return super().change_view(request, object_id, form_url, extra_context)
|
return super().change_view(request, object_id, form_url, extra_context)
|
||||||
@@ -145,4 +163,3 @@ class NewsAdmin(ModelAdmin):
|
|||||||
@admin.register(UserNotificationModel)
|
@admin.register(UserNotificationModel)
|
||||||
class UserNotificationAdmin(ModelAdmin):
|
class UserNotificationAdmin(ModelAdmin):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
# Generated by Django 5.1.2 on 2025-11-04 06:29
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('account', '0030_userfavorites'),
|
||||||
|
('auth', '0012_alter_user_first_name_max_length'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='groups',
|
||||||
|
field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='user_permissions',
|
||||||
|
field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -57,9 +57,9 @@ class User(AbstractBaseUser, PermissionsMixin):
|
|||||||
USERNAME_FIELD = 'phone'
|
USERNAME_FIELD = 'phone'
|
||||||
REQUIRED_FIELDS = []
|
REQUIRED_FIELDS = []
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def groups(self):
|
# def groups(self):
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
@@ -67,9 +67,9 @@ class User(AbstractBaseUser, PermissionsMixin):
|
|||||||
return f"{self.first_name} {self.last_name}"
|
return f"{self.first_name} {self.last_name}"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def user_permissions(self):
|
# def user_permissions(self):
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = 'کاربر'
|
verbose_name = 'کاربر'
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ UNFOLD = {
|
|||||||
|
|
||||||
"SIDEBAR": {
|
"SIDEBAR": {
|
||||||
"show_search": False,
|
"show_search": False,
|
||||||
"show_all_applications": True,
|
"show_all_applications": False,
|
||||||
"navigation": [
|
"navigation": [
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -147,6 +147,7 @@ UNFOLD = {
|
|||||||
"title": _("کد تخفیف"),
|
"title": _("کد تخفیف"),
|
||||||
"icon": "payments",
|
"icon": "payments",
|
||||||
"link": reverse_lazy("admin:order_discountcode_changelist"),
|
"link": reverse_lazy("admin:order_discountcode_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
|
|
||||||
],
|
],
|
||||||
@@ -163,16 +164,19 @@ UNFOLD = {
|
|||||||
"title": _("دسته بندی"),
|
"title": _("دسته بندی"),
|
||||||
"icon": "category",
|
"icon": "category",
|
||||||
"link": reverse_lazy("admin:product_maincategorymodel_changelist"),
|
"link": reverse_lazy("admin:product_maincategorymodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("زیر دسته بندی"),
|
"title": _("زیر دسته بندی"),
|
||||||
"icon": "category",
|
"icon": "category",
|
||||||
"link": reverse_lazy("admin:product_subcategorymodel_changelist"),
|
"link": reverse_lazy("admin:product_subcategorymodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("دسته بندی پورسانتی"),
|
"title": _("دسته بندی پورسانتی"),
|
||||||
"icon": "devices",
|
"icon": "devices",
|
||||||
"link": reverse_lazy("admin:home_showcaseslider_changelist"),
|
"link": reverse_lazy("admin:home_showcaseslider_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -186,17 +190,20 @@ UNFOLD = {
|
|||||||
"title": _("اسلاید ها"),
|
"title": _("اسلاید ها"),
|
||||||
"icon": "slide_library",
|
"icon": "slide_library",
|
||||||
"link": reverse_lazy("admin:home_slidermodel_changelist"),
|
"link": reverse_lazy("admin:home_slidermodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("عکس مقایسه"),
|
"title": _("عکس مقایسه"),
|
||||||
"icon": "compare",
|
"icon": "compare",
|
||||||
"link": reverse_lazy("admin:home_homeimagemodel_changelist"),
|
"link": reverse_lazy("admin:home_homeimagemodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
}
|
}
|
||||||
,
|
,
|
||||||
{
|
{
|
||||||
"title": _("مقالات و بلاگ ها"),
|
"title": _("مقالات و بلاگ ها"),
|
||||||
"icon": "newsmode",
|
"icon": "newsmode",
|
||||||
"link": reverse_lazy("admin:blog_blogmodel_changelist"),
|
"link": reverse_lazy("admin:blog_blogmodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
@@ -217,6 +224,7 @@ UNFOLD = {
|
|||||||
"title": _("چت محصول"),
|
"title": _("چت محصول"),
|
||||||
"icon": "chat",
|
"icon": "chat",
|
||||||
"link": reverse_lazy("admin:chat_productchatmodel_changelist"),
|
"link": reverse_lazy("admin:chat_productchatmodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("ادرس ها"),
|
"title": _("ادرس ها"),
|
||||||
@@ -227,18 +235,21 @@ UNFOLD = {
|
|||||||
"title": _("تلاشهای نفوذ"),
|
"title": _("تلاشهای نفوذ"),
|
||||||
"icon": "gpp_maybe",
|
"icon": "gpp_maybe",
|
||||||
"link": reverse_lazy("admin:account_securitybreachattemptmodel_changelist"),
|
"link": reverse_lazy("admin:account_securitybreachattemptmodel_changelist"),
|
||||||
"badge": 'utils.admin.new_attck_count'
|
"badge": 'utils.admin.new_attck_count',
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("اخبار"),
|
"title": _("اخبار"),
|
||||||
"icon": "newspaper",
|
"icon": "newspaper",
|
||||||
"link": reverse_lazy("admin:account_newsmodel_changelist"),
|
"link": reverse_lazy("admin:account_newsmodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": _("اطلاعیه ها"),
|
"title": _("اطلاعیه ها"),
|
||||||
"icon": "notifications",
|
"icon": "notifications",
|
||||||
"link": reverse_lazy("admin:account_usernotificationmodel_changelist"),
|
"link": reverse_lazy("admin:account_usernotificationmodel_changelist"),
|
||||||
|
"permission": lambda request: request.user.is_superuser,
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -295,47 +306,47 @@ UNFOLD = {
|
|||||||
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
# {
|
||||||
"title": _("تسک های سلری"),
|
# "title": _("تسک های سلری"),
|
||||||
"collapsible": True,
|
# "collapsible": True,
|
||||||
"items": [
|
# "items": [
|
||||||
{
|
# {
|
||||||
"title": _("Clocked"),
|
# "title": _("Clocked"),
|
||||||
"icon": "hourglass_bottom",
|
# "icon": "hourglass_bottom",
|
||||||
"link": reverse_lazy(
|
# "link": reverse_lazy(
|
||||||
"admin:django_celery_beat_clockedschedule_changelist"
|
# "admin:django_celery_beat_clockedschedule_changelist"
|
||||||
),
|
# ),
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"title": _("Crontabs"),
|
# "title": _("Crontabs"),
|
||||||
"icon": "update",
|
# "icon": "update",
|
||||||
"link": reverse_lazy(
|
# "link": reverse_lazy(
|
||||||
"admin:django_celery_beat_crontabschedule_changelist"
|
# "admin:django_celery_beat_crontabschedule_changelist"
|
||||||
),
|
# ),
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"title": _("Intervals"),
|
# "title": _("Intervals"),
|
||||||
"icon": "arrow_range",
|
# "icon": "arrow_range",
|
||||||
"link": reverse_lazy(
|
# "link": reverse_lazy(
|
||||||
"admin:django_celery_beat_intervalschedule_changelist"
|
# "admin:django_celery_beat_intervalschedule_changelist"
|
||||||
),
|
# ),
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"title": _("Periodic tasks"),
|
# "title": _("Periodic tasks"),
|
||||||
"icon": "task",
|
# "icon": "task",
|
||||||
"link": reverse_lazy(
|
# "link": reverse_lazy(
|
||||||
"admin:django_celery_beat_periodictask_changelist"
|
# "admin:django_celery_beat_periodictask_changelist"
|
||||||
),
|
# ),
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"title": _("Solar events"),
|
# "title": _("Solar events"),
|
||||||
"icon": "event",
|
# "icon": "event",
|
||||||
"link": reverse_lazy(
|
# "link": reverse_lazy(
|
||||||
"admin:django_celery_beat_solarschedule_changelist"
|
# "admin:django_celery_beat_solarschedule_changelist"
|
||||||
),
|
# ),
|
||||||
},
|
# },
|
||||||
],
|
# ],
|
||||||
},
|
# },
|
||||||
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from unfold.decorators import action, display
|
|||||||
from utils.admin import ModelAdmin
|
from utils.admin import ModelAdmin
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
|
from .permissions import ProductDetailCategoryPermission
|
||||||
|
|
||||||
@admin.register(ProductDetailCategory)
|
@admin.register(ProductDetailCategory)
|
||||||
class ProductDetailCategoryAdmin(ModelAdmin, ImportExportModelAdmin):
|
class ProductDetailCategoryAdmin(ModelAdmin, ImportExportModelAdmin):
|
||||||
@@ -26,6 +26,8 @@ class ProductDetailCategoryAdmin(ModelAdmin, ImportExportModelAdmin):
|
|||||||
"widget": ArrayWidget,
|
"widget": ArrayWidget,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
def has_add_permission(self, request):
|
||||||
|
return request.user.is_superuser
|
||||||
|
|
||||||
|
|
||||||
@admin.register(InPackItems)
|
@admin.register(InPackItems)
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ProductDetailCategoryPermission:
|
||||||
|
|
||||||
|
def has_add_permission(self, request):
|
||||||
|
return True
|
||||||
|
|
||||||
|
def has_change_permission(self, request, obj=None):
|
||||||
|
return True
|
||||||
|
|
||||||
|
def has_delete_permission(self, request, obj=None):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def has_view_permission(self, request, obj=None):
|
||||||
|
return True
|
||||||
|
|
||||||
Reference in New Issue
Block a user