
    si*                        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j<                         d   Zd Z d Z! G d dejD                  j"                        Z# G d dejH                        Z%d Z& ejN                  e&d       y)    )get_version)settings)Group)GenericForeignKey)ContentType)ImproperlyConfigured)models)	JSONField)QuerySet)timezone)Choices)
load_model)notify)id2slugUSE_JSONFIELDc                  0    t        j                         d   S )NSOFT_DELETE)notifications_settings
get_config     =/var/www/python-projects/worksol/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.unsent(   s    {{5{))r   c                 &    | j                  d      S )NTr!   r#   r%   s    r   sentzNotificationQuerySet.sent+   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.unread.   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.   r/   r0   s     r   readzNotificationQuerySet.read7   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&   r6   qsets      r   mark_all_as_readz%NotificationQuerySet.mark_all_as_read@   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.
        Tr5   r.   )r3   r$   r7   r8   s      r   mark_all_as_unreadz'NotificationQuerySet.mark_all_as_unreadM   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.deletedY   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.active^   s    {{5{))r   c                     t                | j                         }|r|j                  |      }|j                  d      S )zYMark current queryset as deleted.
        Optionally, filter by recipient first.
        r5   Tr>   )r   rA   r$   r7   r8   s      r   mark_all_as_deletedz(NotificationQuerySet.mark_all_as_deletedc   s9     	{{};;;3D{{4{((r   c                     t                | j                         }|r|j                  |      }|j                  d      S )zdMark current queryset as active(un-deleted).
        Optionally, filter by recipient first.
        r5   Fr>   )r   r-   r$   r7   r8   s      r   mark_all_as_activez'NotificationQuerySet.mark_all_as_activen   s9     	||~;;;3D{{5{))r   c                 n    | j                         }|r|j                  |      }|j                  d      S )Nr5   Fr!   )r)   r$   r7   r8   s      r   mark_as_unsentz#NotificationQuerySet.mark_as_unsenty   s1    yy{;;;3D{{5{))r   c                 n    | j                         }|r|j                  |      }|j                  d      S )Nr5   Tr!   )r'   r$   r7   r8   s      r   mark_as_sentz!NotificationQuerySet.mark_as_sent   s1    {{};;;3D{{4{((r   )FN)__name__
__module____qualname____doc__r'   r)   r,   r3   r:   r<   r-   rA   rC   rE   rG   rI   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)rW   rZ   db_indexnotify_actor)r[   r\      )rX   actor_content_typeactor_object_id)rZ   nullnotify_target)r[   rZ   rb   r\   )rX   rZ   rb   target_content_typetarget_object_idnotify_action_object)rZ   rb   r[   r\   action_object_content_typeaction_object_object_id)rW   r]   c                   @    e Zd ZdZdZ ej                  ddg      gZy)AbstractNotification.MetaT)z
-timestampr6   r,   )fieldsN)rK   rL   rM   abstractorderingr	   Indexindexesr   r   r   Metarj      s(    " FLLh 78
r   rp   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 agorr   )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   )rw   )django.utils.timesincerw   	timestamp)r&   now
timesince_s      r   rw   zAbstractNotification.timesince   s    
 	C$..#..r   c                 ,    t        | j                        S rJ   )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   rJ   )2rK   rL   rM   rN   r   LEVELSr	   	CharFieldrR   level
ForeignKeyr   AUTH_USER_MODELCASCADEr6   BooleanFieldr,   r   r`   ra   r   rs   rt   	TextFielddescriptionrd   re   rv   rg   rh   ru   DateTimeFieldr   r}   r|   publicr-   r"   r
   datar   
as_managerobjectsrp   ry   rw   propertyr   r   r   r   r   r   rP   rP      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
 
<&/    
r   rP   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.
    signalNr6   sender)rv   ru   r   Tr   r|   rY   Notificationr   )r6   r`   ra   rt   r   r   r|   r   z%s_object_idz%s_content_typeverb_arverb_deverb_esverb_fr)popboolr   r}   r   r   rR   
isinstancer   user_setallr   listr   r   get_for_modelpkstrsetattr
EXTRA_DATAr   getr   r   r   r   r   append)rt   kwargsr6   rs   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)(djangor   django.confr   django.contrib.auth.modelsr   "django.contrib.contenttypes.fieldsr   "django.contrib.contenttypes.modelsr   django.core.exceptionsr   	django.dbr	   django.db.modelsr
   django.db.models.queryr   django.utilsr   model_utilsr   swapperr   rY   r   notifications.signalsr   notifications.utilsr   r   r   r   r   queryr   ModelrP   r   connectr   r   r   <module>r      s       , @ : 7  & + !   < ( '.#..0A
>(^)6<<00 ^)BC6<< CL:| ~,O Pr   