Esempio n. 1
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. 2
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)