Example #1
0
def show_export(request,app_label,model_name):
    models={}
    filetype={}
    template={}
    #models["Employee"]=_("Employee Table")
    #models["Department"]=_("Department Table")
    rn=request.REQUEST.get("reportname",'')    
    if app_label!="list":    
        models[model_name]=rn and rn or GetModel(app_label,model_name)._meta.verbose_name
    else:
        models[model_name]=rn and rn or model_name
    filetype["txt"]=_(u"TXT 文件")
    filetype["csv"]=_(u"CSV 文件")
    filetype["pdf"]=_(u"PDF 文件")
    filetype["xls"] = _(u"EXCEL 文件")
    #filetype["json"]=_("JSON File")
    template["stdemployee"]=_(u"标准的员工模板")
    template["smpemployee"]=_(u"简单的雇员的模板")
    template["stddepartment"]=_(u"指纹模板")
    template["standard"]=_(u"标准模板")
    tables={'Employee':_(u'人员'),'department':_(u'授权部门')}

    return render_to_response('export.html', {
            'models': models,
            'filetype': filetype,
            'template': template,
            'filecoding':filecoding,
            'model_name':model_name,
            })
Example #2
0
def file_simple_import(request,app_label,model_name):
    
    data=dict(request.POST)
    filename=data["txtfilename"][0]           
    filepath = uploadpath+filename
    mymodel=GetModel(app_label,model_name)
    
    sheetdata=ParseXlsUxlrd(filepath)
    datash=sheetdata[0][1]  
    
    fail_list=[]
    for row in range(len(datash)):  
        if row==0:
            continue
        obj = mymodel()
        ipt_data = row+1
        ept = obj.do_import(datash[row])
        if isinstance(ept,Exception):
            fail_list.append({'data':ipt_data,'error':ept.message})
    if fail_list:
        output = ""
        for e in fail_list:
            output += (u"第%s行"%(e['data'])+u" : %s \n"%(e['error']) )
        return HttpResponse(u"%s"%_(u"处理以下行出错\n")+"%s"%output)
    else:
        return HttpResponse(u"%s"%_(u"文件导入操作完成!"))
Example #3
0
def DataDetail(request, app_label, model_name, DataKey):
    from admin_detail_view import retUserForm, adminForm, doPostAdmin, doCreateAdmin
    dataModel = GetModel(app_label, model_name)
    if not dataModel: return NoFound404Response(request)
    lock = request.GET.get("_lock", None)
    read_only = (lock == 'ALL')
    if not read_only:
        try:
            if dataModel.Admin.read_only: read_only = True
        except:
            pass
    perm = hasPerm(request.user, dataModel, "change")
    if not perm and not read_only:
        if not hasPerm(request.user, dataModel, "browse"):
            return NoPermissionResponse()
        read_only = True
    master = get_model_master(dataModel)
    if dataModel == User:  # 管理员 管理
        if request.method == "POST" and not read_only:
            if not perm: return NoPermissionResponse()
            return doPostAdmin(request, dataModel, DataKey)
        else:
            return doCreateAdmin(request, dataModel, DataKey)

    if master:
        try:
            m_instance = master.rel.to.objects.get(pk=DataKey)
        except ObjectDoesNotExist:
            return NoFound404Response(request)
        try:
            instance = dataModel.objects.get(**{master.name: m_instance})
        except ObjectDoesNotExist:
            instance = dataModel(**{master.name: m_instance})
    else:
        try:
            instance = dataModel.objects.get(pk=DataKey)
        except ObjectDoesNotExist:
            return NoFound404Response(request)
    level_type = get_leveltype_byhttpreferer(request)
    if request.method == "POST" and not read_only:
        if not perm: return NoPermissionResponse()
        return DataChangePost(
            request, dataModel,
            form_for_instance(instance,
                              post=request.POST,
                              level_type=level_type), instance)
    if lock:
        fields = [
            field.find("__") and field.split("__")[0] or field
            for field in dict(request.GET)
        ]
    return DataDetailResponse(
        request, dataModel,
        form_for_instance(instance,
                          lock_fields=master and [master.name]
                          or (lock and fields or []),
                          read_only=read_only,
                          level_type=level_type), instance.pk, instance)
Example #4
0
def DataList(request, app_label, model_name):
    dataModel = GetModel(app_label, model_name)
    if not dataModel: return NoFound404Response(request)
    try:
        #print"datalist____1"
        return model_data_list(request, dataModel)
    except:
        print_exc()
        raise
Example #5
0
def show_simple_import(request,app_label,model_name):
    from modelutils import GetModel

    model=GetModel(app_label,model_name)
    tables={model._meta.module_name:model._meta.verbose_name}

    from django.utils.translation.trans_real import get_language_from_request
    lang_code=get_language_from_request(request)
    return render_to_response('simple_import.html', {'tables': tables,"lang_code":lang_code})
Example #6
0
def get_view(request, app_label, model_name, view_name):
    model = GetModel(app_label, model_name)
    op = threadlocals.get_current_user()

    obj = ViewModel.objects.filter(
        model__exact=ContentType.objects.get_for_model(model),
        name__exact=view_name,
        create_operator__exact=op.username)
    ret = ""
    if len(obj) > 0:
        ret = eval(obj[0].info)
        ret["viewtype"] = obj[0].viewtype
        ret = simplejson.dumps(ret)
    return getJSResponse(ret)
Example #7
0
def show_import(request,app_label,model_name):
    from modelutils import GetModel
    #tables={'Employee':_(u'人员表'),'department':_(u'部门表')}
    model=GetModel(app_label,model_name)
    tables={model._meta.module_name:model._meta.verbose_name}
    #tables[model_name]=model_name;
    #t=loader.get_template("import.html")
    #return HttpResponse(t.render(RequestContext(request,{
   #                    'tables':tables
   #                    }))
    from django.utils.translation.trans_real import get_language_from_request
    lang_code=get_language_from_request(request)
    if model_name =='EmpSpecDay':
        return render_to_response('simple_import.html', {'tables': tables,"lang_code":lang_code})
    else:
        return render_to_response('import.html', {'tables': tables,"lang_code":lang_code})
Example #8
0
def delete_view(request, app_label, model_name, view_name):
    try:

        model = GetModel(app_label, model_name)
        op = threadlocals.get_current_user()

        ret = ViewModel.objects.filter(
            model__exact=ContentType.objects.get_for_model(model),
            name__exact=view_name,
            create_operator__exact=op.username)
        if len(ret) <= 0:
            return HttpResponse(
                "View not found or You are not the view's creator !")
        ret.delete()
    except:
        return HttpResponse("delete fail !")
    return HttpResponse("delete Ok !")
