Ejemplo n.º 1
0
    def export_dedicated_link(cls, assess_id):
        u"""

        :return: 项目导入模版文件路径 文件名  df
        """
        file_name = u"wd-assess" + str(assess_id) + u"-export_dedicated_link-v7.xlsx"
        titles = cls.get_link_title()
        # 项目的所有用户
        people_obj_user_ids = AssessUser.objects.filter_active(assess_id=assess_id).values_list("people_id", flat=True).distinct()
        people_authuser_ids = People.objects.filter_active(id__in=people_obj_user_ids).values_list('user_id', flat=True).distinct()
        # 找到people
        data = []
        authuser_objs = AuthUser.objects.filter(id__in=people_authuser_ids)
        for authuser_obj in authuser_objs:
            if not authuser_obj.dedicated_link:
                id_str = "%10d" % authuser_obj.id
                sha1 = hashlib.sha1()
                sha1.update(id_str)
                dedicated_link = sha1.hexdigest()
                authuser_obj.dedicated_link = dedicated_link
                authuser_obj.save()
            link = CLIENT_HOST + '/people/'+str(authuser_obj.dedicated_link)+'/'
            # people.username
            # 参与人员列表里面的是people.username 就是nickname
            data.append([str_check(authuser_obj.account_name), authuser_obj.nickname, authuser_obj.phone, authuser_obj.email, link])
        file_full_path = ExcelUtils().create_excel(file_name, titles, data, sheet_name="link", parent_dir=cls.link_dir, sheet_index=0)
        return file_full_path, file_name
Ejemplo n.º 2
0
    def export_org_data_old(cls, assess_id):
        u"""
        导出企业组织数据
        :param assess_id: 项目ID
        :return: 组织数据文件路径  文件名
        """
        def get_org_list(org_tree, porg_name=u"", porg_code=u""):
            for org in org_tree:
                try:
                    level_name = cls.ORG_LEVEL[org["level"]]
                except:
                    level_name = u"子组织"
                org_list.append([
                    level_name,
                    org["name"],
                    org["identification_code"],
                    porg_name,
                    porg_code,
                    u"现有"
                ])
                if org["child_orgs"]:
                    get_org_list(org["child_orgs"], org["name"], org["identification_code"])

        org_tree = OrganizationUtils.get_tree_organization(assess_id)
        org_list = []
        get_org_list(org_tree)

        timestamp = int(time.time())
        file_name = "wd-org-data-export-%s.xlsx" % timestamp
        file_path = ExcelUtils().create_excel(file_name, cls.get_title(), org_list, parent_dir=cls.assess_path)
        return file_path, file_name
Ejemplo n.º 3
0
    def export_template(cls):
        u"""

        :return: 组织模版文件路径 文件名
        """
        file_name = u"wd-org-import-template-v4.xlsx"
        titles = cls.get_title()
        file_full_path = os.path.join(cls.template_dir, file_name)
        if os.path.exists(file_full_path):
            return file_full_path, file_name
        file_full_path = ExcelUtils().create_excel(file_name, titles, [], parent_dir=cls.template_dir, sheet_index=0)
        return file_full_path, file_name
Ejemplo n.º 4
0
 def export_template(cls, assess_id=None):
     u"""
     :return: 项目导入模版文件路径 文件名
     """
     file_name = u"wd-assess-import-template-v17.xlsx"
     assess_id = settings.dingzhi_DEBUG
     if assess_id:
         file_name = u"wd-assess-import-template-v16-%s.xlsx" % assess_id
     titles = cls.get_title(assess_id)
     file_full_path = os.path.join(cls.template_dir, file_name)
     if os.path.exists(file_full_path):
         return file_full_path, file_name
     file_full_path = ExcelUtils().create_excel(file_name, titles, [], parent_dir=cls.template_dir, sheet_index=0)
     return file_full_path, file_name
