mcpField
F
frappe-core-utils

frappe-core-utils

github.com/impertio-studio开发198115 次安装30 周前更新
代码仓库

关于 frappe-core-utils

供 Frappe 开发者查找 Python 与 JavaScript 常用工具函数的参考资料,按任务整理日期、数值、字符串和验证等 API,并指出版本差异与常见反模式。

主要功能

  1. 01按用途索引 Python 工具函数
  2. 02整理日期与时间处理 API
  3. 03提供数值、货币和字符串函数
  4. 04列出 JavaScript 工具函数
  5. 05对照 v14、v15、v16 函数差异
  6. 06说明标准库替代方案与反模式

适用场景

  1. 01查找日期或金额格式化函数
  2. 02处理输入转换与数据验证
  3. 03编写 Server Script 时选择可用函数
  4. 04检查代码是否误用标准库函数

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/impertio-studio/frappe_claude_skill_package --skill frappe-core-utils

安装后在 Claude Code 中输入/frappe-core-utils确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
anti-patterns.md
date-time-functions.md
javascript-utilities.md
number-money-functions.md
string-validation-functions.md

详细介绍

Frappe 实用函数

快速参考:Python

需求函数返回值
当前日期nowdate() / today()datetime.date
当前日期时间now_datetime()datetime.datetime
解析日期字符串getdate(str)datetime.date
解析日期时间字符串get_datetime(str)datetime.datetime
增加天数add_days(date, n)datetime.date
增加月数add_months(date, n)datetime.date
日期差date_diff(end, start)int(天数)
格式化为用户格式format_date(dt)str(用户区域设置)
相对时间pretty_date(dt)str(“2 小时前”)
安全浮点数转换flt(val, precision)float
安全整数转换cint(val)int
安全字符串转换cstr(val)str
安全布尔值转换sbool(val)bool
安全除法safe_div(a, b)float [v15+]
货币格式fmt_money(amt, currency)str
金额大写money_in_words(amt, cur)str
去除 HTMLstrip_html(text)str
列表转为文本comma_and(items)str(“a、b 和 c”)
验证邮箱validate_email_address(e)str 或 ""
验证 URLvalidate_url(url)bool
解析 JSONparse_json(s)Any
文件路径get_files_path(is_private)str
站点路径get_site_path(*parts)str
唯一列表unique(seq)list
哈希generate_hash(s, length)str

所有导入:在控制器/白名单方法中使用 from frappe.utils import nowdate, flt, ...。 在 Server Scripts 中:直接使用 frappe.utils.nowdate() —— 不允许使用导入语句。


决策树:“该使用哪个函数?”

Need a date/time value?
├─ Current date → nowdate() or today()
├─ Current datetime → now_datetime()
├─ Parse a string → getdate() or get_datetime()
├─ Add/subtract time → add_days(), add_months(), add_to_date()
├─ Difference → date_diff() (days), month_diff(), time_diff_in_seconds()
├─ Period boundary → get_first_day(), get_last_day(), get_quarter_start()
└─ Display to user → format_date(), format_datetime(), pretty_date()

Need a number?
├─ Convert safely → flt(), cint(), cstr(), sbool()
├─ Round → rounded() (banker's rounding)
├─ Safe divide → safe_div(a, b, default=0) [v15+]
├─ Format money → fmt_money(amount, currency)
└─ Money to words → money_in_words(amount, currency)

Need string processing?
├─ HTML → strip_html(), escape_html(), is_html()
├─ Join list → comma_and(), comma_or(), comma_sep()
├─ Markdown ↔ HTML → to_markdown(), md_to_html()
└─ Mask sensitive → mask_string(input, show_first=4) [v16+]

Need validation?
├─ Email → validate_email_address(email, throw=False)
├─ URL → validate_url(url, valid_schemes=["https"])
├─ Phone → validate_phone_number(phone, throw=False)
├─ JSON → validate_json_string(s)
└─ IBAN → validate_iban(iban) [v16+]

Need file/path?
├─ Public files → get_files_path()
├─ Private files → get_files_path(is_private=True)
├─ Site directory → get_site_path("private", "backups")
├─ Bench root → get_bench_path()
└─ File size → get_file_size(path, format=True)

重要反模式

frappe.utils 中存在对应函数时,绝不要使用 Python 标准库
绝不要(标准库)始终使用(frappe.utils)原因
datetime.datetime.now()now_datetime()忽略系统时区
datetime.date.today()nowdate()忽略系统时区
float(val)flt(val, precision)遇到 None/空值时会崩溃
int(val)cint(val)遇到 None/空值时会崩溃
round(val, 2)rounded(val, 2)舍入结果不一致
val1 / val2safe_div(val1, val2)ZeroDivisionError [v15+]
json.loads(s)parse_json(s)遇到 None/空值时会崩溃
json.dumps(obj)frappe.as_json(obj)序列化结果不一致
"{:,.2f}".format(a)fmt_money(a, currency)忽略区域设置/货币
os.path.join(...)get_site_path(...)会破坏多租户
", ".join(items)comma_and(items)“and” 不会本地化
dt.strftime(fmt)format_date(dt)忽略用户偏好
re.sub(r'<.*?>', '', h)strip_html(h)无法处理边界情况
Server Script 沙箱
# ❌ NEVER in Server Scripts
from frappe.utils import nowdate, flt
import json

# ✅ ALWAYS in Server Scripts (no imports allowed)
today = frappe.utils.nowdate()
amount = frappe.utils.flt(doc.amount, 2)
data = frappe.parse_json(doc.json_field)

JavaScript 快速参考

需求函数
转义 HTMLfrappe.utils.escape_html(txt)
HTML 转文本frappe.utils.html2text(html)
检查是否为 HTMLfrappe.utils.is_html(txt)
解析 JSONfrappe.utils.parse_json(str)
验证 URLfrappe.utils.is_url(txt)
标题式大小写frappe.utils.to_title_case(str)
用“and”连接frappe.utils.comma_and(list)
唯一数组frappe.utils.unique(list)
复制到剪贴板frappe.utils.copy_to_clipboard(txt)
滚动到元素frappe.utils.scroll_to(el)
是否为移动设备frappe.utils.is_mobile()
节流frappe.utils.throttle(fn, delay)
防抖frappe.utils.debounce(fn, delay)
格式化值frappe.format(value, df, options, doc)
时长显示frappe.utils.get_formatted_duration(secs)

版本差异

功能v14v15v16
safe_div()--已添加是
duration_to_seconds()--已添加是
guess_date_format()--已添加是
validate_duration_format()--已添加是
mask_string()----已添加
validate_iban()----已添加
validate_name()----已添加
safe_json_loads()----已添加
groupby_metric()----已添加
核心函数是是是

参考文件

评论

支持 Markdown