Esempio n. 1
0
def approval_document(request,doca_id,template_name="oa/document_approval.html"):
    """审批公文"""
    doca = get_object_or_404(DocumentApproval,id=doca_id)
    document = doca.document
    files = document.files.all()
    receivers = DocumentReceiver.objects.filter(document=document)
    user_pks = [u.user_id for u in receivers]
    history = DocumentApproval.objects.filter(document=document).order_by('ctime')
    school = document.school
    category_group = get_category_group(school)
    teachers = Teacher.objects.filter(school=school)
    type = request.GET.get("ty", "")
    if doca.status == 2:
        try:
            doca_l = DocumentApproval.objects.filter(approvaler=doca.approvaler)\
                .exclude(id=doca.id).order_by('-send_time')[0]
        except:
            doca_l = None
    else:
        doca_l = None
    
    if request.method == 'POST':
        form = DocumentForm(request.POST,instance=document)
        
        if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
        if form.is_valid():
            doc = form.save(commit=False)
            doc.save()
             
            #发布对象
            receiver_pks = request.POST.getlist("to")

            teacher_list = User.objects.filter(pk__in=receiver_pks)
#             exist_user_pks = [u.user.id for u in DocumentReceiver.objects.filter(document=doc)]
#             teacher_list = teacher_list.exclude(user_id__in=exist_user_pks)
            try:
                DocumentReceiver.objects.filter(document=doc).delete()
            except:
                pass
            for t in teacher_list:
                receiver = DocumentReceiver()
                receiver.user = t
                receiver.document = doc
                receiver.save()
                       
            approvaler_pks = request.POST.getlist("approvaler")
            ty = int(request.POST.get("commit_type"))
            
            #直接发出
            if ty == 1:
                doca.remark = doc.remark
                doca.status = 1
                doca.send_time = datetime.datetime.now()
                doca.save()
                
                doc.is_approvaled = True
                doc.save()
                
                #发送信息
                receivers = DocumentReceiver.objects.filter(document=doc)
                receivers.update(is_send=True,send_time=datetime.datetime.now())
            
            #发回公文
            if ty == 2:
                doca.receiver = doca.sender
                doca.remark = doc.remark
                doca.status = 3
                doca.send_time = datetime.datetime.now()
                doca.save()
                
                doc.remark = ''
                doc.save()
                
                appr = DocumentApproval()
                appr.sender = request.user
                appr.document = doc
                appr.remark = doc.remark
                if doca_l:
                    appr.approvaler = doca_l.sender
                else:
                    appr.approvaler = doca.sender
                appr.send_time = datetime.datetime.now() + datetime.timedelta(seconds = 10)
                appr.status = 2
                appr.save()
                
            #送审公文
            if ty == 3:
                teacher_list = Teacher.objects.filter(user__pk__in=approvaler_pks)
                if doc.is_submit and approvaler_pks:
                    doca.receiver = teacher_list[0].user
                    doca.remark = doc.remark
                    doca.status = 3
                    doca.send_time = datetime.datetime.now()
                    doca.save() 
                    
                    doc.remark = ''
                    doc.save()
                    
                    appr = DocumentApproval()
                    appr.sender = request.user
                    appr.document = doc
                    appr.status = 0
                    appr.remark = doc.remark
                    appr.approvaler = teacher_list[0].user
                    appr.send_time = datetime.datetime.now() + datetime.timedelta(seconds = 10)
                    appr.save()
            #附件
            for f in request.FILES.getlist('files'):
                atta = Attachment()
                atta.name = f.name.split('.')[0]
                atta.file = f
                atta.save()
                doc.files.add(atta)
        
            messages.success(request, u"审批公文成功") 
            return redirect('oa_approvaled_document')

    else:
        form = DocumentForm(instance=document)
    schools = get_school_with_workgroup(request.user)
    ctx = {'doca':doca,'history':history,'doc':document,'form':form,'files':files,'receivers':receivers,'doca_l':doca_l,\
           'teachers':teachers,'user_pks':user_pks,'category_group':category_group,'type':type,'schools':schools}
    return render(request, template_name, ctx) 
