Exemple #1
0
def department_list(request, template_name='my.html', next='/', department_page='1',):
    """
    单位部门查询视图
    """
    page_title = u'查询单位部门'
    if request.method == 'POST':
        post_data = request.POST.copy()
        department_search_form = DepartmentSearchForm(post_data)
        if department_search_form.is_valid():

            department_search_form.save_to_session(request)
            if department_search_form.is_null() is False:
                if department_search_form.fuzzy_search() is False:
                    query_set = Department.objects.filter(Q(is_active = True) & 
                                                           Q(name__startswith=department_search_form.cleaned_data['department_name']))
                else:
                    department_search_form.fields['is_fuzzy'].widget.attrs['checked'] = u'true'
                    query_set = Department.objects.filter(Q(is_active = True) &
                                                       Q(name__icontains=department_search_form.cleaned_data['department_name']))
            else:
                query_set = Department.objects.filter(Q(is_active = True))

        else:
            query_set = None
        if query_set is not None:
            results_page = pagination_results(department_page, query_set, settings.DEPARTMENT_PER_PAGE)
        else:
            results_page = None
        return render_to_response(template_name,
                                  {'search_form': department_search_form,
                                   'page_title': page_title,
                                   'results_page': results_page,
                                   },
                                  context_instance=RequestContext(request))
    else:
        data = {'department_name':request.session.get(gl.session_department_name, u''),
                'is_fuzzy':request.session.get(gl.session_department_is_fuzzy, False),
                }        
#       print data['is_fuzzy']

        department_search_form = DepartmentSearchForm(data)
        if department_search_form.is_valid():
            if department_search_form.is_null() is False:
                if department_search_form.fuzzy_search() is False:
                    query_set = Department.objects.filter(Q(is_active = True) & 
                                                           Q(name__startswith=department_search_form.cleaned_data['department_name']))
                else:
#                    print '********'
                    department_search_form.fields['is_fuzzy'].widget.attrs['checked'] = u'true'
                    query_set = Department.objects.filter(Q(is_active = True) &
                                                       Q(name__icontains=department_search_form.cleaned_data['department_name']))
            else:
                query_set = Department.objects.filter(Q(is_active = True))

        else:
            query_set = Department.objects.filter(Q(is_active = True))
        results_page = pagination_results(department_page, query_set, settings.DEPARTMENT_PER_PAGE)
        return render_to_response(template_name,
                                  {'search_form': department_search_form,
                                   'page_title': page_title,
                                   'results_page': results_page,
                                   },
                                  context_instance=RequestContext(request))
Exemple #2
0
def department_add(request, template_name='my.html', next='/', department_page='1'):
    """
    单位部门添加视图,带添加预览功能!
    """
    page_title = u'添加单位部门'
    user = get_user(request)

    if request.method == 'POST':
        post_data = request.POST.copy()
        submit_value = post_data.get(u'submit', False)
        if submit_value == u'添加':
            department_add_form = DepartmentAddForm(post_data)
            if department_add_form.is_valid():
                department_add_form.department_add(user)
            else:
                pass
            data = {'department_name':request.session.get(gl.session_department_name, u''),
                    'is_fuzzy':request.session.get(gl.session_department_is_fuzzy, False),
                    }
#            print data['department_name']
#            print data['is_fuzzy']
            department_search_form = DepartmentSearchForm(data)
            if department_search_form.is_valid():
                if department_search_form.is_null() is False:
                    if department_search_form.fuzzy_search() is False:
                        query_set = Department.objects.filter(Q(is_active = True) & 
                                                               Q(name__startswith=department_search_form.cleaned_data['department_name']))
                    else:
                        department_search_form.fields['is_fuzzy'].widget.attrs['checked'] = u'true'
                        query_set = Department.objects.filter(Q(is_active = True) &
                                                               Q(name__icontains=department_search_form.cleaned_data['department_name']))
                else:
                    query_set = Department.objects.filter(Q(is_active = True))
            else:
                raise Http404('search form error!')
        else:
            department_add_form = DepartmentAddForm()
            if submit_value == u'查询':
                department_search_form = DepartmentSearchForm(post_data)
                if department_search_form.is_valid():
                    department_search_form.save_to_session(request)
                    if department_search_form.is_null() is False:
                        if department_search_form.fuzzy_search() is False:
                            query_set = Department.objects.filter(Q(is_active = True) & 
                                                                   Q(name__startswith=department_search_form.cleaned_data['department_name']))
                        else:
                            department_search_form.fields['is_fuzzy'].widget.attrs['checked'] = u'true'
                            query_set = Department.objects.filter(Q(is_active = True) &
                                                       Q(name__icontains=department_search_form.cleaned_data['department_name']))
                    else:
                        query_set = Department.objects.filter(Q(is_active = True))

                else:
#                    query_set = Department.objects.filter(Q(is_active = True))
                    query_set = None
            else:
                raise Http404('search form error!')
        if query_set is not None:
            results_page = pagination_results(department_page, query_set, settings.DEPARTMENT_PER_PAGE)
        else:
            results_page = None
        return render_to_response(template_name,
                                  {'search_form': department_search_form,
                                   'add_form': department_add_form,
                                   'page_title': page_title,
                                   'results_page':results_page,
                                   },
                                  context_instance=RequestContext(request))
    else:
        department_add_form = DepartmentAddForm()
        data = {'department_name':request.session.get(gl.session_department_name, u''),
                'is_fuzzy':request.session.get(gl.session_department_is_fuzzy, False),
                }
        department_search_form = DepartmentSearchForm(data)
        if department_search_form.is_valid():
            if department_search_form.is_null() is False:
                if department_search_form.fuzzy_search() is False:
                    query_set = Department.objects.filter(Q(is_active = True) & 
                                                           Q(name__startswith=department_search_form.cleaned_data['department_name']))
                else:
                    department_search_form.fields['is_fuzzy'].widget.attrs['checked'] = u'true'
                    query_set = Department.objects.filter(Q(is_active = True) &
                                                       Q(name__icontains=department_search_form.cleaned_data['department_name']))
            else:
                query_set = Department.objects.filter(Q(is_active = True))

        else:
            query_set = Department.objects.filter(Q(is_active = True))
        results_page = pagination_results(department_page, query_set, settings.DEPARTMENT_PER_PAGE)

        return render_to_response(template_name,
                                  {'search_form': department_search_form,
                                   'add_form': department_add_form,
                                   'page_title': page_title,
                                   'results_page':results_page,
                                   },
                                  context_instance=RequestContext(request))