Example #9
0
def DataNew(request, app_label, model_name):
    from admin_detail_view import retUserForm, adminForm, doPostAdmin, doCreateAdmin
    try:
        dataModel = GetModel(app_label, model_name)
        lock = request.GET.get("_lock", None)
        read_only = lock == 'ALL'
        if not hasPerm(request.user, dataModel, "add"):
            return NoPermissionResponse()
        if not dataModel: return NoFound404Response(request)
        if request.method == "POST" and not read_only:
            return DataNewPost(request, dataModel)

        instance, fields = new_object(dataModel, request.GET)
        if dataModel == User:
            return retUserForm(request, adminForm(request), isAdd=True)
        level_type = get_leveltype_byhttpreferer(request)
        dataForm = form_for_instance(instance,
                                     lock_fields=lock and fields or [],
                                     read_only=read_only,
                                     level_type=level_type)
        return DataDetailResponse(request, dataModel, dataForm, None, instance)
    except:
        import traceback
        traceback.print_exc()
Example #10
0
def get_form_(request, app_label, model_name, op_name):
    #    from mysite import authorize_fun
    #     #软件狗控制/*******start*******/
    #    if not authorize_fun.can_operate(request):
    #        if request.is_ajax():
    #            return HttpResponse("session_fail_or_no_permission")
    #        else:
    #            return render_to_response('no_permission_info.html',RequestContext(request,{'dbapp_url': dbapp_url}))
    #/********end**********/
    is_worktable = ""
    if request.META.has_key('HTTP_REFERER'):
        is_worktable = request.META['HTTP_REFERER']

    if is_worktable.find("worktable") != -1:
        is_worktable = True
    else:
        is_worktable = False
    model = GetModel(app_label, model_name)
    if not model: return NoFound404Response(request)
    if not hasattr(model, op_name): return NoFound404Response(request)
    op = getattr(model, op_name)
    obj_op = None
    if not (type(op) == types.TypeType and issubclass(op, ModelOperation)):
        return NoFound404Response(request)
    if op_name.startswith("_"):
        opn = op_name[1:]
    else:
        opn = op_name
    if model.__name__ == "Group":
        opn = "groupdel"


#    if not datautils.hasPerm(request.user, model, opn.lower()):
#        return HttpResponse(_(u'会话已过期或没有权限'))
    if op_name == "View_detail":
        if request.method == "GET":
            data_key = request.REQUEST.get("K")
            return view_detail(request, app_label, model_name, data_key)
        else:
            return HttpResponse('{ Info:"OK" }')

    if request.method == 'POST':
        if issubclass(model, Model) and not issubclass(model, CachingModel):
            keys = request.REQUEST.getlist("K")
            if not keys:
                #ModelOperation path
                try:
                    ret = model.model_do_action(op_name,
                                                request,
                                                form_field=form_field)
                    if not ret: return HttpResponse('{ Info:"OK" }')
                    if isinstance(ret, HttpResponse): return ret
                    if isinstance(ret, forms.Form):
                        f = ret
                        return HttpResponse(u"<div>%s</div>" % f.as_table())
                    else:
                        return HttpResponse(
                            u"{ errorCode:-1,\nerrorInfo:\"%s\" }" % ret)
                except Exception, e:
                    return HttpResponse(
                        u"{ errorCode:-1,\nerrorInfo:\"%s\" }" % e)
            else:
                ret = []
                objs = model.objects.filter(pk__in=keys)
                try:
                    op_class = op
                    for obj in objs:
                        if len(op_class.params) == 0:
                            op = op_class(obj)
                            ret.append("%s" % (op.action(**{}) or ""))
                            msg = u"%s(%s) %s" % (op.verbose_name, "", ret
                                                  or "")
                            LogEntry.objects.log_action_other(
                                request.user.pk, obj, msg)
                except Exception, e:
                    ret.append(u"%s" % e)
                if not "".join(ret):
                    return HttpResponse('{ Info:"OK" }')
                else:
                    return HttpResponse(
                        '<ul class="errorlist"><li>%s </li></ul>' %
                        ("".join(ret)))
Example #11
0
def view_detail(request, app_label, model_name, data_key):
    u'''查看对象详情'''
    ModelClass = GetModel(app_label, model_name)
    op = getattr(ModelClass, "View_detail")
    try:
        instance = ModelClass.objects.get(pk=data_key)
        exclude_fields = [
            'change_operator', 'change_time', 'create_operator', 'create_time',
            'delete_operator', 'delete_time', 'status'
        ]
        #没有配置的时候需要排除CacheModel中的7个字段
        ret_fields = {}
        view_fields = []
        if hasattr(ModelClass.Admin, "view_fields"):
            view_fields = ModelClass.Admin.view_fields

        all_fields = ModelClass._meta.fields
        if view_fields:  #配置了
            for e in view_fields:
                flag = False
                for ee in all_fields:
                    if ee.name == e:
                        value = instance.__getattribute__(ee.name)
                        if hasattr(instance, "get_" + e + "_display"):
                            value = (
                                instance.__getattribute__("get_" + e +
                                                          "_display")()) or ""
                        #if ee.editable:
                        ret_fields[u"%s" % ee.verbose_name] = value or ""
                        flag = True
                        break
                if not flag:
                    if hasattr(instance, e):
                        ret_fields[u"%s" %
                                   _(u"%s" % e)] = instance.__getattribute__(e)
        else:  #没有配置
            for ee in all_fields:
                e_name = ee.name
                if e_name not in exclude_fields:
                    value = instance.__getattribute__(e_name) or ""
                    if hasattr(instance, "get_" + e_name + "_display"):
                        value = (instance.__getattribute__("get_" + e_name +
                                                           "_display")()) or ""
                    #if ee.editable:
                    ret_fields[u"%s" % ee.verbose_name] = value

        for m_m in ModelClass._meta.many_to_many:
            m_m_display = []
            qs = instance.__getattribute__(m_m.name).get_query_set()
            for elem in qs:
                m_m_display.append(u"%s" % elem)
            ret_fields[m_m.verbose_name] = ",".join(m_m_display)
        if hasattr(op, "verbose_name"):
            op_verbose_name = op.verbose_name
        else:
            op_verbose_name = u"%s" % _(u"详情")
        kargs = {
            "fields": ret_fields,
            "app_menu": app_label,
            "title": (u"%s" % ModelClass._meta.verbose_name).capitalize(),
            "verbose_name": op_verbose_name
        }
        if hasattr(ModelClass, "render_view_obj") and callable(
                ModelClass.render_view_obj):
            #如果写了render_view_obj函数,则直接走此函数,该函数必须返回一个HttpResponse对象
            return ModelClass.render_view_obj(request)
        return render_to_response(
            ["view_%s_detail.html" % model_name, "view_detail.html"],
            RequestContext(request, kargs))
    except Exception, e:
        return HttpResponse(u"%s" % e)
