Skip to content

Commit

Permalink
minor: collect all parent department when parse workbook
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Sep 19, 2023
1 parent 0539648 commit f7914d9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apps/data_source/initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def initialize(self, user: DataSourceUser) -> None:

self._init_users_identity_info([user])

def _can_skip(self):
def _can_skip(self) -> bool:
"""预先判断能否直接跳过"""

# 不是本地数据源的,不需要初始化
Expand Down
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/apps/sync/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
class SyncConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "bkuser.apps.sync"

def ready(self):
from . import handlers # noqa
43 changes: 0 additions & 43 deletions src/bk-user/bkuser/apps/sync/migrations/0002_auto_20230913_1626.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.20 on 2023-09-19 01:59
# Generated by Django 3.2.20 on 2023-09-19 12:17

import datetime
from django.db import migrations, models
Expand All @@ -8,7 +8,7 @@
class Migration(migrations.Migration):

dependencies = [
('sync', '0002_auto_20230913_1626'),
('sync', '0001_initial'),
]

operations = [
Expand All @@ -20,12 +20,22 @@ class Migration(migrations.Migration):
model_name='tenantsynctask',
name='start_time',
),
migrations.AddField(
model_name='datasourcesynctask',
name='extra',
field=models.JSONField(default=dict, verbose_name='扩展信息'),
),
migrations.AddField(
model_name='datasourcesynctask',
name='start_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now, verbose_name='任务开始时间'),
preserve_default=False,
),
migrations.AddField(
model_name='tenantsynctask',
name='extra',
field=models.JSONField(default=dict, verbose_name='扩展信息'),
),
migrations.AddField(
model_name='tenantsynctask',
name='start_at',
Expand All @@ -37,9 +47,24 @@ class Migration(migrations.Migration):
name='duration',
field=models.DurationField(default=datetime.timedelta(0), verbose_name='任务持续时间'),
),
migrations.AlterField(
model_name='datasourcesynctask',
name='trigger',
field=models.CharField(choices=[('crontab', '定时任务'), ('manual', '手动'), ('signal', '信号触发')], max_length=32, verbose_name='触发方式'),
),
migrations.AlterField(
model_name='tenantsynctask',
name='duration',
field=models.DurationField(default=datetime.timedelta(0), verbose_name='任务持续时间'),
),
migrations.AlterField(
model_name='tenantsynctask',
name='tenant_id',
field=models.CharField(max_length=128, verbose_name='租户 ID'),
),
migrations.AlterField(
model_name='tenantsynctask',
name='trigger',
field=models.CharField(choices=[('crontab', '定时任务'), ('manual', '手动'), ('signal', '信号触发')], max_length=32, verbose_name='触发方式'),
),
]
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apps/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class DataSourceDepartmentChangeLog(TimestampedModel):
class TenantSyncTask(TimestampedModel):
"""租户同步任务"""

tenant_id = models.IntegerField("租户 ID")
tenant_id = models.CharField("租户 ID", max_length=128)
data_source_id = models.IntegerField("数据源 ID")
status = models.CharField("任务总状态", choices=SyncTaskStatus.get_choices(), max_length=32)
trigger = models.CharField("触发方式", choices=SyncTaskTrigger.get_choices(), max_length=32)
Expand Down
7 changes: 6 additions & 1 deletion src/bk-user/bkuser/plugins/local/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ def _parse_departments(self):
for row in self.sheet.iter_rows(min_row=self.user_data_min_row_idx):
if user_orgs := row[self.org_col_idx].value:
for org in user_orgs.split(","):
organizations.add(org.strip())
cur_org = org.strip()
organizations.add(cur_org)
# 所有的父部门都要被添加进来
while "/" in cur_org:
cur_org, __, __ = cur_org.rpartition("/")
organizations.add(cur_org.strip())

