Esempio n. 1
0
def text_history(request, key):
    text = get_text_by_keys_or_404(key)

    if request.method == 'POST':
        v1_key = request.POST.get('newkey', None)
        v2_key = request.POST.get('oldkey', None)
        if v1_key and v2_key:
            return redirect(request,
                            'text-history-compare',
                            args=[text.key, v2_key, v1_key])

    text_versions = text.get_versions()
    paginate_by = get_int(request.GET, 'paginate', TEXT_PAGINATION)

    last_last_version = text_versions[1] if len(text_versions) > 1 else None
    first_version = text_versions[len(text_versions) - 1]
    context = {
        'text': text,
        'last_version': text.last_text_version,
        'last_last_version': last_last_version,
        'first_version': first_version
    }
    return object_list(
        request,
        text_versions,
        template_name='site/text_history.html',
        paginate_by=paginate_by,
        extra_context=context,
    )
Esempio n. 2
0
def text_list(request):
    paginate_by = get_int(request.GET,'paginate',TEXT_PAGINATION)
    tag_selected = request.GET.get('tag_selected', 0)
        
    order_by = get_among(request.GET,'order',('title','author','modified','-title','-author','-modified'),'-modified')

    if request.method == 'POST':
        action = request.POST.get('action',None)
        text_keys = get_keys_from_dict(request.POST, 'check-').keys()        
        if action == 'delete':
            for text_key in text_keys:
                text = Text.objects.get(key=text_key)
                if has_perm(request, 'can_delete_text', text=text):
                    text.delete()
                else:
                    raise UnauthorizedException('No perm can_delete_text on comment') 
            display_message(request, _(u'%(nb_texts)i text(s) deleted') %{'nb_texts':len(text_keys)})
            return HttpResponseRedirect(reverse('text'))

    texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by)

    try:
        tag_list = Tag.objects.usage_for_queryset(TextVersion.objects.filter(id__in = [t.last_text_version_id for t in get_texts_with_perm(request, 'can_view_text')]))
    except EmptyResultSet:
        tag_list = []
    context = {    
               'tag_list' : tag_list,
               'tag_selected': tag_selected,
               }

    if tag_selected:     
        tag_ids = Tag.objects.filter(name=tag_selected)
        if tag_ids:   
            content_type_id = ContentType.objects.get_for_model(TextVersion).pk
            # table cm_userprofile is not present if display_suspended_users: fix this 
            texts = texts.extra(where=['tagging_taggeditem.object_id = cm_text.last_text_version_id', 
                                       'tagging_taggeditem.content_type_id = %i' %content_type_id,
                                       'tagging_taggeditem.tag_id = %i' %tag_ids[0].id],
                                tables=['tagging_taggeditem'],
                                )
    
    return object_list(request, texts,
                       template_name = 'site/text_list.html',
                       paginate_by = paginate_by,
                       extra_context=context,
                       )
Esempio n. 3
0
def text_history(request, key):
    text = get_text_by_keys_or_404(key)
    
    if request.method == 'POST':
        v1_key = request.POST.get('newkey',None)
        v2_key = request.POST.get('oldkey',None)
        if v1_key and v2_key:  
            return redirect(request, 'text-history-compare', args=[text.key, v2_key, v1_key ])
        
    text_versions = text.get_versions()
    paginate_by = get_int(request.GET,'paginate',TEXT_PAGINATION)

    last_last_version = text_versions[1] if len(text_versions)>1 else None 
    first_version = text_versions[len(text_versions) - 1]
    context = {'text':text, 'last_version':text.last_text_version, 'last_last_version':last_last_version, 'first_version':first_version}
    return object_list(request, text_versions,
                       template_name = 'site/text_history.html',
                       paginate_by = paginate_by,
                       extra_context=context,
                       )
