<s> from django import template <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext as _ <EOL> from . . import notify_settings <EOL> from . . utils import render_notification <EOL> register = template . Library ( ) <EOL> class RenderNotificationsNode ( template . Node ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( <EOL> _ ( "<STR_LIT>" ) % ( tokens [ <NUM_LIT:0> ] ) ) <EOL> if len ( tokens ) == <NUM_LIT:3> : <EOL> return cls ( parser . compile_filter ( tokens [ <NUM_LIT:2> ] ) ) <EOL> elif len ( tokens ) == <NUM_LIT:5> : <EOL> if tokens [ <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( <EOL> _ ( "<STR_LIT>" ) % ( tokens [ <NUM_LIT:0> ] ) ) <EOL> return cls ( obj = parser . compile_filter ( tokens [ <NUM_LIT:2> ] ) , target = tokens [ <NUM_LIT:4> ] ) <EOL> else : <EOL> raise template . TemplateSyntaxError ( <EOL> _ ( "<STR_LIT>" ) . format ( <EOL> tag = tokens [ <NUM_LIT:0> ] , len = len ( tokens ) ) ) <EOL> def __init__ ( self , obj , target = '<STR_LIT>' ) : <EOL> self . obj = obj <EOL> self . target = target <EOL> def generate_html ( self , notifications ) : <EOL> """<STR_LIT>""" <EOL> html_chunks = [ ] <EOL> for nf in notifications : <EOL> extra = nf . as_json ( ) if self . target == '<STR_LIT>' else { } <EOL> html = render_notification ( nf , render_target = self . target , ** extra ) <EOL> html_chunks . append ( html ) <EOL> if not html_chunks : <EOL> html_chunks . append ( _ ( "<STR_LIT>" ) ) <EOL> html_string = '<STR_LIT:\n>' . join ( html_chunks ) <EOL> return html_string <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> notifications = self . obj . resolve ( context ) <EOL> return self . generate_html ( notifications ) <EOL> @ register . tag <EOL> def render_notifications ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return RenderNotificationsNode . handle_token ( parser , token ) <EOL> JS_INCLUSION_TEMPATE = '<STR_LIT>' <EOL> @ register . inclusion_tag ( JS_INCLUSION_TEMPATE , takes_context = True ) <EOL> def include_notify_js_variables ( context ) : <EOL> """<STR_LIT>""" <EOL> ctx = { <EOL> '<STR_LIT:user>' : context [ '<STR_LIT>' ] . user , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : notify_settings . NF_LIST_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . SINGLE_NF_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . NF_BOX_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . SINGLE_NF_BOX_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . MARK_NF_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . MARK_ALL_NF_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . DELETE_NF_CLASS_SELECTOR , <EOL> '<STR_LIT>' : notify_settings . READ_NF_CLASS , <EOL> '<STR_LIT>' : notify_settings . UNREAD_NF_CLASS , <EOL> '<STR_LIT>' : notify_settings . UPDATE_TIME_INTERVAL , <EOL> } <EOL> return ctx <EOL> class UserNotification ( RenderNotificationsNode ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> tokens = token . split_contents ( ) <EOL> if len ( tokens ) == <NUM_LIT:1> : <EOL> return cls ( obj = '<STR_LIT>' , target = '<STR_LIT>' ) <EOL> if len ( tokens ) > <NUM_LIT:3> : <EOL> raise template . TemplateSyntaxError ( _ ( "<STR_LIT>" ) ) <EOL> elif tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( _ ( "<STR_LIT>" ) ) <EOL> elif not tokens [ <NUM_LIT:2> ] : <EOL> raise template . TemplateSyntaxError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> return cls ( obj = '<STR_LIT>' , target = tokens [ <NUM_LIT:2> ] ) <EOL> def render ( self , context ) : <EOL> if self . obj == '<STR_LIT>' : <EOL> request = context [ '<STR_LIT>' ] <EOL> user = request . user <EOL> if user . is_authenticated ( ) : <EOL> notifications = user . notifications . active ( ) . prefetch ( ) <EOL> return self . generate_html ( notifications ) <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def user_notifications ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return UserNotification . handle_token ( parser , token ) </s>
