use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_translation",
app = "cms",
admin(
list_display = "page_id, locale_id, field_path, value, updated_at",
ordering = "page_id",
list_filter = "locale_id, field_path",
)
)]
pub struct Translation {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(fk = "cms_page", on = "id", index)]
pub page_id: i64,
#[rustango(fk = "cms_locale", on = "id", index)]
pub locale_id: i64,
#[rustango(max_length = 255, index)]
pub field_path: String,
pub value: String,
#[rustango(auto_now)]
pub updated_at: Auto<DateTime<Utc>>,
}
pub async fn fetch_for(
pool: &rustango::sql::Pool,
page_id: i64,
locale_id: i64,
) -> Result<std::collections::HashMap<String, String>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<Translation> = Translation::objects()
.where_(Translation::page_id.eq(page_id))
.where_(Translation::locale_id.eq(locale_id))
.fetch(pool)
.await?;
Ok(rows
.into_iter()
.filter(|t| !t.value.is_empty())
.map(|t| (t.field_path, t.value))
.collect())
}
pub async fn fetch_for_pages(
pool: &rustango::sql::Pool,
page_ids: &[i64],
locale_id: i64,
) -> Result<
std::collections::HashMap<i64, std::collections::HashMap<String, String>>,
rustango::sql::ExecError,
> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
if page_ids.is_empty() {
return Ok(std::collections::HashMap::new());
}
let rows: Vec<Translation> = Translation::objects()
.where_(Translation::locale_id.eq(locale_id))
.where_(Translation::page_id.is_in(page_ids.iter().copied()))
.fetch(pool)
.await?;
let mut out: std::collections::HashMap<i64, std::collections::HashMap<String, String>> =
std::collections::HashMap::new();
for row in rows {
if row.value.is_empty() {
continue;
}
out.entry(row.page_id)
.or_default()
.insert(row.field_path, row.value);
}
Ok(out)
}
pub fn overlay_translations(
value: &mut serde_json::Value,
by_page: &std::collections::HashMap<i64, std::collections::HashMap<String, String>>,
) {
match value {
serde_json::Value::Object(map) => {
let pid = map
.get("page_id")
.or_else(|| map.get("id"))
.and_then(serde_json::Value::as_i64);
if let Some(tr) = pid.and_then(|p| by_page.get(&p)) {
for (field_path, translated) in tr {
if !field_path.contains('.')
&& !translated.is_empty()
&& map.contains_key(field_path)
{
map.insert(
field_path.clone(),
serde_json::Value::String(translated.clone()),
);
}
}
}
for v in map.values_mut() {
overlay_translations(v, by_page);
}
}
serde_json::Value::Array(arr) => {
for v in arr.iter_mut() {
overlay_translations(v, by_page);
}
}
_ => {}
}
}
pub fn validate_field_path(raw: &str) -> Result<String, String> {
let p = raw.trim();
if p.is_empty() {
return Err("Field name is required.".to_owned());
}
if p.chars().count() > 255 {
return Err("Field name is too long (max 255 characters).".to_owned());
}
if !p
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == '-')
{
return Err("Field name may only contain letters, digits, '_', '.' and '-'.".to_owned());
}
Ok(p.to_owned())
}
pub async fn resolve_locale(
pool: &rustango::sql::Pool,
explicit_code: Option<&str>,
) -> Result<Option<crate::locale::Locale>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
if let Some(code) = explicit_code {
let mut hits: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
.where_(crate::locale::Locale::code.eq(code.to_owned()))
.where_(crate::locale::Locale::active.eq(true))
.fetch(pool)
.await?;
if let Some(l) = hits.pop() {
return Ok(Some(l));
}
}
let mut defaults: Vec<crate::locale::Locale> = crate::locale::Locale::objects()
.where_(crate::locale::Locale::is_default.eq(true))
.fetch(pool)
.await?;
Ok(defaults.pop())
}
pub fn register_tera_filter(tera: &mut tera::Tera) {
tera.register_filter("t", t_filter);
}
fn t_filter(
value: &tera::Value,
args: &std::collections::HashMap<String, tera::Value>,
) -> tera::Result<tera::Value> {
let field = args
.get("field")
.and_then(tera::Value::as_str)
.ok_or_else(|| tera::Error::msg("t filter: `field` (string) is required"))?;
if let Some(map) = args.get("translations").and_then(tera::Value::as_object) {
if let Some(v) = map.get(field).and_then(tera::Value::as_str) {
if !v.is_empty() {
return Ok(tera::Value::String(v.to_owned()));
}
}
}
if let (Some(by_page), Some(page_id)) = (args.get("by_page"), args.get("page_id")) {
let key = match page_id {
tera::Value::Number(n) => n.to_string(),
tera::Value::String(s) => s.clone(),
_ => String::new(),
};
if let Some(per_page) = by_page.as_object().and_then(|m| m.get(&key)) {
if let Some(v) = per_page
.as_object()
.and_then(|m| m.get(field))
.and_then(tera::Value::as_str)
{
if !v.is_empty() {
return Ok(tera::Value::String(v.to_owned()));
}
}
}
}
Ok(value.clone())
}
#[cfg(test)]
mod tests {
use super::validate_field_path;
#[test]
fn validate_field_path_accepts_scalar_dotted_and_block_paths() {
assert_eq!(validate_field_path("title").unwrap(), "title");
assert_eq!(validate_field_path(" body ").unwrap(), "body");
let p = "body.550e8400-e29b-41d4-a716-446655440000.title";
assert_eq!(validate_field_path(p).unwrap(), p);
let nested = "body.550e8400-e29b-41d4-a716-446655440000.params.\
11111111-2222-3333-4444-555555555555.description";
assert_eq!(validate_field_path(nested).unwrap(), nested);
}
#[test]
fn validate_field_path_rejects_garbage_and_overlong() {
assert!(validate_field_path("").is_err());
assert!(validate_field_path(" ").is_err());
assert!(validate_field_path("has space").is_err());
assert!(validate_field_path("semi;colon").is_err());
assert!(validate_field_path(&"x".repeat(256)).is_err());
assert!(validate_field_path(&"a".repeat(255)).is_ok());
}
}