示例#1
0
文件: views.py 项目: dahool/vertaal
def message_submit(request):
    if request.method == "POST":
        res = {}
        instance = None
        action = request.POST.get('action')
        current = request.POST.get('id', None)
        if current:
            instance = get_object_or_404(PMMessage, pk=current)
        form = MessageForm(request.POST, instance=instance)
        if form.is_valid():
            if action == 'send' and len(form.cleaned_data['recipients']) > 0:
                form.save(request.user, send=True)
                return XMLResponse({'message': ResponseMessage.success(_('Message sent.'))})
            elif action != 'send':
                form.save(request.user, send=False)
                return XMLResponse({'message': ResponseMessage.success(_('Message saved as draft.'))})
            else:
                res['message'] = ResponseMessage.error(_("You didn't select any recipient")); 
        
        recipients = []
        for ri in request.POST.getlist('recipients'):
            try:
                u = User.objects.get(pk=ri)
                recipients.append(u)
            except:
                pass

        res['content_HTML'] = render_to_string('djangopm/message_compose.html',
                                  {'form': form, 'recipients': recipients, 'instance': instance}, 
                                      context_instance = RequestContext(request))
        return XMLResponse(res)
    else:
        raise Http403
示例#2
0
def toggle(request, slug, template='files/file_list_row.html'):
    res = {}
    if request.method == "POST":
        try:
            file = POFile.objects.get(slug=slug)
        except:
            return XMLResponse({'message': ResponseMessage.error(_('File not found.'))})
        else:
            if not file.release.enabled or not file.release.project.enabled:
                return XMLResponse({'message': ResponseMessage.error(_('Sorry, you are not supposed to change anything on a disabled component.'))})
            
            team = Team.objects.get(project=file.component.project, language=file.language)
            if not team.is_member(request.user):
                return XMLResponse({'message': ResponseMessage.error(_('You are not a member of this team.'))})
                
            res['id'] = file.slug
            if file.locked:
                # if the file is locked by the same user or the user is admin or coord
                # then can unlock the file
                if file.locks.get().can_unlock(request.user):
                    if file.submits.all_pending():
                        res['message'] = ResponseMessage.info(_('%s will be automatically unlocked once submitted.') % file.filename)       
                    else:
                        file.locks.get().delete()
                        if request.POST.has_key('text'):
                            comment = request.POST.get('text')
                        else:
                            comment = ''
                        POFileLog.objects.create(pofile=file, user=request.user, action='R', comment=comment)
                        res['message'] = ResponseMessage.success(_('Unlocked %s') % file.filename)
                else:
                    res['message'] = ResponseMessage.error(_('%s is already locked.') % file.filename)
            else:
                file.locks.create(owner=request.user)
                res['message'] = ResponseMessage.success(_('Locked %s') % file.filename)
        page = render_to_string(template,
                                {'pofile': file, 'team': team},
                                context_instance = RequestContext(request))
        res['content_HTML'] = page
        return XMLResponse(res)
    else:
        raise Http403
