Esempio n. 1
0
def settings(request,text_id):
    text = get_object_or_404(Text,pk = text_id)
    version = text.get_latest_version()
    nb_pots = version.get_commentsandreplies_count()     

    local_roles = list(ObjectUserRole.objects.filter(user = None, object_id__exact = text.id,
                                            content_type = ContentType.objects.get_for_model(Text),
                                           )) 
    local_roles = local_roles + list(ObjectUserRole.objects.filter(object_id__exact = text.id,
                                            content_type = ContentType.objects.get_for_model(Text),
                                           ).order_by('-user')) 

    forbidden_anonymous_permission = Permission.objects.get(codename = 'can_manage_comment_local_text')
    forbidden_anonymous_roleids = [role.id for role in Role.objects.filter(permissions__id =forbidden_anonymous_permission.id)]
    roles = Role.objects.all().order_by('order')
    role_choices = [(role.id, _(role.name)) for role in roles]
    role_anonymous_choices = [(0, _("None"))] + [(role.id, _(role.name)) for role in roles if role.id not in forbidden_anonymous_roleids]
    roles_help = [(0, string_concat(ROLES_INFO["None"][0], "<br />",ROLES_INFO["None"][1]))]
    roles_help +=  [(role.id, string_concat(ROLES_INFO[role.name][0], "<br />",ROLES_INFO[role.name][1])) for role in roles]
                                           
    add_form = AddEmailsForm(initial={'invite':True}, user=request.user, text=text, local_roles=local_roles, role_choices=role_choices)
    remove_remail_form = RemoveReMailUsersForm(initial={'invite':True}, local_roles=local_roles)
    users_rights_form = UsersRightsForm(user=request.user, 
                                        local_roles=local_roles, 
                                        role_choices=role_choices, 
                                        role_anonymous_choices = role_anonymous_choices, 
                                        removeremailform = remove_remail_form)

    comment_workflow = version.comment_workflow
    if comment_workflow.id in [3, 1] :
        apriori = '1'
    else :
        apriori = '0'
        
    if comment_workflow.id in [3, 4] :
        rich = '1'
    else :
        rich = '0'
    mod_form = ModForm(initial = {'aprioriworkflow' : apriori,'richworkflow' : rich})
    
    form_dict = {}
    if request.method == 'POST':
        form_name = request.POST['form']
        all_post_data = request.POST.copy()
        del all_post_data["form"]
        if form_name == "settings":
            settings_form,response = settings_settings(text,all_post_data,request)
        elif form_name == "add":
            add_form,response = settings_add(add_form, text,all_post_data,request,local_roles,role_choices)            
        elif form_name == "mod":
            apriori = bool(int(request.POST['aprioriworkflow']))
            rich = bool(int(request.POST['richworkflow']))
            
            workflowid = -1
            if apriori :
                if rich :
                    workflowid = 3
                else :    
                    workflowid = 1
            else :    
                if rich :
                    workflowid = 4
                else :    
                    workflowid = 2
                    
            workflow = Workflow.objects.get(id=workflowid)
            
            version.change_workflow(workflow)
                
            settings_url = reverse('text-settings',args=[text.id])            
            response = HttpResponseRedirect(settings_url)
        if response:
            return response
    return render_to_response('texts/settings.html', 
                              {'role_choices': role_choices, 
                              'add_form': add_form,
                              'remove_remail_form': remove_remail_form,
                              'users_rights_form':users_rights_form,
                              'mod_form': mod_form,
                              'default_new_user_roleid':DEFAULT_NEW_USER_ROLEID,
                              'text' : text,
                              'has_comments': nb_pots > 0,
                              'is_rich': rich == '1',
                              'user_can_share_text' : user_can_share_text(request.user, text),
                              'max_shared_text_number' : request.user.get_profile().get_max_shared_number_text(),
                              'user_can_add_participant' : user_can_add_participant(request.user, text),
                              'roles_help' : roles_help,
                              'max_collaborators_text' : request.user.get_profile().get_max_collaborators_text(),
                              } , 
                              context_instance=RequestContext(request),
                              )
Esempio n. 2
0
def settings_add(add_form, text,data,request, local_roles, role_choices):
    number_added = 0
    # bounding the form
    add_form = AddEmailsForm(data, user=request.user, text=text, local_roles=local_roles, role_choices=role_choices)
    if add_form.is_valid():
        # simple security check if user tries to trick us with tabs
        if not user_can_share_text(request.user, text) or not user_can_add_participant(request.user, text):
            settings_url = reverse('text-settings',args=[text.id])
            request.session['message'] = _(u"Nothing added ... please check.")                        
            return (None,HttpResponseRedirect(settings_url))            
        else:
            if not add_form.data.get('role_add', []) :
                add_form._errors[forms.forms.NON_FIELD_ERRORS] = forms.util.ErrorList([_(u"please check some role/permission to delegate to those new users")])
                return (add_form,None)
            # process new emails
            emails = extract_tokens(add_form.cleaned_data['new_emails'],EMAIL_SEPARATORS)
            transEMAIL_EXAMPLE1 = EMAIL_EXAMPLE1.decode()
            transEMAIL_EXAMPLE2 = EMAIL_EXAMPLE2.decode()
            if transEMAIL_EXAMPLE1 in emails :
                emails.remove(transEMAIL_EXAMPLE1)
            if transEMAIL_EXAMPLE2 in emails :
                emails.remove(transEMAIL_EXAMPLE2)
                 
            role_id = add_form.cleaned_data['role_add']
            # dont send many email to a single user
            notified_users = set()
            for email in emails:
                created,user = Profile.objects.invite_user(email)
                if not ObjectUserRole.objects.filter(object_id__exact = text.id,
                                                   content_type = ContentType.objects.get_for_model(Text),
                                                   user=user,
                                                   role=role_id).count():
                    row_level_role = ObjectUserRole.objects.create(object=text,user=user,role_id=role_id)
                    # invite only if sayed so OR if new users
                    if add_form.cleaned_data['invite'] or created:
                        if user not in notified_users: 
                            send_res = send_notify_email(user, request.user, text)
                            # error sending email : flag user
                            if not send_res:
                                profile = user.get_profile()
                                profile.is_mail_error = True
                                profile.save()
                            notified_users.add(user)                        
                    number_added += 1                        

            # process new users
            if 'new_users' in add_form.cleaned_data:
                new_users_id = add_form.cleaned_data['new_users']
                for new_user_id in new_users_id:
                    new_user = User.objects.get(pk = new_user_id) 
                    if not ObjectUserRole.objects.filter(object_id__exact = text.id,
                                                       content_type = ContentType.objects.get_for_model(Text),
                                                       user=new_user,
                                                       role=role_id).count():
                        row_level_role = ObjectUserRole.objects.create(object=text,user=new_user,role_id=role_id)
                    if add_form.cleaned_data['invite']:
                        if new_user not in notified_users: 
                            send_res = send_notify_email(new_user, request.user, text)
                            # error sending email : flag user
                            if not send_res:
                                profile = user.get_profile()
                                profile.is_mail_error = True
                                profile.save()                                
                            notified_users.add(new_user)                   
                    number_added += 1
    
            settings_url = reverse('text-settings',args=[text.id])
            if number_added:
                request.session['message'] = ungettext(u"%(number)s new collaboration added.", u"%(number)s new collaborations added.", number_added) % {'number':number_added}
                        
            return (None,HttpResponseRedirect(settings_url))            
    else:
        return (add_form,None)