src/cm/views/notifications.py
changeset 12 f69ff46d3240
parent 2 bc4b7b630f6a
child 24 c8a95e540b79
equal deleted inserted replaced
11:7b8167c4aa6f 12:f69ff46d3240
    17 import re
    17 import re
    18 import time
    18 import time
    19 
    19 
    20 @login_required
    20 @login_required
    21 def notifications(request):
    21 def notifications(request):
    22     notify_check = Notification.objects.filter(text=None,type=None,user=request.user, active=True).count()
    22     workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=request.user, active=True).count()
    23     own_check = Notification.objects.filter(text=None,type='own',user=request.user, active=True).count()
    23     own_notify_check = Notification.objects.filter(text=None,type='own',user=request.user, active=True).count()
       
    24     
    24     if request.method == 'POST':
    25     if request.method == 'POST':
    25         if 'activate' in request.POST:
    26         if 'activate' in request.POST:
    26             Configuration.objects.set_key('private_feed_key', generate_key())
    27             Configuration.objects.set_key('private_feed_key', generate_key())
    27             display_message(request, _(u"Private feed activated."))            
    28             display_message(request, _(u"Private feed activated."))            
    28         if 'reset' in request.POST:
    29         if 'reset' in request.POST:
    29             Configuration.objects.set_key('private_feed_key', generate_key())
    30             Configuration.objects.set_key('private_feed_key', generate_key())
    30             display_message(request, _(u"Private feed reseted."))            
    31             display_message(request, _(u"Private feed reseted."))            
    31         if request.POST.get('notify_check',None) == u'true':
    32         if request.POST.get('notif_id',None):
    32             if not notify_check:            
    33             notif_id = request.POST.get('notif_id')
    33                 notification = Notification.objects.create_notification(text=None, type=None, email_or_user=request.user)
    34             notif_type = 'own' if notif_id == 'own_notify_check' else 'workspace' 
    34                 # ajax display_message(request, _(u"Notifications activated."))
    35             notif_val = request.POST.get(notif_id,None)
    35         elif request.POST.get('notify_check',None) == u'false':
    36             if notif_val != None :
    36             Notification.objects.filter(text=None,type=None,user=request.user).delete()
    37                 Notification.objects.set_notification(text=None, type=notif_type, active=(notif_val == 'true'), email_or_user=request.user)
    37             notify_check = False
       
    38                 
       
    39         if request.POST.get('own_check',None) == u'true':
       
    40             Notification.objects.set_notification_to_own_discussions(text=None,email_or_user=request.user, active=True)
       
    41         elif request.POST.get('own_check',None) == u'false':
       
    42             Notification.objects.set_notification_to_own_discussions(text=None,email_or_user=request.user, active=False)
       
    43             own_check = False
       
    44     
    38     
    45     return render_to_response('site/notifications.html', {'notify_check':notify_check,
    39     return render_to_response('site/notifications.html', {'workspace_notify_check':workspace_notify_check,
    46                                                           'own_check' :own_check, 
    40                                                           'own_notify_check' :own_notify_check, 
    47                                                           }, context_instance=RequestContext(request))
    41                                                           }, context_instance=RequestContext(request))
    48     
    42     
    49 
    43 
    50 # force a POST (database modifications)
    44 # force a POST (database modifications)
    51 def desactivate_notification(request, adminkey):
    45 def desactivate_notification(request, adminkey):
    71     text = get_text_by_keys_or_404(key)
    65     text = get_text_by_keys_or_404(key)
    72     user = request.user if request.user.is_authenticated() else None
    66     user = request.user if request.user.is_authenticated() else None
    73 
    67 
    74     from cm.security import user_has_perm # import here!
    68     from cm.security import user_has_perm # import here!
    75     anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text)
    69     anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text)
    76     own_check = Notification.objects.filter(text=text,type='own',user=user).count()
    70     text_notify_check = Notification.objects.filter(text=text,type='text',user=user, active=True).count()
    77     all_check = Notification.objects.filter(text=text,type=None,user=user).count()
    71     workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count()
    78     
       
    79     
    72     
    80     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]))   
    73     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]))   
    81     
    74     
    82     if request.method == 'POST':
    75     if request.method == 'POST':
    83         if 'activate' in request.POST:
    76         if 'activate' in request.POST:
    87         if 'reset' in request.POST:
    80         if 'reset' in request.POST:
    88             text.private_feed_key = generate_key()
    81             text.private_feed_key = generate_key()
    89             text.save()
    82             text.save()
    90             display_message(request, _(u"Private notifications feed reseted."))
    83             display_message(request, _(u"Private notifications feed reseted."))
    91             
    84             
    92         if request.POST.get('all_check',None) == u'true':            
    85         if request.POST.get('notif_id',None):
    93             if not all_check:          
    86             notif_id = request.POST.get('notif_id')
    94                 notification = Notification.objects.create_notification(text=text, type=None, email_or_user=user)
    87             notif_val = request.POST.get(notif_id,None)
    95 
    88             if notif_val != None :
    96         if request.POST.get('all_check',None) == u'false':            
    89                 Notification.objects.set_notification(text=text, type='text', active=(notif_val == 'true'), email_or_user=request.user)
    97             notification = Notification.objects.filter(text=text, type=None, user=user).delete()
       
    98 
    90 
    99     template_dict = {
    91     template_dict = {
   100                      'text' : text,
    92                      'text' : text,
   101                      'all_check' : all_check,
    93                      'workspace_notify_check' : workspace_notify_check,
       
    94                      'text_notify_check' : text_notify_check,
   102                      'anonymous_can_view_text' : anonymous_can_view_text,
    95                      'anonymous_can_view_text' : anonymous_can_view_text,
   103                      'embed_code': embed_code
    96                      'embed_code': embed_code
   104                      }
    97                      }
   105     return render_to_response('site/text_notifications.html', template_dict , context_instance=RequestContext(request))
    98     return render_to_response('site/text_notifications.html', template_dict , context_instance=RequestContext(request))