#NEWLINE# @ login_required #NEWLINE# def notification_update( request ) : #NEWLINE# #INDENT# "\n    Handles live updating of notifications, follows ajax-polling approach.\n\n    Read more: http://stackoverflow.com/a/12855533/4726598\n\n    Required URL parameters: ``flag``.\n\n    Explanation:\n\n        - The ``flag`` parameter carries the last notification ID         received by the user's browser.\n\n        - This ``flag`` is most likely to be generated by using         a simple JS/JQuery DOM. Just grab the first element of         the notification list.\n\n            - The element will have a ``data-id`` attribute set to the             corresponding notification.\n            - We'll use it's value as the flag parameter.\n\n        - The view treats the ``last notification flag`` as a model         ```filter()`` and fetches all notifications greater than         the flag for the user.\n\n        - Then the a JSON data is prepared with all necessary         details such as, ``verb``, ``actor``, ``target`` and their         URL etc. The foreignkey are serialized as their         default ``__str__`` value.\n\n            - Everything will be HTML escaped by django's ``escape()``.\n\n        - Since these notification sent will only serve temporarily         on the notification box and will be generated fresh         using a whole template, to avoid client-side notification         generation using the JSON data, the JSON data will also         contain a rendered HTML string so that you can easily         do a JQuery ``$yourNotificationBox.prepend()`` on the         rendered html string of the notification.\n\n        - The template used is expected to be different than the         template used in full page notification as the css         and some other elements are highly likely to be         different than the full page notification list. \n        - The template used will be the ``notification type`` of the         notification suffixed ``_box.html``. So, if your         notification type is ``comment_reply``, the template         will be ``comment_reply_box.html``.\n\n            - This template will be stored in ``notifications/includes/``             of  your template directory.\n\n            - That makes: ``notifications/includes/comment_reply_box.html``\n\n        - The rest is self-explanatory.\n\n    :param request: HTTP request context.\n\n    :return: Notification updates (if any) in JSON format.\n    " #NEWLINE# flag = request . GET . get ( 'flag' , None ) #NEWLINE# target = request . GET . get ( 'target' , 'box' ) #NEWLINE# last_notification = ( int ( flag ) if flag . isdigit ( ) else None ) #NEWLINE# if last_notification : #NEWLINE# #INDENT# new_notifications = request . user . notifications . filter ( id__gt = last_notification ) . active ( ) . prefetch ( ) #NEWLINE# msg = ( _ ( 'Notifications successfully retrieved.' ) if new_notifications else _ ( 'No new notifications.' ) ) #NEWLINE# notification_list = [ ] #NEWLINE# for nf in new_notifications : #NEWLINE# #INDENT# notification = nf . as_json ( ) #NEWLINE# notification_list . append ( notification ) #NEWLINE# notification [ 'html' ] = render_notification ( nf , render_target = target , ** notification ) #NEWLINE# #UNINDENT# ctx = { #NEWLINE# #INDENT# 'retrieved' : len ( new_notifications ) , #NEWLINE# 'unread_count' : request . user . notifications . unread ( ) . count ( ) , #NEWLINE# 'notifications' : notification_list , #NEWLINE# 'success' : True , #NEWLINE# 'msg' : msg , #NEWLINE# #UNINDENT# } #NEWLINE# return JsonResponse ( ctx ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# msg = _ ( 'Notification flag not sent.' ) #NEWLINE# #UNINDENT# ctx = { #NEWLINE# #INDENT# 'success' : False , #NEWLINE# 'msg' : msg , #NEWLINE# #UNINDENT# } #NEWLINE# return JsonResponse ( ctx )