Example #12
0
def file_export(request,app_label,model_name):       
    
    exportpath=settings.ADDITION_FILE_ROOT+"/"
    #print exportpath
    filename=""
    filetype=""
    model=""
    templatename=""
    filecode=""
    viewname=""
    
    data=dict(request.POST)
    filetype=str(data["filetype"][0])
    model=str(data["model"][0])
    templatename=data["templatename"][0].decode("utf-8")
    filecode=str(data["filecode"][0])
    viewname=str(data["txtviewname"][0])
    op=threadlocals.get_current_user()
   
    dtstr=""
    dt=datetime.now()
   
    dtstr=str(dt.year)+str(dt.month)+str(dt.day)+str(dt.hour)+str(dt.minute)+str(dt.second)
    
    Displayfileds=""                                #导出字段列表,可从视图中提取,或定制
    data=[]
    tblname=""
    model=GetModel(app_label,model_name)
    if viewname:
        from viewmodels import get_view_byname_js
        Displayfileds =get_view_byname_js[viewname]["fields"]
        
        
    if filetype=='txt':
        try:
            if templatename=='stdemployee':
                tblname="emp"
                Displayfileds=["id","EName","Gender","DeptID"]
                data=Employee.objects.all().values_list(*Displayfileds).order_by("id")

            elif templatename=='smpemployee':
                Displayfileds=["id","EName","Gender","DeptID"]
                data=Employee.objects.all().values_list(*Displayfileds).order_by("id")
                tblname="emp"
            elif templatename=='stddepartment': 
                Displayfileds=["DeptID","DeptCode","DeptName","parent"]
                data=Department.objects.all().values_list(*Displayfileds).order_by("DeptID")
                tblname="dep"
            else:
                Displayfileds=[fl.name for fl in model._meta.fields]
                data=model.objects.all().values_list(*Displayfileds).order_by("id")
                tblname=model.__name__
            #print "%s"%data
            filename=op.username+dtstr+tblname+"."+filetype

            ret= render_to_string(templatename+".txt", {
                    'fields': Displayfileds,
                    'tdata': data,
                    })
            f=file(exportpath+filename,'w')
            f.write(ret.encode(filecode))
            f.close()
            #print ret
            response = HttpResponse(ret,mimetype='application/octet-stream')   
            response['Content-Disposition'] = 'attachment; filename=%s' % filename  
            return response  
            
            
#           return HttpResponseRedirect("/data/file/%s"%filename)
            
            
#            response = HttpResponse(mimetype='text/csv')
#            response['Content-Disposition'] = 'attachment; filename=%s' % filename
#            
#            t = loader.get_template(templatename+".txt")
#            c = Context({
#                'tdata': data,
#                'fields': Displayfileds,
#            })
#            response.write(t.render(c))
#            return response
            
        except:
            import traceback; traceback.print_exc()
    elif filetype=='xls':
        pass
    elif filetype=='pdf':
        pass
    else:
        pass
    return HttpResponse(u"%s"%_(u"文件导入操作完成!"))
Example #13
0
def filterdata_by_user(query_set, user):
    #    from mysite.iclock.iutils import get_max_in,userAreaList,userDeptList
    from django.contrib.auth.models import User
    from mysite.personnel.models.model_deptadmin import DeptAdmin
    from mysite.personnel.models.model_areaadmin import AreaAdmin
    model = query_set.model
    lname = "limit_%s_to" % model.__name__.lower()
    if hasattr(model, lname):  #特殊的地方就直接用自定义的方法(定义的模型中),否则走通用的
        return getattr(model(), lname)(query_set, user)

#   if model == User:
#       return auth_user_filter(query_set,user)

    if user.is_superuser:  #超级管理员
        return query_set

    if user.username == 'employee':  #员工自助
        return query_set
    dept_admin_ids = DeptAdmin.objects.filter(user=user).values_list("dept_id",
                                                                     flat=True)
    area_admin_ids = AreaAdmin.objects.filter(user=user).values_list("area_id",
                                                                     flat=True)
    #    print "dept_admin_ids",dept_admin_ids
    #    print "area_admin_ids",area_admin_ids

    Department = GetModel("personnel", "Department")
    Area = GetModel("personnel", "Area")
    Device = GetModel("iclock", "Device")

    if model == Area:  #区域表直接过滤
        if not area_admin_ids:  #用户权限不按区域过滤
            return query_set
        else:
            #            query_set=query_set.extra(where=['id  in (select area_id from areaadmin where user_id='+ str(user.pk) +')'])
            area_id_list = ','.join([str(i) for i in area_admin_ids])
            query_set = query_set.extra(
                where=['id  in (' + area_id_list + ')'])
            return query_set
    if model == Device:  #如果设备表就直接根据区域控制行权限
        if not area_admin_ids:  #用户权限不按区域过滤
            return query_set
#        query_set=query_set.extra(where=['area_id  in (select area_id from areaadmin where user_id='+ str(user.pk) +')'])
        else:
            area_id_list = ','.join([str(i) for i in area_admin_ids])
            query_set = query_set.extra(
                where=['area_id in (' + area_id_list + ')'])
            return query_set
        return query_set

    q = {}
    #以设备为外键的模型按照区域过滤(通用)
    for f in model._meta.fields:
        if isinstance(f, models.fields.related.ForeignKey):
            if f.rel.to.__name__ == "Device":
                #                query_set=query_set.extra(where=[model._meta.db_table+"."+f.column + ' in (select id from iclock where area_id in  (select area_id from areaadmin where user_id='+ str(user.pk) +'))'])
                if not area_admin_ids:  #用户权限不按区域过滤
                    return query_set
                else:
                    area_id_list = ','.join([str(i) for i in area_admin_ids])
                    query_set = query_set.extra(where=[
                        model._meta.db_table + "." + f.column +
                        ' in (select id from iclock where area_id in  (' +
                        area_id_list + '))'
                    ])
                return query_set

    if model == Department:  #组织表直接过滤
        #query_set = get_max_in(query_set,deptids,"pk__in")
        if not dept_admin_ids:  #用户权限不按部门过滤
            return query_set
        else:
            dept_id_list = ','.join([str(i) for i in dept_admin_ids])
            #            query_set=query_set.extra(where=['deptid  in (select dept_id from deptadmin where user_id='+ str(user.pk) +')'])
            query_set = query_set.extra(
                where=['deptid  in (' + dept_id_list + ')'])
            return query_set

    q = {}
    for f in model._meta.fields:
        if isinstance(f, models.fields.related.ForeignKey):
            if f.rel.to.__name__ == "Department":  #以组织为外键的组织过滤
                #q = {f.name +"__in":deptids}
                #query_set = get_max_in(query_set,deptids,f.name +"__in")
                if not dept_admin_ids:  #用户权限不按部门过滤
                    return query_set
                else:
                    #                    query_set=query_set.extra(where=[model._meta.db_table+"."+f.column + '  in (select dept_id from deptadmin where user_id='+ str(user.pk) +')'])
                    dept_id_list = ','.join([str(i) for i in dept_admin_ids])
                    query_set = query_set.extra(where=[
                        model._meta.db_table + "." + f.column + '  in (' +
                        dept_id_list + ')'
                    ])
                    return query_set
            elif f.rel.to.__name__ == "Employee":
                #query_set = get_max_in(query_set,deptids,f.name+"__DeptID__in")
                if not dept_admin_ids:  #用户权限不按部门过滤
                    return query_set
                else:
                    dept_id_list = ','.join([str(i) for i in dept_admin_ids])
                    query_set = query_set.extra(where=[
                        model._meta.db_table + "." + f.column +
                        ' in (select userid from userinfo where defaultdeptid in ('
                        + dept_id_list + '))'
                    ])
                    return query_set
