
    si-C                         d Z ddlZddl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mZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ ddlmZmZmZmZm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z( e%e$d               Z)e%e$d               Z*e%e$d               Z+e%e$d               Z,e%e$d               Z-e%e$d               Z.e%e$d               Z/e%e$d               Z0e%e$dd              Z1e%e$ e&d      d                      Z2y)zV
Module for managing announcements, including creation, updates, comments, and views.
    N)datetime	timedelta)messages)User)Q)HttpResponse)get_object_or_404redirectrender)reverse)gettext_lazy)AnnouncementCommentFormAnnouncementForm)closest_numbersfilter_own_records)AnnouncementAnnouncementCommentAnnouncementExpireAnnouncementView
Attachment)Employee)hx_request_requiredlogin_requiredpermission_required)notifyc                    t         j                  j                  dd      j                         xs d}t        j                  j                         }g }|j                  d      D ]1  }|j                  t        |      z   |_	        |j                  |       3 |r!t        j                  j                  |dg       | j                  j                  d      }|j                  t        j                         j!                         	      }|r|n;|j                  t#        | j                  j$                  
      t#        d      z        }|j'                  d      j)                  d      }|D ]<  }|j*                  j                  | j                  d      j-                         |_        > t1        j2                  |D cg c]  }|j4                   c}      }	|||	d}
t7        | d|
      S c c}w )ae  
    Renders a list of announcements for the authenticated user.

    This view fetches all announcements and updates their expiration dates if not already set.
    It filters announcements based on the user's permissions and whether the announcements
    are still valid (not expired). Additionally, it checks if the user has viewed each announcement.
    daysTflat   )expire_date__isnull)r   expire_datebase.view_announcement)expire_date__gte)	employees)employees__isnullannouncementview_set-created_at)userviewed)announcementsgeneral_expire_dateinstance_idsz$announcement/announcements_list.html)r   objectsvalues_listfirstr   allfilter
created_atr   r"   appendbulk_updater)   has_permr   todaydater   employee_getprefetch_relatedorder_byr'   exists
has_viewedjsondumpsidr   )requestr,   r+   announcements_to_updateannouncementhas_view_permissionannouncement_itemsfiltered_announcementsinstancer-   contexts              5/var/www/python-projects/worksol/base/announcement.pyannouncement_listrJ      s    	""..vD.AGGIOR  !((,,.M %,,,F 5#/#:#:Y$>
 $
  	 &&|4	5 (()@=/R!,,//0HI!(((..:J:O:O:Q(RM  	!!112Q5NN
  0@@h}  / "."C"C"J"Jd #K #

&( 	 ::;QRxx{{RSL/2$G
 'A7KK Ss   >G+c                 J   t               }| j                  dk(  rt        | j                  | j                        }|j	                         r|j                  d      \  }}|j                          |j                  j                  |       |j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  j                  |       |j                  j                  |       |j                  j                  |       |j                  dd	
      }|j                  dd	
      }	t        j                  j                  |      }
t        j                  j                  |	      }||
z  |z  j!                         } |j"                  j$                  |  |
|z  |z  }t&        j                  j                  |      j!                         }t        |
j                  dd	
            }t        |j                  dd	
            }t        |j                  dd	
            }|j)                  |      }||z
  }| j*                  j,                  fd} ||j                  |      t/        d              ||j                  |      t/        d              ||j                  |      t/        d             t1        j2                  | t/        d             t               }t&        j                  j                  |      }t&        j                  j                  |      }|t        j                  j                  |      z  }|t        j                  j                  |      z  } |j"                  j$                  |  |j                          t5        j6                  | j*                  j,                  |ddddddd	       t5        j6                  | j*                  j,                  |ddd d!d"dd	       t               }t9        | d#d$|i      S )%z>
    Create a new announcement and notify relevant users.
    POSTFcommitr%   
