
    ȴhr2                        S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	J
r
Jr  \R                  " \5       " S S\R                  5      5       r\R                  " \	5       " S S	\R                  5      5       r\R                  " \
5       " S
 S\R                  5      5       r\R                  " \5       " S S\R                  5      5       rg)z
Django Admin Configuration for Notifications Application

This module configures the Django admin interface for notification
management, channel configuration, and delivery monitoring.
    )admin)timezone)format_html)NotificationChannelNotificationTemplateNotificationNotificationRulec                      ^  \ rS rSrSr/ SQr/ SQrSS/rSSS	04S
SSS.4SSS04SSSS.44rSS/r	SS/r
S rS\l        U 4S jrSrU =r$ )NotificationChannelAdmin   z
Admin interface for NotificationChannel model.

Provides management of notification delivery channels including
configuration, rate limiting, and status monitoring.
)namechannel_type	is_active
rate_limitget_recent_deliveries
created_by
created_at)r   r   r   r   r   r   zChannel Informationfields)r   r   r   Configuration)configurationz7Channel-specific configuration parameters (JSON format)r   descriptionzDelivery Settings)r   retry_attemptstimeout_secondsMetadata)r   collapser   classesr   
updated_atc                 ~   SSK Jn  [        R                  " 5       U" SS9-
  nUR                  R                  US9nUR                  5       nUR                  SS9R                  5       nUR                  SS9R                  5       nUS:  a(  Xe-  S	-  nUS
:  a  SO	US:  a  SOSn	[        SU	UUU5      $ [        S5      $ )z0Get recent delivery statistics for this channel.r   )	timedelta   )hourscreated_at__gte	completedstatusfailedd   Z   greenF   orangeredz8<span style="color: {};">{}/{} sent<br/>({:.1f}%)</span>z4<span style="color: gray;">No recent activity</span>)datetimer"   r   nownotificationsfiltercountr   )
selfobjr"   recent_timerecent_notificationstotalsentr*   success_ratecolors
             DC:\Users\brahi\OneDrive\Desktop\Code\src\apps\notifications\admin.pyr   .NotificationChannelAdmin.get_recent_deliveriesA   s    & llnyr'::"00777T$**,#**+*>DDF%,,H,=CCE19 LC/L+b0G,QSBShY^EJ  QRR    zRecent Deliveries (24h)c                 @   > [         TU ]  U5      R                  S5      $ )&Optimize queryset with select_related.r   superget_querysetselect_relatedr6   request	__class__s     r>   rE   %NotificationChannelAdmin.get_queryset\   s    w#G,;;LIIr@    )__name__
__module____qualname____firstlineno____doc__list_displaylist_filtersearch_fields	fieldsetsreadonly_fieldsorderingr   short_descriptionrE   __static_attributes____classcell__rI   s   @r>   r   r      s    LK 	M 
;!
 	 
(T
 	 
I
 	 
%$
 	I$ $\2O 'HS2 /H+J Jr@   r   c                       \ rS rSrSr/ SQr/ SQr/ SQrSSS04S	SS
04SSSS.44rSS/r	SS/r
S rS\l        S rS\l        Srg)NotificationTemplateAdmina   z
Admin interface for NotificationTemplate model.

Provides management of notification message templates including
content editing, variable management, and compatibility settings.
)r   template_typer   get_usage_countget_compatible_channelsr   )r^   r   r   )r   r^   subject_templatemessage_templatezTemplate Informationr   )r   r^   r   Message Content)ra   rb   zTemplate Configuration)	variableschannel_typesz>Available variables and compatible channel types (JSON format)r   r   r    r^   r   c                    UR                   R                  5       nUR                   R                  [        R                  " 5       [        R
                  " SS9-
  S9R                  5       nUS:  a  [        SUU5      $ [        S5      $ )z4Get the number of notifications using this template.   )daysr%   r   z*{} total<br/><small>({} this week)</small>z*<span style="color: gray;">Not used</span>)r3   r5   r4   r   r2   r"   r   )r6   r7   r5   recent_counts       r>   r_   )NotificationTemplateAdmin.get_usage_count   s    !!'')((//$LLNX-?-?Q-GG 0 

%' 	 19< 
 GHHr@   Usagec                     UR                   (       aC  SR                  UR                   5      n[        SU[        U5      S:  a  USS S-   5      $ U5      $ [        S5      $ )z!Display compatible channel types.z, z<span title="{}">{}</span>   N...z.<span style="color: gray;">All channels</span>)re   joinr   len)r6   r7   channel_lists      r>   r`   1NotificationTemplateAdmin.get_compatible_channels   sr    99S%6%67L;\hklxhy|~h~<X[Y[K\_dKd  R  R  EQ  R  RKLLr@   zCompatible ChannelsrK   N)rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r_   rW   r`   rX   rK   r@   r>   r\   r\   a   s    LK
M 
 <"
 	 
>
 	 
"4[$
 	I $\2O  (HI )0O%M 1F-r@   r\   c                      ^  \ rS rSrSr/ SQr/ SQr/ SQrSSS04S	SS
04SSS04SSS04SSSS.44r/ SQr	S/r
S rS\l        S\l        U 4S jrSS/rS rS\l        S rS\l        SrU =r$ ) NotificationAdmin   z
Admin interface for Notification model.