#                query_set=query_set.extra(where=[model._meta.db_table+"."+f.column +' in (select userid from userinfo where defaultdeptid in (select dept_id from deptadmin where user_id='+ str(user.pk) +'))'])
#                return query_set
#q = {f.name+"__DeptID__in":deptids}#以组织为外键的人员过滤(如Employee中通过DeptID过滤)
#return query_set.filter(Q(**q))
    return query_set
Example #14
0
def detailthread(data,session_key):
    filename=""
    filetype=""
    tablename=""
    sparatorvalue=","
    fields={}               #已选择字段列表
    target=[]               #目标文件列号
    errmode=1               #出错处理方式     1  跳过错误 继续处理     2  退出,并删除已导入记录
    headln=1
    recordln=2
    unicode_="utf-8"
    relatefield=[]
    relrecs=[]
    addrelate=1             #关联记录处理  1   自动增加       2  关键记录不存在,跳过当次记录,不增加关联记录
    
#    data=dict(request.POST)
    #print data
    errmode=int(data["errmode"][0])
    filename=data["txtfilename"][0]
    filetype=data["txtfiletype"][0]
    sparatorvalue=data["sparatorvalue"][0]
    headln=int(data["headln"][0])
    recordln=int(data["recordln"][0])
    tablename=data["txttablename"][0]
    unicode_=data["unicode"][0].decode()
    addrelate=int(data["addrelate"][0])
#    session_key=request.session.session_key
    
    session_key = str(session_key)
    #查找字段列表,目标列号
    for n,v in data.items():
        if n.startswith("_chk_"):
            
            field=str(data["_select_"+ n[5:]][0]).decode(unicode_)
            fields[field]=int(n[5:])
    #查找模型
    model,flds,rlfield=findAllFieldsAndModel(tablename)

    objlist=[]
    Employee=GetModel("personnel","Employee")
    Department=GetModel("personnel","Department")
    error_list=[]
    
#    global user_process
#    #加入全局变量
#    processdata_global={}
#    
#    user_process[session_key]=processdata_global
#    user_process[session_key]["process"]=0
#    user_process[session_key]["index"]=1
#    user_process[session_key]["return_value"]=""
#    user_process[session_key]["total"]=1
    
    processdata={}
#    processdata["process"]=1
#    processdata["index"]=1
#    processdata["return_value"]=""
#    processdata["total"]=1
#    cache.set(session_key,processdata,3600)
    
    
    
    try:
        if filetype=="txt" or filetype=="csv":                        
            fs=file(uploadpath+filename,"r")
            rec=fs.readline()#读取每行
            sheet_data = []
            ln=1
            while rec!="":
                if ln>=recordln:
                    rec=rec.decode(unicode_)
                    if rec.endswith("\r\n"):
                        rec=rec[:len(rec)-2]
                    linedata=rec.split(sparatorvalue)
                    ltmp=[]
                    for l in linedata:
                        if l.startswith('"') and l.endswith('"'):
                            ltmp.append(u"%s"%l[1:len(l)-1])
                        else:
                            ltmp.append(u"%s"%l)
                    sheet_data.append(ltmp)
                ln+=1
                rec=fs.readline()           
            fs.close()
            return sheet_data
        elif filetype == "xls" : # word 2007 xls文件后缀应该添加
            sheetdata=ParseXlsUxlrd(uploadpath+filename)
          #  global UPLOAD_FILEDATA
            #datash = UPLOAD_FILEDATA
            datash=sheetdata[0][1]
            #在运行开始让进度条 初始化为 0
            processdata['total'] = len(datash)#文件总数
            cache.set(session_key,processdata,3600)
#            user_process[session_key]['total'] = len(datash)#文件总数
#            deptfkey={}#部门外键存放的地方
#            hasdeptf=False
#            perfkey={}# 模型的数据存放的地方
#            for fld,value in fields.items():#遍历提交 对应关系
#                if fld.find(".")>0:
#                    fr=fld.split(".")
#                    if fr[0]=="Department":
#                        deptfkey[fr[1]]=value
#                else:
#                    perfkey[fld]=value
#                    
#            if deptfkey.has_key("code"):
#                hasdeptf=True
            
            
            for row in range(len(datash)):#每行每行开始读取
                if row==len(datash):
                    processdata["process"]=processdata["total"]
                    #变量
#                    user_process[session_key]["process"]=user_process[session_key]["total"]
                else:
                    processdata["process"] = processdata["process"]+1
                    #全局变量
