def departs_get(jwt=None): user = get_login_user() if user.issystemuser: datas = Depart.query.all() else: # datas_q1 = Depart.query.join(Company, Company.id == Depart.companyid).\ # join(Employee, Employee.departid == Depart.id). \ # filter(Employee.login_user == user.username) datas_q2 = Depart.query.join(Company, Company.id == Depart.companyid). \ join(Company_auth, Company_auth.companyid == Company.id). \ filter(Company_auth.appuserid == user.uid) datas = datas_q2.distinct().all() return [data.to_json() for data in datas], 200, { "content-type": "chatset=utf8" }
def get_employees(emp_name = None,page = None,perpage = None): user = get_login_user() if user.issystemuser: datap = Employee.query else: # Employee_login = aliased(Employee) # 授权公司的员工 datap = Employee.query.\ join(Depart, Depart.id == Employee.departid).\ join(Company, Company.id == Depart.companyid).\ join(Company_auth, Company_auth.companyid == Company.id).\ filter(Company_auth.appuserid == user.uid) if emp_name is not None: datap = datap.filter(Employee.emp_name.like('%'+'%s'%emp_name+'%')) datap = datap.order_by(Employee.emp_name).distinct().paginate(page,perpage) return datap.total,datap.items
def company_auths_get(jwt=None): try: user = get_login_user() if user.issystemuser: datas = Company_auth.query.all() return [data.to_json() for data in datas], 200, { "content-type": "chatset=utf8" } else: datas=db.session.query(Company_auth).\ filter(Company_auth.appuserid==user.uid).all() return [data.to_json() for data in datas], 200, { 'content-type': 'chatset=utf8' } except Exception as e: db.session.rollback() return {"error": str(e)}, 422, {"content-type": "chatset=utf8"}
def appusers_get(jwt=None): try: user = get_login_user() if user.issystemuser: datas = Appuser.query.all() elif user.ismanageuser: #管理者可以看到自己管理的用户 datas = Appuser.query.filter( or_(Appuser.manageuserid == user.uid, Appuser.id == user.uid)).all() else: datas = Appuser.query.filter(Appuser.id == user.uid).all() return [data.to_json() for data in datas], 200, { "content-type": "chatset=utf8" } except Exception as e: db.session.rollback() return {"error", str(e)}, 422, {"content-type": "chatset=utf8"}
def companys_id_delete(id, jwt=None): user = get_login_user() try: if not (user.issystemuser or user.ismanageuser): raise Exception('非管理员用户不能删除公司资料!') cmp_q = Company.query.filter(Company.id == id) if user.ismanageuser: cmp_q = cmp_q.filter(Company.create_userid == user.uid) cmp = cmp_q.first() if cmp is None: raise Exception('不能删除非本登入用户创建的公司') db.session.delete(cmp) # db.session.query(Company).filter(Company.id == id).delete() ucas = Company_auth.query.filter(Company_auth.companyid==id).\ filter(Company_auth.appuserid==g.user_id).all() for uca in ucas: db.session.delete(uca) db.session.commit() except Exception as e: db.session.rollback() return {"error": str(e)}, 422, {"content-type": "chatset=utf8"} return "", 204
def companys_post(body, jwt=None): try: user = get_login_user() if not (user.issystemuser or user.ismanageuser): raise Exception('非管理员用户不能新增公司资料!') if body.get('companyid') == '': body['companyid'] = None body['create_userid'] = user.uid data = Company(**body) # todo 把管理员的员工做公司授权 db.session.add(data) db.session.commit() if user.ismanageuser: uca = Company_auth() uca.companyid = data.id uca.appuserid = user.uid db.session.add(uca) db.session.commit() except Exception as e: db.session.rollback() return {"error": str(e)}, 422, {"content-type": "chatset=utf8"} return data.to_json(), 201, {"content-type": "chatset=utf8"}
def get_companys(): ''' 根据当前用户授权,取出分配给他的权限 :return: ''' user = get_login_user() if user.issystemuser: datas = Company.query.all() # elif user.ismanageuser: # datas_q1 = Company.query.\ # join(User_company_auth,User_company_auth.companyid==Company.id).\ # filter(User_company_auth.user_id==user.uid) #管理者自己创建的公司 # datas_q2 = Company.query. \ # join(Company_auth, Company_auth.companyid == Company.id). \ # filter(Company_auth.appuserid == user.uid) #其他用户给此管理者授权的公司 # datas = datas_q1.union(datas_q2).distinct().all() else: datas_q1 = Company.query.\ join(Company_auth,Company_auth.companyid==Company.id).\ filter(Company_auth.appuserid==user.uid) datas = datas_q1.all() return datas
def check_valid(cls,data): id = data.get('id') login_user = data.get('login_user') # login_user不能重复 if login_user: # 判断login_user是否被其他员工引用 count = db.session.query(func.count(Employee.id)).\ filter(Employee.login_user==login_user,Employee.id!=id).\ first() if count[0] > 0: raise Exception('登录用户(%s)已被使用,请重新指定!' % login_user) cuser = get_login_user() if cuser.ismanageuser: manageid = cuser.uid else: manageid = cuser.manageuserid # 登录用户必须存在,且必须由他的管理者用户新增的 emp_user = Appuser.query. \ filter(Appuser.manageuserid == manageid). \ filter(Appuser.name == login_user). \ first() if not emp_user: raise Exception('登录用户(%s)无效,请重新指定!' % login_user) return True
def import_xls_post(file_csv,jwt = None): def analyze_xls(): s = file_csv.stream.read() data = xlrd.open_workbook(file_contents=s) table = data.sheets()[0] nrows = table.nrows # 行数 ncols = table.ncols # 列数 if ncols != 13: raise Exception('资料格式错误,请检查') datas = {} for i in range(1, nrows): ems = table.row_values(i) # 某一行数据 scname = ems[7].strip() if scname == '': scname = '空' dename = ems[8].strip() if dename == '': dename = '空' if scname in datas: deobj = datas[scname] if dename in deobj: deobj[dename] += [ems] else: deobj[dename] = [ems] else: datas[scname] = {dename: [ems]} return datas try: errmsg='' d=str(int(time.time())) empid=0 if file_csv and allowed_file(file_csv.filename): datas=analyze_xls() cdatas=dict(db.session.query(Company.company_name,Company.id).all()) user = get_login_user() if not (user.issystemuser or user.ismanageuser): raise Exception('非管理员用户不能新增公司资料!') ee_indexs=[]#避免员工重复,跨公司跨部门 for ckey in datas: #判断公司是否已经存在 try: if ckey in cdatas: scid = cdatas[ckey] else: data = Company(company_name=ckey,company_code=ckey,create_userid=user.uid) db.session.add(data) db.session.commit() scid = data.id if user.ismanageuser: uca = Company_auth() uca.id=new_id() uca.companyid = data.id uca.appuserid = user.uid db.session.add(uca) db.session.commit() dess=datas[ckey] ddatas = dict(db.session.query(Depart.depart_name, Depart.id).filter(Depart.companyid== scid).all()) for dkey in dess: if dkey in ddatas: did=ddatas[dkey] else: data = Depart(depart_name=dkey,companyid=scid,code=dkey,inside_id='') db.session.add(data) db.session.commit() did=data.id ess=dess[dkey] edatas = dict(db.session.query(Employee.code, Employee.id).filter(Employee.departid == did).all()) for item in ess: if item[0] in ee_indexs: continue if item[6]=='未婚': marriage='0' elif item[6]=='已婚': marriage = '1' elif item[6]=='离婚': marriage = '2' elif item[6]=='丧偶': marriage = '3' elif item[6]=='其他': marriage='4' else: marriage = '5' id_card=item[4] if len(id_card)==18: bdate=datetime.datetime.strptime(id_card[6:14], "%Y%m%d") else: bdate=None if item[12]!='': hire_date=datetime.datetime.strptime(item[12], "%Y-%m-%d") else: hire_date =None body={ #'emp_id':item[0], 'emp_name':item[1], 'sex':1 if item[2]=='女' else 0, 'id_card':item[4], 'marriage':marriage, 'departid':did, 'code':item[0], 'nation':item[5], 'job_title':item[10], 'hire_date':hire_date, 'birth_date':bdate } try: if item[1].strip()=='': raise Exception('工号['+item[0]+']姓名为空\n') if item[0].strip()=='': raise Exception('姓名['+item[1]+']工号为空\n') if item[4].strip()=='': raise Exception('姓名['+item[1]+']身份证号码为空\n') if item[0] in edatas: Employee.query.filter(Employee.code == item[0]).filter(Employee.departid == did).update(body) else: empid += 1 data = Employee(**body) data.emp_id=d+str(empid) db.session.add(data) db.session.commit() ee_indexs += [item[0]] except Exception as ee: db.session.rollback() errmsg+= str(ee) except Exception as sce: db.session.rollback() errmsg += str(sce) if errmsg!='': return '有未被导入的数据,信息如下:\n'+errmsg, 422, {"content-type": "chatset=utf8"} else: return file_csv.filename, 201, {"content-type": "chatset=utf8"} except Exception as e: # todo handle error db.session.rollback() return str(e), 422, {"content-type": "chatset=utf8"}