o
    [=g],                     @   sf  d dl mZ d dlmZ ddlmZmZ d dlmZ ddlm	Z	m
Z
mZ d dlmZ ddlmZ d d	lmZ dd
lmZ d dlmZ d dlmZ ddlmZ d dlmZmZ d dlZedZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd  d eZ$G d!d" d"eeZ%G d#d$ d$ej&Z'dS )%    )APIView)status   )EmailNotificationSerializerNotificationReceiverSerializer)	send_mail)AppNotificationSerializerNotificationsSerializerNotificationtypeSerializer)	GCMDevice)NotificationReceiver)get_object_or_404)notifications)LimitOffsetPagination)viewsets)Notificationtype)success_responsefailed_responseNr   c                   @      e Zd Zdd ZdS )SendEmailNotificationViewc              
   C   s   t d t|jd}| rfz0|jd }|jd }|jd }t d| d| d t||d	|gd
d t d tddW S  tye } zt j	dt
| dd tdditjW  Y d }~S d }~ww t d|j  t|jtjS )Nz$Starting email notification process.datasubjectmessagerecipient_emailzSending email to z with subject ''.zyour_email@example.comF)fail_silentlyzEmail sent successfully.zEmail sent successfullymsgzFailed to send email: Texc_infoerrorzFailed to send emailValidation failed: )loggerinfor   r   is_validvalidated_datar   r   	Exceptionr!   strr   r   HTTP_500_INTERNAL_SERVER_ERRORwarningerrorsHTTP_400_BAD_REQUEST)selfrequest
serializerr   r   r   e r1   6/var/www/html/notificationserver/notification/views.pypost   s0   




zSendEmailNotificationView.postN__name__
__module____qualname__r3   r1   r1   r1   r2   r          r   c                   @   r   )SendAppNotificationViewc              
   C   s$  t d t|jd}| rzL|jd }|jd }|jd }t d| d| d tjj|d	}|	 rL|j
||d
 t d| d tddW S t d| d tdtjW S  ty } zt jdt| dd tdditjW  Y d }~S d }~ww t d|j  t|jtjS )Nz"Starting app notification process.r   titler   recipient_idz Sending notification to user ID z with title 'r   )user__id)r:   zNotification sent to user ID .zNotification sent successfullyr   zRecipient with user ID z not found.zRecipient not foundz!Failed to send app notification: Tr   r!   zFailed to send notificationr"   )r#   r$   r   r   r%   r&   r   objectsfilterexistssend_messager   r*   r   r   HTTP_404_NOT_FOUNDr'   r!   r(   r)   r+   r,   )r-   r.   r/   r:   r   r;   devicesr0   r1   r1   r2   r3   6   s,   