#                    user_process[session_key]["process"] = user_process[session_key]["process"]+1
                cache.set(session_key,processdata,3600)
                try:
                   
                    if row>=recordln-1:#从第二行开始读取数据 
                        isSave=True #这里定义几个标识符 
                        has_property=False
                        strwhere={}
                        upobj=""
                        
                        
                        try:
                            for tmpfld,tmpfldvalue in fields.items():#fields.item() [(u'Department.code', 2), (u'EName', 1), (u'PIN', 0), (u'Department.name', 3)]
                                rf=""
                                #datash[1]=[[2, 1128.0], [1, u'\u5b8b\u7231\u73b2'], [1, u'2'], [1, u'\u90e8\u95e8\u540d\u79f0']], 
                                #datash[1][tmpfldvalue] 选取的是 数据对应的列 datash[row][tmpfldvalue][0]=1
                                if datash[row][tmpfldvalue][0]==2:#datsh[row]读取的每行数据 然后部选择对应的部门 编号所在的列
                                    dv=str(int(datash[row][tmpfldvalue][1]))
                                else:
                                    dv=datash[row][tmpfldvalue][1]# 为什么 判断,感觉没什么必要?? dv=2
                                
                                if tmpfld.find(".")>0:#查找是否有外键的开始, 以.号 判断
                                    for f in model._meta.fields:#遍历循环(估计这个也最 消耗时间)
                                        if isinstance(f,ForeignKey):#匹配 外键  属性 也就是模型中的 每个数据对象
                                            if f.rel.to.__name__==tmpfld.split(".")[0]:#其中 f.rel.to.__name__ 是 模型下面的对象名称 如果找到了 tmpfld.split(".")[0]  = Department
                                                if str(dv).strip():#先strwhere = {'DeptID__code__exact':'1'}
                                                    strwhere[str(f.name+"__"+tmpfld.split(".")[1]+"__exact")]=dv.strip()#
                                                   
                                else:  
                                    if tmpfld=="code" and model==Department:
                                        strwhere={}
                                        strwhere["code__exact"]=datash[row][tmpfldvalue][1].strip()
                                        break
                                    if tmpfld=="PIN" and model==Employee:
                                        strwhere={}
                                        strwhere["PIN__exact"]=str(int(datash[row][tmpfldvalue][1])).strip()
                                        break 
                                    #strwhere[str(tmpfld+"__exact")]=datash[row][tmpfldvalue][1]=dv.strip()
                            #print strwhere
                            #print(Q(**strwhere))
                            upobj=model.objects.filter(Q(**strwhere))
                        except Exception, e:
                            logger.error("%s"%e)
                        if upobj:
                            obj=upobj[0]
                        else:
                            obj=model()
                        currelobj=[]
                        for fld,value in fields.items():
                            relObj=""
                            isForeignKey=False
                            if fld.find(".")>0:                                                     #查找到需要保存关联字段
                                for nkey,val in rlfield.items():                                 #查找关联记录,更新或创建,并保存
                                    if nkey.__name__==fld.split(".")[0]:
                                        strfilter={}
                                        for f  in fields:                                       #查找关联表多个相应的字段,并生成表达式
                                            if f.find(".")>0 :
                                                if f.split(".")[0]==nkey.__name__:
                                                    if datash[row][value][0]==2:
                                                        tmpvalue=str(int(datash[row][fields[f]][1]))
                                                    else:
                                                        tmpvalue=datash[row][fields[f]][1]
                                                    if tmpvalue.strip():
                                                        if f.split(".")[1]=="code" and model==Employee: 
                                                            strfilter={}  #人员表,查找部门表时,当有选择部门名称时,只需要查找部门编号                                                       
                                                            strfilter[str(f.split(".")[1]+"__exact")]=tmpvalue.strip()
                                                            break
                                                        else:
                                                            strfilter[str(f.split(".")[1]+"__exact")]=tmpvalue.strip()
                                        if strfilter:
                                            #print "strfilter:",strfilter
                                            dir(nkey)
                                            relObj=nkey.objects.filter(Q(**strfilter))
                                            
                                        if len(relObj)<=0:                              #查找不到记录,生成新记录
        #                                    print "not found"
                                            
                                            if addrelate!=1:
                                                isSave=False
                                                break                                                           #跳出当前行
                                            else:
                                                relObj=nkey()      
                                                is_save_rel=False                       
                                                for tfld in fields.keys():
                                                    if tfld.find(".")>0:
                                                        if tfld.split(".")[0]==nkey.__name__:
                                                            if datash[row][fields[tfld]][0]==2:
                                                                t_value=str(int(datash[row][fields[tfld]][1]))
                                                            else:
                                                                t_value=datash[row][fields[tfld]][1]
                                                            #print "t_value,",t_value
                                                            if t_value.strip():
                                                                relObj.__setattr__(tfld.split(".")[1],t_value.strip())
                                                                is_save_rel=True
                                                if is_save_rel:
                                                    
                                                    relObj.save()
                                                    relrecs.append(relObj)
                                                    isForeignKey=True
                                                else:
                                                    relObj=None
                                        else:
                                            isForeignKey=True
                                            relObj=relObj[0]
                                            #print "find: %s "%relObj.__doc__
                                        currelobj.append(relObj)
                                        break
                            if not isSave:
                                break                   #跳过当前行
                            tobj=""
                            fieldname=""
                            #print"%s:%s"%(fld,datash[row][value][1])
                            if isForeignKey:
                                for f  in obj._meta.fields:    #查找字段是否是外键
                                    if isinstance(f,ForeignKey) and  f.rel.to.__name__==fld.split(".")[0]:
                                        for tobj in currelobj:
                                            if tobj==f.rel.to:
                                                break        
                                        fieldname=f.name
                                        
                                        break
                                #print "%s :%s"%(fieldname,tobj.pk)
                                obj.__setattr__(fieldname,tobj)
                            else:
                                if datash[row][value][0]==2:
                                    cellvalue=str(int(datash[row][value][1]))
                                else:
                                    cellvalue=datash[row][value][1]
                                #print "field :%s    value:%s"%(fld,cellvalue)
                                if cellvalue.strip():
                                    if fld=="PIN":
                                        model_emp=sys.modules['mysite.personnel.models.model_emp']
                                        settings=sys.modules['erp.settings']
                                        if len(str(cellvalue).strip())>getattr(settings,"PIN_WIDTH"):
                                            raise Exception(u"%s"%_(u"人员编号长度过长"))
                                        else:
                                            cellvalue=getattr(model_emp,"format_pin")(str(cellvalue.strip()))
                                    if fld=="code":
                                        dept=Department.objects.filter(code=cellvalue.strip())
                                        if dept:
                                            raise Exception(u"%s"%_(u'部门编号已存在'))
                                    obj.__setattr__(fld,cellvalue.strip())
                                    has_property=True
                        if isSave and has_property:
                            obj.save()
                            if(type(obj)==Employee):
                                if not obj.DeptID:#导入人员的时候 部门为空的时候 给默认的
                                    obj.__setattr__('DeptID',Department.objects.get(pk=1))
                                obj.__setattr__('attarea',(1,))
                                obj.save()
                            if(type(obj)==Department):
                                if obj.parent==None or obj.parent=="":                                        
                                     obj.parent_id=1
                                     obj.save()
                            objlist.append(obj)
                            
                except Exception,e:
                    #logger.error("%s"%e)
                    try:
                        error_list.append(str(row+1)+u"%s"%_(u" 行      ")+u"%s"%e)
                    except:
                        error_list.append(str(row+1))
                        pass
                    if errmode==1:                      # 按错误处理方式处理数据
                        continue                        
                    else:
                        raise
            
        else:
