
    vg[+                        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 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  e e              ed      k\  rd dlmZ nd dl mZ  ejB                         d   Z"d Z#d Z$ G d dejJ                  j"                        Z& G d dejN                        Z(d Z) ejT                  e)d       y)    )StrictVersion)get_version)settings)Group)ContentType)ImproperlyConfigured)models)	JSONField)QuerySet)timezone)Choices)
load_model)notify)id2slugz1.8.0)GenericForeignKeyUSE_JSONFIELDc                  0    t        j                         d   S )NSOFT_DELETE)notifications_settings
get_config     -/var/www/horilla/notifications/base/models.pyis_soft_deleter       s    !,,.}==r   c                  2    t               sd} t        |       y )NREVERTME)r   r   )msgs    r   assert_soft_deleter   $   s     "3'' r   c                   h    e Zd ZdZd Zd ZddZddZddZddZ	d	 Z
d
 ZddZddZddZddZy)NotificationQuerySetzNotification QuerySetc                 &    | j                  d      S )NFemailedfilterselfs    r   unsentzNotificationQuerySet.unsent0   s    {{5{))r   c                 &    | j                  d      S )NTr"   r$   r&   s    r   sentzNotificationQuerySet.sent3   s    {{4{((r   c                 d    t               r|s| j                  dd      S | j                  d      S )z0Return only unread items in the current querysetTFunreaddeletedr-   r   r%   r'   include_deleteds     r   r-   zNotificationQuerySet.unread6   s0    O;;dE;:: {{${''r   c                 d    t               r|s| j                  dd      S | j                  d      S )z.Return only read items in the current querysetFr,   r/   r0   r1   s     r   readzNotificationQuerySet.read?   s0    O;;eU;;; {{%{((r   Nc                 p    | j                  d      }|r|j                  |      }|j                  d      S )zxMark as read any unread messages in the current queryset.

        Optionally, filter these by recipient first.
        T	recipientFr/   )r-   r%   updater'   r7   qsets      r   mark_all_as_readz%NotificationQuerySet.mark_all_as_readH   s6     {{4 ;;;3D{{%{((r   c                 p    | j                  d      }|r|j                  |      }|j                  d      S )zxMark as unread any read messages in the current queryset.

        Optionally, filter these by recipient first.
        Tr6   r/   )r4   r%   r8   r9   s      r   mark_all_as_unreadz'NotificationQuerySet.mark_all_as_unreadU   s5    
 yy;;;3D{{${''r   c                 :    t                | j                  d      S )z1Return only deleted items in the current querysetTr.   r   r%   r&   s    r   r.   zNotificationQuerySet.deleteda   s    {{4{((r   c                 :    t                | j                  d      S )z<Return only active(un-deleted) items in the current querysetFr?   r@   r&   s    r   activezNotificationQuerySet.activef   s    {{5{))r   c                     t                | j                         }|r|j                  |      }|j                  d      S )zYMark current queryset as deleted.
        Optionally, filter by recipient first.
        r6   Tr?   )r   rB   r%   r8   r9   s      r   mark_all_as_deletedz(NotificationQuerySet.mark_all_as_deletedk   s9     	{{};;;3D{{4{((r   c                     t                | j                         }|r|j                  |      }|j                  d      S )zdMark current queryset as active(un-deleted).
        Optionally, filter by recipient first.
        r6   Fr?   )r   r.   r%   r8   r9   s      r   mark_all_as_activez'NotificationQuerySet.mark_all_as_activev   s9     	||~;;;3D{{5{))r   c                 n    | j                         }|r|j                  |      }|j                  d      S )Nr6   Fr"   )r*   r%   r8   r9   s      r   mark_as_unsentz#NotificationQuerySet.mark_as_unsent   s1    yy{;;;3D{{5{))r   c                 n    | j                         }|r|j                  |      }|j                  d      S )Nr6   Tr"   )r(   r%   r8   r9   s      r   mark_as_sentz!NotificationQuerySet.mark_as_sent   s1    {{};;;3D{{4{((r   )FN)__name__
__module____qualname____doc__r(   r*   r-   r4   r;   r=   r.   rB   rD   rF   rH   rJ   r   r   r   r    r    -   sA    *)())
()
*
	)	**)r   r    c                      e Zd ZdZ edddd      Z ej                  eej                  d      Z	 ej                  ej                  dd	ej                  
      Z ej                  ddd      Z ej                  edej                        Z ej                  d      Z edd      Z ej                  d      Z ej.                  dd      Z ej                  edddej                        Z ej                  ddd      Z edd      Z ej                  edddej                        Z ej                  ddd      Z edd      Z ej>                  e jB                  d      Z" ej                  dd      Z# ej                  dd      Z$ ej                  dd      Z% e&dd      Z'e(jS                         Z* G d d      Z+d  Z,d&d"Z-e.d#        Z/d$ Z0d% Z1y!)'AbstractNotificationa  
    Action model describing the actor acting out a verb (on an optional
    target).
    Nomenclature based on http://activitystrea.ms/specs/atom/1.0/

    Generalized Format::

        <actor> <verb> <time>
        <actor> <verb> <target> <time>
        <actor> <verb> <action_object> <target> <time>

    Examples::

        <justquick> <reached level 60> <1 minute ago>
        <brosner> <commented on> <pinax/pinax> <2 hours ago>
        <washingtontimes> <started follow> <justquick> <8 minutes ago>
        <mitsuhiko> <closed> <issue 70> on <mitsuhiko/flask> <about 2 hours ago>

    Unicode Representation::

        justquick reached level 60 1 minute ago
        mitsuhiko closed issue 70 on mitsuhiko/flask 3 hours ago

    HTML Representation::

        <a href="http://oebfare.com/">brosner</a> commented on <a href="http://github.com/pinax/pinax">pinax/pinax</a> 2 hours ago # noqa

    successinfowarningerror   )choicesdefault
max_lengthFnotifications)blankrelated_name	on_deleteT)rX   r[   db_indexnotify_actor)r\   r]      )rY   actor_content_typeactor_object_id)r[   nullnotify_target)r\   r[   rc   r]   )rY   r[   rc   target_content_typetarget_object_idnotify_action_object)r[   rc   r\   r]   action_object_content_typeaction_object_object_id)rX   r^   c                       e Zd ZdZdZdZy)AbstractNotification.MetaT)z
-timestamp)r7   r-   N)rL   rM   rN   abstractorderingindex_togetherr   r   r   Metark      s    "0r   ro   c                     | j                   | j                  | j                  | j                  | j	                         d}| j                  r| j                  rd|z  S d|z  S | j                  rd|z  S d|z  S )Nactorverbaction_objecttarget	timesincezD%(actor)s %(verb)s %(action_object)s on %(target)s %(timesince)s agoz/%(actor)s %(verb)s %(target)s %(timesince)s agoz6%(actor)s %(verb)s %(action_object)s %(timesince)s agoz$%(actor)s %(verb)s %(timesince)s agorq   )r'   ctxs     r   __str__zAbstractNotification.__str__   s    ZZII!//kk)
 ;;!!Z EsJJKcQQ5;;r   Nc                 4    ddl m}  || j                  |      S )zr
        Shortcut for the ``django.utils.timesince.timesince`` function of the
        current timestamp.
        r   )rv   )django.utils.timesincerv   	timestamp)r'   now