Ejemplo n.º 5
0
def time_assess_out(assess_id, people_ids, survey_id, x):
    title = [
        u'账号',
    ]
    name = "survey_%s_assess%s_part_%s_answertime.xlsx" % (assess_id,
                                                           survey_id, x)
    data = []
    for index, people_id in enumerate(people_ids):
        if index % 100 == 0:
            print(index)
        if index == 0:
            sqr_q_ids = SurveyQuestionResult.objects.filter_active(
                survey_id=survey_id).values_list(
                    'question_id',
                    flat=True).order_by('question_id').distinct()
            title_2 = list(sqr_q_ids)
            title.extend(title_2)
        try:
            q_a_time = UserQuestionAnswerInfo.objects.filter_active(
                question_id__in=title_2,
                people_id=people_id,
                survey_id=survey_id,
                project_id=assess_id).order_by("question_id").values_list(
                    "question_id", 'answer_time')
            p_u_id = People.objects.filter(id=people_id)[0].user_id
            a_n = AuthUser.objects.filter(id=p_u_id)[0].account_name
            one_data = [a_n]
            q_id_row = []
            for id, time in q_a_time:
                if id not in q_id_row:
                    one_data.append(time)
                    q_id_row.append(id)
                else:
                    pass
        except:
            print('error')
            one_data = [people_id]
        data.append(one_data)
    file_path = ExcelUtils().create_excel(name,
                                          title,
                                          data,
                                          sheet_name=u"s_id_%s,a_id_%s" %
                                          (survey_id, assess_id),
                                          force_save=True,
                                          sheet_index=0)
    return file_path, name
Ejemplo n.º 6
0
    def export_org_data(cls, assess_id):
        u"""
        导出企业组织数据
        :param enterprise_id: 企业ID
        :return: 组织数据文件路径  文件名
        """
        def get_org_list(org_tree, porg_names=[]):
            for org in org_tree:
                # try:
                #     level_name = cls.ORG_LEVEL[org["level"]]
                # except:
                #     level_name = u"子组织"
                # org_names = porg_names.append(org["name"])
                # org_list.append(org_names)
                # if org["child_orgs"]:
                #     get_org_list(org["child_orgs"], org_names)
                org_names = copy.deepcopy(porg_names)
                org_names.append(org["name"])
                if org["child_orgs"]:
                    get_org_list(org["child_orgs"], org_names)
                else:
                    org_list.append(org_names)

        org_tree = OrganizationUtils.get_tree_organization(assess_id)
        org_list = []
        get_org_list(org_tree)
        timestamp = int(time.time())
        title = cls.get_title()
        title.append(u'组织码')
        for x in org_list:
            end_org_name = x[-1]
            b = len(x)
            if b < 10:
                x.extend([u'' for i in range(10-b)])
            org_qs = Organization.objects.filter(name=end_org_name, assess_id=assess_id).order_by('-parent_id')
            if org_qs.exists():
                org_code = org_qs[0].identification_code
                x.append(org_code)
        file_name = "wd-org-data-export-%s.xlsx" % timestamp
        file_path = ExcelUtils().create_excel(file_name, title, org_list, parent_dir=cls.assess_path, sheet_index=0)
        return file_path, file_name