Example #15
0
def writefile(data,session_key):
    '''
    新的读写文件
    '''
    filename=""
    filetype=""
    tablename=""
    sparatorvalue=","
    fields={}               #已选择字段列表
    target=[]               #目标文件列号
    errmode=1               #出错处理方式     1  跳过错误 继续处理     2  退出,并删除已导入记录
    headln=1
    recordln=2
    unicode_="utf-8"
    relatefield=[]
    relrecs=[]
    addrelate=1             #关联记录处理  1   自动增加       2  关键记录不存在,跳过当次记录,不增加关联记录
    
#    data=dict(request.POST)
    #print data
    errmode=int(data["errmode"][0])
    filename=data["txtfilename"][0]
    filetype=data["txtfiletype"][0]
    sparatorvalue=data["sparatorvalue"][0]
    headln=int(data["headln"][0])
    recordln=int(data["recordln"][0])
    tablename=data["txttablename"][0]
    unicode_=data["unicode"][0].decode()
    addrelate=int(data["addrelate"][0])
#    session_key=request.session.session_key
    
    savefilekey = session_key+ "importfile"
    global SESSION_PRO_KEY
    if SESSION_PRO_KEY:
        session_key = SESSION_PRO_KEY
    else:
        session_key = str(session_key)+"process"
    #查找字段列表,目标列号
    for n,v in data.items():
        if n.startswith("_chk_"):
            
            field=str(data["_select_"+ n[5:]][0]).decode(unicode_)
            fields[field]=int(n[5:])
    #查找模型
    model,flds,rlfield=findAllFieldsAndModel(tablename)
    objlist=[]
    Employee=GetModel("personnel","Employee")
    Department=GetModel("personnel","Department")
    error_list=[]
    
    
    #加入缓存
    processdata={}
    processdata["process"]=0
    processdata["index"]=1
    processdata["return_value"]=""
    processdata["total"]=1
    cache.set(session_key,processdata,3600)
   
    
    
    try:
#        if filetype=="txt" or filetype=="csv":#处理 txt与csv
#           pass 
#           
#        elif filetype == "xls" : # word 2007 xls文件后缀应该添加
#            sheetdata=ParseXlsUxlrd(uploadpath+filename)
#        datash=cache.get(savefilekey)
        #在运行开始让进度条 初始化为 0
        
        #global IMPORT_DATA
        import pickle
        f=file(uploadpath+filename)
        datash=pickle.load(f)
        f.close()
        import os
        os.remove(uploadpath+filename)
        IMPORT_DATA=[]
        processdata['total'] = len(datash)#文件总数
        cache.set(session_key,processdata,3600)
        deptfkey={}#存放有外键的 字典
        hasdeptf=False
        perfkey={}# 模型 字段 的存放的点  子段  在列表的位置 对应关系
        #这里暂时只是考虑 人员与部门
        for fld,value in fields.items():#遍历提交 对应关系
            if fld.find(".")>0:
                fr=fld.split(".")
                if fr[0]=="Department":
                    deptfkey[fr[1]]=value
            else:
                perfkey[fld]=value                    
        if deptfkey.has_key("code"):
            hasdeptf=True
                    
        for row in range(len(datash)):#每行每行开始读取
            if row==len(datash):
                processdata["process"]=processdata["total"]
            else:
                processdata["process"] = processdata["process"]+1
            cache.set(session_key,processdata,3600)
            try:
                if row>=recordln-1:#从第二行开始读取数据 
                    isSave=True #这里定义几个标识符 
                    has_property=False
                    strwhere={}
                    upobj=""
                    if model == Employee:
                        default_deptcode=1
                        if hasdeptf:
                            de_code=datash[row][deptfkey["code"]][1]
                            if type(de_code) == float:
                                de_code=str(int(de_code))
                            de_name=de_code
                            if deptfkey["name"]:
                                de_name=datash[row][deptfkey["name"]][1]
                                if type(de_name)==float:
                                    de_name=str(int(de_name))
                                
                            tem=Department.objects.filter(code__exact=de_code)
                            if  len(tem) > 0 :
                                pass
                            else:
                                Department(code=de_code,name=de_name).save()
                            default_deptcode=Department.objects.filter(code__exact=de_code)[0].id
                        model_emp=sys.modules['mysite.personnel.models.model_emp']
                        format_pin=getattr(model_emp,"format_pin")
                        
                        update=False
                        for per_fileds in perfkey:
                            if per_fileds=="PIN":
                                try:
                                    tt=Employee.all_objects.get(PIN=format_pin(datash[row][perfkey[per_fileds]][1]))
                                    update=True
                                    break;
                                except:
                                    pass
                        if not update:                            
                            tt=Employee()
                        for per_fileds in perfkey:
                            if per_fileds == "Gender":#性别 特殊处理
                                sex = datash[row][perfkey[per_fileds]][1]
                                if  not sex:
                                    tt.__setattr__(per_fileds,"")
                                else:
                                    if sex  in ["Male",u"男","M"]:
                                        tt.__setattr__(per_fileds,"M")
                                    elif sex in ["Female",u"女","F"]:
                                        tt.__setattr__(per_fileds,"F")
#                                    else:
#                                        tt.__setattr__(per_fileds,"")
                            elif per_fileds=="PIN" and update:
                                continue        
                            else:
                                if datash[row][perfkey[per_fileds]][0]==2:
                                    tvalue=str(int(datash[row][perfkey[per_fileds]][1]))
                                else:
                                    tvalue=datash[row][perfkey[per_fileds]][1]
                                tt.__setattr__(per_fileds,tvalue)
                            
                        tt.DeptID_id=default_deptcode
                        tt.save()
                        tt.__setattr__('attarea',(1,))#默认区域
                        tt.save()
                    elif model == Department:#如果导入是部门的话
                        super_departid=None
                        try:
                            if deptfkey.has_key("code"):
                                de_code=datash[row][deptfkey["code"]][1]
                                if type(de_code) == float:
                                     de_code=str(int(de_code))
                                super_departid=Department.objects.filter(code__exact=de_code)[0].id
                                
                            elif deptfkey["name"]:
                                de_name=datash[row][deptfkey["name"]][1]
                                if type(de_name) == float :
                                    de_name=str(int(de_name))
                                
                                super_departid=Department.objects.filter(name__exact=de_name)[0].id
                        except:
                            super_departid=None
                            pass
                        
                        tt = Department()#开始 往 部门表中插入数据
                        for per_fileds in perfkey:
                            tt.__setattr__(per_fileds,datash[row][perfkey[per_fileds]][1])
                        if  super_departid:
                            tt.parent_id = super_departid
                        tt.save()
            except Exception,e:
               #logger.error("%s"%e)
               try:
                   error_list.append(str(row+1)+u"%s"%_(u" 行      ")+u"%s"%e)
               except:
                   error_list.append(str(row+1))
                   pass
               if errmode==1:                      # 按错误处理方式处理数据
                   continue                        
               else:
                   raise
                
        if error_list:
            if len(error_list)>10:
                processdata["return_value"]=(u"%s"%_(u"处理第\n")+u"%s"%("\n".join(error_list[:10]))+u"%s"%_(u"\n...\n还有%s条未显示,其它行导入操作成功!")%(len(error_list)-10))
            else:
                processdata["return_value"]=(u"%s"%_(u"处理第\n")+u"%s"%("\n".join(error_list))+u"%s"%_(u"\n出错,其它行导入操作成功!"))
        else:
             processdata["return_value"]=(u"%s"%_(u"文件导入操作完成!"))
        cache.set(session_key,processdata,3600)