Esempio n. 4
0
def text_share(request, key):
    display_suspended_users = get_int(request.GET, 'display', 0)
    tag_selected = request.GET.get('tag_selected', 0)
    paginate_by = get_int(request.GET, 'paginate', USER_PAGINATION)    
    
    text = get_text_by_keys_or_404(key)
    order_by = get_among(request.GET,'order',('user__username',
                                              'user__email',
                                              '-user__username',
                                              '-user__email',
                                              'role__name',
                                              '-role__name',
                                              ),
                          'user__username')
    
    UserRole.objects.create_userroles_text(text)
    
    if request.method == 'POST':
        if 'save' in request.POST:
            user_profile_keys_roles = get_keys_from_dict(request.POST, 'user-role-')
            count = 0
            for user_profile_key in user_profile_keys_roles:
                role_id = user_profile_keys_roles[user_profile_key]
                if not user_profile_key:
                    user_role = UserRole.objects.get(user = None, text = text)
                else:                    
                    user_role = UserRole.objects.get(user__userprofile__key = user_profile_key, text = text)
                if (role_id != u'' or user_role.role_id!=None) and role_id!=unicode(user_role.role_id):
                    if role_id:
                        user_role.role_id = int(role_id)
                    else:
                        user_role.role_id = None
                    user_role.save()
                    count += 1
            display_message(request, _(u'%(count)i user(s) role modified') %{'count':count})                
            return HttpResponseRedirect(reverse('text-share', args=[text.key]))    
    
    anon_role = UserRole.objects.get(user = None, text = text).role
    global_anon_role = UserRole.objects.get(user = None, text = None).role
        
    context = {
               'anon_role' : anon_role,
               'global_anon_role' : global_anon_role,
               'all_roles' : Role.objects.all(),
               'anon_roles' : Role.objects.filter(anon = True),
               'text' : text,
               'display_suspended_users' : display_suspended_users,
               'tag_list' : Tag.objects.usage_for_model(UserProfile),
               'tag_selected': tag_selected,               
               }

    query = UserRole.objects.filter(text=text).filter(~Q(user=None)).order_by(order_by)
    if not display_suspended_users:
        query = query.exclude(Q(user__userprofile__is_suspended=True) & Q(user__is_active=True))
    else:
        # trick to include userprofile table anyway (to filter by tags)
        query = query.filter(Q(user__userprofile__is_suspended=True) | Q(user__userprofile__is_suspended=False))

    if tag_selected:     
        tag_ids = Tag.objects.filter(name=tag_selected)
        if tag_ids:   
            content_type_id = ContentType.objects.get_for_model(UserProfile).pk
            query = query.extra(where=['tagging_taggeditem.object_id = cm_userprofile.id', 
                                       'tagging_taggeditem.content_type_id = %i' %content_type_id,
                                       'tagging_taggeditem.tag_id = %i' %tag_ids[0].id],
                                tables=['tagging_taggeditem'],
                                )

    return object_list(request, query,
                       template_name = 'site/text_share.html',
                       paginate_by = paginate_by,
                       extra_context = context,
                       )
Esempio n. 5
0
def dashboard(request):
    request.session.set_test_cookie()
    if request.user.is_authenticated():
        act_view = {
                    'view_texts' : get_int(request.GET, 'view_texts',1),
                    'view_comments' : get_int(request.GET, 'view_comments',1),
                    'view_users' : get_int(request.GET, 'view_users',1),
                    }
            
        paginate_by = get_int(request.GET, 'paginate', ACTIVITY_PAGINATION)
                
        # texts with can_view_unapproved_comment perms
        moderator_texts = get_texts_with_perm(request, 'can_view_unapproved_comment')
        viewer_texts = get_texts_with_perm(request, 'can_view_approved_comment')
        all_texts_ids = [t.id for t in moderator_texts] + [t.id for t in viewer_texts]
                    
        span = get_among(request.GET, 'span', ('day','month','week',),'week')        
        template_dict = { 
                         'span' : span,
                         'last_texts' : get_texts_with_perm(request, 'can_view_text').order_by('-modified')[:RECENT_TEXT_NB],
                         'last_comments' : Comment.objects.filter(text_version__text__in=all_texts_ids).order_by('-created')[:RECENT_COMMENT_NB],# TODO: useful?
                         #'last_users' : User.objects.all().order_by('-date_joined')[:5],
                         }
        template_dict.update(act_view)
        
        all_activities = {
                               'view_comments' : ['comment_created','comment_removed'],
                               'view_users' : ['user_created', 'user_activated', 'user_suspended','user_enabled',],
                               'view_texts' : ['text_created','text_removed', 'text_edited', 'text_edited_new_version'],
                               }
        
        selected_activities = []
        [selected_activities.extend(all_activities[k]) for k in act_view.keys() if act_view[k]]
        
        activities = Activity.objects.filter(type__in = selected_activities)
        if not has_perm(request,'can_manage_workspace'):
            texts = get_texts_with_perm(request, 'can_view_text')
            activities = activities.filter(Q(text__in=texts))
            
            comments = [] 
            [comments.extend(get_viewable_comments(request, t.last_text_version.comment_set.all(), t)) for t in texts]

            activities = activities.filter(Q(comment__in=comments) | Q(comment=None) )
            template_dict['to_mod_profiles'] = []
        else:
            template_dict['to_mod_profiles'] = UserProfile.objects.filter(user__is_active=False).filter(is_suspended=True).order_by('-user__date_joined')[:MODERATE_NB]
        template_dict['to_mod_comments'] = Comment.objects.filter(state='pending').filter(text_version__text__in=moderator_texts).order_by('-modified')[:MODERATE_NB-len(template_dict['to_mod_profiles'])]

        activities = activities.order_by('-created')
        return object_list(request, activities,
                           template_name = 'site/dashboard.html',
                           paginate_by = paginate_by,
                           extra_context = template_dict,
                           )
        
    else:
        if request.method == 'POST':
            form = AuthenticationForm(request, request.POST)
            if form.is_valid():
                user = form.get_user()
                user.backend = 'django.contrib.auth.backends.ModelBackend'
                cm_login(request, user)            
                display_message(request, _(u"You're logged in!"))
                return HttpResponseRedirect(reverse('index'))
        else:
            form = AuthenticationForm()        


        public_texts = get_texts_with_perm(request, 'can_view_text').order_by('-modified')

        template_dict = {
                         'form' : form,
                         'texts' : public_texts,
                         }
        return render_to_response('site/non_authenticated_index.html', template_dict, context_instance=RequestContext(request))
