<s> from __future__ import unicode_literals <EOL> import json <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import render , redirect , get_object_or_404 <EOL> from django . views . decorators . http import require_POST <EOL> from django . http import Http404 , HttpResponse <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . utils . html import escape <EOL> from djconfig import config <EOL> from ... core import utils <EOL> from ... core . utils . paginator import yt_paginate <EOL> from ... core . utils . paginator . infinite_paginator import paginate <EOL> from ... topic . models import Topic <EOL> from . models import TopicNotification <EOL> from . forms import NotificationForm , NotificationCreationForm <EOL> @ require_POST <EOL> @ login_required <EOL> def create ( request , topic_id ) : <EOL> topic = get_object_or_404 ( Topic . objects . for_access ( request . user ) , <EOL> pk = topic_id ) <EOL> form = NotificationCreationForm ( user = request . user , topic = topic , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> else : <EOL> messages . error ( request , utils . render_form_errors ( form ) ) <EOL> return redirect ( request . POST . get ( '<STR_LIT>' , topic . get_absolute_url ( ) ) ) <EOL> @ require_POST <EOL> @ login_required <EOL> def update ( request , pk ) : <EOL> notification = get_object_or_404 ( TopicNotification , pk = pk , user = request . user ) <EOL> form = NotificationForm ( data = request . POST , instance = notification ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> else : <EOL> messages . error ( request , utils . render_form_errors ( form ) ) <EOL> return redirect ( request . POST . get ( '<STR_LIT>' , notification . topic . get_absolute_url ( ) ) ) <EOL> @ login_required <EOL> def index_ajax ( request ) : <EOL> if not request . is_ajax ( ) : <EOL> return Http404 ( ) <EOL> notifications = TopicNotification . objects . for_access ( request . user ) . order_by ( "<STR_LIT>" , "<STR_LIT>" ) . select_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> notifications = notifications [ : settings . ST_NOTIFICATIONS_PER_PAGE ] <EOL> notifications = [ <EOL> { <EOL> '<STR_LIT:user>' : escape ( n . comment . user . username ) , <EOL> '<STR_LIT:action>' : n . action , <EOL> '<STR_LIT:title>' : escape ( n . comment . topic . title ) , <EOL> '<STR_LIT:url>' : n . get_absolute_url ( ) , <EOL> '<STR_LIT>' : n . is_read <EOL> } <EOL> for n in notifications <EOL> ] <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:n>' : notifications , } ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required <EOL> def index_unread ( request ) : <EOL> notifications = TopicNotification . objects . for_access ( request . user ) . filter ( is_read = False ) <EOL> page = paginate ( <EOL> request , <EOL> query_set = notifications , <EOL> lookup_field = '<STR_LIT:date>' , <EOL> page_var = '<STR_LIT>' , <EOL> per_page = settings . ST_NOTIFICATIONS_PER_PAGE <EOL> ) <EOL> next_page_pk = None <EOL> if page : <EOL> next_page_pk = page [ - <NUM_LIT:1> ] . pk <EOL> context = { <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : next_page_pk <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> def index ( request ) : <EOL> notifications = yt_paginate ( <EOL> TopicNotification . objects . for_access ( request . user ) , <EOL> per_page = config . topics_per_page , <EOL> page_number = request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> ) <EOL> context = { '<STR_LIT>' : notifications , } <EOL> return render ( request , '<STR_LIT>' , context ) </s>