Example #16
0
def save_view(request, app_label, model_name):
    try:
        ret = {}
        op = threadlocals.get_current_user()
        #print op
        obj = GetModel(app_label, model_name)
        para = dict(request.POST)
        view_name = para['viewname'][0]
        defaultview = ""

        vi = ViewModel.objects.filter(
            model__exact=ContentType.objects.get_for_model(obj),
            name__exact=view_name,
            create_operator__exact=op.username)
        if vi:
            newview = vi[0]
        else:
            newview = ViewModel()
        newview.name = para['viewname'][0]
        newview.viewtype = para['viewtype'][0]
        col_set = []
        fun_set = {}
        filter_set = {}
        other_set = {}
        sort = {}
        fieldswidth = {}
        sort["firstsort"] = [para['firstsort'][0], para['sort1'][0]]
        sort["secondsort"] = [para['secondsort'][0], para['sort2'][0]]
        field_prefix = "%s." % obj.__name__
        flen = len(field_prefix)
        #print field_prefix, flen
        view_property = {}
        col = []
        for name, value in para.items():
            if name not in [
                    'viewname', 'firstsort', 'secondsort', 'sort1', 'sort2',
                    'viewtype'
            ]:
                if name.startswith("_fun_"):

                    fun_set[name[5:]] = value[0]

                elif name.startswith("_col_"):
                    name = name[5:]

                    if name.find(field_prefix) == 0:
                        col.append([value[0], str(name[flen:])])
                    elif name.find('.') > 0:
                        col.append([value[0], "__".join(name.split('.'))])

                elif name.startswith("_filter_"):
                    filter_set[name[8:]] = value[0]

                elif name.startswith("_other_"):
                    other_set[name[7:]] = value[0]
                elif name.startswith("_txt_"):
                    fieldswidth[name[5:]] = value[0]
                else:
                    view_property[name] = value[0]
        #print pset
        col.sort()
        for i in col:
            col_set.append(i[1])
        defaultview = ""
        if para.has_key('defaultview'):
            defaultview = 'true'
            view_property['defaultview'] = 'true'
        if defaultview == 'true':
            #print 'find'
            allvi = ViewModel.objects.filter(
                model__exact=ContentType.objects.get_for_model(obj),
                create_operator__exact=op.username)
            for v in allvi:
                info = eval(v.info)
                #print info
                info["defaultview"] = 'false'
                v.info = simplejson.dumps(info)
                v.save()

        view_property['fields'] = col_set
        view_property['action'] = fun_set
        view_property['filter'] = filter_set
        view_property['other'] = other_set
        view_property['sort'] = sort

        view_property['fieldswidth'] = fieldswidth
        pset = simplejson.dumps(view_property)
        newview.info = pset
        newview.model = ContentType.objects.get_for_model(obj)
        newview.save()
        vj = get_all_viewJson(obj)

    except:
        import traceback
        traceback.print_exc()

        ret["flag"] = "false"
        ret["msg"] = "save fail"
        ret["options"] = ""
        return getJSResponse(smart_str(simplejson.dumps(ret)))
    ret["flag"] = "true"
    ret["msg"] = "save success!"
    ret["options"] = vj

    return getJSResponse(smart_str(simplejson.dumps(ret)))
Example #17
0
def QueryData(request, dataModel, master_field=None, qs=None, ignore_keys=[]):
    all_emp = request.REQUEST.get("all_emp", None)
    opts = dataModel._meta
    params = dict(request.REQUEST.items())
    #print "----params="  ,params
    # xiaoxiaojun  2011.12.19  调用该方法需要在对应的dataModel下写一个静态的search_by_filter方法
    qs_filter = request.REQUEST.get("qs_filter", "")
    if qs_filter:
        from django.db import connection
        # instance = dataModel.objects.all()[0]
        dbfiled, sql = dataModel.search_by_filter(qs_filter)
        where_sql = u" %s in (%s) " % (dbfiled, sql)
        qs = dataModel.objects.extra(where=[where_sql])
#            cursor = connection.cursor()
#            cursor.execute(sql)
#            data = cursor.fetchall()
#            connection.close()
#            data_list = []
#            if len(data) <= 2096:
#                for d in data:
#                    data_list.append(int(d[0]))
#                qs = dataModel.objects.filter(id__in = data_list)
#            for d in data:
#                data_list.append(int(d[0]))
#            qs = dataModel.objects.filter(id__in = data_list)

