Skip to content

Commit 1bded00

Browse files
committed
core&ui: add group bulk import/export
1 parent 8e9a9cc commit 1bded00

5 files changed

Lines changed: 101 additions & 3 deletions

File tree

packages/hydrooj/locales/zh.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ __langname: 简体中文
3030
'Discussion: Terms Of Service': 讨论区服务条款
3131
'First Blood Notice\n{0} solved problem {1} ({2})': '题目一血提示\n{0} 解决了题目 {1} ({2})'
3232
'Format: category 1, sub category 1.1, sub category 1.2, ..., sub category 1.x, ..., category n, sub category n.1, sub category n.2, ..., sub category n.m, ...': 格式:分类1, 子分类1.1, 子分类1.2, ..., 子分类1.x, ..., 分类n, 子分类n.1, 子分类n.2, ..., 子分类n.m, ...
33+
'Format: one group per line, CSV: groupName,uid1,uid2,...': '格式:每行一个小组,CSV:组名,UID1,UID2,...'
3334
'Join {0}': '加入 {0}'
3435
'No':
3536
'Note: Problem title may not be hidden.': 注意:题目标题可能不会被隐藏。
@@ -181,6 +182,7 @@ Continue: 继续
181182
Contributed Problems: 贡献的题目
182183
Contributions: 贡献
183184
Copy Email: 复制电子邮件
185+
Copy the content below: 复制下方的内容
184186
Copy from: 复制自
185187
Copy QQ Number: 复制QQ号
186188
Copy WeChat Account: 复制微信号
@@ -340,6 +342,7 @@ expand: 展开
340342
Expire: 过期
341343
Export All Code: 导出所有代码
342344
Export as {0}: 导出为 {0}
345+
Export Groups: 批量导出小组
343346
Extension (days): 最长延期 (日)
344347
Extension Score Penalty: 延期递交扣分规则
345348
Failed to join the domain. You are already a member.: 加入域失败,您已是该域的成员。
@@ -400,9 +403,11 @@ If left blank, the built-in template of the corresponding language will be used.
400403
Images: 图片
401404
Import From Hydro: 从 Hydro 导入
402405
Import from: 导入自
406+
Import Groups: 批量导入小组
403407
Import Problem: 导入题目
404408
Import User: 导入用户
405409
Import: 导入
410+
Imported successfully.: 导入成功。
406411
In 1 day: 一天后
407412
In 1 month: 一个月后
408413
In 1 week: 一周后
@@ -549,6 +554,7 @@ Open Source: 开源
549554
Open: 开放
550555
Operating System: 操作系统
551556
Ops: 运维
557+
Or paste CSV content: 或粘贴 CSV 内容
552558
Or, with automatically filled invitation code: 或者,这是可以自动填写邀请码的
553559
Ordered List: 有序列表
554560
Original Score: 原始分数
@@ -850,6 +856,7 @@ Unstar: 取消收藏
850856
Update Permission: 更新权限
851857
Update Settings: 更新设置
852858
Update: 更新
859+
Upload CSV File: 上传 CSV 文件
853860
Upload File: 上传文件
854861
Upload Problem: 上传题目
855862
Upload: 上传