timesince_s      r   rv   zAbstractNotification.timesince   s    
 	C$..#..r   c                 ,    t        | j                        S rK   )r   idr&   s    r   slugzAbstractNotification.slug  s    twwr   c                 L    | j                   rd| _         | j                          y y )NFr-   saver&   s    r   mark_as_readz!AbstractNotification.mark_as_read  s    ;;DKIIK r   c                 L    | j                   sd| _         | j                          y y )NTr   r&   s    r   mark_as_unreadz#AbstractNotification.mark_as_unread  s    {{DKIIK r   rK   )2rL   rM   rN   rO   r   LEVELSr	   	CharFieldrS   level
ForeignKeyr   AUTH_USER_MODELCASCADEr7   BooleanFieldr-   r   ra   rb   r   rr   rs   	TextFielddescriptionre   rf   ru   rh   ri   rt   DateTimeFieldr   r|   r{   publicr.   r#   r
   datar    
as_managerobjectsro   rx   rv   propertyr   r   r   r   r   r   rQ   rQ      s   : Y	7;FFVV[[RPE!!!  $..	I !V  UTJF***.FNN 'f&&#6O24EFE6s+D"&""D9K+&++$.. (v''3dN46HIF!2!2!2+.." /f..#TPTU%$&?M %$$X\\DII V  =F!f!!%$?G!f!!%$?G4d+D"--/G1 1<&/    
r   rQ   c                    |j                  dd       |j                  d      }|j                  d      }dD cg c]  }|j                  |d      |f }}t        |j                  dd            }|j                  dd      }|j                  d	t        j                               }t	        d
d      }	|j                  d|	j
                  j                        }
t        |t              r|j                  j                         }nt        |t        t        f      r|}n|g}g }|D ]K  } |	|t        j                  j                  |      |j                   t#        |       ||||
      }|D ]O  \  }}|	t%        |d|z  |j                          t%        |d|z  t        j                  j                  |             Q |rt&        r||_        |j(                  j+                  dd      |_        |j(                  j+                  dd      |_        |j(                  j+                  dd      |_        |j(                  j+                  dd      |_        |j5                          |j7                  |       N |S c c}w )zS
    Handler function to create Notification instance upon action signal call.
    signalNr7   sender)ru   rt   r   Tr   r{   rZ   Notificationr   )r7   ra   rb   rs   r   r   r{   r   z%s_object_idz%s_content_typeverb_arverb_deverb_esverb_fr)popboolr   r|   r   r   rS   
isinstancer   user_setallr   listr   r   get_for_modelpkstrsetattr
EXTRA_DATAr   getr   r   r   r   r   append)rs   kwargsr7   rr   optoptional_objsr   r   r{   r   r   
recipientsnew_notifications	newnotifyobjs                  r   notify_handlerr     s6   
 JJx

;'IJJx E0K),C	$M  &**Xt,-F**]D1K

;7Io~>LJJw 3 3 8 89E )U#''++-
	I$/	0
[
 ,	 *22@@G!HHT#	
	 & 	HC	>C#7@%+''55c:	 j#IN ) 2 29d CI ) 2 29d CI ) 2 29d CI ) 2 29d CI  +;,> es   I5z!notifications.models.notification)dispatch_uidN)+distutils.versionr   djangor   django.confr   django.contrib.auth.modelsr   "django.contrib.contenttypes.modelsr   django.core.exceptionsr   	django.dbr	   django.db.modelsr
   django.db.models.queryr   django.utilsr   model_utilsr   swapperr   rZ   r   notifications.signalsr   notifications.utilsr   "django.contrib.contenttypes.fieldsr   #django.contrib.contenttypes.genericr   r   r   r   queryr    ModelrQ   r   connectr   r   r   <module>r      s       , : 7  & + !   < ( '=#99DE /#..0A
>(^)6<<00 ^)B@6<< @F:| ~,O Pr   