src/cm/views/notifications.py
author reno
Tue, 08 Dec 2009 16:51:48 +0100
changeset 24 c8a95e540b79
parent 12 f69ff46d3240
permissions -rw-r--r--
ENH : adding comments on previous version now possible
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     1
from cm.exception import UnauthorizedException
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     2
from cm.message import display_message
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     3
from cm.models import ApplicationConfiguration, Notification, Configuration, UserRole
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     4
from cm.models_base import generate_key
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     5
from cm.views import get_text_by_keys_or_404
24
c8a95e540b79 ENH : adding comments on previous version now possible
reno
parents: 12
diff changeset
     6
from cm.utils.embed import embed_html
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     7
from django import forms
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     8
from django.conf import settings
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     9
from django.contrib.auth.decorators import login_required
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    10
from django.core.urlresolvers import reverse
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    11
from django.http import HttpResponse, Http404, HttpResponseRedirect
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    12
from django.shortcuts import get_object_or_404, render_to_response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    13
from django.template import RequestContext
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    14
from django.template.loader import render_to_string
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    15
from django.utils import feedgenerator
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    16
from django.utils.translation import ugettext as _
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    17
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    18
import re
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    19
import time
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    20
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    21
@login_required
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    22
def notifications(request):
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    23
    workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=request.user, active=True).count()
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    24
    own_notify_check = Notification.objects.filter(text=None,type='own',user=request.user, active=True).count()
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    25
    
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    26
    if request.method == 'POST':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    27
        if 'activate' in request.POST:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    28
            Configuration.objects.set_key('private_feed_key', generate_key())
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    29
            display_message(request, _(u"Private feed activated."))            
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    30
        if 'reset' in request.POST:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    31
            Configuration.objects.set_key('private_feed_key', generate_key())
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    32
            display_message(request, _(u"Private feed reseted."))            
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    33
        if request.POST.get('notif_id',None):
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    34
            notif_id = request.POST.get('notif_id')
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    35
            notif_type = 'own' if notif_id == 'own_notify_check' else 'workspace' 
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    36
            notif_val = request.POST.get(notif_id,None)
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    37
            if notif_val != None :
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    38
                Notification.objects.set_notification(text=None, type=notif_type, active=(notif_val == 'true'), email_or_user=request.user)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    39
    
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    40
    return render_to_response('site/notifications.html', {'workspace_notify_check':workspace_notify_check,
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    41
                                                          'own_notify_check' :own_notify_check, 
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    42
                                                          }, context_instance=RequestContext(request))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    43
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    44
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    45
# force a POST (database modifications)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    46
def desactivate_notification(request, adminkey):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    47
    try:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    48
        notification = Notification.objects.get(adminkey=adminkey)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    49
    except Notification.DoesNotExist:        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    50
        display_message(request, _(u"This notification has already been desactivated."))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    51
        return HttpResponseRedirect(reverse('index'))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    52
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    53
    if request.method == 'POST':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    54
        if request.POST['adminkey'] == adminkey:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    55
            notification.desactivate()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    56
            display_message(request, _(u"Notification desactivated."))                
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    57
            return HttpResponseRedirect(reverse('index'))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    58
    return render_to_response('site/notifications_desactivate.html', 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    59
                              {'notification' : notification,
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    60
                               'title' : _(u'Desactivate notification?'),                               
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    61
                               },
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    62
                               context_instance=RequestContext(request))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    63
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    64
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    65
def text_notifications(request, key):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    66
    text = get_text_by_keys_or_404(key)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    67
    user = request.user if request.user.is_authenticated() else None
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    68
2
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    69
    from cm.security import user_has_perm # import here!
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    70
    anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text)
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    71
    text_notify_check = Notification.objects.filter(text=text,type='text',user=user, active=True).count()
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    72
    workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count()
2
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    73
    
24
c8a95e540b79 ENH : adding comments on previous version now possible
reno
parents: 12
diff changeset
    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]))
c8a95e540b79 ENH : adding comments on previous version now possible
reno
parents: 12
diff changeset
    75
    embed_code = embed_html(text.key) ;   
2
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    76
    
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    77
    if request.method == 'POST':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    78
        if 'activate' in request.POST:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    79
            text.private_feed_key = generate_key()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    80
            text.save()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    81
            display_message(request, _(u"Private feed activated."))            
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    82
        if 'reset' in request.POST:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    83
            text.private_feed_key = generate_key()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    84
            text.save()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    85
            display_message(request, _(u"Private notifications feed reseted."))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    86
            
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    87
        if request.POST.get('notif_id',None):
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    88
            notif_id = request.POST.get('notif_id')
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    89
            notif_val = request.POST.get(notif_id,None)
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    90
            if notif_val != None :
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    91
                Notification.objects.set_notification(text=text, type='text', active=(notif_val == 'true'), email_or_user=request.user)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    92
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    93
    template_dict = {
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    94
                     'text' : text,
12
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    95
                     'workspace_notify_check' : workspace_notify_check,
f69ff46d3240 various notification changes
reno
parents: 2
diff changeset
    96
                     'text_notify_check' : text_notify_check,
2
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    97
                     'anonymous_can_view_text' : anonymous_can_view_text,
bc4b7b630f6a fix notifications
reno
parents: 0
diff changeset
    98
                     'embed_code': embed_code
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    99
                     }
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   100
    return render_to_response('site/text_notifications.html', template_dict , context_instance=RequestContext(request))