packages/hydrooj/locales/zh_TW.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ __langname: 正體中文
3030
'Discussion: Terms Of Service': '討論區服務條款'
3131
'First Blood Notice\n{0} solved problem {1} ({2})': '題目首殺提示\n{0} 解決了題目 {1} ({2})'
3232
'Format: category 1, sub category 1.1, sub category 1.2, ..., sub category 1.x, ..., category n, sub category n.1, sub category n.2, ..., sub category n.m, ...': '格式:分類1, 子分類1.1, 子分類1.2, ..., 子分類1.x, ..., 分類n, 子分類n.1, 子分類n.2, ..., 子分類n.m, ...'
33+
'Format: one group per line, CSV: groupName,uid1,uid2,...': '格式:每行一個小組,CSV:組名,UID1,UID2,...'
3334
'No': ''
3435
'Note: Problem title may not be hidden.': '注意:題目標題可能不會被隱藏。'
3536
'Or upload a file:': '或者上傳一個檔案:'
@@ -177,6 +178,7 @@ Continue: 繼續
177178
Contributed Problems: 貢獻的題目
178179
Contributions: 貢獻
179180
Copy Email: 複製電子郵件
181+
Copy the content below: 複製下方的內容
180182
Copy from: 複製自
181183
Copy QQ Number: 複製QQ號
182184
Copy WeChat Account: 複製微訊號
@@ -337,6 +339,7 @@ expand: 展開
337339
Expire: 過期
338340
Export All Code: 匯出所有原始碼
339341
Export as {0}: 匯出為 {0}
342+
Export Groups: 批量匯出小組
340343
Extension (days): 最長延期(日)
341344
Extension Score Penalty: 延期遞交扣分規則
342345
Failed to join the domain. You are already a member.: 加入域失敗,您已是該域的成員。
@@ -397,9 +400,11 @@ If left blank, the built-in template of the corresponding language will be used.
397400
Images: 圖片
398401
Import From Hydro: 從 Hydro 匯入
399402
Import from: 匯入自
403+
Import Groups: 批量匯入小組
400404
Import Problem: 匯入題目
401405
Import User: 匯入使用者
402406
Import: 匯入
407+
Imported successfully.: 匯入成功。
403408
In 1 day: 一天後
404409
In 1 month: 一個月後
405410
In 1 week: 一週後
@@ -547,6 +552,7 @@ Open Source: 開源
547552
Open: 開放
548553
Operating System: 作業系統
549554
Ops: 運維
555+
Or paste CSV content: 或貼上 CSV 內容
550556
Or, with automatically filled invitation code: 或者,可以使用自動填寫邀請碼
551557
Ordered List: 有序列表
552558
Original Score: 原始得分
@@ -849,6 +855,7 @@ Unstar: 取消收藏
849855
Update Permission: 更新許可權
850856
Update Settings: 更新設定
851857
Update: 更新
858+
Upload CSV File: 上傳 CSV 檔案
852859
Upload File: 上傳檔案
853860
Upload Problem: 上傳題目
854861
Upload: 上傳

packages/ui-default/components/dialog/index.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,19 @@ export class ConfirmDialog extends Dialog {
114114
}
115115

116116
export interface Field {
117-
type: 'text' | 'checkbox' | 'user' | 'userId' | 'username' | 'domain';
117+
type: 'text' | 'textarea' | 'checkbox' | 'user' | 'userId' | 'username' | 'domain';
118118
options?: string[] | Record<string, string>;
119119
placeholder?: string;
120120
label?: string;
121121
autofocus?: boolean;
122122
required?: boolean;
123123
default?: string;
124124
columns?: number;
125+
rows?: number;
125126
}
126127