zSendAppNotificationView.postNr4   r1   r1   r1   r2   r9   5   r8   r9   c                   @   r   )NotificationCreateViewc                 C   s<   t |jd}| r| }td|jiddS t|jtj	S )Nr   notification_id!Notification created successfullyr   r   )
r	   r   r%   saver   idr   r+   r   r,   )r-   r.   r/   notificationr1   r1   r2   r3   U   s   zNotificationCreateView.postNr4   r1   r1   r1   r2   rD   T   r8   rD   c                   @   r   )NotificationtypeCreateViewc                 C   s8   t |jd}| r|  t|jddS t|jtjS )Nr   &Notification type created successfullyrG   )	r
   r   r%   rH   r   r   r+   r   r,   )r-   r.   r/   r1   r1   r2   r3   a   s   zNotificationtypeCreateView.postNr4   r1   r1   r1   r2   rK   `   r8   rK   c                   @   r   )NotificationsCreateViewc              
   C   s   t d t|jd}| rKz|  t d|j  t|jddW S  tyJ } zt jdt	| dd t
d	d
itjW  Y d }~S d }~ww t d|j  t
|jtjS )Nz+Starting the notification creation process.r   z-Notification created successfully with data: rF   rG   zFailed to create notification: Tr   r!   zFailed to create notificationr"   )r#   r$   r	   r   r%   rH   r   r'   r!   r(   r   r   r)   r*   r+   r,   )r-   r.   r/   r0   r1   r1   r2   r3   m   s"   
zNotificationsCreateView.postNr4   r1   r1   r1   r2   rM   l   r8   rM   c                   @   r   )NotificationReceiverCreateViewc              
   C   s   t d t|jd}| rMz| }t d|j  td|jiddW S  tyL } zt j	dt
| dd	 td
ditjW  Y d }~S d }~ww t d|j  t|jtjS )Nz4Starting the notification receiver creation process.r   z4Notification receiver created successfully with ID: notification_receiver_idz*Notification receiver created successfullyrG   z(Failed to create notification receiver: Tr   r!   z&Failed to create notification receiverr"   )r#   r$   r   r   r%   rH   rI   r   r'   r!   r(   r   r   r)   r*   r+   r,   )r-   r.   r/   notification_receiverr0   r1   r1   r2   r3      s"   
z#NotificationReceiverCreateView.postNr4   r1   r1   r1   r2   rN      r8   rN   c                   @   r   )ReadNotificationsViewc           	   
   C   s   t d|  zOtjj|dd}|s$t d|  tdtj	W S g }|D ]}|j
}d|jji|j|j|jd|jd}|| q(t d	t| d
|  t|dW S  ty~ } zt jd| dt| dd tdtjW  Y d }~S d }~ww )Nz$Fetching notifications for user_id: user_idrJ   $No notifications found for user_id: %No notifications found for this user.type)notification_type	sender_idread_atr   )rJ   rS   zFound z notifications for user_id: r   z0Error while fetching notifications for user_id:  - Tr   /An error occurred while fetching notifications.)r#   r$   r   r>   r?   select_relatedr*   r   r   rB   rJ   rW   rV   rX   rY   r   rS   appendlenr   r'   r!   r(   r)   )	r-   r.   rS   	receiversnotifications_datareceiverrJ   notification_datar0   r1   r1   r2   get   s2   zReadNotificationsView.getNr5   r6   r7   rc   r1   r1   r1   r2   rQ      r8   rQ   c                   @   r   )DeleteNotificationViewc              
   C   s   t d|  ztt|d}|  t d| d tddW S  tyH } zt jd| dt| d	d
 t	dt
jW  Y d }~S d }~ww )Nz+Attempting to delete notification with id: )rI   zNotification with id: z deleted successfully.z!Notification deleted successfullyr   z<Error occurred while trying to delete notification with id: rZ   Tr   z2An error occurred while deleting the notification.)r#   r$   r   r   deleter   r'   r!   r(   r   r   r)   )r-   r.   rE   rJ   r0   r1   r1   r2   rf      s   zDeleteNotificationView.deleteN)r5   r6   r7   rf   r1   r1   r1   r2   re      r8   re   c                   @   r   )!PaginatedNotificationReceiverViewc              
   C   s   t d|  z<tjj|dd}| j||| d}|s,t d|  tdtj	W S t
|dd}t d	|  t| |jjd
W S  tyk } zt jd| dt| dd tdtjW  Y d }~S d }~ww )Nz5Received request to fetch notifications for user_id: rR   rJ   )viewrT   rU   T)manyz0Successfully fetched notifications for user_id: r   z9Error occurred while fetching notifications for user_id: rZ   r   r[   )r#   r$   r   r>   r?   r\   paginate_querysetr   r   rB   r   r   get_paginated_responser   r'   r!   r(   r)   )r-   r.   rS   notification_receiversresultsr/   r0   r1   r1   r2   rc      s   z%PaginatedNotificationReceiverView.getNrd   r1   r1   r1   r2   rg      r8   rg   c                   @   s"   e Zd Zej ZeZdd Z	dS )NotificationtypeAdminViewSetc              
   O   s   t d|j  | j|jd}| rNz|  t d|j  t|jddW S  tyM } zt jdt	| dd t
d	tjW  Y d }~S d }~ww t d
|j  t
|jtjS )Nz>Received request to create a new notification type with data: r   z2Notification type created successfully with data: rL   rG   z1Error occurred while creating notification type: Tr   z7An error occurred while creating the notification type.zFValidation failed for the notification type creation request. Errors: )r#   r$   r   get_serializerr%   rH   r   r'   r!   r(   r   r   r)   r*   r+   r,   )r-   r.   argskwargsr/   r0   r1   r1   r2   create   s"   z#NotificationtypeAdminViewSet.createN)
r5   r6   r7   r   r>   allquerysetr
   serializer_classrr   r1   r1   r1   r2   rn      s    
rn   )(rest_framework.viewsr   rest_frameworkr   serializersr   r   django.core.mailr   r   r	   r
   push_notifications.modelsr   modelsr   django.shortcutsr   r   rest_framework.paginationr   r   r   utils.helperr   r   logging	getLoggerr#   r   r9   rD   rK   rM   rN   rQ   re   rg   ModelViewSetrn   r1   r1   r1   r2   <module>   s2    
!%