示例#3
0
def toggle_assigned(request, slug, translator=False, remove=False, template='files/file_list_row.html'):
    res = {}
    if request.method == "POST":
        try:
            file = POFile.objects.get(slug=slug)
        except:
            return XMLResponse({'message': ResponseMessage.error(_('File not found.'))})
        
        if not file.release.enabled or not file.release.project.enabled:
            return XMLResponse({'message': ResponseMessage.error(_('Sorry, you are not supposed to change anything on a disabled component.'))})

        team = Team.objects.get(project=file.component.project, language=file.language)
        if not team.is_member(request.user) and not request.user.is_superuser:
            return XMLResponse({'message': ResponseMessage.error(_('You are not a member of this team.'))})
        can_manage = team.can_manage(request.user)
        
        res['id'] = file.slug
        cmt = ''
        act = None
        if remove:
            if file.assigns.all():
                assign = file.assigns.get()
            if assign:
                if translator:
                    auser = assign.translate
                    if auser:
                        if can_manage or auser == request.user:
                            if file.submits.all_pending() and file.submits.get_pending().owner == auser:
                                return XMLResponse({'message': ResponseMessage.error(_('%s has a pending submit. Cannot be released right now.') % file.filename)})
                            else:
                                assign.translate = None
                                if auser != request.user:
                                    cmt = _('Removed %s') % auser.username
                                res['message'] = ResponseMessage.success(_('%s translator removed.') % file.filename)
                                act=LOG_ACTION['RE_TRA']
                        else:
                            return XMLResponse({'message': ResponseMessage.error(_('You are not authorized to perform this action.'))})
                else:
                    auser = assign.review
                    if auser:
                        if can_manage or auser == request.user:
                            if file.submits.all_pending() and file.submits.get_pending().owner == auser:
                                return XMLResponse({'message': ResponseMessage.error(_('%s has a pending submit. Cannot be released right now.') % file.filename)})
                            else:
                                assign.review = None
                                if auser != request.user:
                                    cmt = _('Removed %s') % auser.username
                                res['message'] = ResponseMessage.success(_('%s reviewer removed.') % file.filename)                            
                                act=LOG_ACTION['RE_REV']
                        else:
                            return XMLResponse({'message': ResponseMessage.error(_('You are not authorized to perform this action.'))})
                if file.locked:
                    if file.locks.get().owner == auser:
                        file.locks.get().delete()
                        POFileLog.objects.create(pofile=file, user=request.user, action=LOG_ACTION['ACT_LOCK_DEL'])
                if act:
                    log = POFileLog.objects.create(pofile=file, user=request.user, action=act, comment=cmt)                        
                    assign.save()
        else:
            if request.POST.has_key('userid'):
                userid = request.POST.get('userid')
            else:
                userid = None
            if file.assigns.all():
                assign = file.assigns.get()
            else:
                #assign = file.assigns.create()
                assign = POFileAssign(pofile=file)
            if translator:
                if assign.translate:
                    return XMLResponse({'message': ResponseMessage.error(_('%s already has an assigned translator.') % file.filename)})
                
                if not can_manage or not userid:
                    assign.translate = request.user 
                else:
                    assign.translate = User.objects.get(id=userid)
                    if assign.translate != request.user:
                        send_pm(assign.translate, _('File assigned'), _('You had been designated as translator of %(file)s') % {'file': smart_unicode(file)})
                    res['message'] = ResponseMessage.success(_('Translator set for %s.') % file.filename)
                if assign.translate != request.user:
                    cmt = _('Assigned to %s') % assign.translate.username                
                act=LOG_ACTION['AS_TRA']
                    
            else:
                if assign.review:
                    return XMLResponse({'message': ResponseMessage.error(_('%s already has an assigned reviewer.') % file.filename)})
                
                if not can_manage or not userid:
                    assign.review = request.user 
                else:
                    assign.review = User.objects.get(id=userid)
                    if assign.review != request.user:
                        send_pm(assign.review, _('File assigned'), _('You had been designated as reviewer of %(file)s') % {'file':smart_unicode(file)})
                    res['message'] = ResponseMessage.success(_('Reviewer set for %s.') % file.filename)
                if assign.review != request.user:
                    cmt = _('Assigned to %s') % assign.review.username                
                act=LOG_ACTION['AS_REV']
                
            if assign.translate == assign.review:
                return XMLResponse({'message': ResponseMessage.error(_('Sorry, the translator and the reviewer cannot be the same user.'))})
            
            POFileLog.objects.create(pofile=file, user=request.user, action=act, comment=cmt)
            assign.save()
            
        page = render_to_string(template,
                                {'pofile': file, 'team': team},
                                context_instance = RequestContext(request))
        res['content_HTML'] = page
        return XMLResponse(res)
    else:
        raise Http403