The click_url a producer sends had to be built in that producer's own
Python code (utils/deep_links.py in advertising), so changing the URL
scheme meant a code deploy. Adds NotificationLink: an admin-editable
(code, url_template, is_active) row per notification type, with
url_template supporting a {object_id} placeholder.
PushMessage gains `code` and `click_object_id` — a producer can send either
a raw click_url (unchanged, still wins if both are given) or a code + the
referenced object's id, and resolve_click_url() looks up the template at
send time. An unset or not-yet-configured/inactive code resolves to no
click action rather than an error, consistent with "not every notification
is clickable." Wired through both the single-push API (serializer) and the
bulk Excel path (two new optional columns).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
114 lines
3.7 KiB
Python
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 = ['code', 'url_template', 'is_active', 'description']
|
|
list_filter = ['is_active']
|
|
search_fields = ['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)
|
|
|