o
    ѷ6i                     @   s   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dddZedddZeedddZd	S )    )login_required)Q)get_object_or_404)redirect)gettext)models)	json_view)login_required_ajaxNF
   c                    s   t jjt| jdt| jdB }|dur|j|d}| }|dur)|j|d}|d}|dd}d	d
l	m
  d| fdd|d| D dS )a  
    View that returns a JSON list of notifications for the current user as according
    to ``request.user``.

    :param: latest_id: The latest id of a notification. Use this to avoid
    retrieving the same notifications multiple times.
    :param: is_viewed: Set this to ``True`` if you also want to retrieve
    notifications that have already been viewed.

    :returns: An HTTPResponse object with JSON data::

        {'success': True,
         'total_count': total_count,
         'objects': [{'pk': n.pk,
                     'message': n.message,
                     'url': n.url,
                     'occurrences': n.occurrences,
                     'occurrences_msg': _('%d times') % n.occurrences,
                     'type': n.subscription.notification_type.key if n.subscription else None,
                     'since': naturaltime(n.created)} for n in notifications[:max_results]]}
    subscription__settings__useruserN	is_viewed)id__gtz-idsubscriptionsubscription__notification_typer   naturaltimeTc              
      sH   g | ] }|j |j|j|jtd |j |jr|jjjnd |jdqS )z%d timesN)pkmessageurloccurrencesoccurrences_msgtypesince)	r   r   r   r   _r   notification_typekeycreated).0nr    U/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/django_nyt/views.py
<listcomp>;   s    z%get_notifications.<locals>.<listcomp>)successtotal_countobjects)r   Notificationr(   filterr   r   countorder_byprefetch_related-django.contrib.humanize.templatetags.humanizer   )request	latest_idr   max_resultsnotificationsr'   r#   r   r$   get_notifications   s&   


r3   c                 C   sd   | j dd}|st|S ttjt| jdt| jdB |d}d|_|	  |j
r.t|j
S t|S )NHTTP_REFERER r   r   )idT)METAgetr   r   r   r)   r   r   r   saver   )r/   notification_idreferernotificationr#   r#   r$   gotoL   s   
r=   c                 C   sZ   t jjjt| jdt| jdB |d}|r|j|d}|r#|j|d}|jdd ddiS )	Nr   r   )id__lte)notification_type__id)id__gteTr   r&   )r   r)   r(   r*   r   r   update)r/   id_ltenotification_type_idid_gter2   r#   r#   r$   	mark_read]   s   rE   )NFr
   )N)NN)django.contrib.auth.decoratorsr   django.db.modelsr   django.shortcutsr   r   django.utils.translationr   r   
django_nytr   django_nyt.decoratorsr   r	   r3   r=   rE   r#   r#   r#   r$   <module>   s     >