Provides detailed monitoring of individual notifications including
delivery status, error tracking, and retry management.
)get_notification_infochannel	recipienttemplater)   scheduled_atsent_atdelivery_attempts)r)   rw   ry   rz   r{   )rx   subjectmessagechannel__nametemplate__namezNotification Detailsr   )rw   ry   rx   r)   rc   )r}   r~   
Scheduling)rz   r{   zDelivery Tracking)r|   error_messageexternal_idzTemplate Context)context_datar   r   )r   r    r{   r|   z-scheduled_atc                 2   SSSSSS.R                  UR                  S5      n[        UR                  5      S:  a  UR                  S	S S
-   OUR                  n[	        SU=(       d    UR
                  S	S S
-   UUR                  R                  5       5      $ )z'Get formatted notification information.r/   bluer-   r0   gray)pending
processingr'   r*   	cancelledblackrm   Nrn   z:<strong>{}</strong><br/><span style="color: {};">{}</span>)getr)   rp   r}   r   r~   title)r6   r7   status_colorsubject_previews       r>   rv   'NotificationAdmin.get_notification_info   s        
 #cjj'
" 	 7:#++6F6K#++cr*U2QTQ\Q\17s{{3B/%7JJ
 	
r@   r   r}   c                 B   > [         TU ]  U5      R                  SS5      $ rB   rw   ry   rC   rG   s     r>   rE   NotificationAdmin.get_queryset        w#G,;;IzRRr@   retry_failed_notificationsmark_as_cancelledc                     UR                  SS9nSnU H6  nUR                  5       (       d  M  SUl        UR                  5         US-  nM8     U R	                  USU S35        g)	z,Custom action to retry failed notifications.r*   r(   r   r      Queued z notifications for retry.N)r4   	can_retryr)   savemessage_user)r6   rH   querysetfailed_notificationsretry_countnotifications         r>   r   ,NotificationAdmin.retry_failed_notifications   so    'h?0L%%''&/#!!#q 	 1 	k]";<	
r@   zRetry failed notificationsc                 l    UR                  SS9nUR                  SS9nU R                  USU S35        g)z.Custom action to cancel pending notifications.r   r(   r   z
Cancelled z pending notifications.N)r4   updater   )r6   rH   r   pending_notificationscancelled_counts        r>   r   #NotificationAdmin.mark_as_cancelled  sE     (y A/66k6J))@A	
r@   zCancel pending notificationsrK   )rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rv   rW   admin_order_fieldrE   actionsr   r   rX   rY   rZ   s   @r>   rt   rt      s    LK
M 
 D"
 	 
,
 	 
1
 	 
K
 	 
'$
 	I(O
   H
( /=+.7+S
 ,-@AG
  4P0
 +I''r@   rt   c                      ^  \ rS rSrSr/ SQr/ SQr/ SQrSSS04S	SS
04SSSS.4SSS044r/ SQr	SS/r
S rS\l        U 4S jrS/rS rS\l        SrU =r$ )NotificationRuleAdmini  z
Admin interface for NotificationRule model.

Provides management of automatic notification rules including
event triggering, conditions, and throttling configuration.
)r   
event_typerw   ry   r   throttle_minutesget_trigger_countlast_triggered)r   r   rw   ry   r   )r   r   r   r   zRule Informationr   )r   r   r   priorityzNotification Configuration)rw   ry   zTriggering Conditions)
conditionsz7Additional conditions for rule triggering (JSON format)r   
Throttling)r   r   )r   r    r   z	-priorityr   c                     UR                   (       a%  [        SUR                   R                  S5      5      $ [        S5      $ )z5Get the number of times this rule has been triggered.zE<span style="color: green;">Active</span><br/><small>Last: {}</small>z%m/%d %H:%Mz1<span style="color: gray;">Never triggered</span>)r   r   strftime)r6   r7   s     r>   r   'NotificationRuleAdmin.get_trigger_countK  sA     *""++M: 
 NOOr@   zTrigger Statusc                 B   > [         TU ]  U5      R                  SS5      $ r   rC   rG   s     r>   rE   "NotificationRuleAdmin.get_querysetY  r   r@   test_notification_rulec           	         SSK Jn  SnUR                  SS9 HV  nSUR                  [        R
                  " 5       R                  5       S.n UR                  UR                  U5        US-  nMX     US:  a  U R                  USU S35        gg! [         a,  nU R                  USUR                   SU 3S	S
9   SnAM  SnAff = f)z)Custom action to test notification rules.r   )send_notification_via_ruler   T)r   )	test_mode	rule_name	timestampzFailed to test rule z: ERROR)levelNr   z test notifications.)tasksr   r4   r   r   r2   	isoformatdelayr   	Exceptionr   )r6   rH   r   r   
test_countruletest_contextes           r>   r   ,NotificationRuleAdmin.test_notification_rule`  s    5
OOdO3D "!YY%\\^557L*00,Oa
 4$ >*%9:   !!*499+Rs;! " s   !B
C!CCzSend test notificationsrK   )rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r   rW   rE   r   r   rX   rY   rZ   s   @r>   r   r     s    LK
M 
E
 	 
&-(
 	 
!%T#
 	 
<
 	I" EO V$H
P +;'S
 ((G: 0I,,r@   r   N)rP   django.contribr   django.utilsr   django.utils.htmlr   apps.notifications.modelsr   r   r   r	   register
ModelAdminr   r\   rt   r   rK   r@   r>   <module>r      s    ! ! ) 
 #$MJu// MJ %MJ` $%DF 0 0 DF &DFN nI(( nI nIb  !aIE,, aI "aIr@   