#查询
    if qs is None:
        if master_field:
            qs = master_field.rel.to.objects.all()
        else:
            qs = dataModel.objects.all()
    search = request.REQUEST.get(SEARCH_VAR, "")
    search = unquote(search)
    if request.REQUEST.has_key(SEARCH_VAR):
        search_fields = ()
        try:
            search_fields = dataModel.Admin.search_fields
        except:
            pass
        if search_fields:
            for bit in search.split():
                or_queries = [
                    models.Q(**{construct_search(field_name): bit})
                    for field_name in search_fields
                ]
                if all_emp == "all":
                    other_qs = dataModel.all_objects.all()
                else:
                    other_qs = dataModel.objects.all()
                try:
                    other_qs.dup_select_related(qs)
                except:
                    if qs._select_related:
                        other_qs = other_qs.select_related()
                other_qs = other_qs.filter(reduce(operator.or_, or_queries))
                qs = qs & other_qs

    #过滤
    lookup_params = params.copy()  # a dictionary of the query string
    for i in (ALL_VAR, ORDER_VAR, STAMP_VAR, ORDER_TYPE_VAR, SEARCH_VAR,
              IS_POPUP_VAR, PAGE_VAR, STATE_VAR, EXPORT_VAR, PAGE_LIMIT_VAR,
              TMP_VAR, ACTION_VAR, MAX_NO_PAGE):
        if i in lookup_params:
            del lookup_params[i]

    #用来获取当前模型有的字段
    from django.db.models.fields.related import ManyRelatedObjectsDescriptor as mrod, ReverseManyRelatedObjectsDescriptor as rmrod, ForeignRelatedObjectsDescriptor as frod
    model_fields = [f.name for f in dataModel._meta.fields]
    for p, v in dataModel.__dict__.items():
        if isinstance(v, mrod) or isinstance(v, frod) or isinstance(v, rmrod):
            if str(p).endswith("_set"):
                model_fields.append(str(p[:len(p) - 4]))
            else:
                model_fields.append(p)

    temp_k = ''
    rel_attr = ''
    rel_values = []

    #        print '-----model_fields=',model_fields
    for k, v in lookup_params.items():  #循环去掉不在当前模型中的字段(带":"的跳过)
        if k.__contains__(":"):  #通过从表字段查询主表(当前表)-add by darcy 20100708
            kl = k.split(":")[1]
            lookup_params[kl] = v
            del lookup_params[k]
        else:  #通过主表查询当前表(从表)
            if k.find("__") > 0:
                ky = k.split("__")[0]
            else:
                ky = k

            #处理特殊查询,查询的字段不在当前模型中,并且与当前模型没有外键或者多对多的关系,但是这两个模型必须有某字段一样
            if ky not in model_fields:
                if hasattr(dataModel.Admin,"new_added_fields")and dataModel.Admin.new_added_fields\
                    and ky in dataModel.Admin.new_added_fields.keys():
                    new_lookup_params = {}

                    model_app = dataModel.Admin.new_added_fields[ky][1]
                    model_name = dataModel.Admin.new_added_fields[ky][2]
                    temp_k = dataModel.Admin.new_added_fields[ky][3]
                    rel_attr = dataModel.Admin.new_added_fields[ky][4]
                    rel_model = GetModel(model_app, model_name)

                    new_lookup_params[smart_str(ky + "__contains")] = v
                    #                        print "new----",new_lookup_params
                    new_qs = rel_model.objects.all()
                    new_qs = new_qs.filter(**new_lookup_params).distinct()
                    #                        new_qs = new_qs.filter(**lookup_params).distinct()
                    #                        new_qs = new_qs.filter(**{"EName__contains":u"ck"}).distinct()
                    #                        print "new_qs",new_qs
                    #                        print "employee no1:",new_qs[0].PIN

                    #                        print "rel_attr: ",rel_attr
                    temp_rel_values = []
                    if new_qs and not rel_values.__contains__(u"-1"):
                        for t in new_qs:
                            temp_rel_values.append(
                                t.__getattribute__(rel_attr))
#                            print "temp_rel_values:",temp_rel_values

                        if temp_rel_values and rel_values:
                            rel_values = list(
                                set(rel_values) & set(temp_rel_values))
                        else:
                            rel_values = temp_rel_values

                    else:
                        rel_values.append(FAILURE_SIGN)

                del lookup_params[k]

#        print "rel_values:",rel_values
    rel_values = ",".join(rel_values)

    #如果在模型中查不到任何结果,返回-1
    if rel_values.rfind(FAILURE_SIGN) >= 0:
        rel_values = FAILURE_SIGN

#        print "pin_no: ",rel_values

    if temp_k:
        lookup_params[smart_str(temp_k + "__in")] = rel_values


#        print "before assignment---",lookup_params

#print "model_fields:%s"%model_fields
#print "ddddddlookup_params:%s"%lookup_params
    exclude_query = {}
    for key, value in lookup_params.items():
        if not isinstance(key, str):
            # 'key' will be used as a keyword argument later, so Python
            # requires it to be a string.
            del lookup_params[key]
            k = smart_str(key)
            lookup_params[k] = value
        else:
            k = key
        if (k.find("__in") > 0) or (k.find("__exact") > 0
                                    and value.find(',') > 0):
            del lookup_params[key]
            lookup_params[k.replace("__exact", "__in")] = value.split(",")
        if (k.find("__range") > 0):
            x = eval(value)
            lookup_params[k] = x
        if k.find("__exclude") > 0:
            del lookup_params[key]
            vv = value
            if k.find("__in") > 0:
                vv = vv.split(",")
            exclude_query[k[:-9]] = vv

    # Apply lookup parameters from the query string.
    if ignore_keys:
        for k in ignore_keys:
            if lookup_params.has_key(k):
                del lookup_params[k]

    if lookup_params:
        qs = qs.filter(**lookup_params).distinct()

    #加入排除某些条件的数据
    if exclude_query:
        qs = qs.exclude(**exclude_query)

    #排序
    cl = ChangeList(request, dataModel, master_field)
    if cl.orderStr:
        ot = cl.orderStr.split(",")
        qs = qs.order_by(*ot)
        #print "cl.orderStr", cl.orderStr
    else:
        qs = qs.order_by("-pk")
    qs = filterdata_by_user(qs, request.user)
    return qs, cl
Example #18
0
def get_chioce_data_widget(request, app_label, model_name):
    from widgets import form_field, check_limit
    import sys
    model = GetModel(app_label, model_name)
    multiple = request.GET.get('multiple', False)
    depttree = request.GET.get('depttree', False)
    areatree = request.GET.get('areatree', False)
    c = sys.modules['mysite.personnel.models.depttree']
    if depttree:
        dept_treeview = getattr(c, 'dept_treeview')
        return HttpResponse(dept_treeview())
    if areatree:
        area_treeview = getattr(c, 'area_treeview')
        return HttpResponse(area_treeview())

    field = None
    m = sys.modules[model.__module__]
    for p in dir(m):
        try:
            mp = getattr(m, p)
            if issubclass(mp, models.ForeignKey) and not multiple:
                field = mp()
                break
            elif issubclass(mp, models.ManyToManyField) and multiple:
                field = mp(model)
                break
        except:
            pass
    if field is None:
        if multiple:
            field = models.ManyToManyField(model)
        else:
            field = models.ForeignKey(model)

    w_name = request.GET.get('name', '%s' % model.__name__)
    qs, cl = QueryData(request, model)

    ZDeptMultiChoiceDropDownWidget = getattr(
        c, 'ZDeptMultiChoiceDropDownWidget')(
            attrs={
                "async_model": app_label + "__" + model_name
            })
    ZDeptChoiceWidget = getattr(
        c, 'ZDeptChoiceWidget')(attrs={
            "async_model": app_label + "__" + model_name
        })
    ZDeptChoiceFlatWidget = getattr(
        c,
        'ZDeptChoiceFlatWidget')(attrs={
            "async_model": app_label + "__" + model_name
        })

    flat = request.GET.get('flat', "True")
    wg = request.GET.get('widget', False)
    if model.__name__ == "Department" and multiple and flat == "False":
        f = form_field(field, widget=ZDeptMultiChoiceDropDownWidget)
    elif wg:
        f = form_field(field, widget=locals()[wg])
    else:
        f = form_field(field)
    #check_limit(field, ref_model, f)
    html = f.widget.render(w_name, model.objects.none())
    return HttpResponse(html)