text: # -*- coding: utf-8 -*-
  from __future__ import unicode_literals
  
  import json
  
  from django.shortcuts import render
  from django.contrib.auth.decorators import login_required
  from django.db.models import Q
  from django.http import HttpResponse
  from django.core.paginator import Paginator, EmptyPage, InvalidPage
  from app.core.models import Department, Mailbox, MailboxUser, DepartmentMember
  from app.utils.domain_session import get_domainid_bysession, get_session_domain
  
  @login_required
  def choose_department_list(request):
      lists = Department.objects.all()
      department_id = request.GET.get('model_department_id', '0')
      department_id = department_id and int(department_id) or 0
      obj = Department.objects.filter(pk=department_id).first()
      department_name = obj and obj.title or ""
      return render(request, template_name='core/choose_department_list.html', context={
          'lists': lists,
          'department_id': department_id,
          'department_name': department_name,
      })
  
  @login_required
  def choose_mailbox_list(request):
      """
      {% url 'choose_mailbox_list' %}
      mbox/ch/?domain_id=x&action=y
      action:
          all             :   全家桶
          dept_list       :    部门列表
          dept_member    :    部门成员
              &dept_id=z
      返回值
      {
          mailbox:[
              {"id" : x, "box":k, name" : y, "dept_id" : z,}
          ]
          dept:[
              {"id":x, "name":y, "parent":z, "child":"1"} 若无子部门则 child 不存在
          ]
      }
      """
      def get_dept_list( lists_dpt, current_dpt_id=0 ):
          dataDept = {}
          for obj in lists_dpt:
              dataDept[obj.id] = {
                              "id"        :   obj.id,
                              "name"      :   obj.get_title,
                              "parent"    :   obj.parent_id,
                          }
          for sub_id in dataDept.keys():
              sub = dataDept[sub_id]
              parent_id = int(sub["parent"])
              if parent_id in (0,-1):
                  continue
              if parent_id in dataDept:
                  dataDept[parent_id]["child"] = 1
          return dataDept.values()
      #end get_dept_list
  
      action = request.POST.get('action', 'all')
      domain_id = get_domainid_bysession(request)
      domain_id = 0 if not domain_id else int(domain_id)
      if not domain_id:
          lists_dpt = Department.objects.all()
          lists_mbox = Mailbox.objects.all()
      else:
          lists_dpt = Department.objects.filter(domain_id=domain_id).all()
          lists_mbox = Mailbox.objects.filter(domain_id=domain_id).all()
  
      data = { "mailbox" : [], "dept" : [] }
      if action == "all":
          for obj in lists_mbox:
              objUser = MailboxUser.objects.filter(mailbox_id=obj.id).first()
              name = _(u"未知用户") if not objUser else objUser.realname
              objMember = DepartmentMember.objects.filter(mailbox_id=obj.id).first()
              dept_id = -1 if not objMember else objMember.dept_id
              data["mailbox"].append( {
                      "id"        :   obj.id,
                      "box"       :   obj.username,
                      "name"      :   name,
                      "dept_id"   :   dept_id,
                  }
              )
          data["dept"] = get_dept_list( lists_dpt )
      elif action == "dept_list" :
          data["dept"] = get_dept_list( lists_dpt )
      elif action == "dept_maintan":
          current_dpt_id = request.POST.get('current_dpt_id', '')
          data["dept"] = get_dept_list(lists_dpt)
      elif action == "dept_member" :
          dept_id = request.POST.get('dept_id', '')
          dept_id = -1 if not dept_id else int(dept_id)
          if dept_id in (0, -1):
              for objBox in lists_mbox:
                  objMember = DepartmentMember.objects.filter(mailbox_id=objBox.id).first()
                  if objMember:
                      continue
                  #print ">>>>>>>>> objBox ",objBox
                  objUser = MailboxUser.objects.filter(mailbox_id=objBox.id).first()
                  name = _(u"未知用户") if not objUser else objUser.realname
                  data["mailbox"].append( {
                                      "id"        :   objBox.id,
                                      "box"       :   objBox.username,
                                      "name"      :   name,
                                      "dept_id"   :   -1,
                                  }
                              )
              #print ">>>>>>>>>> data is ",data
          else:
              lists_member = DepartmentMember.objects.filter(dept_id=dept_id).all()
              for objMember in lists_member:
                  objBox = lists_mbox.filter(id=objMember.mailbox_id).first()
                  if not objBox:
                      continue
                  objUser = MailboxUser.objects.filter(mailbox_id=objBox.id).first()
                  name = _(u"未知用户") if not objUser else objUser.realname
                  data["mailbox"].append( {
                                      "id"        :   objBox.id,
                                      "box"       :   objBox.username,
                                      "name"      :   name,
                                      "dept_id"   :   dept_id,
                                  }
                              )
  
      return HttpResponse(json.dumps(data), content_type="application/json")
  