departmentjob_position
company_idr@   Tr   %employee_work_info__department_id__in'employee_work_info__job_position_id__in)employee_get__inc                 d    | j                         rt        j                  | |dddddd	       y y )Nu%   لقد تم ذكرك في إعلان.u*   Sie wurden in einer Ankündigung erwähnt.z"Has sido mencionado en un anuncio.u,   Vous avez été mentionné dans une annonce./chatbox-ellipses	recipientverbverb_arverb_deverb_esverb_frr
   icon)r<   r   send)usersr\   senders     rI   send_notificationz.create_announcement.<locals>.send_notification   s8    <<>KK"'! G L D N!$/
 "    )employee_get__id__inz1Your department was mentioned in an announcement.z3Your job position was mentioned in an announcement.z+You have been mentioned in an announcement.z"Announcement created successfully.3employee_get__employee_work_info__department_id__in5employee_get__employee_work_info__job_position_id__in(Your department was mentioned in a post.%   تم ذكر قسمك في منشور..   Ihr Abteilung wurde in einem Beitrag erwähnt.3   Tu departamento fue mencionado en una publicación.3   Votre département a été mentionné dans un post.rX   rY   rZ   *Your job position was mentioned in a post.)   تم ذكر وظيفتك في منشور.5   Ihre Arbeitsposition wurde in einem Beitrag erwähnt.8   Tu puesto de trabajo fue mencionado en una publicación.7   Votre poste de travail a été mentionné dans un post.z#announcement/announcement_form.htmlform)r   methodrL   FILESis_validsaveattachmentssetcleaned_datarO   rP   rQ   r/   r   r.   r2   distinctr%   addr   unionr)   r9   _r   successr   rb   r   )rA   rv   rC   attachment_idsr%   departmentsjob_positionscompanydept_idsjob_idsemployees_from_deptemployees_from_joball_employeesall_empsuser_mapdept_emp_idsjob_emp_idsdirect_emp_idsnotified_idsdirect_only_idsre   emp_depemp_jobsrd   s                          @rI   create_announcementr   Q   s    D~~gmm<==?+/99E9+B(L.$$((8))+6I++L9K --n=M''5G##''4%%))-8##''0"..t$.?H#//4/@G"*"2"2"9"96> #: # "*!1!1!8!88? "9 "
 //2DDhj  'L""&&6*-??)KH||**H*ENNPH2>>t$>OPL0<<T<MNK !6!6t$!6!GHN'--k:L,|;O\\..F \BEF [AGH _E?@
 Wa(L&MN#%Dll))DO * G ||**FS + H "H$4$4$;$;6A %< % I "H$4$4$;$;8E %< % I 'L""&&	2KK))!??HMM'
 KK))"ACORQ'
 $%D'@64.QQrf   c                    t        j                  |      }|r/|j                          t        j                  | t        d             | j                  j                  d      }t        j                  |      }|rt        ||      nd\  }}||v r|j                  |       |r7||k7  r2t        dd|i      }t        | dt        j                  |             S t        t              S )z6
    This method is used to delete announcements.
    z"Announcement deleted successfully.r-   NNzannouncement-single-viewanoun_id)kwargsz?instance_ids=)r   finddeleter   r   r   GETgetr>   loadsr   remover   r
   r?   announcement_single_view)rA   r   rC   r-   instance_ids_list__next_instance_idurls           rI   delete_announcementr      s      $$X.L!$H"IJ;;??>2L

<0  	)84 B $$  *,80*FV9WX3%~djj9J.K-LMNN,--rf   c                 6   t         j                  j                  |      }t        |      }t	        |j
                  j                               }| j                  j                  d      }| j                  dk(  rut        | j                  | j                  |      }|j                         rB|j                  d      \  }}|j                          |rQt        |      t        t        j                  j                  |            z  }|j
                  j                  |       n|j
                  j                  |       |j                   d   }	|j                   d	   }
|j                   d
   }|j                   d   }|j"                  j                  |
       |j$                  j                  |       |j&                  j                  |       t)        j*                  | t-        d             t.        j                  j                  |
      }t.        j                  j                  |      }|	t0        j                  j                  |
      z  }	|	t0        j                  j                  |      z  }	 |j2                  j4                  |	  t7        j8                  | j:                  j<                  |ddddddd	       t7        j8                  | j:                  j<                  |ddddddd	       t?        | d||| j@                  j                  dd       d!      S )"zF
    This method renders form and template to update Announcement
    r@   )rG   r-   rL   FrM   )id__inr%   rO   rP   rQ   z"Announcement updated successfully.rh   rj   rR   rT   rl   rm   rn   ro   rp   rX   rY   rZ   rq   rr   rs   rt   ru   z*announcement/announcement_update_form.htmlHTTP_HX_TARGET )rv   r-   	hx_target)!r   r.   r   r   listr{   r1   r   rw   rL   rx   ry   rz   r|   r   r2   r}   rO   rP   rQ   r   r   r   r   r   r%   r   r   rb   r)   r9   r   META)rA   r   rC   rv   existing_attachmentsr-   anour   all_attachmentsr%   r   r   r   r   r   s                  rI   update_announcementr      s     ''++x+8L\2D 8 8 < < >?;;??>2L~~gmmlS==?#'99E9#: D.IIK"%&:";c&&--^-D? #   $$_5  $$%9:))+6I++L9K --n=M''5GOO,!!-0OO(Wa(L&MNll))DO * G ||**FS + H "H$4$4$;$;6A %< % I "H$4$4$;$;8E %< % I DNN	*KK))!??HMM'
 KK))"ACORQ'
 4( ))*:B?	
 rf   c                     t        t        |      }t        t        |      }|j                  j	                  |       t        j                  | t        d             t        d      S )Nr   z'The file has been successfully deleted.z!<script>reloadMessage();</script>)	r	   r   r   r{   r   r   r   r   r   )rA   obj_idattachment_idrC   