# 组织路径:本数据源部门 Code 映射表
org_code_map = {org: gen_code(org) for org in organizations}
Expand Down
Binary file modified src/bk-user/tests/assets/fake_users.xlsx
Binary file not shown.
35 changes: 28 additions & 7 deletions src/bk-user/tests/plugins/local/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,29 @@ def test_get_departments(self, user_wk):
company_code = gen_code("公司")
dept_a_code = gen_code("公司/部门A")
dept_b_code = gen_code("公司/部门B")
dept_c_code = gen_code("公司/部门C")
center_aa_code = gen_code("公司/部门A/中心AA")
center_ab_code = gen_code("公司/部门A/中心AB")
group_aaa_code = gen_code("公司/部门A/中心AA/小组AAA")
group_aba_code = gen_code("公司/部门A/中心AB/小组ABA")
center_ba_code = gen_code("公司/部门B/中心BA")
group_baa_code = gen_code("公司/部门B/中心BA/小组BAA")
center_ca_code = gen_code("公司/部门C/中心CA")
group_caa_code = gen_code("公司/部门C/中心CA/小组CAA")

assert sorted(parser.get_departments(), key=lambda d: d.name) == [
RawDataSourceDepartment(code=center_aa_code, name="中心AA", parent=dept_a_code),
RawDataSourceDepartment(code=center_ab_code, name="中心AB", parent=dept_a_code),
RawDataSourceDepartment(code=center_ba_code, name="中心BA", parent=dept_b_code),
RawDataSourceDepartment(code=center_ca_code, name="中心CA", parent=dept_c_code),
RawDataSourceDepartment(code=company_code, name="公司", parent=None),
RawDataSourceDepartment(code=group_aaa_code, name="小组AAA", parent=center_aa_code),
RawDataSourceDepartment(code=group_aba_code, name="小组ABA", parent=center_ab_code),
RawDataSourceDepartment(code=group_baa_code, name="小组BAA", parent=center_ba_code),
RawDataSourceDepartment(code=group_caa_code, name="小组CAA", parent=center_ca_code),
RawDataSourceDepartment(code=dept_a_code, name="部门A", parent=company_code),
RawDataSourceDepartment(code=dept_b_code, name="部门B", parent=company_code),
RawDataSourceDepartment(code=dept_c_code, name="部门C", parent=company_code),
]

def test_get_users(self, user_wk):
Expand Down Expand Up @@ -127,7 +133,7 @@ def test_get_users(self, user_wk):
"full_name": "李四",
"email": "[email protected]",
"age": "21",
"gender": "male",
"gender": "female",
"region": "region-1",
"phone": "13512345672",
"phone_country_code": "86",
Expand Down Expand Up @@ -172,7 +178,7 @@ def test_get_users(self, user_wk):
"full_name": "柳七",
"email": "[email protected]",
"age": "24",
"gender": "male",
"gender": "female",
"region": "region-4",
"phone": "13512345675",
"phone_country_code": "63",
Expand Down Expand Up @@ -202,7 +208,7 @@ def test_get_users(self, user_wk):
"full_name": "杨九",
"email": "[email protected]",
"age": "26",
"gender": "male",
"gender": "female",
"region": "region-6",
"phone": "13512345677",
"phone_country_code": "86",
Expand Down Expand Up @@ -232,7 +238,7 @@ def test_get_users(self, user_wk):
"full_name": "林十一",
"email": "[email protected]",
"age": "28",
"gender": "male",
"gender": "female",
"region": "region-8",
"phone": "13512345679",
"phone_country_code": "86",
Expand All @@ -255,15 +261,30 @@ def test_get_users(self, user_wk):
leaders=[gen_code("lushi")],
departments=[gen_code("公司/部门B/中心BA/小组BAA")],
),
RawDataSourceUser(
code=gen_code("qinshisan"),
properties={
"username": "qinshisan",
"full_name": "秦十三",
"email": "[email protected]",
"age": "30",
"gender": "female",
"region": "region-10",
"phone": "13512245671",
"phone_country_code": "86",
},
leaders=[gen_code("lisi")],
departments=[gen_code("公司/部门C/中心CA/小组CAA")],
),
RawDataSourceUser(
code=gen_code("freedom"),
properties={
"username": "freedom",
"full_name": "自由人",
"email": "[email protected]",
"age": "30",
"gender": "male",
"region": "region-10",
"age": "666",
"gender": "other",
"region": "solar-system",
"phone": "1351234567",
"phone_country_code": "49",
},
Expand Down

0 comments on commit f7914d9

Please sign in to comment.