Ejemplo n.º 7
0
def import_org_task_old(assess_id, file_path, email=None):
    data = ExcelUtils().read_rows(file_path)
    try:
        for index, org in enumerate(data):
            if index == 0:
                continue
            ops_type = org[5]
            level = org[0]
            name = org[1]
            code = org[2]
            parent_name = org[3]
            parent_code = org[4]
            if not ops_type:
                continue
            if type(ops_type) == int:
                ops_type = str(ops_type)
            elif type(ops_type) == float:
                ops_type = str(int(ops_type))
            if ops_type in OrgImportExport.ORG_OPS_EXISTS:
                continue
            elif ops_type in OrgImportExport.ORG_OPS_DELETE:
                org = Organization.objects.get(is_active=True,
                                               assess_id=assess_id,
                                               identification_code=code)
                child_ids = OrganizationUtils.get_child_orgs(
                    assess_id, org.id)[1]
                child_ids.append(org.id)
                Organization.objects.filter_active(id__in=child_ids).update(
                    is_active=False)
            elif ops_type in OrgImportExport.ORG_OPS_UPDATE:
                org = Organization.objects.get(is_active=True,
                                               assess_id=assess_id,
                                               identification_code=code)
                is_modify = False
                if org.name != name:
                    org.name = name
                    is_modify = True
                if code and code != org.identification_code:
                    org.identification_code = code
                    is_modify = True
                if parent_code:
                    porg = Organization.objects.get(
                        is_active=True,
                        assess_id=assess_id,
                        identification_code=parent_code)
                    if porg.id != org.parent_id:
                        org.parent_id = porg.id
                        is_modify = True
                elif parent_name:
                    porg = Organization.objects.get(is_active=True,
                                                    assess_id=assess_id,
                                                    name=parent_name)
                    if porg.id != org.parent_id:
                        org.parent_id = porg.id
                        is_modify = True
                else:
                    if org.parent_id > 0:
                        org.parent_id = 0
                        is_modify = True
                if is_modify:
                    org.save()
            elif ops_type in OrgImportExport.ORG_OPS_CREATED:
                parent_id = 0
                if parent_code:
                    porg = Organization.objects.get(
                        is_active=True,
                        assess_id=assess_id,
                        identification_code=parent_code)
                    parent_id = porg.id
                elif parent_name:
                    porg = Organization.objects.get(is_active=True,
                                                    assess_id=assess_id,
                                                    name=parent_name)
                    parent_id = porg.id
                if type(name) == str:
                    try:
                        name = name.encode("utf-8")
                    except Exception, e:
                        logger.error("org name encode utf8 error: %s" % e)
                # if not code:
                #     code = OrganizationUtils.generate_org_code(enterprise_id, name)
                # code 全部由系统生成
                code = OrganizationUtils.generate_org_code(assess_id, name)
                Organization.objects.create(name=name,
                                            assess_id=assess_id,
                                            parent_id=parent_id,
                                            identification_code=code)
        return ErrorCode.SUCCESS
Ejemplo n.º 8
0
def import_org_task(assess_id, file_path, email=None):
    data = ExcelUtils().read_rows(file_path)
    try:
        # 一:获得另一个data, 二:创建的地方全部拿出来,一起创建,下一个创建要用到前一个id, 所以1
        # 要检查一遍参数再遍历一个创建,那么就遍历2遍
        org_list_list = []
        new_parent_list = []
        for index, org in enumerate(data):
            # 一行组织
            org_list_list.append(org)
            if index == 0:
                continue
            #  所有分组不得与某分组的一级分组相同
            if len(org) > 0:
                new_parent_list.append(str_check(org[0]))
            for index, org_name in enumerate(org):
                # 一个组织
                if not org_name:
                    break
                # 检查新数据对新数据合法
                if index != 0:
                    # 次级组织名不能与某一级的一级名相同
                    if org_name in new_parent_list:
                        return ErrorCode.ORG_NAME_DOUBLE_ERROR
                org_name = str_check(org_name)

                # 检查新数据对旧数据合法
                def check_org_name(name, assess_id):
                    org_qs = Organization.objects.filter_active(
                        name=name, assess_id=assess_id, parent_id=0)
                    # 分两次导入的话,后台不知道是同名的组织,还是一个新组织,所以还是不允许导入拓展,拓展请手动.
                    # get_or_create的话也可以不用判断
                    if org_qs.count() > 0:
                        return ErrorCode.ORG_NAME_DOUBLE_ERROR
                    else:
                        return ErrorCode.SUCCESS

                ret = check_org_name(org_name, assess_id)
                if ret != ErrorCode.SUCCESS:
                    return ret
        # 至此数据合法
        for index, org in enumerate(org_list_list):
            # for index, org in enumerate(data):
            if index == 0:
                # title
                continue
            parent_id = 0
            for org_name in org:
                # 一行组织中的每一个组织的创建
                if not org_name:
                    break
                org_name = str_check(org_name)
                org_obj, is_created = Organization.objects.get_or_create(
                    is_active=True,
                    assess_id=assess_id,
                    name=org_name,
                    parent_id=parent_id)
                if is_created:
                    # 仅刚创建的时候才需要关联
                    org_obj.identification_code = OrganizationUtils.generate_org_code(
                        assess_id, org_name)
                    org_obj.save()
                    AssessOrganization.objects.create(
                        assess_id=assess_id,
                        organization_id=org_obj.id,
                        organization_code=org_obj.identification_code)
                parent_id = org_obj.id
        return ErrorCode.SUCCESS
    except Exception, e:
        traceback.print_exc()
        logger.error("import org data error, msg: %s, assess_id id: %s" %
                     (e, assess_id))
        return ErrorCode.ORG_IMPORT_DATA_ERROR