src/cm/views/notifications.py
changeset 0 40c8f766c9b8
child 2 bc4b7b630f6a
equal deleted inserted replaced
-1:000000000000 0:40c8f766c9b8
       
     1 from cm.exception import UnauthorizedException
       
     2 from cm.message import display_message
       
     3 from cm.models import ApplicationConfiguration, Notification, Configuration, UserRole
       
     4 from cm.models_base import generate_key
       
     5 from cm.views import get_text_by_keys_or_404
       
     6 from django import forms
       
     7 from django.conf import settings
       
     8 from django.contrib.auth.decorators import login_required
       
     9 from django.core.urlresolvers import reverse
       
    10 from django.http import HttpResponse, Http404, HttpResponseRedirect
       
    11 from django.shortcuts import get_object_or_404, render_to_response
       
    12 from django.template import RequestContext
       
    13 from django.template.loader import render_to_string
       
    14 from django.utils import feedgenerator
       
    15 from django.utils.translation import ugettext as _
       
    16 from cm.security import user_has_perm # import here!
       
    17 
       
    18 import re
       
    19 import time
       
    20 
       
    21 @login_required
       
    22 def notifications(request):
       
    23     notify_check = Notification.objects.filter(text=None,type=None,user=request.user, active=True).count()
       
    24     own_check = Notification.objects.filter(text=None,type='own',user=request.user, active=True).count()
       
    25     if request.method == 'POST':
       
    26         if 'activate' in request.POST:
       
    27             Configuration.objects.set_key('private_feed_key', generate_key())
       
    28             display_message(request, _(u"Private feed activated."))            
       
    29         if 'reset' in request.POST:
       
    30             Configuration.objects.set_key('private_feed_key', generate_key())
       
    31             display_message(request, _(u"Private feed reseted."))            
       
    32         if request.POST.get('notify_check',None) == u'true':
       
    33             if not notify_check:            
       
    34                 notification = Notification.objects.create_notification(text=None, type=None, email_or_user=request.user)
       
    35                 # ajax display_message(request, _(u"Notifications activated."))
       
    36         elif request.POST.get('notify_check',None) == u'false':
       
    37             Notification.objects.filter(text=None,type=None,user=request.user).delete()
       
    38             notify_check = False
       
    39                 
       
    40         if request.POST.get('own_check',None) == u'true':
       
    41             Notification.objects.set_notification_to_own_discussions(text=None,email_or_user=request.user, active=True)
       
    42         elif request.POST.get('own_check',None) == u'false':
       
    43             Notification.objects.set_notification_to_own_discussions(text=None,email_or_user=request.user, active=False)
       
    44             own_check = False
       
    45     
       
    46     return render_to_response('site/notifications.html', {'notify_check':notify_check,
       
    47                                                           'own_check' :own_check, 
       
    48                                                           }, context_instance=RequestContext(request))
       
    49     
       
    50 
       
    51 # force a POST (database modifications)
       
    52 def desactivate_notification(request, adminkey):
       
    53     try:
       
    54         notification = Notification.objects.get(adminkey=adminkey)
       
    55     except Notification.DoesNotExist:        
       
    56         display_message(request, _(u"This notification has already been desactivated."))
       
    57         return HttpResponseRedirect(reverse('index'))
       
    58     
       
    59     if request.method == 'POST':
       
    60         if request.POST['adminkey'] == adminkey:
       
    61             notification.desactivate()
       
    62             display_message(request, _(u"Notification desactivated."))                
       
    63             return HttpResponseRedirect(reverse('index'))
       
    64     return render_to_response('site/notifications_desactivate.html', 
       
    65                               {'notification' : notification,
       
    66                                'title' : _(u'Desactivate notification?'),                               
       
    67                                },
       
    68                                context_instance=RequestContext(request))
       
    69 
       
    70 
       
    71 def text_notifications(request, key):
       
    72     text = get_text_by_keys_or_404(key)
       
    73     user = request.user if request.user.is_authenticated() else None
       
    74 
       
    75     anonymous_can_view_text = False
       
    76     anonymous_user_role = UserRole.objects.get(user=None, text=text)
       
    77     if anonymous_user_role :
       
    78 	    anonymous_can_view_text = user_has_perm(anonymous_user_role.user, 'can_view_text', text=text)
       
    79     own_check = Notification.objects.filter(text=text,type='own',user=user).count()
       
    80     all_check = Notification.objects.filter(text=text,type=None,user=user).count()
       
    81     if request.method == 'POST':
       
    82         if 'activate' in request.POST:
       
    83             text.private_feed_key = generate_key()
       
    84             text.save()
       
    85             display_message(request, _(u"Private feed activated."))            
       
    86         if 'reset' in request.POST:
       
    87             text.private_feed_key = generate_key()
       
    88             text.save()
       
    89             display_message(request, _(u"Private notifications feed reseted."))
       
    90             
       
    91         if request.POST.get('all_check',None) == u'true':            
       
    92             if not all_check:          
       
    93                 notification = Notification.objects.create_notification(text=text, type=None, email_or_user=user)
       
    94 
       
    95         if request.POST.get('all_check',None) == u'false':            
       
    96             notification = Notification.objects.filter(text=text, type=None, user=user).delete()
       
    97 
       
    98     template_dict = {
       
    99                      'text' : text,
       
   100                      'all_check' : all_check,
       
   101                      'anonymous_can_view_text' : anonymous_can_view_text
       
   102                      }
       
   103     return render_to_response('site/text_notifications.html', template_dict , context_instance=RequestContext(request))