attachments        rI   remove_announcement_filer   :  sR     %\f=L":-@J##J/Wa IJK;<<rf   c                    t         j                  j                  |      j                         }| j                  j
                  }t        |j                  |d      }t        j                  j                  |      }g }|r,|D ]'  }|j                  |j                  j                         ) t        t        |            }| j                  dk(  rt        | j                        }|j!                         r||j"                  _
        ||j"                  _        |j'                          t        |j                  |d      }t)        j*                  | t-        d             t/        j0                  | j                  j
                  |d|j2                   dd	|j2                   dd
|j2                   dd|j2                   dd|j2                   ddd	       t5        d|      S t7        | d||d      S )zO
    This method renders form and template to create Announcement comments
    r   )employee_id
request_id)initialannouncement_idrL   zYou commented a post.zComment under the announcement .u!   تعليق تحت الإعلان u!   Kommentar unter der Ankündigung zComentario bajo el anuncio zCommentaire sous l'annonce rX   rY   rZ   zannouncement-view-comment)r   announcement/comment_view.html)rv   r   )r   r.   r2   r0   r)   r9   r   r@   r   r4   r   employee_user_idr   r|   rw   rL   ry   rG   r   rz   r   r   r   r   rb   titler
   r   )	rA   r   anounemprv   commentscommentatorsiunique_userss	            rI   create_announcement_commentr   E  s      ''8'4::<E
,,
#
#C" #h?D #**11(1KHL 	@A > >?	@L)*L~~&w||4==?(+DMM%,1DMM)IIK*(+hGD Wa(?&@AKK))&6u{{m1E;EKK=J;EKK=J5ekk]!D5ekk]!D'
 3hGG(X. rf   c           	         t         j                  j                  |      }t        j                  j	                  |      j                  d      }|j                  st        | |d      }|j                          }t        | d||||d      S )zG
    This method is used to view all comments in the announcements
    r   r   r(   zbase.view_announcementcommentr   )r   no_commentsr   rC   )
r   r.   r   r   r2   r;   public_commentsr   r<   r   )rA   r   rC   r   r   s        rI   comment_viewr   u  s      ''++x+8L"**11(1KTTH ''%X>
 oo''K( &"(		
	 	rf   c                     t         j                  j                  |      }|j                          t	        j
                  | t        d             t               S )z=
    This method is used to delete announcement comments
    r   zComment deleted successfully!)r   r.   r   r   r   r   r   r   )rA   
comment_idcomments      rI   delete_announcement_commentr     sE     "))---<GNNWa ?@A>rf   c                 p   t        j                  |      }| j                  j                  d      }|rt	        j
                  |      ng }|rt        ||      nd\  }}|rEt        j                  j                  | j                  |      \  }}d|_        |j                          ||||d}	t        | d|	      S )z=
    This method is used to render single announcements.
    r-   r   )r)   rC   T)rC   r-   previous_instance_idr   z"announcement/announcement_one.html)r   r   r   r   r>   r   r   r   r.   get_or_creater)   r*   rz   r   )
rA   r   announcement_instancer-   r   r   r   announcement_view_objr   rH   s
             rI   r   r     s     )--h7;;??>2L4@

<0b  	)84 +*
 #3#;#;#I#I,A $J $
 q (,$""$ .$ 4,	G '?IIrf   r#   c                     | j                   j                  d      }t        j                  j	                  |d      }t        | dd|i      S )z3
    This method is used to view the employees
    r   T)announcement_id__idr*   zannouncement/viewed_by.html	viewed_by)r   r   r   r.   r2   r   )rA   r   viewed_userss      rI   r   r     sN     kkoo&78O#++22+D 3 L '8;:UVVrf   )N)3__doc__r>   r   r   django.contribr   django.contrib.auth.modelsr   django.db.modelsr   django.httpr   django.shortcutsr	   r
   r   django.urlsr   django.utils.translationr   r   
base.formsr   r   base.methodsr   r   base.modelsr   r   r   r   r   employee.modelsr   horilla.decoratorsr   r   r   notifications.signalsr   rJ   r   r   r   r   r   r   r   r   r    rf   rI   <module>r      s~    ( # +  $ @ @  6 @ <  % W W ( .L  .Lb uR  uRp .  .4 N  Nb =  = +  +\   4    J  J: -.W /  Wrf   