127128
type Result<T extends string, R extends Record<T, Field>> = {
128-
[K in keyof R]: R[K]['type'] extends ('text' | 'password' | 'username' | 'domain') ? string
129+
[K in keyof R]: R[K]['type'] extends ('text' | 'password' | 'username' | 'domain' | 'textarea') ? string
129130
: R[K]['type'] extends 'checkbox' ? boolean
130131
: R[K]['type'] extends 'userId' ? number
131132
: R[K]['type'] extends 'user' ? any
@@ -175,6 +176,18 @@ export async function prompt<T extends string, R extends Record<T, Field>>(title
175176
</div></div>
176177
{layout.map((i) => <div className="row" key={i[0][0]}>
177178
{i.map(([name, field]: [string, Field]) => <div key={name} className={`columns medium-${Math.abs(field.columns || 12)}`}>
179+
{field.type === 'textarea' && <label>
180+
{field.label}
181+
<textarea
182+
className="textbox"
183+
rows={field.rows || 6}
184+
placeholder={field.placeholder}
185+
defaultValue={field.default}
186+
data-autofocus={field.autofocus}
187+
style={{ fontFamily: 'monospace' }}
188+
onChange={(e) => setValues({ ...values, [name]: e.target.value })}
189+
/>
190+
</label>}
178191
{['text', 'user', 'userId', 'username', 'domain'].includes(field.type) && <label>
179192
{field.label}
180193
<div className="textbox-container">

packages/ui-default/pages/domain_group.page.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $ from 'jquery';
22
import _ from 'lodash';
33
import UserSelectAutoComplete from 'vj/components/autocomplete/UserSelectAutoComplete';
4-
import { ActionDialog, ConfirmDialog } from 'vj/components/dialog';
4+
import { ActionDialog, ConfirmDialog, prompt } from 'vj/components/dialog';
55
import Notification from 'vj/components/notification';
66
import { NamedPage } from 'vj/misc/Page';
77
import {
@@ -132,7 +132,76 @@ const page = new NamedPage('domain_group', () => {
132132
Notification.success(i18n('Saved.'));
133133
}
134134

135+
async function handleClickImportGroups() {
136+
const result = await prompt(i18n('Import Groups'), {
137+
groups: {
138+
type: 'textarea',
139+
label: i18n('Format: one group per line, CSV: groupName,uid1,uid2,...'),
140+
rows: 10,
141+
required: true,
142+
placeholder: 'group1,1001,1002,1003\ngroup2,1004,1005',
143+
},
144+
});
145+
if (!result) return;
146+
const lines = String(result.groups || '').replace(/^\uFEFF/, '').split('\n');
147+
const parsed = new Map<string, number[]>();
148+
const errors: string[] = [];
149+
lines.forEach((raw, idx) => {
150+
const line = raw.trim();
151+
if (!line) return;
152+
const [name, ...rest] = line.split(',').map((t) => t.trim());
153+
if (!name) return;
154+
const uids: number[] = [];
155+
for (const t of rest) {
156+
if (!t) continue;
157+
const uid = +t;
158+
if (!Number.isInteger(uid)) {
159+
errors.push(i18n('Line {0}: Invalid UID "{1}".', idx + 1, t));
160+
return;
161+
}
162+
uids.push(uid);
163+
}
164+
parsed.set(name, Array.from(new Set(uids)));
165+
});
166+
if (errors.length) {
167+
Notification.error(errors.join('\n'));
168+
return;
169+
}
170+
if (!parsed.size) {
171+
Notification.error(i18n('No groups to import.'));
172+
return;
173+
}
174+
try {
175+
for (const [name, uids] of parsed) {
176+
await update(name, uids);
177+
}
178+
Notification.success(i18n('Imported successfully.'));
179+
await delay(1500);
180+
window.location.reload();
181+
} catch (error) {
182+
Notification.error(error.message);
183+
}
184+
}
185+
186+
async function handleClickExportGroups() {
187+
const rows: string[] = [];
188+
for (const gid of Object.keys(targets)) {
189+
const uids = targets[gid].value() as number[];
190+
rows.push([gid, ...uids].join(','));
191+
}
192+
await prompt(i18n('Export Groups'), {
193+
groups: {
194+
type: 'textarea',
195+
label: i18n('Copy the content below'),
196+
rows: 15,
197+
default: rows.join('\n'),
198+
},
199+
});
200+
}
201+
135202
$('[name="create_group"]').click(() => handleClickCreateGroup());
203+
$('[name="import_groups"]').click(() => handleClickImportGroups());
204+
$('[name="export_groups"]').click(() => handleClickExportGroups());
136205
$('[name="remove_selected"]').click(() => handleClickDeleteSelected());
137206
$('[name="save_all"]').on('click', () => handleClickSaveAll());
138207
});

packages/ui-default/templates/domain_group.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<div class="section__header">
55
<h1 class="section__title">{{ _('{0}: Groups').format(domain.name) }}</h1>
66
<div class="section__tools">
7+
<button class="rounded button" name="import_groups">{{ _('Import Groups') }}</button>
8+
<button class="rounded button" name="export_groups">{{ _('Export Groups') }}</button>
79
<button class="primary rounded button" name="create_group">{{ _('Create Group') }}</button>
810
</div>
911
</div>

0 commit comments

Comments
 (0)