#coding=utf-8 ''' ''' import json from django.db.models import Q from django.db import transaction import common.models as cm import common.common_functions as ccf import common.common_control as ccc import common.error_info as ce def search_organization(name): """ """ qset = cm.Organization.objects.filter(name__icontains=name) qdata = list(qset.values_list("name",flat=True)) return qdata def get_organization_tree(uid): """ """ user = cm.UserInfo.objects.filter(pk=uid).first() if not user: raise ce.TipException(u"用户不存在") uid = user.pk total,users_info = ccc.get_sub_users(uid) print users_info user_ids = [x["id"] for x in users_info] permissions = list(user.role.permission.all().values_list("codename",flat=True)) if user.role.platform == "portal": #数据权限下用户所在的企业 org_ids = list(cm.UserInfo.objects.filter(id__in=user_ids).values_list("organization_id",flat=True)) #数据权限下的用户创建的企业 for uid in user_ids: _user = cm.UserInfo.objects.filter(id=uid).first() if "SystemManagement.Organization.Check" in permissions: org_ids_created = list(cm.Organization.objects.filter(cid__in=[uid]).values_list("id",flat=True)) org_ids.extend(org_ids_created) #orgs = cm.Organization.objects.filter(id__in=org_ids) orgs = cm.Organization.objects.filter(id__in=org_ids,cid=uid).exclude(pid__in=org_ids) else: print 6666666666666666 orgs = cm.Organization.objects.filter(cid__in=user_ids) org_ids = list(orgs.values_list("id",flat=True)) print org_ids,9999 #组装树结构 trees = [] for org in orgs: if user.role.platform == "portal": trees.append(ccc.get_sub_organization_tree(org.id)) else: if not org.pid: trees.append(ccc.get_sub_organization_tree(org.id)) otree = [ { "id":1, "nodes":[ { "id":2, "nodes":[ ], "tree_label":u"成都代理运营中心" } ], "tree_label":u"成都运营中心" } ] return trees def add_organization(**kwargs): """ """ need_params = ["name","sname"] mse = ccf.check_params(*need_params,**kwargs) if mse: raise ce.TipException(mse) need_params.extend(["pid","cid","cperson","desc"]) cvals = ccf.get_need_params(*need_params,**kwargs) with transaction.atomic(): oobj = cm.Organization.objects.create(**cvals) def update_organization(**kwargs): """ """ need_params = ["id"] mse = ccf.check_params(*need_params,**kwargs) if mse: raise ce.TipException(mse) id = kwargs.get("id") uvals = {} if "name" in kwargs: uvals["name"] = kwargs.get("name") if "sname" in kwargs: uvals["sname"] = kwargs.get("sname") if "desc" in kwargs: uvals["desc"] = kwargs.get("desc") with transaction.atomic(): oobj = cm.Organization.objects.filter(id=id).update(**uvals) def delete_organization(**kwargs): """ """ need_params = ["id"] mse = ccf.check_params(*need_params,**kwargs) if mse: raise ce.TipException(mse) ids = str(kwargs.get("id")).split(",") cm.Organization.objects.filter(id__in=ids).delete() #级联删除相关联数据待完善... def get_organization_info(*ids): """ """ qset = cm.Organization.objects.filter(id__in=ids) oinfo = list(qset.values("id","name","sname","desc","pid")) return oinfo