use super::AdminModel;
#[derive(Debug, Clone)]
pub struct Fieldset {
pub title: &'static str,
pub fields: &'static [&'static str],
}
pub trait ModelAdmin: AdminModel {
fn list_display() -> &'static [&'static str] {
&[]
}
fn list_filter() -> &'static [&'static str] {
&[]
}
fn search_fields() -> &'static [&'static str] {
&[]
}
fn ordering() -> &'static [&'static str] {
&["-id"]
}
fn list_per_page() -> usize {
50
}
fn readonly_fields() -> &'static [&'static str] {
&[]
}
fn fieldsets() -> &'static [Fieldset] {
&[]
}
fn bulk_actions() -> &'static [BulkAction] {
&[]
}
fn execute_bulk_action<'a>(
action: &'a str,
_ids: &'a [i64],
_db: &'a crate::orm::Db,
_ctx: &'a crate::admin::bulk::BulkActionContext<'a>,
) -> ::std::pin::Pin<
::std::boxed::Box<
dyn ::std::future::Future<
Output = crate::error::Result<crate::admin::bulk::BulkActionResult>,
> + ::std::marker::Send
+ 'a,
>,
> {
let owned = action.to_string();
Box::pin(async move {
Err(crate::error::Error::BadRequest(format!(
"bulk action `{owned}` has no project handler — \
override `ModelAdmin::execute_bulk_action` on this model"
)))
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct BulkAction {
pub name: &'static str,
pub label: &'static str,
pub destructive: bool,
pub confirm: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDir {
Asc,
Desc,
}
impl SortDir {
pub fn sql(self) -> &'static str {
match self {
SortDir::Asc => "ASC",
SortDir::Desc => "DESC",
}
}
}
pub fn parse_order_spec(spec: &str) -> (String, SortDir) {
if let Some(rest) = spec.strip_prefix('-') {
(rest.to_string(), SortDir::Desc)
} else {
(spec.to_string(), SortDir::Asc)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_order_spec_handles_leading_minus() {
assert_eq!(parse_order_spec("-id"), ("id".to_string(), SortDir::Desc));
assert_eq!(parse_order_spec("name"), ("name".to_string(), SortDir::Asc));
}
#[test]
fn sort_dir_sql_is_stable() {
assert_eq!(SortDir::Asc.sql(), "ASC");
assert_eq!(SortDir::Desc.sql(), "DESC");
}
}