src/cm/views/notifications.py
changeset 63 0a610af4033f
parent 62 381538cb3a8b
child 64 d0478be65c29
equal deleted inserted replaced
62:381538cb3a8b 63:0a610af4033f
     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 cm.utils.embed import embed_html
       
     7 from django import forms
       
     8 from django.conf import settings
       
     9 from django.contrib.auth.decorators import login_required
       
    10 from django.core.urlresolvers import reverse
       
    11 from django.http import HttpResponse, Http404, HttpResponseRedirect
       
    12 from django.shortcuts import get_object_or_404, render_to_response
       
    13 from django.template import RequestContext
       
    14 from django.template.loader import render_to_string
       
    15 from django.utils import feedgenerator
       
    16 from django.utils.translation import ugettext as _
       
    17 
       
    18 import re
       
    19 import time
       
    20 
       
    21 @login_required
       
    22 def notifications(request):
       
    23     workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=request.user, active=True).count()
       
    24     own_notify_check = Notification.objects.filter(text=None,type='own',user=request.user, active=True).count()
       
    25     
       
    26     if request.method == 'POST':
       
    27         if 'activate' in request.POST:
       
    28             Configuration.objects.set_key('private_feed_key', generate_key())
       
    29             display_message(request, _(u"Private feed activated."))            
       
    30         if 'reset' in request.POST:
       
    31             Configuration.objects.set_key('private_feed_key', generate_key())
       
    32             display_message(request, _(u"Private feed reseted."))            
       
    33         if request.POST.get('notif_id',None):
       
    34             notif_id = request.POST.get('notif_id')
       
    35             notif_type = 'own' if notif_id == 'own_notify_check' else 'workspace' 
       
    36             notif_val = request.POST.get(notif_id,None)
       
    37             if notif_val != None :
       
    38                 Notification.objects.set_notification(text=None, type=notif_type, active=(notif_val == 'true'), email_or_user=request.user)
       
    39     
       
    40     return render_to_response('site/notifications.html', {'workspace_notify_check':workspace_notify_check,
       
    41                                                           'own_notify_check' :own_notify_check, 
       
    42                                                           }, context_instance=RequestContext(request))
       
    43     
       
    44 
       
    45 # force a POST (database modifications)
       
    46 def desactivate_notification(request, adminkey):
       
    47     try:
       
    48         notification = Notification.objects.get(adminkey=adminkey)
       
    49     except Notification.DoesNotExist:        
       
    50         display_message(request, _(u"This notification has already been desactivated."))
       
    51         return HttpResponseRedirect(reverse('index'))
       
    52     
       
    53     if request.method == 'POST':
       
    54         if request.POST['adminkey'] == adminkey:
       
    55             notification.desactivate()
       
    56             display_message(request, _(u"Notification desactivated."))                
       
    57             return HttpResponseRedirect(reverse('index'))
       
    58     return render_to_response('site/notifications_desactivate.html', 
       
    59                               {'notification' : notification,
       
    60                                'title' : _(u'Desactivate notification?'),                               
       
    61                                },
       
    62                                context_instance=RequestContext(request))
       
    63 
       
    64 
       
    65 def text_notifications(request, key):
       
    66     text = get_text_by_keys_or_404(key)
       
    67     user = request.user if request.user.is_authenticated() else None
       
    68 
       
    69     from cm.security import user_has_perm # import here!
       
    70     anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text)
       
    71     text_notify_check = Notification.objects.filter(text=text,type='text',user=user, active=True).count()
       
    72     workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count()
       
    73     
       
    74     #embed_code = '<iframe frameborder="0" src="%s%s" style="height: 166px; width: 99.9%%; position: relative; top: 0px;">'%(settings.SITE_URL, reverse('text-view-comments-frame', args=[text.key]))
       
    75     embed_code = embed_html(text.key) ;   
       
    76     
       
    77     if request.method == 'POST':
       
    78         if 'activate' in request.POST:
       
    79             text.private_feed_key = generate_key()
       
    80             text.save()
       
    81             display_message(request, _(u"Private feed activated."))            
       
    82         if 'reset' in request.POST:
       
    83             text.private_feed_key = generate_key()
       
    84             text.save()
       
    85             display_message(request, _(u"Private notifications feed reseted."))
       
    86             
       
    87         if request.POST.get('notif_id',None):
       
    88             notif_id = request.POST.get('notif_id')
       
    89             notif_val = request.POST.get(notif_id,None)
       
    90             if notif_val != None :
       
    91                 Notification.objects.set_notification(text=text, type='text', active=(notif_val == 'true'), email_or_user=request.user)
       
    92 
       
    93     template_dict = {
       
    94                      'text' : text,
       
    95                      'workspace_notify_check' : workspace_notify_check,
       
    96                      'text_notify_check' : text_notify_check,
       
    97                      'anonymous_can_view_text' : anonymous_can_view_text,
       
    98                      'embed_code': embed_code
       
    99                      }
       
   100     return render_to_response('site/text_notifications.html', template_dict , context_instance=RequestContext(request))