Esempio n. 6
0
File: user.py Progetto: debon/comt
def user_list(request):    
    display_suspended_users = get_int(request.GET, 'display', 0)
    tag_selected = request.GET.get('tag_selected', 0)
    paginate_by = get_int(request.GET, 'paginate', USER_PAGINATION)
    order_by = get_among(request.GET, 'order', ('user__username',
                                              'user__email',
                                              '-user__username',
                                              '-user__email',
                                              'role__name',
                                              '-role__name',
                                              'user__date_joined',
                                              '-user__date_joined',
                                              ),
                          'user__username')
    
    UserRole.objects.create_userroles_text(None)
    
    if request.method == 'POST':
        # bulk apply
        if 'apply' in request.POST and not 'save' in request.POST:
            action = request.POST.get('action', None)
            user_profile_keys = get_keys_from_dict(request.POST, 'check-').keys()
            if action == 'disable':
                for user_profile_key in user_profile_keys:
                    profile = UserProfile.objects.get(key=user_profile_key)
                    if profile != request.user.get_profile():
                        profile.is_suspended = True
                        profile.save()             
                display_message(request, _(u"%(count)i User's access suspended") % {'count':len(user_profile_keys)})

            if action == 'enable':
                for user_profile_key in user_profile_keys:
                    profile = UserProfile.objects.get(key=user_profile_key)
                    profile.is_suspended = False
                    profile.save()             
                display_message(request, _(u"%(count)i User's access enabled") % {'count':len(user_profile_keys)})
            
            ROLE_RE = re.compile('role_(\d*)')
            match = ROLE_RE.match(action)
              
            if match:
                role_id = match.group(1)
                for user_profile_key in user_profile_keys:
                    user_role = UserRole.objects.get(user__userprofile__key=user_profile_key, text=None)
                    user_role.role_id = role_id
                    user_role.save()
                display_message(request, _(u"%(count)i user(s) role modified") % {'count':len(user_profile_keys)})
                
            return HttpResponseRedirect(reverse('user'))
        
        if 'save' in request.POST:
            user_profile_keys_roles = get_keys_from_dict(request.POST, 'user-role-')
            count = 0
            for user_profile_key in user_profile_keys_roles:
                role_id = user_profile_keys_roles[user_profile_key]
                if not user_profile_key:
                    user_role = UserRole.objects.get(user=None, text=None)
                else:                    
                    user_role = UserRole.objects.get(user__userprofile__key=user_profile_key, text=None)
                if (role_id != u'' or user_role.role_id != None) and role_id != unicode(user_role.role_id):
                    if role_id:
                        user_role.role_id = int(role_id)
                    else:
                        user_role.role_id = None
                    user_role.save()
                    count += 1
            display_message(request, _(u"%(count)i user(s) role modified") % {'count':count})                
            return HttpResponseRedirect(reverse('user'))
    try:
        anon_role = UserRole.objects.get(user=None, text=None).role
    except UserRole.DoesNotExist:
        anon_role = None
        
    context = {
               'anon_role' : anon_role,
               'all_roles' : Role.objects.all(),
               'anon_roles' : Role.objects.filter(anon=True),
               'display_suspended_users' : display_suspended_users,
               'tag_list' : Tag.objects.usage_for_model(UserProfile),
               'tag_selected': tag_selected,
               'SHOW_EMAILS_IN_ADMIN': SHOW_EMAILS_IN_ADMIN,
               }
    
    query = UserRole.objects.select_related().filter(text=None).filter(~Q(user=None)).order_by(order_by)
    if not display_suspended_users:
        query = query.exclude(Q(user__userprofile__is_suspended=True) & Q(user__is_active=True))
    else:
        # trick to include userprofile table anyway (to filter by tags)
        query = query.filter(Q(user__userprofile__is_suspended=True) | Q(user__userprofile__is_suspended=False))

    if tag_selected:     
        tag_ids = Tag.objects.filter(name=tag_selected)
        if tag_ids:   
            content_type_id = ContentType.objects.get_for_model(UserProfile).pk
            query = query.extra(where=['tagging_taggeditem.object_id = cm_userprofile.id', 
                                       'tagging_taggeditem.content_type_id = %i' %content_type_id,
                                       'tagging_taggeditem.tag_id = %i' %tag_ids[0].id],
                                tables=['tagging_taggeditem'],
                                )

    return object_list(request, query,
                       template_name='site/user_list.html',
                       paginate_by=paginate_by,
                       extra_context=context,
                       )