Esempio n. 2
0
def update_document(request,doc_id,template_name="oa/document_form.html"):
    """更新公文"""
    ctx = {}
    doc = get_object_or_404(Document,id=doc_id)
    files = doc.files.all()
    category_group = get_category_group(doc.school)
    receivers = DocumentReceiver.objects.filter(document=doc)
    teachers = Teacher.objects.filter(school=doc.school)
    if request.method == 'POST':
        form = DocumentForm(request.POST,instance=doc)
        
        if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
        if form.is_valid():
            doc = form.save(commit=False)
            doc.save()
             
            #发布对象
            receiver_pks = request.POST.getlist("to")
            teacher_list = Teacher.objects.filter(pk__in=receiver_pks)
            try:
                DocumentReceiver.objects.filter(document=doc).delete()
            except:
                pass
            for t in teacher_list:
                receiver = DocumentReceiver()
                receiver.user = t.user
                receiver.document = doc
                if not doc.is_submit and doc.status == 0:
                    receiver.is_send = True
                    receiver.send_time = datetime.datetime.now()
                    doc.is_approvaled = True
                    doc.save()
                receiver.save()
                       
            #送审
            approvaler_pks = request.POST.getlist("approver")
            if doc.is_submit and approvaler_pks:
                teacher_list = Teacher.objects.filter(pk__in=approvaler_pks)
                appr = DocumentApproval()
                appr.sender = doc.sender
                appr.document = doc
                appr.remark = doc.remark
                appr.approvaler = teacher_list[0].user
                appr.send_time = doc.send_time
                appr.save()
                
                doc.remark = ''
                doc.save()
                
            #附件
            for f in request.FILES.getlist('files'):
                atta = Attachment()
                atta.name = f.name.split('.')[0]
                atta.file = f
                atta.save()
                doc.files.add(atta)
        return redirect(reverse('oa_personal_document'))
    else:
        form = DocumentForm(instance=doc)
    
    schools = get_school_with_workgroup(request.user)
    ctx.update({'form':form,'teachers':teachers,'doc':doc,'category_group':category_group,\
                'receivers':receivers,'schools':schools,'files':files})
    return render(request, template_name, ctx)
Esempio n. 3
0
def write_document(request,template_name="oa/document_form.html"):
    """撰写公文"""
    ctx = {}
    school = get_schools(request.user)[0]
    category_group = get_category_group(school)
    teachers = Teacher.objects.filter(school=school)
    
    if request.method == 'POST':
        form = DocumentForm(request.POST)
        receiver_pks = request.POST.getlist("to")
        teacher_list = User.objects.filter(pk__in=receiver_pks)
        approvaler_pk = request.POST.get("approvaler",'')
        try:
            approvaler = User.objects.get(pk=approvaler_pk)
        except:
            approvaler = None
        category_pk = int(request.POST.get("category",0))
        ctx.update({'teacher_list':teacher_list,'approvaler':approvaler,'category_pk':category_pk})    
        
        if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
        if form.is_valid():
            doc = form.save(commit=False)
            doc.school = school
            doc.sender = request.user
            doc.save()
             
            #发布对象
            for t in teacher_list:
                receiver = DocumentReceiver()
                receiver.user = t
                receiver.document = doc
                if not doc.is_submit and doc.status == 0:
                    receiver.is_send = True
                    receiver.send_time = datetime.datetime.now()
                    doc.is_approvaled = True
                    doc.save()
                if doc.send_msg and doc.status == 0 and doc.msg_body:
                    msg = Sms()
                    msg.sender = doc.sender
                    msg.receiver = t
                    msg.mobile = t.get_profile().mobile
                    msg.type_id = 7
                    msg.content = str(doc.msg_body) + '/' + str(doc.sender.get_profile().chinese_name_or_username()) 
                    msg.save()
                receiver.save()
                       
            #送审
            approvaler_pk = request.POST.get("approvaler",0)
            if doc.is_submit and approvaler_pk:
                approvaler = User.objects.get(pk=approvaler_pk)
                appr = DocumentApproval()
                appr.sender = doc.sender
                appr.document = doc
                appr.remark = doc.remark
                appr.approvaler = approvaler
                appr.send_time = datetime.datetime.now()
                appr.save()
#                doc.send_time = datetime.datetime.now()
                doc.remark = ''
                doc.save()
                
            #附件
            for f in request.FILES.getlist('files'):
                atta = Attachment()
                atta.name = f.name
                atta.file = f
                atta.save()
                doc.files.add(atta)
            
            messages.success(request, u"撰写公文成功") 
            if doc.status == 1:
                return redirect('oa_personal_document')
            else:
                return redirect('oa_issued_document')
    else:
        form = DocumentForm()
    
    schools = get_school_with_workgroup(request.user)
    ctx.update({'form':form,'teachers':teachers,'category_group':category_group,'schools':schools})
    return render(request, template_name, ctx)