notifications/apps/push_notifications/admin.py
Ali Asadi 2dbe021fe5 REFACTOR(notifications): rename code/click_object_id to action_code/click_object_id_value
`code` was ambiguous next to Python's own use of "code"; `action_code` names
its actual role (a click-action lookup key). click_object_id_value pairs it
consistently with action_code. Renamed on both PushMessage and
NotificationLink via a data-preserving RenameField migration, threaded
through the serializer, admin, bulk-push path, and docs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 12:13:29 +03:30

114 lines
3.7 KiB
Python

from functools import update_wrapper
from django.contrib import admin
from django.contrib.admin.exceptions import DisallowedModelAdminToField
from django.contrib.admin.options import TO_FIELD_VAR
from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
from .models import PushUser, PushMessage, BulkPushMessage, NotificationLink
admin.site.register(PushUser)
@admin.register(NotificationLink)
class NotificationLinkAdmin(admin.ModelAdmin):
list_display = ['action_code', 'url_template', 'is_active', 'description']
list_filter = ['is_active']
search_fields = ['action_code', 'description']
from django import forms
from django.shortcuts import render, redirect
class PushMessageForm(forms.Form):
user_uuid = forms.UUIDField(required=False)
user_phone_number = forms.CharField(required=False)
title = forms.CharField(required=False)
message = forms.CharField(required=False)
priority = forms.IntegerField(required=False)
extras = forms.JSONField(required=False)
class PushMessageAdmin(admin.ModelAdmin):
# change_list_template = 'admin/custom_form.html'
def get_urls(self):
from django.urls import path
def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
wrapper.model_admin = self
return update_wrapper(wrapper, view)
info = self.opts.app_label, self.opts.model_name
urls = super().get_urls()
custom_urls = [
path("custom-action2/", wrap(self.add_view), name="%s_%s_custom-action2" % info),
# path("custom-action/", wrap(self.custom_action), name="%s_%s_custom-action" % info),
# path('custom-action/', self.admin_site.admin_view(self.custom_action), name='custom-action'),
]
return custom_urls + urls
# def custom_action(self, request, form_url="", extra_context=None):
#
#
#
# return self.render_change_form(
# request, context, add=add, change=not add, obj=obj, form_url=form_url
# )
# def custom_action(self, request):
# if request.method == 'POST':
# form = PushMessageForm(request.POST)
# if form.is_valid():
# # ایجاد آبجکت‌ها بر اساس داده‌های فرم
# data = form.cleaned_data
#
# # ایجاد آبجکت در Model1
# obj1 = PushMessage.objects.create(
# field1=data['field1'],
# field2=data['field2']
# )
#
# # ایجاد آبجکت در Model2
# obj2 = PushMessage.objects.create(
# related_field=obj1,
# other_field=data['field2'] * 2
# )
#
# self.message_user(request, "آبجکت‌ها با موفقیت ایجاد شدند")
# # ریدایرکت به صفحه‌ای مناسب
# return redirect('..')
# else:
# form = PushMessageForm()
#
# context = self.admin_site.each_context(request)
# context['opts'] = self.model._meta
# context['form'] = form
# return TemplateResponse(request, 'admin/custom_form.html', context)
@admin.action(description="bulk_push")
def bulk_push(modeladmin, request, queryset):
for s in queryset:
s.bulk_push_task()
class BulkPushMessageAdmin(admin.ModelAdmin):
list_display = ['uuid', 'state', 'success_count', 'failure_count', 'created_at']
actions = [bulk_push]
admin.site.register(PushMessage)
admin.site.register(BulkPushMessage, BulkPushMessageAdmin)