Esempio n. 7
0
def user_list(request):
    display_suspended_users = get_int(request.GET, 'display', 0)
    tag_selected = request.GET.get('tag_selected', 0)
    paginate_by = get_int(request.GET, 'paginate', USER_PAGINATION)
    order_by = get_among(request.GET, 'order', (
        'user__username',
        'user__email',
        '-user__username',
        '-user__email',
        'role__name',
        '-role__name',
        'user__date_joined',
        '-user__date_joined',
    ), 'user__username')

    UserRole.objects.create_userroles_text(None)

    if request.method == 'POST':
        # bulk apply
        if 'apply' in request.POST and not 'save' in request.POST:
            action = request.POST.get('action', None)
            user_profile_keys = get_keys_from_dict(request.POST,
                                                   'check-').keys()
            if action == 'disable':
                for user_profile_key in user_profile_keys:
                    profile = UserProfile.objects.get(key=user_profile_key)
                    if profile != request.user.get_profile():
                        profile.is_suspended = True
                        profile.save()
                display_message(
                    request,
                    _(u"%(count)i User's access suspended") %
                    {'count': len(user_profile_keys)})

            if action == 'enable':
                for user_profile_key in user_profile_keys:
                    profile = UserProfile.objects.get(key=user_profile_key)
                    profile.is_suspended = False
                    profile.save()
                display_message(
                    request,
                    _(u"%(count)i User's access enabled") %
                    {'count': len(user_profile_keys)})

            ROLE_RE = re.compile('role_(\d*)')
            match = ROLE_RE.match(action)

            if match:
                role_id = match.group(1)
                for user_profile_key in user_profile_keys:
                    user_role = UserRole.objects.get(
                        user__userprofile__key=user_profile_key, text=None)
                    user_role.role_id = role_id
                    user_role.save()
                display_message(
                    request,
                    _(u"%(count)i user(s) role modified") %
                    {'count': len(user_profile_keys)})

            return HttpResponseRedirect(reverse('user'))

        if 'save' in request.POST:
            user_profile_keys_roles = get_keys_from_dict(
                request.POST, 'user-role-')
            count = 0
            for user_profile_key in user_profile_keys_roles:
                role_id = user_profile_keys_roles[user_profile_key]
                if not user_profile_key:
                    user_role = UserRole.objects.get(user=None, text=None)
                else:
                    user_role = UserRole.objects.get(
                        user__userprofile__key=user_profile_key, text=None)
                if (role_id != u'' or user_role.role_id != None
                    ) and role_id != unicode(user_role.role_id):
                    if role_id:
                        user_role.role_id = int(role_id)
                    else:
                        user_role.role_id = None
                    user_role.save()
                    count += 1
            display_message(
                request,
                _(u"%(count)i user(s) role modified") % {'count': count})
            return HttpResponseRedirect(reverse('user'))
    try:
        anon_role = UserRole.objects.get(user=None, text=None).role
    except UserRole.DoesNotExist:
        anon_role = None

    context = {
        'anon_role': anon_role,
        'all_roles': Role.objects.all(),
        'anon_roles': Role.objects.filter(anon=True),
        'display_suspended_users': display_suspended_users,
        'tag_list': Tag.objects.usage_for_model(UserProfile),
        'tag_selected': tag_selected,
        'SHOW_EMAILS_IN_ADMIN': SHOW_EMAILS_IN_ADMIN,
    }

    query = UserRole.objects.select_related().filter(
        text=None).filter(~Q(user=None)).order_by(order_by)
    if not display_suspended_users:
        query = query.exclude(
            Q(user__userprofile__is_suspended=True) & Q(user__is_active=True))
    else:
        # trick to include userprofile table anyway (to filter by tags)
        query = query.filter(
            Q(user__userprofile__is_suspended=True)
            | Q(user__userprofile__is_suspended=False))

    if tag_selected:
        tag_ids = Tag.objects.filter(name=tag_selected)
        if tag_ids:
            content_type_id = ContentType.objects.get_for_model(UserProfile).pk
            query = query.extra(
                where=[
                    'tagging_taggeditem.object_id = cm_userprofile.id',
                    'tagging_taggeditem.content_type_id = %i' %
                    content_type_id,
                    'tagging_taggeditem.tag_id = %i' % tag_ids[0].id
                ],
                tables=['tagging_taggeditem'],
            )

    return object_list(
        request,
        query,
        template_name='site/user_list.html',
        paginate_by=paginate_by,
        extra_context=context,
    )